Skip to content

Commit b15e9aa

Browse files
committed
[AST] RuntimeMetadata: Generator context should match that of the decl its attached to
This makes it possible to to reference to `Self` if attribute is attached to a method or a property of a nominal type. Resolves: rdar://104210108
1 parent 0812484 commit b15e9aa

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

include/swift/AST/Initializer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class RuntimeAttributeInitializer : public Initializer {
247247
public:
248248
explicit RuntimeAttributeInitializer(CustomAttr *attr, ValueDecl *attachedTo)
249249
: Initializer(InitializerKind::RuntimeAttribute,
250-
attachedTo->getDeclContext()->getModuleScopeContext()),
250+
attachedTo->getDeclContext()),
251251
Attr(attr), AttachedTo(attachedTo) {}
252252

253253
CustomAttr *getAttr() const { return Attr; }

test/SILGen/runtime_attributes.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,39 @@ struct MemberTests {
6565
// CHECK-NEXT: {{.*}} = apply [[IGNORE_INIT]]<(inout MemberTests) -> Int>({{.*}})
6666
@Ignore mutating func mutatingFn() -> Int { 42 }
6767
}
68+
69+
@runtimeMetadata
70+
struct Flag<U> {
71+
init<T>(attachedTo: T, value: U) {}
72+
}
73+
74+
struct TestSelfUse {
75+
static var answer: Int = 42
76+
static var question: String = ""
77+
78+
// CHECK-LABEL: sil hidden [runtime_accessible] [ossa] @$s18runtime_attributes11TestSelfUseV1xSSvpfaAA4Flag : $@convention(thin) () -> @out Optional<Flag<Int>>
79+
// CHECK: [[ADDRESSOR:%.*]] = function_ref @$s18runtime_attributes11TestSelfUseV6answerSivau
80+
// CHECK-NEXT: [[ADDR_RESULT:%.*]] = apply [[ADDRESSOR]]() : $@convention(thin) () -> Builtin.RawPointer
81+
// CHECK-NEXT: [[PROP_ADDR:%.*]] = pointer_to_address [[ADDR_RESULT]] : $Builtin.RawPointer to [strict] $*Int
82+
// CHECK-NEXT: [[PROP_ACCESS:%.*]] = begin_access [read] [dynamic] [[PROP_ADDR]] : $*Int
83+
// CHECK-NEXT: [[PROP_VALUE:%.*]] = load [trivial] [[PROP_ACCESS]] : $*Int
84+
// CHECK_NEXT: end_access [[PROP_ACCESS]] : $*Int
85+
// CHECK: [[PROP_VAL_COPY:%.*]] = alloc_stack $Int
86+
// CHECK: store [[PROP_VALUE]] to [trivial] [[PROP_VAL_COPY]] : $*Int
87+
// CHECK: [[FLAG_INIT_REF:%.*]] = function_ref @$s18runtime_attributes4FlagV10attachedTo5valueACyxGqd___xtclufC
88+
// CHECK-NEXT: {{.*}} = apply [[FLAG_INIT_REF]]<Int, WritableKeyPath<TestSelfUse, String>>({{.*}}, [[PROP_VAL_COPY]], {{.*}})
89+
@Flag(value: Self.answer) var x: String = ""
90+
91+
// CHECK-LABEL: sil hidden [runtime_accessible] [ossa] @$s18runtime_attributes11TestSelfUseV4testyycvpfaAA4Flag : $@convention(thin) () -> @out Optional<Flag<String>>
92+
// CHECK: [[ADDRESSOR:%.*]] = function_ref @$s18runtime_attributes11TestSelfUseV8questionSSvau
93+
// CHECK-NEXT: [[ADDR_RESULT:%.*]] = apply [[ADDRESSOR]]() : $@convention(thin) () -> Builtin.RawPointer
94+
// CHECK-NEXT: [[PROP_ADDR:%.*]] = pointer_to_address [[ADDR_RESULT]] : $Builtin.RawPointer to [strict] $*String
95+
// CHECK-NEXT: [[PROP_ACCESS:%.*]] = begin_access [read] [dynamic] [[PROP_ADDR]] : $*String
96+
// CHECK-NEXT: [[PROP_VALUE:%.*]] = load [copy] [[PROP_ACCESS]] : $*String
97+
// CHECK_NEXT: end_access [[PROP_ACCESS]] : $*String
98+
// CHECK: [[PROP_VAL_COPY:%.*]] = alloc_stack $String
99+
// CHECK: store [[PROP_VALUE]] to [init] [[PROP_VAL_COPY]] : $*String
100+
// CHECK: [[FLAG_INIT_REF:%.*]] = function_ref @$s18runtime_attributes4FlagV10attachedTo5valueACyxGqd___xtclufC
101+
// CHECK-NEXT: {{.*}} = apply [[FLAG_INIT_REF]]<String, (TestSelfUse) -> ()>({{.*}}, [[PROP_VAL_COPY]], {{.*}})
102+
@Flag(value: Self.question) func test() {}
103+
}

test/type/runtime_discoverable_attrs.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,10 @@ struct TestStaticFuncs {
225225
@FlagForStaticFuncs static func test1(_: Int.Type) {}
226226
@FlagForStaticFuncs static func test2(_: inout [String], x: Int) {}
227227
}
228+
229+
struct TestSelfUse {
230+
static var description: String = "TestSelfUse"
231+
232+
@Flag(Self.description) var x: Int = 42
233+
@Flag(Self.description) func test() {}
234+
}

0 commit comments

Comments
 (0)