Skip to content

[AST] RuntimeMetadata: Allow using enums as custom runtime metadata types #63198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 26, 2023
Merged
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
2 changes: 1 addition & 1 deletion include/swift/AST/Attr.def.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ TYPE_ATTR(thin)
TYPE_ATTR(thick)

// Declaration Attributes and Modifers
// To add a new entry here, update https://github.com/apple/swift-syntax
// To add a new entry here, update `utils/gyb_syntax_support/AttributeKinds.py`
% for attr in DECL_ATTR_KINDS + DECL_MODIFIER_KINDS + DEPRECATED_MODIFIER_KINDS:
% if type(attr) is ContextualDeclAttributeAlias:
CONTEXTUAL_DECL_ATTR_ALIAS(${attr.name}, ${attr.class_name})
Expand Down
2 changes: 1 addition & 1 deletion lib/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t SWIFTMODULE_VERSION_MINOR = 736; // add package access modifier
const uint16_t SWIFTMODULE_VERSION_MINOR = 737; // allow @runtimeMetadata on enums

/// A standard hash seed used for all string hashes in a serialized module.
///
Expand Down
2 changes: 1 addition & 1 deletion test/IDE/complete_decl_attribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ actor MyGlobalActor {
// KEYWORD4-NEXT: Keyword/None: globalActor[#Enum Attribute#]; name=globalActor
// KEYWORD4-NEXT: Keyword/None: preconcurrency[#Enum Attribute#]; name=preconcurrency
// KEYWORD4-NEXT: Keyword/None: typeWrapper[#Enum Attribute#]; name=typeWrapper
// KEYWORD4-NEXT: Keyword/None: runtimeMetadata[#Enum Attribute#]; name=runtimeMetadata
// KEYWORD4-NEXT: End completions


@#^KEYWORD5^# struct S{}
// KEYWORD5: Begin completions
// KEYWORD5-NEXT: Keyword/None: available[#Struct Attribute#]; name=available{{$}}
Expand Down
21 changes: 21 additions & 0 deletions test/IRGen/Inputs/runtime_attrs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,24 @@ public struct TestAmbiguity {
@Ignore
public protocol Ignored {
}

@runtimeMetadata
public enum EnumFlag<B, V> {
case type(B.Type)
case method((B) -> V)
case property(KeyPath<B, V>)
case function(() -> V)
}

extension EnumFlag {
public init(attachedTo: KeyPath<B, V>) { self = .property(attachedTo) }
public init(attachedTo: @escaping (B) -> V) { self = .method(attachedTo) }
}

extension EnumFlag where V == Void {
public init(attachedTo: B.Type) { self = .type(attachedTo) }
}

extension EnumFlag where B == Void {
public init(attachedTo: @escaping () -> V) { self = .function(attachedTo) }
}
26 changes: 26 additions & 0 deletions test/IRGen/runtime_attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@
// CHECK-SAME: @"$s18runtime_attributes9OuterTypeV15outerMutatingFnSiycvpfaAA19FlagForInnerMethodsHF"
// CHECK-SAME: section "__TEXT, __swift5_rattrs, regular"

// CHECK: @"$s3RAD8EnumFlagOHa" = internal constant
// CHECK-SAME: i32 0
// CHECK-SAME: %swift.type_descriptor** @"got.$s3RAD8EnumFlagOMn"
// CHECK-SAME: i32 5
// CHECK-SAME: @"$s18runtime_attributes14globalEnumTestSi_SaySSGtSgycvpfa3RAD0D4FlagHF"
// CHECK-SAME: @"$s18runtime_attributes12EnumTypeTestV1xSivpfa3RAD0C4FlagHF"
// CHECK-SAME: @"$s18runtime_attributes12EnumTypeTestV8testInstyycvpfa3RAD0C4FlagHF"
// CHECK-SAME: @"$s18runtime_attributes12EnumTypeTestV10testStaticSiycvpZfa3RAD0C4FlagHF"
// CHECK-SAME: @"$s18runtime_attributes12EnumTypeTestAaBVmvpfa3RAD0C4FlagHF"
// CHECK-SAME: section "__TEXT, __swift5_rattrs, regular"

import RAD

@runtimeMetadata
Expand Down Expand Up @@ -315,3 +326,18 @@ extension OuterType {
// CHECK-LABEL: define hidden swiftcc void @"$s18runtime_attributes9OuterTypeV15outerMutatingFnSiycvpfaAA19FlagForInnerMethods"(%T18runtime_attributes19FlagForInnerMethodsVySiGSg* noalias nocapture sret(%T18runtime_attributes19FlagForInnerMethodsVySiGSg) %0)
@FlagForInnerMethods mutating func outerMutatingFn() -> Int { 42 }
}

// CHECK-LABEL: define hidden swiftcc void @"$s18runtime_attributes14globalEnumTestSi_SaySSGtSgycvpfa3RAD0D4Flag"(%T3RAD8EnumFlagOyytSi_SaySSGtSgGSg* noalias nocapture sret(%T3RAD8EnumFlagOyytSi_SaySSGtSgGSg) %0)
@EnumFlag func globalEnumTest() -> (Int, [String])? {
nil
}

@EnumFlag struct EnumTypeTest {
// CHECK-LABEL: define hidden swiftcc void @"$s18runtime_attributes12EnumTypeTestV1xSivpfa3RAD0C4Flag"(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVSiGSg* noalias nocapture sret(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVSiGSg) %0)
@EnumFlag var x: Int = 42
// CHECK-LABEL: define hidden swiftcc void @"$s18runtime_attributes12EnumTypeTestV8testInstyycvpfa3RAD0C4Flag"(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVytGSg* noalias nocapture sret(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVytGSg) %0)
@EnumFlag func testInst() {}
// CHECK-LABEL: define hidden swiftcc void @"$s18runtime_attributes12EnumTypeTestV10testStaticSiycvpZfa3RAD0C4Flag"(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVmSiGSg* noalias nocapture sret(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVmSiGSg) %0)
@EnumFlag static func testStatic() -> Int { 42 }
}
// CHECK-LABEL: define hidden swiftcc void @"$s18runtime_attributes12EnumTypeTestAaBVmvpfa3RAD0C4Flag"(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVytGSg* noalias nocapture sret(%T3RAD8EnumFlagOy18runtime_attributes0B8TypeTestVytGSg) %0)
32 changes: 32 additions & 0 deletions test/type/runtime_discoverable_attrs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,35 @@ struct TestSelfUse {
@Flag(Self.description) var x: Int = 42
@Flag(Self.description) func test() {}
}


@runtimeMetadata
enum EnumFlag<B, V> {
case type(B.Type)
case method((B) -> V)
case property(KeyPath<B, V>)
case function(() -> V)
}

extension EnumFlag {
init(attachedTo: KeyPath<B, V>) { self = .property(attachedTo) }
init(attachedTo: @escaping (B) -> V) { self = .method(attachedTo) }
}

extension EnumFlag where V == Void {
init(attachedTo: B.Type) { self = .type(attachedTo) }
}

extension EnumFlag where B == Void {
init(attachedTo: @escaping () -> V) { self = .function(attachedTo) }
}

@EnumFlag func globalEnumTest() -> (Int, [String])? {
nil
}

@EnumFlag struct EnumTypeTest {
@EnumFlag var x: Int = 42
@EnumFlag func testInst() {}
@EnumFlag static func testStatic() -> Int { 42 }
}
2 changes: 1 addition & 1 deletion utils/gyb_syntax_support/AttributeKinds.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ def __init__(self, name, swift_name=None):
code=138),

SimpleDeclAttribute('runtimeMetadata', 'RuntimeMetadata',
OnStruct, OnClass,
OnStruct, OnClass, OnEnum,
ABIBreakingToAdd, ABIBreakingToRemove, APIBreakingToAdd, APIBreakingToRemove, # noqa: E501
code=139)
]
Expand Down