Skip to content

Commit 477a54a

Browse files
committed
[AST] RuntimeMetadata: Allow using enums as custom runtime metadata types
1 parent 91f7854 commit 477a54a

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

test/IDE/complete_decl_attribute.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ actor MyGlobalActor {
138138
// KEYWORD4-NEXT: Keyword/None: globalActor[#Enum Attribute#]; name=globalActor
139139
// KEYWORD4-NEXT: Keyword/None: preconcurrency[#Enum Attribute#]; name=preconcurrency
140140
// KEYWORD4-NEXT: Keyword/None: typeWrapper[#Enum Attribute#]; name=typeWrapper
141+
// KEYWORD4-NEXT: Keyword/None: runtimeMetadata[#Enum Attribute#]; name=runtimeMetadata
141142
// KEYWORD4-NEXT: End completions
142143

143-
144144
@#^KEYWORD5^# struct S{}
145145
// KEYWORD5: Begin completions
146146
// KEYWORD5-NEXT: Keyword/None: available[#Struct Attribute#]; name=available{{$}}

test/IRGen/Inputs/runtime_attrs.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,24 @@ public struct TestAmbiguity {
1111
@Ignore
1212
public protocol Ignored {
1313
}
14+
15+
@runtimeMetadata
16+
public enum EnumFlag<B, V> {
17+
case type(B.Type)
18+
case method((B) -> V)
19+
case property(KeyPath<B, V>)
20+
case function(() -> V)
21+
}
22+
23+
extension EnumFlag {
24+
public init(attachedTo: KeyPath<B, V>) { self = .property(attachedTo) }
25+
public init(attachedTo: @escaping (B) -> V) { self = .method(attachedTo) }
26+
}
27+
28+
extension EnumFlag where V == Void {
29+
public init(attachedTo: B.Type) { self = .type(attachedTo) }
30+
}
31+
32+
extension EnumFlag where B == Void {
33+
public init(attachedTo: @escaping () -> V) { self = .function(attachedTo) }
34+
}

test/IRGen/runtime_attributes.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@
134134
// CHECK-SAME: @"$s18runtime_attributes9OuterTypeV15outerMutatingFnSiycvpfaAA19FlagForInnerMethodsHF"
135135
// CHECK-SAME: section "__TEXT, __swift5_rattrs, regular"
136136

137+
// CHECK: @"$s3RAD8EnumFlagOHa" = internal constant
138+
// CHECK-SAME: i32 0
139+
// CHECK-SAME: %swift.type_descriptor** @"got.$s3RAD8EnumFlagOMn"
140+
// CHECK-SAME: i32 5
141+
// CHECK-SAME: @"$s18runtime_attributes14globalEnumTestSi_SaySSGtSgycvpfa3RAD0D4FlagHF"
142+
// CHECK-SAME: @"$s18runtime_attributes12EnumTypeTestV1xSivpfa3RAD0C4FlagHF"
143+
// CHECK-SAME: @"$s18runtime_attributes12EnumTypeTestV8testInstyycvpfa3RAD0C4FlagHF"
144+
// CHECK-SAME: @"$s18runtime_attributes12EnumTypeTestV10testStaticSiycvpZfa3RAD0C4FlagHF"
145+
// CHECK-SAME: @"$s18runtime_attributes12EnumTypeTestAaBVmvpfa3RAD0C4FlagHF"
146+
// CHECK-SAME: section "__TEXT, __swift5_rattrs, regular"
147+
137148
import RAD
138149

139150
@runtimeMetadata
@@ -315,3 +326,18 @@ extension OuterType {
315326
// CHECK-LABEL: define hidden swiftcc void @"$s18runtime_attributes9OuterTypeV15outerMutatingFnSiycvpfaAA19FlagForInnerMethods"(%T18runtime_attributes19FlagForInnerMethodsVySiGSg* noalias nocapture sret(%T18runtime_attributes19FlagForInnerMethodsVySiGSg) %0)
316327
@FlagForInnerMethods mutating func outerMutatingFn() -> Int { 42 }
317328
}
329+
330+
// CHECK-LABEL: define hidden swiftcc void @"$s18runtime_attributes14globalEnumTestSi_SaySSGtSgycvpfa3RAD0D4Flag"(%T3RAD8EnumFlagOyytSi_SaySSGtSgGSg* noalias nocapture sret(%T3RAD8EnumFlagOyytSi_SaySSGtSgGSg) %0)
331+
@EnumFlag func globalEnumTest() -> (Int, [String])? {
332+
nil
333+
}
334+
335+
@EnumFlag struct EnumTypeTest {
336+
// CHECK-LABEL: define hidden swiftcc void @"$s18runtime_attributes12EnumTypeTestV1xSivpfa3RAD0C4Flag"(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVSiGSg* noalias nocapture sret(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVSiGSg) %0)
337+
@EnumFlag var x: Int = 42
338+
// CHECK-LABEL: define hidden swiftcc void @"$s18runtime_attributes12EnumTypeTestV8testInstyycvpfa3RAD0C4Flag"(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVytGSg* noalias nocapture sret(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVytGSg) %0)
339+
@EnumFlag func testInst() {}
340+
// CHECK-LABEL: define hidden swiftcc void @"$s18runtime_attributes12EnumTypeTestV10testStaticSiycvpZfa3RAD0C4Flag"(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVmSiGSg* noalias nocapture sret(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVmSiGSg) %0)
341+
@EnumFlag static func testStatic() -> Int { 42 }
342+
}
343+
// CHECK-LABEL: define hidden swiftcc void @"$s18runtime_attributes12EnumTypeTestAaBVmvpfa3RAD0C4Flag"(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVytGSg* noalias nocapture sret(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVytGSg) %0)

test/type/runtime_discoverable_attrs.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,35 @@ struct TestSelfUse {
232232
@Flag(Self.description) var x: Int = 42
233233
@Flag(Self.description) func test() {}
234234
}
235+
236+
237+
@runtimeMetadata
238+
enum EnumFlag<B, V> {
239+
case type(B.Type)
240+
case method((B) -> V)
241+
case property(KeyPath<B, V>)
242+
case function(() -> V)
243+
}
244+
245+
extension EnumFlag {
246+
init(attachedTo: KeyPath<B, V>) { self = .property(attachedTo) }
247+
init(attachedTo: @escaping (B) -> V) { self = .method(attachedTo) }
248+
}
249+
250+
extension EnumFlag where V == Void {
251+
init(attachedTo: B.Type) { self = .type(attachedTo) }
252+
}
253+
254+
extension EnumFlag where B == Void {
255+
init(attachedTo: @escaping () -> V) { self = .function(attachedTo) }
256+
}
257+
258+
@EnumFlag func globalEnumTest() -> (Int, [String])? {
259+
nil
260+
}
261+
262+
@EnumFlag struct EnumTypeTest {
263+
@EnumFlag var x: Int = 42
264+
@EnumFlag func testInst() {}
265+
@EnumFlag static func testStatic() -> Int { 42 }
266+
}

utils/gyb_syntax_support/AttributeKinds.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ def __init__(self, name, swift_name=None):
718718
code=138),
719719

720720
SimpleDeclAttribute('runtimeMetadata', 'RuntimeMetadata',
721-
OnStruct, OnClass,
721+
OnStruct, OnClass, OnEnum,
722722
ABIBreakingToAdd, ABIBreakingToRemove, APIBreakingToAdd, APIBreakingToRemove, # noqa: E501
723723
code=139)
724724
]

0 commit comments

Comments
 (0)