Add tests

This commit is contained in:
Kuba Mracek
2025-03-03 11:13:08 -08:00
committed by Artem Chikin
parent d484ec7c1f
commit a925d6af20
17 changed files with 212 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
// Constant globals should "work" when referencing C-imported constants
// 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
//--- bridging_header.h
static const int c_integer = 42;
static const long c_long = 42;
struct CStruct {
int a, b, c, d;
};
//--- main.swift
_const let constGlobal1: Int = Int(c_integer)
_const let constGlobal2: Int = Int(c_long)
_const let constGlobal3: Int = MemoryLayout<CStruct>.size

View File

@@ -0,0 +1,20 @@
// Constant globals should "work" even when used across files in non-WMO builds.
// 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
//--- MyModule.swift
public func foo() -> Int {
return 42
}
//--- Main.swift
import MyModule
_const let constGlobal1: Int = foo()
// expected-error@-1 {{_const let should be initialized with a compile-time value}}

View File

@@ -0,0 +1,13 @@
// Constant globals rejected for not being constant values
// 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}}
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}}

View File

@@ -0,0 +1,8 @@
// Constant globals referencing other constant globals and forming a cycle
// 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
// expected-error@-1 {{cycle in definitions of constant values}}

View File

@@ -0,0 +1,8 @@
// Constant globals on simple floating-point literals
// 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

View File

@@ -0,0 +1,22 @@
// Constant globals on function types / function pointers
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
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 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 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

@@ -0,0 +1,8 @@
// Constant globals on inline arrays
// 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

View File

@@ -0,0 +1,11 @@
// Constant globals on integer arithmetics
// 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)

View File

@@ -0,0 +1,9 @@
// Constant globals on integer expressions
// 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

View File

@@ -0,0 +1,12 @@
// Constant globals on simple integer literals
// 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
#if _pointerBitWidth(_64)
_const let constGlobal4: UInt = 0xf000f000_f000f000
#endif
_const let constGlobal5: UInt8 = 255
_const let constGlobal6: UInt32 = 0xffff_ffff

View File

@@ -0,0 +1,20 @@
// Constant values should be able to call "const" functions from other modules
// 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
//--- MyModule.swift
_const
public func foo() -> Int {
return 42
}
//--- Main.swift
import MyModule
_const let constGlobal1: Int = foo()

View File

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

View File

@@ -0,0 +1,14 @@
// Constant globals on optionals
// 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

View File

@@ -0,0 +1,8 @@
// Constant globals referencing other constant globals in their initializer expressions
// 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

View File

@@ -0,0 +1,7 @@
// Constant globals on strings
// 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

View File

@@ -0,0 +1,6 @@
// Constant globals should "work" even in top-level code mode.
// 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

View File

@@ -0,0 +1,11 @@
// Constant globals on tuples
// 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 })