Skip to content

Commit 13a8d4b

Browse files
committed
[Compile Time Values] Add a new experimental feature and the parsing of the '@const' attribute
1 parent de26e96 commit 13a8d4b

File tree

9 files changed

+50
-1
lines changed

9 files changed

+50
-1
lines changed

include/swift/AST/DeclAttr.def

+6-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,12 @@ DECL_ATTR(execution, Execution,
547547
166)
548548
DECL_ATTR_FEATURE_REQUIREMENT(Execution, ExecutionAttribute)
549549

550-
LAST_DECL_ATTR(Execution)
550+
SIMPLE_DECL_ATTR(const, ConstVal,
551+
OnParam | OnVar | OnFunc | ABIStableToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove,
552+
167)
553+
DECL_ATTR_FEATURE_REQUIREMENT(ConstVal, CompileTimeValues)
554+
555+
LAST_DECL_ATTR(ConstVal)
551556

552557
#undef DECL_ATTR_ALIAS
553558
#undef CONTEXTUAL_DECL_ATTR_ALIAS

include/swift/Basic/Features.def

+3
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ EXPERIMENTAL_FEATURE(IsolatedConformances, true)
476476
/// Syntax sugar features for concurrency.
477477
EXPERIMENTAL_FEATURE(ConcurrencySyntaxSugar, true)
478478

479+
/// Allow declaration of compile-time values
480+
EXPERIMENTAL_FEATURE(CompileTimeValues, true)
481+
479482
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
480483
#undef EXPERIMENTAL_FEATURE
481484
#undef UPCOMING_FEATURE

lib/AST/ASTDumper.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -4811,6 +4811,7 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
48114811
TRIVIAL_ATTR_PRINTER(Borrowed, borrowed)
48124812
TRIVIAL_ATTR_PRINTER(Borrowing, borrowing)
48134813
TRIVIAL_ATTR_PRINTER(CompileTimeLiteral, compile_time_literal)
4814+
TRIVIAL_ATTR_PRINTER(ConstVal, compile_time_value)
48144815
TRIVIAL_ATTR_PRINTER(CompilerInitialized, compiler_initialized)
48154816
TRIVIAL_ATTR_PRINTER(Consuming, consuming)
48164817
TRIVIAL_ATTR_PRINTER(Convenience, convenience)

lib/AST/FeatureSet.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ static bool usesFeatureConcurrencySyntaxSugar(Decl *decl) {
354354
return false;
355355
}
356356

357+
static bool usesFeatureCompileTimeValues(Decl *decl) {
358+
return decl->getAttrs().hasAttribute<ConstValAttr>();
359+
}
360+
357361
static bool usesFeatureMemorySafetyAttributes(Decl *decl) {
358362
if (decl->getAttrs().hasAttribute<SafeAttr>() ||
359363
decl->getAttrs().hasAttribute<UnsafeAttr>())

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ extension ASTGenVisitor {
199199
.atRethrows,
200200
.borrowed,
201201
.compilerInitialized,
202+
.constVal,
202203
.dynamicCallable,
203204
.eagerMove,
204205
.exported,

lib/Sema/TypeCheckAttr.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
434434
void visitFinalAttr(FinalAttr *attr);
435435
void visitMoveOnlyAttr(MoveOnlyAttr *attr);
436436
void visitCompileTimeLiteralAttr(CompileTimeLiteralAttr *attr) {}
437+
void visitConstValAttr(ConstValAttr *attr) {}
437438
void visitIBActionAttr(IBActionAttr *attr);
438439
void visitIBSegueActionAttr(IBSegueActionAttr *attr);
439440
void visitLazyAttr(LazyAttr *attr);

lib/Sema/TypeCheckDeclOverride.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,7 @@ namespace {
17251725

17261726
UNINTERESTING_ATTR(NoMetadata)
17271727
UNINTERESTING_ATTR(CompileTimeLiteral)
1728+
UNINTERESTING_ATTR(ConstVal)
17281729

17291730
UNINTERESTING_ATTR(BackDeployed)
17301731
UNINTERESTING_ATTR(KnownToBeLocal)

test/Parse/const.swift

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature CompileTimeValues
2+
// REQUIRES: swift_feature_CompileTimeValues
3+
@const let x: Int = 42
4+
5+
// FIXME: Only allow 'let' for `@const` properties, even in protocol requirements
6+
protocol ConstUserProto {
7+
@const static var v: String { get }
8+
}
9+
10+
class ConstFanClassWrong: ConstUserProto {
11+
@const static let v: String = ""
12+
// FIXME: Only allow 'let' for `@const` properties
13+
@const static var B: String = ""
14+
}
15+
16+
func takeIntConst(@const _ a: Int) {}
17+
18+
@const func constFunc(_ a: Int) {}
19+
20+
struct Article {
21+
let id: String
22+
}
23+
@const let keypath = \Article.id
24+
25+
func LocalConstVarUser() -> Int {
26+
@const let localConst = 3
27+
return localConst + 1
28+
}
29+
30+
// FIXME: This should be diagnosed
31+
@const let a: Bool = Bool.random()

test/Parse/const_no_feature.swift

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// RUN: %target-typecheck-verify-swift
2+
@const let x: Int = 42 // expected-error{{'const' attribute is only valid when experimental feature CompileTimeValues is enabled}}

0 commit comments

Comments
 (0)