Skip to content

Commit 1c395e2

Browse files
authored
Merge pull request #79923 from kubamracek/mracek/constinitialized
[Compile Time Values] Add parsing of @constInitialized attribute under CompileTimeValues experimental feature
2 parents 3bb8cfb + 8a9d7e6 commit 1c395e2

16 files changed

+114
-21
lines changed

include/swift/AST/Decl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5898,6 +5898,7 @@ class AbstractStorageDecl : public ValueDecl {
58985898
Bits.AbstractStorageDecl.IsStatic = IsStatic;
58995899
}
59005900
bool isCompileTimeLiteral() const;
5901+
bool isConstVal() const;
59015902

59025903
/// \returns the way 'static'/'class' should be spelled for this declaration.
59035904
StaticSpellingKind getCorrectStaticSpelling() const;

include/swift/AST/DeclAttr.def

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,13 @@ SIMPLE_DECL_ATTR(const, ConstVal,
872872
ABIStableToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove,
873873
167)
874874
DECL_ATTR_FEATURE_REQUIREMENT(ConstVal, CompileTimeValues)
875+
SIMPLE_DECL_ATTR(constInitialized, ConstInitialized,
876+
OnVar,
877+
ABIStableToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove,
878+
168)
879+
DECL_ATTR_FEATURE_REQUIREMENT(ConstInitialized, CompileTimeValues)
875880

876-
LAST_DECL_ATTR(ConstVal)
881+
LAST_DECL_ATTR(ConstInitialized)
877882

878883
#undef DECL_ATTR_ALIAS
879884
#undef CONTEXTUAL_DECL_ATTR_ALIAS

include/swift/AST/DiagnosticsCommon.def

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,20 @@ ERROR(class_subscript_not_in_class,none,
7777
"class subscripts are only allowed within classes; "
7878
"use 'static' to declare a %select{static|requirement fulfilled by either a static or class}0 subscript", (bool))
7979

80-
ERROR(require_static_for_const,none,
80+
ERROR(require_static_for_literal,none,
8181
"'static' is required for _const variable declaration", ())
8282

83-
ERROR(require_let_for_const,none,
83+
ERROR(require_let_for_literal,none,
8484
"let is required for a _const variable declaration", ())
8585

86+
ERROR(require_literal_initializer_for_literal,none,
87+
"_const let should be initialized with a literal value", ())
88+
8689
ERROR(require_const_initializer_for_const,none,
87-
"_const let should be initialized with a compile-time literal", ())
90+
"@const value should be initialized with a compile-time value", ())
91+
92+
ERROR(require_const_arg_for_parameter,none,
93+
"expected a compile-time value for a '@const' parameter", ())
8894

8995
// FIXME: Used by both the parser and the type-checker.
9096
ERROR(func_decl_without_brace,PointsToFirstBadToken,

include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2149,7 +2149,7 @@ ERROR(attr_only_at_non_local_scope, none,
21492149
ERROR(attr_only_at_non_generic_scope, none,
21502150
"attribute '%0' cannot be used in a generic context", (StringRef))
21512151
ERROR(attr_only_on_static_properties, none,
2152-
"properties with attribute '_section' must be static", (StringRef))
2152+
"properties with attribute '%0' must be static", (StringRef))
21532153

21542154
ERROR(weak_unowned_in_embedded_swift, none,
21552155
"attribute %0 cannot be used in embedded Swift", (ReferenceOwnership))
@@ -4046,6 +4046,10 @@ ERROR(attr_incompatible_with_objc,none,
40464046
"'%0' must not be used on an '@objc' %1",
40474047
(DeclAttribute, DescriptiveDeclKind))
40484048

4049+
ERROR(attr_unusable_in_protocol,none,
4050+
"'%0' cannot be used inside a protocol declaration",
4051+
(DeclAttribute))
4052+
40494053
ERROR(final_not_on_accessors,none,
40504054
"only the %select{property|subscript}0 itself can be marked as 'final'"
40514055
,(bool))

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4819,6 +4819,7 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
48194819
TRIVIAL_ATTR_PRINTER(Borrowing, borrowing)
48204820
TRIVIAL_ATTR_PRINTER(CompileTimeLiteral, compile_time_literal)
48214821
TRIVIAL_ATTR_PRINTER(ConstVal, compile_time_value)
4822+
TRIVIAL_ATTR_PRINTER(ConstInitialized, const_initialized)
48224823
TRIVIAL_ATTR_PRINTER(CompilerInitialized, compiler_initialized)
48234824
TRIVIAL_ATTR_PRINTER(Consuming, consuming)
48244825
TRIVIAL_ATTR_PRINTER(Convenience, convenience)

lib/AST/Decl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,10 @@ bool AbstractStorageDecl::isCompileTimeLiteral() const {
12941294
return getAttrs().hasAttribute<CompileTimeLiteralAttr>();
12951295
}
12961296

1297+
bool AbstractStorageDecl::isConstVal() const {
1298+
return getAttrs().hasAttribute<ConstValAttr>();
1299+
}
1300+
12971301
bool AbstractStorageDecl::isTransparent() const {
12981302
return getAttrs().hasAttribute<TransparentAttr>();
12991303
}

lib/AST/FeatureSet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ static bool usesFeatureConcurrencySyntaxSugar(Decl *decl) {
363363
}
364364

365365
static bool usesFeatureCompileTimeValues(Decl *decl) {
366-
return decl->getAttrs().hasAttribute<ConstValAttr>();
366+
return decl->getAttrs().hasAttribute<ConstValAttr>() ||
367+
decl->getAttrs().hasAttribute<ConstInitializedAttr>();
367368
}
368369

369370
static bool usesFeatureClosureBodyMacro(Decl *decl) {

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ extension ASTGenVisitor {
224224
.borrowed,
225225
.compilerInitialized,
226226
.constVal,
227+
.constInitialized,
227228
.dynamicCallable,
228229
.eagerMove,
229230
.exported,

lib/SIL/IR/SILGlobalVariable.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,16 @@ bool SILGlobalVariable::mustBeInitializedStatically() const {
106106
if (getSectionAttr())
107107
return true;
108108

109-
auto *decl = getDecl();
109+
auto *decl = getDecl();
110110
if (decl && isDefinition() && decl->getAttrs().hasAttribute<SILGenNameAttr>())
111111
return true;
112112

113+
if (decl && isDefinition() && decl->getAttrs().hasAttribute<ConstValAttr>())
114+
return true;
115+
116+
if (decl && isDefinition() && decl->getAttrs().hasAttribute<ConstInitializedAttr>())
117+
return true;
118+
113119
return false;
114120
}
115121

lib/Sema/TypeCheckAttr.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,8 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
435435
void visitFinalAttr(FinalAttr *attr);
436436
void visitMoveOnlyAttr(MoveOnlyAttr *attr);
437437
void visitCompileTimeLiteralAttr(CompileTimeLiteralAttr *attr) {}
438-
void visitConstValAttr(ConstValAttr *attr) {}
438+
void visitConstValAttr(ConstValAttr *attr);
439+
void visitConstInitializedAttr(ConstInitializedAttr *attr);
439440
void visitIBActionAttr(IBActionAttr *attr);
440441
void visitIBSegueActionAttr(IBSegueActionAttr *attr);
441442
void visitLazyAttr(LazyAttr *attr);
@@ -2843,6 +2844,41 @@ void AttributeChecker::visitMoveOnlyAttr(MoveOnlyAttr *attr) {
28432844
.fixItRemove(attr->getRange());
28442845
}
28452846

2847+
void AttributeChecker::visitConstValAttr(ConstValAttr *attr) {
2848+
auto *VD = dyn_cast<VarDecl>(D);
2849+
if (VD) {
2850+
// FIXME: Do not allow 'var' on @const protocol requirements, only allow
2851+
// 'let' (once that's implemented to be allowed at all).
2852+
if (!VD->isLet() && !isa<ProtocolDecl>(D->getDeclContext())) {
2853+
diagnose(D->getStartLoc(), diag::attr_only_one_decl_kind,
2854+
attr, "let");
2855+
attr->setInvalid();
2856+
return;
2857+
}
2858+
}
2859+
}
2860+
2861+
void AttributeChecker::visitConstInitializedAttr(ConstInitializedAttr *attr) {
2862+
auto *VD = cast<VarDecl>(D);
2863+
2864+
if (D->getDeclContext()->isLocalContext()) {
2865+
diagnose(attr->getLocation(), diag::attr_only_at_non_local_scope,
2866+
attr->getAttrName());
2867+
} else
2868+
if (isa<ProtocolDecl>(D->getDeclContext())) {
2869+
diagnose(attr->getLocation(), diag::attr_unusable_in_protocol,
2870+
attr);
2871+
} else
2872+
if (!VD->isStatic() && !D->getDeclContext()->isModuleScopeContext()) {
2873+
diagnose(attr->getLocation(), diag::attr_only_on_static_properties,
2874+
attr->getAttrName());
2875+
} else
2876+
if (!VD->hasStorageOrWrapsStorage()) {
2877+
diagnose(attr->getLocation(), diag::attr_not_on_computed_properties,
2878+
attr);
2879+
}
2880+
}
2881+
28462882
/// Return true if this is a builtin operator that cannot be defined in user
28472883
/// code.
28482884
static bool isBuiltinOperator(StringRef name, DeclAttribute *attr) {

0 commit comments

Comments
 (0)