Skip to content

Commit d8a5fb5

Browse files
authored
Merge pull request #62119 from xedin/runtime-discoverable-attrs-attr-impl
[AST/Sema] Add `@runtimeMetadata` attribute and experimental flag
2 parents 2471d70 + 9a02c79 commit d8a5fb5

File tree

7 files changed

+34
-1
lines changed

7 files changed

+34
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6756,6 +6756,14 @@ ERROR(moveonly_cannot_conform_to_protocol, none,
67566756
ERROR(moveonly_cannot_conform_to_protocol_with_name, none,
67576757
"move-only %0 %1 cannot conform to protocol %2",
67586758
(DescriptiveDeclKind, DeclName, DeclName))
6759+
6760+
//------------------------------------------------------------------------------
6761+
// MARK: Runtime discoverable attributes (@runtimeMetadata)
6762+
//------------------------------------------------------------------------------
6763+
6764+
ERROR(runtime_discoverable_attrs_are_experimental,none,
6765+
"runtime discoverable attributes are an experimental feature", ())
6766+
67596767
#define UNDEFINE_DIAGNOSTIC_MACROS
67606768
#include "DefineDiagnosticMacros.h"
67616769

include/swift/Basic/Features.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ EXPERIMENTAL_FEATURE(Macros, false)
154154
/// corresponding syntax tree.
155155
EXPERIMENTAL_FEATURE(BuiltinMacros, false)
156156

157+
/// Whether to enable experimental @runtimeMetadata feature which allows to
158+
/// declare an attribute which is discoverable and constructable at runtime.
159+
EXPERIMENTAL_FEATURE(RuntimeDiscoverableAttrs, false)
160+
157161
#undef EXPERIMENTAL_FEATURE
158162
#undef UPCOMING_FEATURE
159163
#undef SUPPRESSIBLE_LANGUAGE_FEATURE

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,6 +2930,10 @@ static bool usesFeatureTypeWrappers(Decl *decl) {
29302930
return decl->getAttrs().hasAttribute<TypeWrapperAttr>();
29312931
}
29322932

2933+
static bool usesFeatureRuntimeDiscoverableAttrs(Decl *decl) {
2934+
return decl->getAttrs().hasAttribute<RuntimeMetadataAttr>();
2935+
}
2936+
29332937
static bool usesFeatureParserRoundTrip(Decl *decl) {
29342938
return false;
29352939
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
336336
void visitKnownToBeLocalAttr(KnownToBeLocalAttr *attr);
337337

338338
void visitSendableAttr(SendableAttr *attr);
339+
340+
void visitRuntimeMetadataAttr(RuntimeMetadataAttr *attr);
339341
};
340342

341343
} // end anonymous namespace
@@ -6934,6 +6936,15 @@ void AttributeChecker::visitCompilerInitializedAttr(
69346936
}
69356937
}
69366938

6939+
void AttributeChecker::visitRuntimeMetadataAttr(RuntimeMetadataAttr *attr) {
6940+
if (!Ctx.LangOpts.hasFeature(Feature::RuntimeDiscoverableAttrs)) {
6941+
diagnose(attr->getLocation(),
6942+
diag::runtime_discoverable_attrs_are_experimental);
6943+
attr->setInvalid();
6944+
return;
6945+
}
6946+
}
6947+
69376948
namespace {
69386949

69396950
class ClosureAttributeChecker

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,8 @@ namespace {
16231623

16241624
UNINTERESTING_ATTR(EagerMove)
16251625
UNINTERESTING_ATTR(NoEagerMove)
1626+
1627+
UNINTERESTING_ATTR(RuntimeMetadata)
16261628
#undef UNINTERESTING_ATTR
16271629

16281630
void visitAvailableAttr(AvailableAttr *attr) {

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 719; // isBuiltFromInterface
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 720; // @runtimeMetadata attribute
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///

test/IDE/complete_decl_attribute.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ actor MyGlobalActor {
118118
// KEYWORD3-NEXT: Keyword/None: globalActor[#Class Attribute#]; name=globalActor
119119
// KEYWORD3-NEXT: Keyword/None: preconcurrency[#Class Attribute#]; name=preconcurrency
120120
// KEYWORD3-NEXT: Keyword/None: typeWrapper[#Class Attribute#]; name=typeWrapper
121+
// KEYWORD3-NEXT: Keyword/None: runtimeMetadata[#Class Attribute#]; name=runtimeMetadata
121122
// KEYWORD3-NEXT: End completions
122123

123124
@#^KEYWORD3_2^#IB class C2 {}
@@ -153,6 +154,7 @@ actor MyGlobalActor {
153154
// KEYWORD5-NEXT: Keyword/None: globalActor[#Struct Attribute#]; name=globalActor
154155
// KEYWORD5-NEXT: Keyword/None: preconcurrency[#Struct Attribute#]; name=preconcurrency
155156
// KEYWORD5-NEXT: Keyword/None: typeWrapper[#Struct Attribute#]; name=typeWrapper
157+
// KEYWORD5-NEXT: Keyword/None: runtimeMetadata[#Struct Attribute#]; name=runtimeMetadata
156158
// KEYWORD5-NEXT: End completions
157159

158160
@#^ON_GLOBALVAR^# var globalVar
@@ -312,6 +314,7 @@ struct _S {
312314
// ON_MEMBER_LAST-DAG: Keyword/None: preconcurrency[#Declaration Attribute#]; name=preconcurrency
313315
// ON_MEMBER_LAST-DAG: Keyword/None: typeWrapper[#Declaration Attribute#]; name=typeWrapper
314316
// ON_MEMBER_LAST-DAG: Keyword/None: typeWrapperIgnored[#Declaration Attribute#]; name=typeWrapperIgnored
317+
// ON_MEMBER_LAST-DAG: Keyword/None: runtimeMetadata[#Declaration Attribute#]; name=runtimeMetadata
315318
// ON_MEMBER_LAST-NOT: Keyword
316319
// ON_MEMBER_LAST-DAG: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
317320
// ON_MEMBER_LAST-DAG: Decl[Struct]/CurrModule/TypeRelation[Convertible]: MyPropertyWrapper[#MyPropertyWrapper#]; name=MyPropertyWrapper
@@ -383,6 +386,7 @@ func dummy2() {}
383386
// KEYWORD_LAST-DAG: Keyword/None: preconcurrency[#Declaration Attribute#]; name=preconcurrency
384387
// KEYWORD_LAST-DAG: Keyword/None: typeWrapper[#Declaration Attribute#]; name=typeWrapper
385388
// KEYWORD_LAST-DAG: Keyword/None: typeWrapperIgnored[#Declaration Attribute#]; name=typeWrapperIgnored
389+
// KEYWORD_LAST-DAG: Keyword/None: runtimeMetadata[#Declaration Attribute#]; name=runtimeMetadata
386390
// KEYWORD_LAST-NOT: Keyword
387391
// KEYWORD_LAST-DAG: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
388392
// KEYWORD_LAST-DAG: Decl[Struct]/CurrModule/TypeRelation[Convertible]: MyPropertyWrapper[#MyPropertyWrapper#]; name=MyPropertyWrapper

0 commit comments

Comments
 (0)