Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Sources/SourceGraph/Mutators/DynamicMemberRetainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ final class DynamicMemberRetainer: SourceGraphMutator {
}

func mutate() throws {
// Retain all subscript(dynamicMember:) declarations. This signature is specific to
// @dynamicMemberLookup and may be declared in extensions of external types where we
// cannot verify the attribute exists.
for decl in graph.declarations(ofKind: .functionSubscript) {
if decl.name == "subscript(dynamicMember:)", decl.parent?.attributes.contains(where: { $0.name == "dynamicMemberLookup" }) ?? false {
if decl.name == "subscript(dynamicMember:)" {
graph.markRetained(decl)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Foundation

// Extension on external @dynamicMemberLookup type (AttributeDynamicLookup)
public extension AttributeDynamicLookup {
subscript<T: AttributedStringKey>(
dynamicMember keyPath: KeyPath<AttributeScopes.FixtureAttributes, T>
) -> T {
self[T.self]
}
}

public extension AttributeScopes {
struct FixtureAttributes: AttributeScope {
let myAttribute: MyFixtureAttribute
}
}

public enum MyFixtureAttribute: AttributedStringKey {
public typealias Value = String
public static let name = "MyFixtureAttribute"
}
8 changes: 8 additions & 0 deletions Tests/PeripheryTests/RetentionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,14 @@ final class RetentionTest: FixtureSourceGraphTestCase {
}
}

func testRetainsDynamicMemberLookupSubscriptInExternalTypeExtension() {
analyze(retainPublic: true) {
assertReferenced(.extensionEnum("AttributeDynamicLookup")) {
self.assertReferenced(.functionSubscript("subscript(dynamicMember:)"))
}
}
}

func testRetainsCodableProperties() {
analyze(
retainPublic: true,
Expand Down
4 changes: 4 additions & 0 deletions Tests/Shared/DeclarationDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,8 @@ struct DeclarationDescription: CustomStringConvertible {
static func extensionClass(_ name: String, line: Int? = nil) -> Self {
self.init(kind: .extensionClass, name: name, line: line)
}

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