Skip to content

Commit ea1baed

Browse files
authored
Address 128-bit integer comparison type inference issue (swiftlang#75529)
1 parent 628d4c5 commit ea1baed

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

stdlib/public/core/Int128.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,30 @@ extension Int128: FixedWidthInteger, SignedInteger {
499499
Self(Builtin.mul_Int128(lhs._value, rhs._value))
500500
}
501501
}
502+
503+
// MARK: - Integer comparison type inference
504+
@available(SwiftStdlib 6.0, *)
505+
extension Int128 {
506+
// IMPORTANT: The following four apparently unnecessary overloads of
507+
// comparison operations are necessary for literal comparands to be
508+
// inferred as the desired type.
509+
@_transparent @_alwaysEmitIntoClient
510+
public static func != (lhs: Self, rhs: Self) -> Bool {
511+
return !(lhs == rhs)
512+
}
513+
514+
@_transparent @_alwaysEmitIntoClient
515+
public static func <= (lhs: Self, rhs: Self) -> Bool {
516+
return !(rhs < lhs)
517+
}
518+
519+
@_transparent @_alwaysEmitIntoClient
520+
public static func >= (lhs: Self, rhs: Self) -> Bool {
521+
return !(lhs < rhs)
522+
}
523+
524+
@_transparent @_alwaysEmitIntoClient
525+
public static func > (lhs: Self, rhs: Self) -> Bool {
526+
return rhs < lhs
527+
}
528+
}

stdlib/public/core/UInt128.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,30 @@ extension UInt128: FixedWidthInteger, UnsignedInteger {
552552
return Self(_low: _high.byteSwapped, _high: _low.byteSwapped)
553553
}
554554
}
555+
556+
// MARK: - Integer comparison type inference
557+
@available(SwiftStdlib 6.0, *)
558+
extension UInt128 {
559+
// IMPORTANT: The following four apparently unnecessary overloads of
560+
// comparison operations are necessary for literal comparands to be
561+
// inferred as the desired type.
562+
@_transparent @_alwaysEmitIntoClient
563+
public static func != (lhs: Self, rhs: Self) -> Bool {
564+
return !(lhs == rhs)
565+
}
566+
567+
@_transparent @_alwaysEmitIntoClient
568+
public static func <= (lhs: Self, rhs: Self) -> Bool {
569+
return !(rhs < lhs)
570+
}
571+
572+
@_transparent @_alwaysEmitIntoClient
573+
public static func >= (lhs: Self, rhs: Self) -> Bool {
574+
return !(lhs < rhs)
575+
}
576+
577+
@_transparent @_alwaysEmitIntoClient
578+
public static func > (lhs: Self, rhs: Self) -> Bool {
579+
return rhs < lhs
580+
}
581+
}

test/stdlib/integer_comparison_typeinference.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,22 @@ ComparisonTypeInferenceTests.test("Int8") {
1414
expectTrue( 1 << 7 <= Int8.max)
1515
}
1616

17+
ComparisonTypeInferenceTests.test("Int128") {
18+
if #available(SwiftStdlib 6.0, *) {
19+
expectTrue(Int128.max != 9_999_999_999_999_999_999_999_999_999_999_999)
20+
expectTrue(Int128.max > 9_999_999_999_999_999_999_999_999_999_999_999)
21+
expectTrue(Int128.max >= 9_999_999_999_999_999_999_999_999_999_999_999)
22+
expectFalse(Int128.max <= 9_999_999_999_999_999_999_999_999_999_999_999)
23+
}
24+
}
25+
26+
ComparisonTypeInferenceTests.test("UInt128") {
27+
if #available(SwiftStdlib 6.0, *) {
28+
expectTrue(UInt128.max != 9_999_999_999_999_999_999_999_999_999_999_999)
29+
expectTrue(UInt128.max > 9_999_999_999_999_999_999_999_999_999_999_999)
30+
expectTrue(UInt128.max >= 9_999_999_999_999_999_999_999_999_999_999_999)
31+
expectFalse(UInt128.max <= 9_999_999_999_999_999_999_999_999_999_999_999)
32+
}
33+
}
34+
1735
runAllTests()

0 commit comments

Comments
 (0)