Skip to content

[AST/Sema] Add @runtimeMetadata attribute and experimental flag #62119

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
Nov 16, 2022
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
8 changes: 8 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -6756,6 +6756,14 @@ ERROR(moveonly_cannot_conform_to_protocol, none,
ERROR(moveonly_cannot_conform_to_protocol_with_name, none,
"move-only %0 %1 cannot conform to protocol %2",
(DescriptiveDeclKind, DeclName, DeclName))

//------------------------------------------------------------------------------
// MARK: Runtime discoverable attributes (@runtimeMetadata)
//------------------------------------------------------------------------------

ERROR(runtime_discoverable_attrs_are_experimental,none,
"runtime discoverable attributes are an experimental feature", ())

#define UNDEFINE_DIAGNOSTIC_MACROS
#include "DefineDiagnosticMacros.h"

4 changes: 4 additions & 0 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ EXPERIMENTAL_FEATURE(Macros, false)
/// corresponding syntax tree.
EXPERIMENTAL_FEATURE(BuiltinMacros, false)

/// Whether to enable experimental @runtimeMetadata feature which allows to
/// declare an attribute which is discoverable and constructable at runtime.
EXPERIMENTAL_FEATURE(RuntimeDiscoverableAttrs, false)

#undef EXPERIMENTAL_FEATURE
#undef UPCOMING_FEATURE
#undef SUPPRESSIBLE_LANGUAGE_FEATURE
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2930,6 +2930,10 @@ static bool usesFeatureTypeWrappers(Decl *decl) {
return decl->getAttrs().hasAttribute<TypeWrapperAttr>();
}

static bool usesFeatureRuntimeDiscoverableAttrs(Decl *decl) {
return decl->getAttrs().hasAttribute<RuntimeMetadataAttr>();
}

static bool usesFeatureParserRoundTrip(Decl *decl) {
return false;
}
Expand Down
11 changes: 11 additions & 0 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
void visitKnownToBeLocalAttr(KnownToBeLocalAttr *attr);

void visitSendableAttr(SendableAttr *attr);

void visitRuntimeMetadataAttr(RuntimeMetadataAttr *attr);
};

} // end anonymous namespace
Expand Down Expand Up @@ -6934,6 +6936,15 @@ void AttributeChecker::visitCompilerInitializedAttr(
}
}

void AttributeChecker::visitRuntimeMetadataAttr(RuntimeMetadataAttr *attr) {
if (!Ctx.LangOpts.hasFeature(Feature::RuntimeDiscoverableAttrs)) {
diagnose(attr->getLocation(),
diag::runtime_discoverable_attrs_are_experimental);
attr->setInvalid();
return;
}
}

namespace {

class ClosureAttributeChecker
Expand Down
2 changes: 2 additions & 0 deletions lib/Sema/TypeCheckDeclOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,8 @@ namespace {

UNINTERESTING_ATTR(EagerMove)
UNINTERESTING_ATTR(NoEagerMove)

UNINTERESTING_ATTR(RuntimeMetadata)
#undef UNINTERESTING_ATTR

void visitAvailableAttr(AvailableAttr *attr) {
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 = 719; // isBuiltFromInterface
const uint16_t SWIFTMODULE_VERSION_MINOR = 720; // @runtimeMetadata attribute

/// A standard hash seed used for all string hashes in a serialized module.
///
Expand Down
4 changes: 4 additions & 0 deletions test/IDE/complete_decl_attribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ actor MyGlobalActor {
// KEYWORD3-NEXT: Keyword/None: globalActor[#Class Attribute#]; name=globalActor
// KEYWORD3-NEXT: Keyword/None: preconcurrency[#Class Attribute#]; name=preconcurrency
// KEYWORD3-NEXT: Keyword/None: typeWrapper[#Class Attribute#]; name=typeWrapper
// KEYWORD3-NEXT: Keyword/None: runtimeMetadata[#Class Attribute#]; name=runtimeMetadata
// KEYWORD3-NEXT: End completions

@#^KEYWORD3_2^#IB class C2 {}
Expand Down Expand Up @@ -153,6 +154,7 @@ actor MyGlobalActor {
// KEYWORD5-NEXT: Keyword/None: globalActor[#Struct Attribute#]; name=globalActor
// KEYWORD5-NEXT: Keyword/None: preconcurrency[#Struct Attribute#]; name=preconcurrency
// KEYWORD5-NEXT: Keyword/None: typeWrapper[#Struct Attribute#]; name=typeWrapper
// KEYWORD5-NEXT: Keyword/None: runtimeMetadata[#Struct Attribute#]; name=runtimeMetadata
// KEYWORD5-NEXT: End completions

@#^ON_GLOBALVAR^# var globalVar
Expand Down Expand Up @@ -312,6 +314,7 @@ struct _S {
// ON_MEMBER_LAST-DAG: Keyword/None: preconcurrency[#Declaration Attribute#]; name=preconcurrency
// ON_MEMBER_LAST-DAG: Keyword/None: typeWrapper[#Declaration Attribute#]; name=typeWrapper
// ON_MEMBER_LAST-DAG: Keyword/None: typeWrapperIgnored[#Declaration Attribute#]; name=typeWrapperIgnored
// ON_MEMBER_LAST-DAG: Keyword/None: runtimeMetadata[#Declaration Attribute#]; name=runtimeMetadata
// ON_MEMBER_LAST-NOT: Keyword
// ON_MEMBER_LAST-DAG: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
// ON_MEMBER_LAST-DAG: Decl[Struct]/CurrModule/TypeRelation[Convertible]: MyPropertyWrapper[#MyPropertyWrapper#]; name=MyPropertyWrapper
Expand Down Expand Up @@ -383,6 +386,7 @@ func dummy2() {}
// KEYWORD_LAST-DAG: Keyword/None: preconcurrency[#Declaration Attribute#]; name=preconcurrency
// KEYWORD_LAST-DAG: Keyword/None: typeWrapper[#Declaration Attribute#]; name=typeWrapper
// KEYWORD_LAST-DAG: Keyword/None: typeWrapperIgnored[#Declaration Attribute#]; name=typeWrapperIgnored
// KEYWORD_LAST-DAG: Keyword/None: runtimeMetadata[#Declaration Attribute#]; name=runtimeMetadata
// KEYWORD_LAST-NOT: Keyword
// KEYWORD_LAST-DAG: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
// KEYWORD_LAST-DAG: Decl[Struct]/CurrModule/TypeRelation[Convertible]: MyPropertyWrapper[#MyPropertyWrapper#]; name=MyPropertyWrapper
Expand Down