Skip to content

Commit 24f2975

Browse files
authored
Merge pull request swiftlang#81510 from artemcm/ConstSyntacticVerify
[Compile Time Values] Add syntactic verification of valid expressions in `@const` contexts
2 parents e240dd5 + d8176a7 commit 24f2975

36 files changed

+582
-18
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,9 +816,34 @@ ERROR(cannot_return_value_from_void_func,none,
816816
NOTE(add_return_type_note,none,
817817
"did you mean to add a return type?", ())
818818

819-
ERROR(expect_compile_time_const,none,
819+
ERROR(expect_compile_time_literal,none,
820820
"expect a compile-time constant literal", ())
821821

822+
ERROR(const_unsupported_enum_associated_value,none,
823+
"enums with associated values not supported in a '@const' expression", ())
824+
ERROR(const_unsupported_operator,none,
825+
"unsupported operator in a '@const' expression", ())
826+
ERROR(const_unsupported_type,none,
827+
"unsupported type in a '@const' expression", ())
828+
ERROR(const_unsupported_type_expr,none,
829+
"type expressions not supported in a '@const' expression", ())
830+
ERROR(const_unsupported_closure,none,
831+
"closures not supported in a '@const' expression", ())
832+
ERROR(const_unsupported_keypath,none,
833+
"keypaths not supported in a '@const' expression", ())
834+
ERROR(const_opaque_decl_ref,none,
835+
"unable to resolve variable reference in a '@const' expression", ())
836+
ERROR(const_opaque_func_decl_ref,none,
837+
"unable to resolve function reference in a '@const' expression", ())
838+
ERROR(const_non_convention_c_conversion,none,
839+
"only 'convention(c)' function values are supported in a '@const' expression", ())
840+
ERROR(const_opaque_callee,none,
841+
"unable to resolve callee in a '@const' expression", ())
842+
ERROR(const_non_const_param,none,
843+
"reference to a non-'@const' parameter in a '@const' expression", ())
844+
ERROR(const_unknown_default,none,
845+
"not supported in a '@const' expression", ())
846+
822847
//------------------------------------------------------------------------------
823848
// MARK: Import Resolution
824849
//------------------------------------------------------------------------------

include/swift/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ EXPERIMENTAL_FEATURE(InlineArrayTypeSugar, false)
498498
/// Allow declaration of compile-time values
499499
EXPERIMENTAL_FEATURE(CompileTimeValues, true)
500500

501+
/// Allow declaration of compile-time values
502+
EXPERIMENTAL_FEATURE(CompileTimeValuesPreview, false)
503+
501504
/// Allow function body macros applied to closures.
502505
EXPERIMENTAL_FEATURE(ClosureBodyMacro, true)
503506

lib/AST/FeatureSet.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ static bool usesFeatureCompileTimeValues(Decl *decl) {
408408
decl->getAttrs().hasAttribute<ConstInitializedAttr>();
409409
}
410410

411+
static bool usesFeatureCompileTimeValuesPreview(Decl *decl) {
412+
return false;
413+
}
414+
411415
static bool usesFeatureClosureBodyMacro(Decl *decl) {
412416
return false;
413417
}

lib/Sema/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ add_swift_host_library(swiftSema STATIC
3838
DerivedConformance/DerivedConformanceRawRepresentable.cpp
3939
ImportResolution.cpp
4040
InstrumenterSupport.cpp
41+
LegalConstExprVerifier.cpp
4142
LookupVisibleDecls.cpp
4243
MiscDiagnostics.cpp
4344
OpenedExistentials.cpp

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6521,7 +6521,7 @@ bool ExtraneousReturnFailure::diagnoseAsError() {
65216521
}
65226522

65236523
bool NotCompileTimeLiteralFailure::diagnoseAsError() {
6524-
emitDiagnostic(diag::expect_compile_time_const);
6524+
emitDiagnostic(diag::expect_compile_time_literal);
65256525
return true;
65266526
}
65276527

0 commit comments

Comments
 (0)