Skip to content

[CSSimplify] CGFloat-Double: Fix ambiguity when assigning CGFloat to … #77592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14664,13 +14664,26 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
case ConversionRestrictionKind::CGFloatToDouble: {
// Prefer CGFloat -> Double over other way araund.
auto impact =
restriction == ConversionRestrictionKind::CGFloatToDouble ? 1 : 10;
restriction == ConversionRestrictionKind::CGFloatToDouble ? 2 : 10;

if (restriction == ConversionRestrictionKind::DoubleToCGFloat) {
if (auto *anchor = locator.trySimplifyToExpr()) {
if (auto depth = getExprDepth(anchor))
impact = (*depth + 1) * impact;
}
} else if (locator.directlyAt<AssignExpr>() ||
locator.endsWith<LocatorPathElt::ContextualType>()) {
// Situations like:
//
// let _: Double = <<CGFloat>>
// <var/property of type Double> = <<CGFloat>>
//
// Used to be supported due to an incorrect fix added in
// diagnostic mode. Lower impact 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 and keeps literals as Double when possible.
impact = 1;
}

increaseScore(SK_ImplicitValueConversion, locator, impact);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// RUN: %target-typecheck-verify-swift

// REQUIRES: objc_interop

// Note this cannot use a fake Foundation because it lacks required operator overloads

import Foundation
import CoreGraphics

do {
func test(a: CGFloat, b: CGFloat) {
let _: Double = a + b + 1 // Ok
let _: Double = a + b + 1.0 // Ok

var test: Double

test = a + b + 1 // Ok
test = a + b + 1.0 // Ok

_ = test

let _ = a + b + 1 // Ok
let _ = a + b + 1.0 // Ok

let _: Double = 1 + 2 + 3 // Ok

test = 1 + 2 + 3 // Ok
}
}

func test(cond: Bool, a: CGFloat, b: CGFloat) {
var test: Double

let width = a - b // CGFloat

if cond {
test = (width - 10) / 2 // Ok
} else {
test = (width - 20.0) / 3 // Ok
}

_ = test
}

func test_atan_ambiguity(points: (CGPoint, CGPoint)) {
var test = 0.0
test = atan((points.1.y - points.0.y) / (points.1.x - points.0.x)) // Ok
_ = test
}