blob: 930cea49efd901e6cb0b5e73d6c90bb452cfac77 [file] [log] [blame]
// RUN: %target-sil-opt -enable-experimental-static-assert %s -dataflow-diagnostics -verify
sil_stage canonical
import Builtin
import Swift
// Static assertion that "1 + 1 == 2".
sil @test1 : $@convention(thin) () -> () {
bb0:
%0 = integer_literal $Builtin.Int32, 1
%1 = builtin "add_Int32"(%0 : $Builtin.Int32, %0 : $Builtin.Int32) : $(Builtin.Int32)
%2 = integer_literal $Builtin.Int32, 2
%3 = builtin "cmp_eq_Int32"(%1 : $Builtin.Int32, %2 : $Builtin.Int32) : $(Builtin.Int1)
%4 = string_literal utf8 ""
%5 = builtin "poundAssert"(%3 : $Builtin.Int1, %4 : $Builtin.RawPointer) : $()
return undef : $()
}
// Static assertion that "2 + 2 == 5".
sil @test2 : $@convention(thin) () -> () {
bb0:
%0 = integer_literal $Builtin.Int32, 2
%1 = builtin "add_Int32"(%0 : $Builtin.Int32, %0 : $Builtin.Int32) : $(Builtin.Int32)
%2 = integer_literal $Builtin.Int32, 5
%3 = builtin "cmp_eq_Int32"(%1 : $Builtin.Int32, %2 : $Builtin.Int32) : $(Builtin.Int1)
%4 = string_literal utf8 ""
// expected-error @+1 {{assertion failed}}
%5 = builtin "poundAssert"(%3 : $Builtin.Int1, %4 : $Builtin.RawPointer) : $()
return undef : $()
}
// Tests that piecewise initialization of memory works, by piecewise
// initializing a tuple.
sil @piecewiseInit : $@convention(thin) () -> Bool {
bb0:
// Allocate and initialize the tuple to (1, 2).
%0 = alloc_stack $(Int64, Int64), var, name "tup"
%1 = tuple_element_addr %0 : $*(Int64, Int64), 0
%2 = tuple_element_addr %0 : $*(Int64, Int64), 1
%3 = integer_literal $Builtin.Int64, 1
%4 = struct $Int64 (%3 : $Builtin.Int64)
store %4 to %1 : $*Int64
%6 = integer_literal $Builtin.Int64, 2
%7 = struct $Int64 (%6 : $Builtin.Int64)
store %7 to %2 : $*Int64
// Read the first element from the tuple.
%9 = begin_access [read] [static] %0 : $*(Int64, Int64)
%10 = tuple_element_addr %9 : $*(Int64, Int64), 0
%11 = load %10 : $*Int64
end_access %9 : $*(Int64, Int64)
// Check that the first element is what we put in.
%13 = struct_extract %11 : $Int64, #Int64._value
%14 = builtin "cmp_eq_Int64"(%3 : $Builtin.Int64, %13 : $Builtin.Int64) : $Builtin.Int1
%15 = struct $Bool (%14 : $Builtin.Int1)
// Deallocate and return.
dealloc_stack %0 : $*(Int64, Int64)
return %15 : $Bool
}
// Tests copy_addr interpretation.
sil @copyAddr : $@convention(thin) () -> Bool {
// Allocate an initialize an Int64 to 1.
%0 = alloc_stack $Int64
%1 = integer_literal $Builtin.Int64, 1
%2 = struct $Int64 (%1 : $Builtin.Int64)
store %2 to %0 : $*Int64
// Allocate another Int64 and copy to it.
%4 = alloc_stack $Int64
copy_addr %0 to %4 : $*Int64
// Check that the value is what we put in the original Int64.
%5 = begin_access [read] [static] %4 : $*Int64
%6 = load %5 : $*Int64
end_access %5 : $*Int64
%8 = struct_extract %6 : $Int64, #Int64._value
%9 = builtin "cmp_eq_Int64"(%1 : $Builtin.Int64, %8 : $Builtin.Int64) : $Builtin.Int1
%10 = struct $Bool (%9 : $Builtin.Int1)
// Deallocate and return.
dealloc_stack %4 : $*Int64
dealloc_stack %0 : $*Int64
return %10 : $Bool
}
sil @invokeTests : $@convention(thin) () -> () {
%0 = function_ref @piecewiseInit : $@convention(thin) () -> Bool
%1 = apply %0() : $@convention(thin) () -> Bool
%2 = struct_extract %1 : $Bool, #Bool._value
%3 = string_literal utf8 ""
%4 = builtin "poundAssert"(%2 : $Builtin.Int1, %3 : $Builtin.RawPointer) : $()
%5 = function_ref @copyAddr : $@convention(thin) () -> Bool
%6 = apply %5() : $@convention(thin) () -> Bool
%7 = struct_extract %6 : $Bool, #Bool._value
%8 = string_literal utf8 ""
%9 = builtin "poundAssert"(%7 : $Builtin.Int1, %8 : $Builtin.RawPointer) : $()
%ret = tuple ()
return %ret : $()
}