Skip to content

Commit a925d6a

Browse files
kubamracekartemcm
authored andcommitted
Add tests
1 parent d484ec7 commit a925d6a

17 files changed

+212
-0
lines changed

test/ConstValues/CImports.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Constant globals should "work" when referencing C-imported constants
2+
3+
// RUN: %empty-directory(%t)
4+
// RUN: split-file %s %t
5+
6+
// RUN: %target-swift-frontend -emit-ir -primary-file %t/main.swift -parse-as-library -import-bridging-header %t/bridging_header.h
7+
8+
//--- bridging_header.h
9+
10+
static const int c_integer = 42;
11+
static const long c_long = 42;
12+
13+
struct CStruct {
14+
int a, b, c, d;
15+
};
16+
17+
//--- main.swift
18+
19+
_const let constGlobal1: Int = Int(c_integer)
20+
_const let constGlobal2: Int = Int(c_long)
21+
_const let constGlobal3: Int = MemoryLayout<CStruct>.size

test/ConstValues/DiagModules.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Constant globals should "work" even when used across files in non-WMO builds.
2+
3+
// RUN: %empty-directory(%t)
4+
// RUN: split-file %s %t
5+
6+
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -parse-as-library
7+
// RUN: %target-swift-frontend -emit-ir -I %t %t/Main.swift -verify
8+
9+
//--- MyModule.swift
10+
11+
public func foo() -> Int {
12+
return 42
13+
}
14+
15+
//--- Main.swift
16+
17+
import MyModule
18+
19+
_const let constGlobal1: Int = foo()
20+
// expected-error@-1 {{_const let should be initialized with a compile-time value}}

test/ConstValues/DiagNotConst.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Constant globals rejected for not being constant values
2+
3+
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -verify
4+
5+
_const let a: Bool = Bool.random()
6+
// expected-error@-1 {{_const let should be initialized with a compile-time value}}
7+
8+
func foo() -> Int {
9+
return 42 * Int.random(in: 0 ..< 10)
10+
}
11+
12+
_const let b: Int = foo()
13+
// expected-error@-1 {{_const let should be initialized with a compile-time value}}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Constant globals referencing other constant globals and forming a cycle
2+
3+
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -verify
4+
5+
_const let a: Int = c
6+
_const let b: Int = a
7+
_const let c: Int = b
8+
// expected-error@-1 {{cycle in definitions of constant values}}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Constant globals on simple floating-point literals
2+
3+
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
4+
5+
_const let constGlobal1 = 42.0
6+
_const let constGlobal2: Double = 42.0
7+
_const let constGlobal3: Float = 42.0
8+
_const let constGlobal4: Float16 = 42.0

test/ConstValues/FunctionTypes.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Constant globals on function types / function pointers
2+
3+
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
4+
5+
func foo_void_to_void() {}
6+
func foo_int_to_int(x: Int) -> Int { return 42 }
7+
8+
_const let constGlobalA1: ()->() = { }
9+
_const let constGlobalA2: @convention(c) ()->() = { }
10+
_const let constGlobalA3: @convention(thin) ()->() = { }
11+
12+
_const let constGlobalB1: ()->() = foo_void_to_void
13+
_const let constGlobalB2: @convention(c) ()->() = foo_void_to_void
14+
_const let constGlobalB3: @convention(thin) ()->() = foo_void_to_void
15+
16+
_const let constGlobalC1: (Int)->(Int) = { _ in return 42 }
17+
_const let constGlobalC2: @convention(c) (Int)->(Int) = { _ in return 42 }
18+
_const let constGlobalC3: @convention(thin) (Int)->(Int) = { _ in return 42 }
19+
20+
_const let constGlobalD1: (Int)->(Int) = foo_int_to_int
21+
_const let constGlobalD2: @convention(c) (Int)->(Int) = foo_int_to_int
22+
_const let constGlobalD3: @convention(thin) (Int)->(Int) = foo_int_to_int

test/ConstValues/InlineArrays.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Constant globals on inline arrays
2+
3+
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -disable-availability-checking
4+
5+
_const let constGlobal1: InlineArray = [1, 2, 3]
6+
_const let constGlobal2: InlineArray = [1.0, 2.0, 3.0]
7+
_const let constGlobal3: InlineArray = constGlobal1
8+
_const let constGlobal4: Int = ([1, 2, 3] as InlineArray).count
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Constant globals on integer arithmetics
2+
3+
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
4+
5+
_const let constGlobal1: Int = 42
6+
_const let constGlobal2: UInt = 42 + 42
7+
_const let constGlobal3: UInt = 42 * 42
8+
_const let constGlobal4: UInt = 42 - 42
9+
_const let constGlobal5: UInt = 42 / 42
10+
_const let constGlobal6: UInt = 42 % 2
11+
_const let constGlobal7: UInt = (42 % 2)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Constant globals on integer expressions
2+
3+
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
4+
5+
_const let constGlobal1: Int = (42 + 42 + 42) / 3
6+
_const let constGlobal2: Int = MemoryLayout<UInt32>.size + 4
7+
_const let constGlobal3: Int = Int(17.0 / 3.5)
8+
_const let constGlobal4: Int = constGlobal1 + 1
9+
_const let constGlobal5: Int = -constGlobal1 + 1
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Constant globals on simple integer literals
2+
3+
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
4+
5+
_const let constGlobal1: Int = 42
6+
_const let constGlobal2: UInt = 42
7+
_const let constGlobal3: UInt = 0xf000f000
8+
#if _pointerBitWidth(_64)
9+
_const let constGlobal4: UInt = 0xf000f000_f000f000
10+
#endif
11+
_const let constGlobal5: UInt8 = 255
12+
_const let constGlobal6: UInt32 = 0xffff_ffff

0 commit comments

Comments
 (0)