Skip to content

Commit f0f5ffc

Browse files
committed
Sema: Allow optional-to-optional CGFloat <-> Double conversion
After #78957, there is no technical reason to not allow this conversion. This is needed for an upcoming optimization.
1 parent 725bd91 commit f0f5ffc

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7392,28 +7392,18 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
73927392
// Look through all value-to-optional promotions to allow
73937393
// conversions like Double -> CGFloat?? and vice versa.
73947394
// T -> Optional<T>
7395-
if (location.endsWith<LocatorPathElt::OptionalPayload>()) {
7395+
if (location.endsWith<LocatorPathElt::OptionalPayload>() ||
7396+
location.endsWith<LocatorPathElt::GenericArgument>()) {
73967397
SmallVector<LocatorPathElt, 4> path;
73977398
auto anchor = location.getLocatorParts(path);
73987399

7399-
// An attempt at Double/CGFloat conversion through
7400-
// optional chaining. This is not supported at the
7401-
// moment because solution application doesn't know
7402-
// how to map Double to/from CGFloat through optionals.
7403-
if (isExpr<OptionalEvaluationExpr>(anchor)) {
7404-
if (!shouldAttemptFixes())
7405-
return getTypeMatchFailure(locator);
7406-
7407-
conversionsOrFixes.push_back(ContextualMismatch::create(
7408-
*this, nominal1, nominal2, getConstraintLocator(locator)));
7409-
break;
7410-
}
7411-
7412-
// Drop all of the applied `value-to-optional` promotions.
7400+
// Drop all of the applied `value-to-optional` and
7401+
// `optional-to-optional` conversions.
74137402
path.erase(llvm::remove_if(
74147403
path,
74157404
[](const LocatorPathElt &elt) {
7416-
return elt.is<LocatorPathElt::OptionalPayload>();
7405+
return elt.is<LocatorPathElt::OptionalPayload>() ||
7406+
elt.is<LocatorPathElt::GenericArgument>();
74177407
}),
74187408
path.end());
74197409

test/Constraints/implicit_double_cgfloat_conversion.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,8 @@ func test_init_validation() {
342342
}
343343
}
344344
}
345+
346+
// Optional-to-optional conversion
347+
func optional_to_optional(x: CGFloat?) -> Double? {
348+
return x
349+
}

validation-test/Sema/type_checker_crashers_fixed/rdar83666783.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ func compute(_: Double?) -> Double? {
1212
}
1313

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

0 commit comments

Comments
 (0)