mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
141 lines
9.4 KiB
Plaintext
141 lines
9.4 KiB
Plaintext
// RUN: rm -rf %t && mkdir -p %t && %S/../../utils/gyb %s -o %t/main.swift
|
|
// RUN: %S/../../utils/line-directive %t/main.swift -- %target-swift-frontend -parse -verify %t/main.swift
|
|
|
|
// Pending a fix for <rdar://problem/19434979>, remove exact diagnostic strings.
|
|
|
|
func testUnaryMinusInUnsigned() {
|
|
var a: UInt8 = -(1) // expected-error {{}}
|
|
// expected-note @-1 ? {{overloads for '-' exist with these partially matching parameter lists: }}
|
|
|
|
var b: UInt16 = -(1) // expected-error {{}}
|
|
// expected-note @-1 ? {{overloads for '-' exist with these partially matching parameter lists: }}
|
|
|
|
var c: UInt32 = -(1) // expected-error {{}}
|
|
// expected-note @-1 ? {{overloads for '-' exist with these partially matching parameter lists: }}
|
|
|
|
var d: UInt64 = -(1) // expected-error {{}}
|
|
// expected-note @-1 ? {{overloads for '-' exist with these partially matching parameter lists: }}
|
|
}
|
|
|
|
// Int and UInt are not identical to any fixed-size integer type
|
|
var i : Int = 0
|
|
var w : Word = i
|
|
var i64 : Int64 = i // expected-error {{'Int' is not convertible to 'Int64'}}
|
|
var i32 : Int32 = i // expected-error {{'Int' is not convertible to 'Int32'}}
|
|
var i16 : Int16 = i // expected-error {{'Int' is not convertible to 'Int16'}}
|
|
var i8 : Int8 = i // expected-error {{'Int' is not convertible to 'Int8'}}
|
|
|
|
var u : UInt = 0
|
|
var uw : UWord = u
|
|
var u64 : UInt64 = u // expected-error {{'UInt' is not convertible to 'UInt64'}}
|
|
var u32 : UInt32 = u // expected-error {{'UInt' is not convertible to 'UInt32'}}
|
|
var u16 : UInt16 = u // expected-error {{'UInt' is not convertible to 'UInt16'}}
|
|
var u8 : UInt8 = u // expected-error {{'UInt' is not convertible to 'UInt8'}}
|
|
|
|
func expectSameType<T>(_: T.Type, _: T.Type) {}
|
|
|
|
func test_truncatingBitPatternAPIIsStableAcrossPlatforms() {
|
|
// Audit and update this test when adding new integer types.
|
|
expectSameType(Int64.self, IntMax.self)
|
|
expectSameType(UInt64.self, UIntMax.self)
|
|
|
|
UInt8(truncatingBitPattern: UInt(0)) // expected-warning{{unused}}
|
|
UInt16(truncatingBitPattern: UInt(0)) // expected-warning{{unused}}
|
|
UInt32(truncatingBitPattern: UInt(0)) // expected-warning{{unused}}
|
|
UInt64(truncatingBitPattern: UInt(0)) // expected-error {{extraneous argument label 'truncatingBitPattern:' in call}}
|
|
UInt(truncatingBitPattern: UInt(0)) // expected-error {{cannot find an initializer for type 'UInt' that accepts an argument list of type}} expected-note {{overloads for 'UInt' exist with these partially matching parameter lists: }}
|
|
|
|
Int8(truncatingBitPattern: UInt(0)) // expected-warning{{unused}}
|
|
Int16(truncatingBitPattern: UInt(0)) // expected-warning{{unused}}
|
|
Int32(truncatingBitPattern: UInt(0)) // expected-warning{{unused}}
|
|
Int64(truncatingBitPattern: UInt(0)) // expected-error {{extraneous argument label 'truncatingBitPattern:' in call}}
|
|
Int(truncatingBitPattern: UInt(0)) // expected-error {{cannot find an initializer for type 'Int' that accepts an argument list of type}} expected-note {{overloads for 'Int' exist with these partially matching parameter lists: }}
|
|
|
|
UInt8(truncatingBitPattern: Int(0)) // expected-warning{{unused}}
|
|
UInt16(truncatingBitPattern: Int(0)) // expected-warning{{unused}}
|
|
UInt32(truncatingBitPattern: Int(0)) // expected-warning{{unused}}
|
|
UInt64(truncatingBitPattern: Int(0)) // expected-error {{extraneous argument label 'truncatingBitPattern:' in call}}
|
|
UInt(truncatingBitPattern: Int(0)) // expected-error {{cannot find an initializer for type 'UInt' that accepts an argument list of type}} expected-note {{overloads for 'UInt' exist with these partially matching parameter lists: }}
|
|
|
|
Int8(truncatingBitPattern: Int(0)) // expected-warning{{unused}}
|
|
Int16(truncatingBitPattern: Int(0)) // expected-warning{{unused}}
|
|
Int32(truncatingBitPattern: Int(0)) // expected-warning{{unused}}
|
|
Int64(truncatingBitPattern: Int(0)) // expected-error {{extraneous argument label 'truncatingBitPattern:' in call}}
|
|
Int(truncatingBitPattern: Int(0)) // expected-error {{cannot find an initializer for type 'Int' that accepts an argument list of type}} expected-note {{overloads for 'Int' exist with these partially matching parameter lists: }}
|
|
|
|
UInt(truncatingBitPattern: UInt8(0)) // expected-error {{cannot invoke initializer for type 'UInt' with an argument list of type '(truncatingBitPattern: UInt8)}} expected-note {{expected an argument list of type '(UInt8)'}}
|
|
UInt(truncatingBitPattern: UInt16(0)) // expected-error {{cannot invoke initializer for type 'UInt' with an argument list of type '(truncatingBitPattern: UInt16)}} expected-note {{expected an argument list of type '(UInt16)'}}
|
|
UInt(truncatingBitPattern: UInt32(0)) // expected-error {{cannot invoke initializer for type 'UInt' with an argument list of type '(truncatingBitPattern: UInt32)}} expected-note {{expected an argument list of type '(UInt32)'}}
|
|
UInt(truncatingBitPattern: UInt64(0)) // expected-warning{{unused}}
|
|
|
|
Int(truncatingBitPattern: UInt8(0)) // expected-error {{cannot invoke initializer for type 'Int' with an argument list of type '(truncatingBitPattern: UInt8)}} expected-note {{expected an argument list of type '(UInt8)'}}
|
|
Int(truncatingBitPattern: UInt16(0)) // expected-error {{cannot invoke initializer for type 'Int' with an argument list of type '(truncatingBitPattern: UInt16)}} expected-note {{expected an argument list of type '(UInt16)'}}
|
|
Int(truncatingBitPattern: UInt32(0)) // expected-error {{cannot invoke initializer for type 'Int' with an argument list of type '(truncatingBitPattern: UInt32)}} expected-note {{expected an argument list of type '(UInt32)'}}
|
|
Int(truncatingBitPattern: UInt64(0)) // expected-warning{{unused}}
|
|
|
|
UInt(truncatingBitPattern: Int8(0)) // expected-error {{cannot invoke initializer for type 'UInt' with an argument list of type '(truncatingBitPattern: Int8)}} expected-note {{expected an argument list of type '(Int8)'}}
|
|
UInt(truncatingBitPattern: Int16(0)) // expected-error {{cannot invoke initializer for type 'UInt' with an argument list of type '(truncatingBitPattern: Int16)}} expected-note {{expected an argument list of type '(Int16)'}}
|
|
UInt(truncatingBitPattern: Int32(0)) // expected-error {{cannot invoke initializer for type 'UInt' with an argument list of type '(truncatingBitPattern: Int32)}} expected-note {{expected an argument list of type '(Int32)'}}
|
|
UInt(truncatingBitPattern: Int64(0)) // expected-warning{{unused}}
|
|
|
|
Int(truncatingBitPattern: Int8(0)) // expected-error {{cannot invoke initializer for type 'Int' with an argument list of type '(truncatingBitPattern: Int8)}} expected-note {{expected an argument list of type '(Int8)'}}
|
|
Int(truncatingBitPattern: Int16(0)) // expected-error {{cannot invoke initializer for type 'Int' with an argument list of type '(truncatingBitPattern: Int16)}} expected-note {{expected an argument list of type '(Int16)'}}
|
|
Int(truncatingBitPattern: Int32(0)) // expected-error {{cannot invoke initializer for type 'Int' with an argument list of type '(truncatingBitPattern: Int32)}} expected-note {{expected an argument list of type '(Int32)'}}
|
|
Int(truncatingBitPattern: Int64(0)) // expected-warning{{unused}}
|
|
}
|
|
|
|
func testMixedSignArithmetic() {
|
|
// Ensure that the generic arithmetic operators for Strideable don't
|
|
// allow mixed-sign arithmetic to compile. We create a deliberate
|
|
// ambiguity in these cases.
|
|
% for T in "UInt UWord UInt64 UInt32 UInt16 UInt8".split():
|
|
if true {
|
|
typealias Stride = ${T}.Stride
|
|
${T}(1) + 0 // OK
|
|
0 + ${T}(1) // OK
|
|
${T}(1) + Stride(0) // expected-error {{ambiguous use of operator '+'}}
|
|
Stride(1) + ${T}(0) // expected-error {{ambiguous use of operator '+'}}
|
|
${T}(1) - Stride(0) // expected-error {{ambiguous use of operator '-'}}
|
|
var x: ${T} = 0
|
|
x += 1 // OK
|
|
x += Stride(1) // expected-error {{ambiguous use of operator '+='}}
|
|
x -= Stride(1) // expected-error {{ambiguous use of operator '-='}}
|
|
|
|
(x - x) as Stride // expected-error {{ambiguous use of operator '-'}}
|
|
|
|
//===------------------------------------------------------------------===//
|
|
// The following errors are different because they're not being
|
|
// disabled by the ambiguity trick.
|
|
//===------------------------------------------------------------------===//
|
|
|
|
(x + x) as Stride // expected-error {{is not convertible to 'Stride'}}
|
|
Stride(1) - ${T}(0) // expected-error {{binary operator '-' cannot be applied to operands of type 'Stride' and}} expected-note {{overloads for '-' exist with these partially matching parameter lists:}}
|
|
|
|
// These tests are expected to start failing when we get improved diagnostics.
|
|
var y: Stride = 0
|
|
y += ${T}(1) // expected-error {{binary operator '+=' cannot be applied to operands of type 'Stride' and '${T}'}} expected-note {{expected an argument list of type}}
|
|
y -= ${T}(1) // expected-error {{binary operator '-=' cannot be applied to operands of type 'Stride' and '${T}'}} expected-note {{expected an argument list of type}}
|
|
}
|
|
% end
|
|
}
|
|
|
|
func testOps<T : IntegerArithmeticType>(x: T, _ y: T) -> T {
|
|
let a = x + y
|
|
let s = x - y
|
|
let m = x * y
|
|
let d = x / y
|
|
let r = x % y
|
|
return a + s + m + d + r
|
|
}
|
|
|
|
let s_ops: Int = testOps(5, 2)
|
|
let u_ops: UInt = testOps(5, 2)
|
|
let s8_ops: Int8 = testOps(5, 2)
|
|
let u8_ops: UInt8 = testOps(5, 2)
|
|
let s16_ops: Int16 = testOps(5, 2)
|
|
let u16_ops: UInt16 = testOps(5, 2)
|
|
let s32_ops: Int32 = testOps(5, 2)
|
|
let u32_ops: UInt32 = testOps(5, 2)
|
|
let s64_ops: Int64 = testOps(5, 2)
|
|
let u64_ops: UInt64 = testOps(5, 2)
|