Skip to content

Commit

Permalink
Retain @dynamicMemberLookup subscript functions. Closes #684
Browse files Browse the repository at this point in the history
  • Loading branch information
ileitch committed Dec 18, 2023
1 parent 405409d commit 49210f4
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

##### Bug Fixes

- None.
- Subscript functions required by `@dynamicMemberLookup` are now retained.

## 2.17.1 (2023-12-04)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Foundation
import Shared

final class DynamicMemberLookupReferenceBuilder: SourceGraphMutator {
private let graph: SourceGraph

required init(graph: SourceGraph, configuration: Configuration) {
self.graph = graph
}

func mutate() throws {
for decl in graph.declarations(ofKind: .functionSubscript) {
if decl.name == "subscript(dynamicMember:)", decl.parent?.attributes.contains("dynamicMemberLookup") ?? false {
graph.markRetained(decl)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public final class SourceGraphMutatorRunner {
DefaultConstructorReferenceBuilder.self,
ComplexPropertyAccessorReferenceBuilder.self,
EnumCaseReferenceBuilder.self,
DynamicMemberLookupReferenceBuilder.self,

UnusedParameterRetainer.self,
AssetReferenceRetainer.self,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@dynamicMemberLookup
public struct FixtureStruct7 {
subscript(dynamicMember member: String) -> String {
""
}

subscript(other: String) -> String {
""
}
}
9 changes: 9 additions & 0 deletions Tests/PeripheryTests/RetentionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,15 @@ final class RetentionTest: FixtureSourceGraphTestCase {
}
}

func testRetainsDynamicMemberLookupSubscript() {
analyze(retainPublic: true) {
assertReferenced(.struct("FixtureStruct7")) {
self.assertReferenced(.functionSubscript("subscript(dynamicMember:)"))
self.assertNotReferenced(.functionSubscript("subscript(_:)"))
}
}
}

// MARK: - Assign-only properties

func testSimplePropertyAssignedButNeverRead() {
Expand Down
4 changes: 4 additions & 0 deletions Tests/Shared/DeclarationDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ struct DeclarationDescription: CustomStringConvertible {
self.init(kind: .functionDestructor, name: name, line: line)
}

static func functionSubscript(_ name: String, line: Int? = nil) -> Self {
self.init(kind: .functionSubscript, name: name, line: line)
}

static func varStatic(_ name: String, line: Int? = nil) -> Self {
self.init(kind: .varStatic, name: name, line: line)
}
Expand Down

0 comments on commit 49210f4

Please sign in to comment.