Skip to content

Revert "Sema: Allow optional-to-optional CGFloat <-> Double conversion" #80419

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 3 commits into from
Apr 2, 2025
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
7 changes: 7 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -7563,6 +7563,13 @@ ERROR(result_builder_buildpartialblock_accumulated_not_accessible,none,
"expression shuffles the elements of this tuple; "
"this behavior is deprecated", ())

//------------------------------------------------------------------------------
// MARK: Implicit conversion diagnostics
//------------------------------------------------------------------------------
ERROR(cannot_implicitly_convert_in_optional_context,none,
"cannot implicitly convert value of type %0 to expected type %1",
(Type, Type))

//------------------------------------------------------------------------------
// MARK: marker protocol diagnostics
//------------------------------------------------------------------------------
Expand Down
16 changes: 16 additions & 0 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2852,6 +2852,22 @@ bool ContextualFailure::diagnoseAsError() {
break;
}

case ConstraintLocator::OptionalInjection: {
// If this is an attempt at a Double <-> CGFloat conversion
// through optional chaining, let's produce a tailored diagnostic.
if (isExpr<OptionalEvaluationExpr>(getAnchor())) {
if ((fromType->isDouble() || fromType->isCGFloat()) &&
(toType->isDouble() || toType->isCGFloat())) {
fromType = OptionalType::get(fromType);
toType = OptionalType::get(toType);
diagnostic = diag::cannot_implicitly_convert_in_optional_context;
break;
}
}

return false;
}

case ConstraintLocator::EnumPatternImplicitCastMatch: {
// In this case, the types are reversed, as we are checking whether we
// can convert the pattern type to the context type.
Expand Down
22 changes: 16 additions & 6 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7489,18 +7489,28 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
// Look through all value-to-optional promotions to allow
// conversions like Double -> CGFloat?? and vice versa.
// T -> Optional<T>
if (location.endsWith<LocatorPathElt::OptionalInjection>() ||
location.endsWith<LocatorPathElt::GenericArgument>()) {
if (location.endsWith<LocatorPathElt::OptionalInjection>()) {
SmallVector<LocatorPathElt, 4> path;
auto anchor = location.getLocatorParts(path);

// Drop all of the applied `value-to-optional` and
// `optional-to-optional` conversions.
// An attempt at Double/CGFloat conversion through
// optional chaining. This is not supported at the
// moment because solution application doesn't know
// how to map Double to/from CGFloat through optionals.
if (isExpr<OptionalEvaluationExpr>(anchor)) {
if (!shouldAttemptFixes())
return getTypeMatchFailure(locator);

conversionsOrFixes.push_back(ContextualMismatch::create(
*this, nominal1, nominal2, getConstraintLocator(locator)));
break;
}

// Drop all of the applied `value-to-optional` promotions.
path.erase(llvm::remove_if(
path,
[](const LocatorPathElt &elt) {
return elt.is<LocatorPathElt::OptionalInjection>() ||
elt.is<LocatorPathElt::GenericArgument>();
return elt.is<LocatorPathElt::OptionalInjection>();
}),
path.end());

Expand Down
13 changes: 10 additions & 3 deletions test/Constraints/implicit_double_cgfloat_conversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,14 @@ func test_init_validation() {
}
}

// Optional-to-optional conversion
func optional_to_optional(x: CGFloat?) -> Double? {
return x
func test_ternary_and_nil_coalescing() {
func test(_: Double?) {}

func ternary(v: CGFloat) {
test(true ? v : nil) // Ok
}

func test_nil_coalescing(v: CGFloat?) {
test(v ?? 0.0) // Ok
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ func compute(_: Double?) -> Double? {
}

func test(s: S?) {
_ = compute(s?.test)
_ = compute(s?.test) // expected-error {{cannot implicitly convert value of type 'CGFloat?' to expected type 'Double?'}}
}