Skip to content

Commit 701a03b

Browse files
Remove prefix+ from StaticBigInt (#62733)
* Remove prefix+ from StaticBigInt This operator causes source breakage in cases like: ``` let a:Int = 7 let b = +1 let c = a + b // Error: Cannot convert `b` from `StaticBigInt` to `Int` ```
1 parent e9d1d30 commit 701a03b

File tree

2 files changed

+45
-31
lines changed

2 files changed

+45
-31
lines changed

stdlib/public/core/StaticBigInt.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@ public struct StaticBigInt:
4343
public init(_builtinIntegerLiteral value: Builtin.IntLiteral) {
4444
_value = value
4545
}
46-
47-
/// Returns the given value unchanged.
48-
@_alwaysEmitIntoClient
49-
@available(SwiftStdlib 5.8, *)
50-
@inline(__always)
51-
public static prefix func + (_ rhs: Self) -> Self {
52-
rhs
53-
}
5446
}
5547

5648
//===----------------------------------------------------------------------===//
@@ -117,7 +109,7 @@ extension StaticBigInt {
117109
/// negative[1] //-> 0xFFEEDDCCBBAA9988
118110
/// negative[2] //-> 0xFFFFFFFFFFFFFFFF
119111
///
120-
/// let positive: StaticBigInt = +0x0011223344556677_8899AABBCCDDEEFF
112+
/// let positive: StaticBigInt = 0x0011223344556677_8899AABBCCDDEEFF
121113
/// positive.signum() //-> +1
122114
/// positive.bitWidth //-> 118
123115
/// positive[0] //-> 0x8899AABBCCDDEEFF

test/stdlib/StaticBigInt.swift

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ final class StaticBigIntTests {
3030
let testSuite = TestSuite("StaticBigIntTests")
3131
testSuite.test("BinaryRepresentation", testCase.testBinaryRepresentation)
3232
testSuite.test("TextualRepresentation", testCase.testTextualRepresentation)
33+
testSuite.test("PrefixPlusTypeInference", testCase.testPrefixPlusTypeInference)
34+
testSuite.test("PrefixMinusTypeInference", testCase.testPrefixMinusTypeInference)
3335
testSuite.test("WrapperAssociatedType", testCase.testWrapperAssociatedType)
3436
runAllTests()
3537
}
@@ -55,14 +57,14 @@ extension StaticBigIntTests {
5557
-0x3: (-1, 3, [~2, ~0, ~0, ~0, ~0], [~2, ~0, ~0]),
5658
-0x2: (-1, 2, [~1, ~0, ~0, ~0, ~0], [~1, ~0, ~0]),
5759
-0x1: (-1, 1, [~0, ~0, ~0, ~0, ~0], [~0, ~0, ~0]),
58-
+0x0: ( 0, 1, [ 0, 0, 0, 0, 0], [ 0, 0, 0]),
59-
+0x1: (+1, 2, [ 1, 0, 0, 0, 0], [ 1, 0, 0]),
60-
+0x2: (+1, 3, [ 2, 0, 0, 0, 0], [ 2, 0, 0]),
61-
+0x3: (+1, 3, [ 3, 0, 0, 0, 0], [ 3, 0, 0]),
62-
+0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE: (+1, 128, [~1, ~0, ~0, ~m, 0], [~1, ~m, 0]),
63-
+0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF: (+1, 128, [~0, ~0, ~0, ~m, 0], [~0, ~m, 0]),
64-
+0x80000000000000000000000000000000: (+1, 129, [ 0, 0, 0, m, 0], [ 0, m, 0]),
65-
+0x80000000000000000000000000000001: (+1, 129, [ 1, 0, 0, m, 0], [ 1, m, 0]),
60+
0x0: ( 0, 1, [ 0, 0, 0, 0, 0], [ 0, 0, 0]),
61+
0x1: (+1, 2, [ 1, 0, 0, 0, 0], [ 1, 0, 0]),
62+
0x2: (+1, 3, [ 2, 0, 0, 0, 0], [ 2, 0, 0]),
63+
0x3: (+1, 3, [ 3, 0, 0, 0, 0], [ 3, 0, 0]),
64+
0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE: (+1, 128, [~1, ~0, ~0, ~m, 0], [~1, ~m, 0]),
65+
0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF: (+1, 128, [~0, ~0, ~0, ~m, 0], [~0, ~m, 0]),
66+
0x80000000000000000000000000000000: (+1, 129, [ 0, 0, 0, m, 0], [ 0, m, 0]),
67+
0x80000000000000000000000000000001: (+1, 129, [ 1, 0, 0, m, 0], [ 1, m, 0]),
6668
]
6769
for (actual, expected) in keyValuePairs {
6870
expectEqual(expected.signum, actual.signum())
@@ -108,20 +110,20 @@ extension StaticBigIntTests {
108110
-0x3: "-0x3",
109111
-0x2: "-0x2",
110112
-0x1: "-0x1",
111-
+0x0: "+0x0",
112-
+0x1: "+0x1",
113-
+0x2: "+0x2",
114-
+0x3: "+0x3",
115-
+0x10: "+0x10",
116-
+0x100: "+0x100",
117-
+0x1000: "+0x1000",
118-
+0x123456789ABC: "+0x123456789ABC",
119-
+0x123456789ABCD: "+0x123456789ABCD",
120-
+0x123456789ABCDE: "+0x123456789ABCDE",
121-
+0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE: "+0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE",
122-
+0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF: "+0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
123-
+0x80000000000000000000000000000000: "+0x80000000000000000000000000000000",
124-
+0x80000000000000000000000000000001: "+0x80000000000000000000000000000001",
113+
0x0: "+0x0",
114+
0x1: "+0x1",
115+
0x2: "+0x2",
116+
0x3: "+0x3",
117+
0x10: "+0x10",
118+
0x100: "+0x100",
119+
0x1000: "+0x1000",
120+
0x123456789ABC: "+0x123456789ABC",
121+
0x123456789ABCD: "+0x123456789ABCD",
122+
0x123456789ABCDE: "+0x123456789ABCDE",
123+
0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE: "+0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE",
124+
0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF: "+0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
125+
0x80000000000000000000000000000000: "+0x80000000000000000000000000000000",
126+
0x80000000000000000000000000000001: "+0x80000000000000000000000000000001",
125127
]
126128
for (actual, expected) in keyValuePairs {
127129
expectEqual(expected, String(reflecting: actual))
@@ -147,6 +149,26 @@ extension StaticBigIntTests {
147149
self.actual = actual
148150
}
149151
}
152+
153+
func testPrefixPlusTypeInference() {
154+
let a: Int = 7
155+
// An earlier version of StaticBigInt contained a prefix + operation,
156+
// which caused b to be inferred to have type StaticBigInt rather than
157+
// Int:
158+
let b = +1
159+
// and then this would fail to typecheck, because there's no
160+
// Int + StaticBigInt operation.
161+
let c = a + b
162+
}
163+
164+
func testPrefixMinusTypeInference() {
165+
// This example shouldn't suffer from the same problem as above, because
166+
// -1 is a literal, not a prefix operator followed by a literal.
167+
// Nonetheless, let's test it.
168+
let a: Int = 7
169+
let b = -1
170+
let c = a + b
171+
}
150172

151173
@available(SwiftStdlib 5.8, *)
152174
func testWrapperAssociatedType() {

0 commit comments

Comments
 (0)