Skip to content

Sema: Allow optional-to-optional CGFloat <-> Double conversion #79397

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
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
22 changes: 6 additions & 16 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7392,28 +7392,18 @@ 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::OptionalPayload>()) {
if (location.endsWith<LocatorPathElt::OptionalPayload>() ||
location.endsWith<LocatorPathElt::GenericArgument>()) {
SmallVector<LocatorPathElt, 4> path;
auto anchor = location.getLocatorParts(path);

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

Expand Down
5 changes: 5 additions & 0 deletions test/Constraints/implicit_double_cgfloat_conversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,8 @@ func test_init_validation() {
}
}
}

// Optional-to-optional conversion
func optional_to_optional(x: CGFloat?) -> Double? {
return x
}
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) // expected-error {{cannot implicitly convert value of type 'CGFloat?' to expected type 'Double?'}}
_ = compute(s?.test)
}