-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathFixedPointDiagnostics.swift.gyb
136 lines (112 loc) · 5.94 KB
/
FixedPointDiagnostics.swift.gyb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 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
func testUnaryMinusInUnsigned() {
var a: UInt8 = -(1) // expected-error {{}} expected-note * {{}}
var b: UInt16 = -(1) // expected-error {{}} expected-note * {{}}
var c: UInt32 = -(1) // expected-error {{}} expected-note * {{}}
var d: UInt64 = -(1) // expected-error {{}} expected-note * {{}}
}
// Int and UInt are not identical to any fixed-size integer type
var i : Int = 0
var i64 : Int64 = i // expected-error {{}}
var i32 : Int32 = i // expected-error {{}}
var i16 : Int16 = i // expected-error {{}}
var i8 : Int8 = i // expected-error {{}}
var u : UInt = 0
var u64 : UInt64 = u // expected-error {{}}
var u32 : UInt32 = u // expected-error {{}}
var u16 : UInt16 = u // expected-error {{}}
var u8 : UInt8 = u // expected-error {{}}
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))
_ = UInt16(truncatingBitPattern: UInt(0))
_ = UInt32(truncatingBitPattern: UInt(0))
UInt64(truncatingBitPattern: UInt(0)) // expected-error {{argument labels '(truncatingBitPattern:)' do not match any available overloads}}
// expected-note @-1 {{overloads for 'UInt64' exist with these partially matching parameter lists}}
UInt(truncatingBitPattern: UInt(0)) // expected-error {{}} expected-note * {{}}
_ = Int8(truncatingBitPattern: UInt(0))
_ = Int16(truncatingBitPattern: UInt(0))
_ = Int32(truncatingBitPattern: UInt(0))
Int64(truncatingBitPattern: UInt(0)) // expected-error {{argument labels '(truncatingBitPattern:)' do not match any available overloads}}
// expected-note @-1 {{overloads for 'Int64' exist with}}
Int(truncatingBitPattern: UInt(0)) // expected-error {{}} expected-note * {{}}
_ = UInt8(truncatingBitPattern: Int(0))
_ = UInt16(truncatingBitPattern: Int(0))
_ = UInt32(truncatingBitPattern: Int(0))
UInt64(truncatingBitPattern: Int(0)) // expected-error {{argument labels '(truncatingBitPattern:)' do not match any available overloads}}
// expected-note @-1 {{overloads for 'UInt64' exist with these partially matching parameter lists}}
UInt(truncatingBitPattern: Int(0)) // expected-error {{}} expected-note * {{}}
_ = Int8(truncatingBitPattern: Int(0))
_ = Int16(truncatingBitPattern: Int(0))
_ = Int32(truncatingBitPattern: Int(0))
Int64(truncatingBitPattern: Int(0)) // expected-error {{}} expected-note * {{}}
Int(truncatingBitPattern: Int(0)) // expected-error {{}} expected-note * {{}}
UInt(truncatingBitPattern: UInt8(0)) // expected-error {{}} expected-note * {{}}
UInt(truncatingBitPattern: UInt16(0)) // expected-error {{}} expected-note * {{}}
UInt(truncatingBitPattern: UInt32(0)) // expected-error {{}} expected-note * {{}}
_ = UInt(truncatingBitPattern: UInt64(0))
Int(truncatingBitPattern: UInt8(0)) // expected-error {{}} expected-note * {{}}
Int(truncatingBitPattern: UInt16(0)) // expected-error {{}} expected-note * {{}}
Int(truncatingBitPattern: UInt32(0)) // expected-error {{}} expected-note * {{}}
_ = Int(truncatingBitPattern: UInt64(0))
UInt(truncatingBitPattern: Int8(0)) // expected-error {{}} expected-note * {{}}
UInt(truncatingBitPattern: Int16(0)) // expected-error {{}} expected-note * {{}}
UInt(truncatingBitPattern: Int32(0)) // expected-error {{}} expected-note * {{}}
_ = UInt(truncatingBitPattern: Int64(0))
Int(truncatingBitPattern: Int8(0)) // expected-error {{}} expected-note * {{}}
Int(truncatingBitPattern: Int16(0)) // expected-error {{}} expected-note * {{}}
Int(truncatingBitPattern: Int32(0)) // expected-error {{}} expected-note * {{}}
_ = Int(truncatingBitPattern: Int64(0))
}
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 UInt64 UInt32 UInt16 UInt8".split():
do {
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 {{}} // not convertible to 'Stride'
Stride(1) - ${T}(0) // expected-error {{}} expected-note {{}}
// These tests are expected to start failing when we get improved diagnostics.
var y: Stride = 0
y += ${T}(1) // expected-error {{}} expected-note {{}}
y -= ${T}(1) // expected-error {{}} expected-note {{}}
}
% end
}
func testOps<T : IntegerArithmetic>(_ 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)