Skip to content

Commit 81fad3c

Browse files
committed
[Stdlib] Make the comparison operators (<, >, <= and >=) on concrete Int types transparent.
This makes all operators symmetric, and makes constant folding and unreachable code analysis more precise and consistent. <rdar://39516135>
1 parent 149437b commit 81fad3c

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

stdlib/public/core/Integers.swift.gyb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3882,20 +3882,17 @@ ${assignmentOperatorComment(x.operator, True)}
38823882
return !(lhs == rhs)
38833883
}
38843884

3885-
@inlinable // FIXME(sil-serialize-all)
3886-
@inline(__always)
3885+
@_transparent
38873886
public static func <= (lhs: ${Self}, rhs: ${Self}) -> Bool {
38883887
return !(rhs < lhs)
38893888
}
38903889

3891-
@inlinable // FIXME(sil-serialize-all)
3892-
@inline(__always)
3890+
@_transparent
38933891
public static func >= (lhs: ${Self}, rhs: ${Self}) -> Bool {
38943892
return !(lhs < rhs)
38953893
}
38963894

3897-
@inlinable // FIXME(sil-serialize-all)
3898-
@inline(__always)
3895+
@_transparent
38993896
public static func > (lhs: ${Self}, rhs: ${Self}) -> Bool {
39003897
return rhs < lhs
39013898
}

test/SILOptimizer/unreachable_code.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,49 @@ func ifTrue() -> Int {
1515
return 0 // expected-warning {{will never be executed}}
1616
}
1717

18+
func testUnreachableIfBranch() -> Int {
19+
let a = 2
20+
let c: Int
21+
if a < 2 { // expected-note {{condition always evaluates to false}}
22+
c = 3 // expected-warning {{will never be executed}}
23+
} else {
24+
c = 4
25+
}
26+
return c
27+
}
28+
29+
func testUnreachableIfBranch2() -> Int {
30+
let a = 2
31+
let c: Int
32+
if a > 2 { // expected-note {{condition always evaluates to false}}
33+
c = 3 // expected-warning {{will never be executed}}
34+
} else {
35+
c = 4
36+
}
37+
return c
38+
}
39+
40+
func testUnreachableElseBranch() -> Int {
41+
let a = 2
42+
let c: Int
43+
if a == 2 { // expected-note {{condition always evaluates to true}}
44+
c = 3
45+
} else {
46+
c = 4 // expected-warning {{will never be executed}}
47+
}
48+
return c
49+
}
50+
51+
// FIXME: False Negative: <rdar://39516135>. No warnings are produced here
52+
// as the statements along the unreachable branches are marked implicit.
53+
// Unreachable code analysis suppresses warnings in such cases.
54+
func testQuestionMarkOperator() -> Int {
55+
let a = 2
56+
let c: Int
57+
c = (a < 2) ? 3 : 4
58+
return c
59+
}
60+
1861
// Work-around <rdar://problem/17687851> by ensuring there is
1962
// something that appears to be user code in unreachable blocks.
2063
func userCode() {}

0 commit comments

Comments
 (0)