Skip to content

Commit 4b54fb9

Browse files
committed
[CSSimplify] CGFloat-Double: Fix ambiguity when assigning CGFloat to double property/variable
Situations like: ``` let _: Double = <<CGFloat>> <var/property of type Double> = <<CGFloat>> ``` Used to be supported due to an incorrect fix added in diagnostic mode. Keeping impact the same here means that right-hand side of the assignment is allowed to maintain CGFloat until the very end which minimizes the number of conversions used. Resolves: rdar://139675914
1 parent 11f9ea7 commit 4b54fb9

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14671,6 +14671,19 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
1467114671
if (auto depth = getExprDepth(anchor))
1467214672
impact = (*depth + 1) * impact;
1467314673
}
14674+
} else if (locator.directlyAt<AssignExpr>() ||
14675+
locator.endsWith<LocatorPathElt::ContextualType>()) {
14676+
// Situations like:
14677+
//
14678+
// let _: Double = <<CGFloat>>
14679+
// <var/property of type Double> = <<CGFloat>>
14680+
//
14681+
// Used to be supported due to an incorrect fix added in
14682+
// diagnostic mode. Keeping impact the same here means
14683+
// that right-hand side of the assignment is allowed to
14684+
// maintain CGFloat until the very end which minimizes
14685+
// the number of conversions used.
14686+
impact = 0;
1467414687
}
1467514688

1467614689
increaseScore(SK_ImplicitValueConversion, locator, impact);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// REQUIRES: objc_interop
4+
5+
// Note this cannot use a fake Foundation because it lacks required operator overloads
6+
7+
import Foundation
8+
import CoreGraphics
9+
10+
do {
11+
func test(a: CGFloat, b: CGFloat) {
12+
let _: Double = a + b + 1 // Ok
13+
let _: Double = a + b + 1.0 // Ok
14+
15+
var test: Double
16+
17+
test = a + b + 1 // Ok
18+
test = a + b + 1.0 // Ok
19+
20+
_ = test
21+
22+
let _ = a + b + 1 // Ok
23+
let _ = a + b + 1.0 // Ok
24+
}
25+
}
26+
27+
func test(cond: Bool, a: CGFloat, b: CGFloat) {
28+
var test: Double
29+
30+
let width = a - b // CGFloat
31+
32+
if cond {
33+
test = (width - 10) / 2 // Ok
34+
} else {
35+
test = (width - 20.0) / 3 // Ok
36+
}
37+
38+
_ = test
39+
}
40+
41+
func test_atan_ambiguity(points: (CGPoint, CGPoint)) {
42+
var test = 0.0
43+
test = atan((points.1.y - points.0.y) / (points.1.x - points.0.x)) // Ok
44+
_ = test
45+
}

0 commit comments

Comments
 (0)