[Compile Time Values] Rebase on top of newly-added experimental feature for the '@const' attribute

This commit is contained in:
Artem Chikin
2025-03-04 12:42:27 -08:00
parent b8ef3ff779
commit 114af54875
20 changed files with 181 additions and 176 deletions

View File

@@ -58,10 +58,10 @@ private:
// compile-time known values
verifyGlobals();
// Verify _const lets appearing as local variables
// Verify @const lets appearing as local variables
verifyLocals();
// For each function call, ensure arguments to _const parameters
// For each function call, ensure arguments to @const parameters
// are all compile-time known values
verifyCallArguments();
}
@@ -95,7 +95,7 @@ private:
SILGlobalVariable &Global,
VarDecl *Decl) {
LLVM_DEBUG(llvm::dbgs()
<< "_const static let " << Decl->getName().str().str()
<< "@const static let " << Decl->getName().str().str()
<< ": " << Decl->getTypeInContext().getString() << " = ";);
auto StaticInitializerValue = Global.getStaticInitializerValue();
assert(StaticInitializerValue && "Expected a static initializer");
@@ -115,7 +115,7 @@ private:
void verifyInitializeOnceGlobal(ConstExprFunctionState &ConstExprState,
SILGlobalVariable &Global, VarDecl *Decl) {
LLVM_DEBUG(llvm::dbgs()
<< "_const [init_once] let " << Decl->getName().str().str()
<< "@const [init_once] let " << Decl->getName().str().str()
<< ": " << Decl->getTypeInContext().getString() << " = ";);
SILModule *M = getModule();
for (SILFunction &Fn : *M) {
@@ -164,7 +164,7 @@ private:
// statically with compile-time known values
for (SILGlobalVariable &G : M->getSILGlobals()) {
if (auto Decl = G.getDecl()) {
if (Decl->getAttrs().getAttribute<CompileTimeConstAttr>()) {
if (Decl->isConstVal()) {
if (G.getStaticInitializerValue())
verifyStaticallyInitializedGlobal(ConstExprState, G, Decl);
else
@@ -176,17 +176,17 @@ private:
void verifyLocal(DebugValueInst *DBI) {
auto Decl = DBI->getDecl();
if (!Decl || !Decl->isCompileTimeConst())
if (!Decl || !Decl->isConstVal())
return;
auto Value = ConstExprState.getConstantValue(DBI->getOperand());
LLVM_DEBUG(llvm::dbgs()
<< "_const let " << Decl->getName().str().str() << ": "
<< "@const let " << Decl->getName().str().str() << ": "
<< Decl->getTypeInContext().getString() << " = ";);
LLVM_DEBUG(printSymbolicValueValue(Value, Allocator););
if (!Value.isConstant()) {
getModule()->getASTContext().Diags.diagnose(
Decl->getParentInitializer()->getStartLoc(),
Decl->getStartLoc(),
diag::require_const_arg_for_parameter);
}
}
@@ -200,7 +200,7 @@ private:
}
void verifyCallArguments() {
// Find all calls to functions which have _const parameters
// Find all calls to functions which have @const parameters
for (SILFunction &Fn : *getModule())
for (SILBasicBlock &BB : Fn)
for (SILInstruction &I : BB)
@@ -220,29 +220,31 @@ private:
auto CalleeParameters = CalleeDecl->getParameters();
auto ApplyArgRefs = Apply->getArguments();
// llvm::dbgs() << "\n-------------------------------------------\n";
// llvm::dbgs() << "Apply: ";
// Apply->dump();
// llvm::dbgs() << CalleeDecl->getNameStr() << "\n";
// llvm::dbgs() << "Apply Args: ";
// llvm::dbgs() << ApplyArgRefs.size() << "\n";
// llvm::dbgs() << "CalleeParameters: ";
// llvm::dbgs() << CalleeParameters->size() << "\n";
// llvm::dbgs() << "ArgumentOperandNumber: ";
// llvm::dbgs() << Apply->getArgumentOperandNumber() << "\n";
// LLVM_DEBUG({
// llvm::dbgs() << "\n-------------------------------------------\n";
// llvm::dbgs() << "Apply: ";
// Apply->dump();
// llvm::dbgs() << CalleeDecl->getNameStr() << "\n";
// llvm::dbgs() << "Apply Args: ";
// llvm::dbgs() << ApplyArgRefs.size() << "\n";
// llvm::dbgs() << "CalleeParameters: ";
// llvm::dbgs() << CalleeParameters->size() << "\n";
// llvm::dbgs() << "ArgumentOperandNumber: ";
// llvm::dbgs() << Apply->getArgumentOperandNumber() << "\n";
// });
// TOOD: Needs work to correctly match params to args
// (AC) TODO: Needs work to correctly match params to args
bool hasConst = false;
for (size_t i = 0; i < CalleeParameters->size(); ++i)
if (CalleeParameters->get(i)->isCompileTimeConst())
if (CalleeParameters->get(i)->isConstVal())
hasConst = true;
if (hasConst) {
for (size_t i = 0; i < CalleeParameters->size(); ++i) {
auto CorrespondingArg = ApplyArgRefs[i];
if (CalleeParameters->get(i)->isCompileTimeConst()) {
if (CalleeParameters->get(i)->isConstVal()) {
LLVM_DEBUG({
llvm::dbgs() << "[" << CalleeDecl->getNameStr() << "] argument: ";
llvm::dbgs() << "Argument of fn{" << CalleeDecl->getNameStr() << "} ";
llvm::dbgs() << CalleeParameters->get(i)->getNameStr() << ": ";
std::string typeName;
llvm::raw_string_ostream out(typeName);

View File

@@ -1,9 +1,9 @@
// Constant globals should "work" when referencing C-imported constants
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// RUN: %target-swift-frontend -emit-ir -primary-file %t/main.swift -parse-as-library -import-bridging-header %t/bridging_header.h
// RUN: %target-swift-frontend -emit-ir -primary-file %t/main.swift -parse-as-library -import-bridging-header %t/bridging_header.h -enable-experimental-feature CompileTimeValues
//--- bridging_header.h
@@ -16,6 +16,6 @@ struct CStruct {
//--- main.swift
_const let constGlobal1: Int = Int(c_integer)
_const let constGlobal2: Int = Int(c_long)
_const let constGlobal3: Int = MemoryLayout<CStruct>.size
@const let constGlobal1: Int = Int(c_integer)
@const let constGlobal2: Int = Int(c_long)
@const let constGlobal3: Int = MemoryLayout<CStruct>.size

View File

@@ -1,32 +1,32 @@
// Constant globals on comparisons and conditions
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -pri mary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
@const let constGlobal1: Int = true ? 1 : 0
@const let constGlobal2: Int = (2 * 4 == 8) ? 1 : 0
@const let constGlobal3: Int = (10 > 20) ? 10 : 20
//@const let constGlobal4: Int = max(10, 20)
@const let constGlobal5: Bool = 10 > 20
@const let constGlobal6: Int = constGlobal5 ? 10 : 20
_const let constGlobal1: Int = true ? 1 : 0
_const let constGlobal2: Int = (2 * 4 == 8) ? 1 : 0
_const let constGlobal3: Int = (10 > 20) ? 10 : 20
//_const let constGlobal4: Int = max(10, 20)
_const let constGlobal5: Bool = 10 > 20
_const let constGlobal6: Int = constGlobal5 ? 10 : 20
@const let number: UInt8 = 0xf0
_const let number: UInt8 = 0xf0
@const let bit0: Bool = (number & 0b0000_0001) != 0
@const let bit1: Bool = (number & 0b0000_0010) != 0
@const let bit2: Bool = (number & 0b0000_0100) != 0
@const let bit3: Bool = (number & 0b0000_1000) != 0
@const let bit4: Bool = (number & 0b0001_0000) != 0
@const let bit5: Bool = (number & 0b0010_0000) != 0
@const let bit6: Bool = (number & 0b0100_0000) != 0
@const let bit7: Bool = (number & 0b1000_0000) != 0
_const let bit0: Bool = (number & 0b0000_0001) != 0
_const let bit1: Bool = (number & 0b0000_0010) != 0
_const let bit2: Bool = (number & 0b0000_0100) != 0
_const let bit3: Bool = (number & 0b0000_1000) != 0
_const let bit4: Bool = (number & 0b0001_0000) != 0
_const let bit5: Bool = (number & 0b0010_0000) != 0
_const let bit6: Bool = (number & 0b0100_0000) != 0
_const let bit7: Bool = (number & 0b1000_0000) != 0
@const let bits0_0: UInt8 = ((bit0 ? 0b0000_0001 : 0b0000_0000) << 0)
@const let bits0_1: UInt8 = bits0_0 | ((bit1 ? 0b0000_0001 : 0b0000_0000) << 1)
@const let bits0_2: UInt8 = bits0_1 | ((bit2 ? 0b0000_0001 : 0b0000_0000) << 2)
@const let bits0_3: UInt8 = bits0_2 | ((bit3 ? 0b0000_0001 : 0b0000_0000) << 3)
@const let bits0_4: UInt8 = bits0_3 | ((bit4 ? 0b0000_0001 : 0b0000_0000) << 4)
@const let bits0_5: UInt8 = bits0_4 | ((bit5 ? 0b0000_0001 : 0b0000_0000) << 5)
@const let bits0_6: UInt8 = bits0_5 | ((bit6 ? 0b0000_0001 : 0b0000_0000) << 6)
@const let bits0_7: UInt8 = bits0_6 | ((bit7 ? 0b0000_0001 : 0b0000_0000) << 7)
_const let bits0_0: UInt8 = ((bit0 ? 0b0000_0001 : 0b0000_0000) << 0)
_const let bits0_1: UInt8 = bits0_0 | ((bit1 ? 0b0000_0001 : 0b0000_0000) << 1)
_const let bits0_2: UInt8 = bits0_1 | ((bit2 ? 0b0000_0001 : 0b0000_0000) << 2)
_const let bits0_3: UInt8 = bits0_2 | ((bit3 ? 0b0000_0001 : 0b0000_0000) << 3)
_const let bits0_4: UInt8 = bits0_3 | ((bit4 ? 0b0000_0001 : 0b0000_0000) << 4)
_const let bits0_5: UInt8 = bits0_4 | ((bit5 ? 0b0000_0001 : 0b0000_0000) << 5)
_const let bits0_6: UInt8 = bits0_5 | ((bit6 ? 0b0000_0001 : 0b0000_0000) << 6)
_const let bits0_7: UInt8 = bits0_6 | ((bit7 ? 0b0000_0001 : 0b0000_0000) << 7)
_const let numberBackIsRight: Bool = bits0_7 == 0xf0
@const let numberBackIsRight: Bool = bits0_7 == 0xf0

View File

@@ -1,10 +1,10 @@
// Constant globals should "work" even when used across files in non-WMO builds.
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -parse-as-library
// RUN: %target-swift-frontend -emit-ir -I %t %t/Main.swift -verify
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -parse-as-library -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -I %t %t/Main.swift -verify -enable-experimental-feature CompileTimeValues
//--- MyModule.swift
@@ -16,5 +16,5 @@ public func foo() -> Int {
import MyModule
_const let constGlobal1: Int = foo()
// expected-error@-1 {{_const let should be initialized with a compile-time value}}
@const let constGlobal1: Int = foo()
// expected-error@-1 {{@const let should be initialized with a compile-time value}}

View File

@@ -1,13 +1,13 @@
// Constant globals rejected for not being constant values
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -verify -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -verify
_const let a: Bool = Bool.random()
// expected-error@-1 {{_const let should be initialized with a compile-time value}}
@const let a: Bool = Bool.random()
// expected-error@-1 {{@const let should be initialized with a compile-time value}}
func foo() -> Int {
return 42 * Int.random(in: 0 ..< 10)
}
_const let b: Int = foo()
// expected-error@-1 {{_const let should be initialized with a compile-time value}}
@const let b: Int = foo()
// expected-error@-1 {{@const let should be initialized with a compile-time value}}

View File

@@ -1,8 +1,8 @@
// Constant globals referencing other constant globals and forming a cycle
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -verify -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -verify
_const let a: Int = c
_const let b: Int = a
_const let c: Int = b
@const let a: Int = c
@const let b: Int = a
@const let c: Int = b
// expected-error@-1 {{cycle in definitions of constant values}}

View File

@@ -1,8 +1,8 @@
// Constant globals on simple floating-point literals
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
_const let constGlobal1 = 42.0
_const let constGlobal2: Double = 42.0
_const let constGlobal3: Float = 42.0
_const let constGlobal4: Float16 = 42.0
@const let constGlobal1 = 42.0
@const let constGlobal2: Double = 42.0
@const let constGlobal3: Float = 42.0
@const let constGlobal4: Float16 = 42.0

View File

@@ -1,22 +1,22 @@
// Constant globals on function types / function pointers
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
func foo_void_to_void() {}
func foo_int_to_int(x: Int) -> Int { return 42 }
_const let constGlobalA1: ()->() = { }
_const let constGlobalA2: @convention(c) ()->() = { }
_const let constGlobalA3: @convention(thin) ()->() = { }
@const let constGlobalA1: ()->() = { }
@const let constGlobalA2: @convention(c) ()->() = { }
@const let constGlobalA3: @convention(thin) ()->() = { }
_const let constGlobalB1: ()->() = foo_void_to_void
_const let constGlobalB2: @convention(c) ()->() = foo_void_to_void
_const let constGlobalB3: @convention(thin) ()->() = foo_void_to_void
@const let constGlobalB1: ()->() = foo_void_to_void
@const let constGlobalB2: @convention(c) ()->() = foo_void_to_void
@const let constGlobalB3: @convention(thin) ()->() = foo_void_to_void
_const let constGlobalC1: (Int)->(Int) = { _ in return 42 }
_const let constGlobalC2: @convention(c) (Int)->(Int) = { _ in return 42 }
_const let constGlobalC3: @convention(thin) (Int)->(Int) = { _ in return 42 }
@const let constGlobalC1: (Int)->(Int) = { _ in return 42 }
@const let constGlobalC2: @convention(c) (Int)->(Int) = { _ in return 42 }
@const let constGlobalC3: @convention(thin) (Int)->(Int) = { _ in return 42 }
_const let constGlobalD1: (Int)->(Int) = foo_int_to_int
_const let constGlobalD2: @convention(c) (Int)->(Int) = foo_int_to_int
_const let constGlobalD3: @convention(thin) (Int)->(Int) = foo_int_to_int
@const let constGlobalD1: (Int)->(Int) = foo_int_to_int
@const let constGlobalD2: @convention(c) (Int)->(Int) = foo_int_to_int
@const let constGlobalD3: @convention(thin) (Int)->(Int) = foo_int_to_int

View File

@@ -1,8 +1,8 @@
// Constant globals on inline arrays
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -disable-availability-checking -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -disable-availability-checking
_const let constGlobal1: InlineArray = [1, 2, 3]
_const let constGlobal2: InlineArray = [1.0, 2.0, 3.0]
_const let constGlobal3: InlineArray = constGlobal1
_const let constGlobal4: Int = ([1, 2, 3] as InlineArray).count
@const let constGlobal1: InlineArray = [1, 2, 3]
@const let constGlobal2: InlineArray = [1.0, 2.0, 3.0]
@const let constGlobal3: InlineArray = constGlobal1
@const let constGlobal4: Int = ([1, 2, 3] as InlineArray).count

View File

@@ -1,11 +1,11 @@
// Constant globals on integer arithmetics
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
_const let constGlobal1: Int = 42
_const let constGlobal2: UInt = 42 + 42
_const let constGlobal3: UInt = 42 * 42
_const let constGlobal4: UInt = 42 - 42
_const let constGlobal5: UInt = 42 / 42
_const let constGlobal6: UInt = 42 % 2
_const let constGlobal7: UInt = (42 % 2)
@const let constGlobal1: Int = 42
@const let constGlobal2: UInt = 42 + 42
@const let constGlobal3: UInt = 42 * 42
@const let constGlobal4: UInt = 42 - 42
@const let constGlobal5: UInt = 42 / 42
@const let constGlobal6: UInt = 42 % 2
@const let constGlobal7: UInt = (42 % 2)

View File

@@ -1,9 +1,9 @@
// Constant globals on integer expressions
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
_const let constGlobal1: Int = (42 + 42 + 42) / 3
_const let constGlobal2: Int = MemoryLayout<UInt32>.size + 4
_const let constGlobal3: Int = Int(17.0 / 3.5)
_const let constGlobal4: Int = constGlobal1 + 1
_const let constGlobal5: Int = -constGlobal1 + 1
@const let constGlobal1: Int = (42 + 42 + 42) / 3
@const let constGlobal2: Int = MemoryLayout<UInt32>.size + 4
@const let constGlobal3: Int = Int(17.0 / 3.5)
@const let constGlobal4: Int = constGlobal1 + 1
@const let constGlobal5: Int = -constGlobal1 + 1

View File

@@ -1,12 +1,12 @@
// Constant globals on simple integer literals
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
_const let constGlobal1: Int = 42
_const let constGlobal2: UInt = 42
_const let constGlobal3: UInt = 0xf000f000
@const let constGlobal1: Int = 42
@const let constGlobal2: UInt = 42
@const let constGlobal3: UInt = 0xf000f000
#if _pointerBitWidth(_64)
_const let constGlobal4: UInt = 0xf000f000_f000f000
@const let constGlobal4: UInt = 0xf000f000_f000f000
#endif
_const let constGlobal5: UInt8 = 255
_const let constGlobal6: UInt32 = 0xffff_ffff
@const let constGlobal5: UInt8 = 255
@const let constGlobal6: UInt32 = 0xffff_ffff

View File

@@ -1,14 +1,14 @@
// Constant values should be able to call "const" functions from other modules
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -parse-as-library
// RUN: %target-swift-frontend -emit-ir -I %t %t/Main.swift
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -parse-as-library -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -I %t %t/Main.swift -enable-experimental-feature CompileTimeValues
//--- MyModule.swift
_const
@const
public func foo() -> Int {
return 42
}
@@ -17,4 +17,4 @@ public func foo() -> Int {
import MyModule
_const let constGlobal1: Int = foo()
@const let constGlobal1: Int = foo()

View File

@@ -1,14 +1,14 @@
// Constant globals should "work" even when used across files in non-WMO builds.
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// RUN: %target-swift-frontend -emit-ir -primary-file %s/file1.swift -parse-as-library
// RUN: %target-swift-frontend -emit-ir -primary-file %s/file1.swift -parse-as-library -enable-experimental-feature CompileTimeValues
//--- file1.swift
_const let constGlobal1: Int = 42
@const let constGlobal1: Int = 42
//--- file2.swift
_const let constGlobal2: Int = constGlobal1
@const let constGlobal2: Int = constGlobal1

View File

@@ -1,14 +1,14 @@
// Constant globals on optionals
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
_const let constGlobal1: Int? = 42
_const let constGlobal2: Int? = nil
_const let constGlobal3: UInt8? = 42
_const let constGlobal4: UInt8? = nil
_const let constGlobal5: Bool? = true
_const let constGlobal6: Bool? = nil
_const let constGlobal7: (Int, Int)? = (42, 42)
_const let constGlobal8: (Int, Int)? = nil
_const let constGlobal9: String? = "hello"
_const let constGlobal10: String? = nil
@const let constGlobal1: Int? = 42
@const let constGlobal2: Int? = nil
@const let constGlobal3: UInt8? = 42
@const let constGlobal4: UInt8? = nil
@const let constGlobal5: Bool? = true
@const let constGlobal6: Bool? = nil
@const let constGlobal7: (Int, Int)? = (42, 42)
@const let constGlobal8: (Int, Int)? = nil
@const let constGlobal9: String? = "hello"
@const let constGlobal10: String? = nil

View File

@@ -1,8 +1,8 @@
// Constant globals referencing other constant globals in their initializer expressions
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
_const let a: Int = 42
_const let b: Int = a
_const let c: Int = b
_const let d: Int = -a
@const let a: Int = 42
@const let b: Int = a
@const let c: Int = b
@const let d: Int = -a

View File

@@ -1,7 +1,7 @@
// Constant globals on strings
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
_const let constGlobal1: String = "hello"
_const let constGlobal2: StaticString = "hello"
_const let constGlobal3: Int = "hello".count
@const let constGlobal1: String = "hello"
@const let constGlobal2: StaticString = "hello"
@const let constGlobal3: Int = "hello".count

View File

@@ -1,6 +1,7 @@
// Constant globals should "work" even in top-level code mode.
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
_const let constGlobal: Int = 42
@const let constGlobal: Int = 42

View File

@@ -1,11 +1,12 @@
// Constant globals on tuples
// REQUIRES: swift_feature_CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
_const let constGlobal1: Int = 42
_const let constGlobal2: (Int, Int) = (42, 42)
_const let constGlobal3: (Int, Bool) = (42, true)
_const let constGlobal4: (Int, (Int, Int)) = (42, (42, 42))
_const let constGlobal5: (Int, Float) = (42, 42.0)
_const let constGlobal6: (Int, String) = (42, "Foo")
_const let constGlobal7: (UInt64, StaticString, @convention(c) ()->Int) = (42, "hi", { return 42 })
@const let constGlobal1: Int = 42
@const let constGlobal2: (Int, Int) = (42, 42)
@const let constGlobal3: (Int, Bool) = (42, true)
@const let constGlobal4: (Int, (Int, Int)) = (42, (42, 42))
@const let constGlobal5: (Int, Float) = (42, 42.0)
@const let constGlobal6: (Int, String) = (42, "Foo")
@const let constGlobal7: (UInt64, StaticString, @convention(c) ()->Int) = (42, "hi", { return 42 })

View File

@@ -1,5 +1,4 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -c -Onone -verify %s -o %t/test.o
// RUN: %target-typecheck-verify-swift
public func takeIntConst(_ a: _const Int) {}
public func takeStringConst(_ a: _const String) {}
@@ -46,16 +45,16 @@ protocol ConstFan {
static _const var v: String { get } // expected-note {{protocol requires property 'v' with type 'String'}}
}
// ACTODO: Move to new test
//class ConstFanClass1: ConstFan { // expected-AC-error {{type 'ConstFanClass1' does not conform to protocol 'ConstFan'}} expected-note {{add stubs for conformance}}
//static let v: String = "" // expected-AC-note {{candidate operates as non-const, not const as required}}
//}
class ConstFanClass1: ConstFan { // expected-error {{type 'ConstFanClass1' does not conform to protocol 'ConstFan'}} expected-note {{add stubs for conformance}}
static let v: String = "" // expected-note {{candidate operates as non-const, not const as required}}
}
class ConstFanClassCorrect: ConstFan {
static _const let v: String = ""
}
class ConstFanClassWrong1: ConstFan {
static _const let v: String // expected-error {{_const let should be initialized with a literal value}}
// expected-error@-1 {{'static let' declaration requires an initializer expression or an explicitly stated getter}}
// expected-note@-2 {{add an initializer to silence this error}}
@@ -65,22 +64,24 @@ class ConstFanClassWrong2: ConstFan {
static _const let v: String = "\(v)" // expected-error {{_const let should be initialized with a literal value}}
}
// ACTODO: Move to new test
//class ConstFanClassWrong3: ConstFan {
// static _const var v: String = "" // expected-AC-error {{let is required for a _const variable declaration}}
//}
class ConstFanClassWrong2: ConstFan {
static _const let v: String = "\(v)" // expected-error {{_const let should be initialized with a compile-time literal}}
}
class ConstFanClassWrong3: ConstFan {
static _const var v: String = "" // expected-error {{let is required for a _const variable declaration}}
}
class ConstFanClassWrong4: ConstFan {
static func giveMeString() -> String { return "" }
static _const let v: String = giveMeString() // expected-error {{_const let should be initialized with a literal value}}
}
// ACTODO: Move to new test
//_const let globalConst = 3
_const let globalConst = 3
class ConstFanClassWrong5 {
func foo() -> Int {
_const let localConst = 3
return /*globalConst + */localConst
return globalConst + localConst
}
}