Skip to content

[5.7] Revert "[TypeChecker] Adjust Double<->CGFloat conversion to always pr… #59896

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
Jul 6, 2022
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
6 changes: 0 additions & 6 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3691,12 +3691,6 @@ class ConstraintSystem {
ConstraintLocator *
getConstraintLocator(const ConstraintLocatorBuilder &builder);

/// Compute a constraint locator for an implicit value-to-value
/// conversion rooted at the given location.
ConstraintLocator *
getImplicitValueConversionLocator(ConstraintLocatorBuilder root,
ConversionRestrictionKind restriction);

/// Lookup and return parent associated with given expression.
Expr *getParentExpr(Expr *expr) {
if (auto result = getExprDepthAndParent(expr))
Expand Down
23 changes: 8 additions & 15 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6855,13 +6855,10 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
callLocator, {ConstraintLocator::ApplyFunction,
ConstraintLocator::ConstructorMember});

ConstraintLocator *baseLoc =
cs.getImplicitValueConversionLocator(locator, conversionKind);

auto overload =
solution.getOverloadChoice(solution.getConstraintLocator(
baseLoc, {ConstraintLocator::ApplyFunction,
ConstraintLocator::ConstructorMember}));
auto overload = solution.getOverloadChoice(cs.getConstraintLocator(
ASTNode(), {LocatorPathElt::ImplicitConversion(conversionKind),
ConstraintLocator::ApplyFunction,
ConstraintLocator::ConstructorMember}));

solution.overloadChoices.insert({memberLoc, overload});
}
Expand Down Expand Up @@ -9066,19 +9063,15 @@ ExprWalker::rewriteTarget(SolutionApplicationTarget target) {
// If we're supposed to convert the expression to some particular type,
// do so now.
if (shouldCoerceToContextualType()) {
resultExpr = Rewriter.coerceToType(
resultExpr, solution.simplifyType(convertType),
cs.getConstraintLocator(resultExpr,
LocatorPathElt::ContextualType(
target.getExprContextualTypePurpose())));
resultExpr =
Rewriter.coerceToType(resultExpr, solution.simplifyType(convertType),
cs.getConstraintLocator(resultExpr));
} else if (cs.getType(resultExpr)->hasLValueType() &&
!target.isDiscardedExpr()) {
// We referenced an lvalue. Load it.
resultExpr = Rewriter.coerceToType(
resultExpr, cs.getType(resultExpr)->getRValueType(),
cs.getConstraintLocator(resultExpr,
LocatorPathElt::ContextualType(
target.getExprContextualTypePurpose())));
cs.getConstraintLocator(resultExpr));
}

if (!resultExpr)
Expand Down
16 changes: 9 additions & 7 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12455,26 +12455,28 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
if (worseThanBestSolution())
return SolutionKind::Error;

auto *conversionLoc =
getImplicitValueConversionLocator(locator, restriction);
auto *conversionLoc = getConstraintLocator(
/*anchor=*/ASTNode(), LocatorPathElt::ImplicitConversion(restriction));

auto *applicationLoc =
getConstraintLocator(conversionLoc, ConstraintLocator::ApplyFunction);

auto *memberLoc = getConstraintLocator(
applicationLoc, ConstraintLocator::ConstructorMember);

// Conversion has been already attempted for this direction
// and constructor choice has been recorded.
if (findSelectedOverloadFor(memberLoc))
return SolutionKind::Solved;

// Allocate a single argument info to cover all possible
// Double <-> CGFloat conversion locations.
auto *argumentsLoc =
getConstraintLocator(conversionLoc, ConstraintLocator::ApplyArgument);

if (!ArgumentLists.count(argumentsLoc)) {
if (!ArgumentLists.count(memberLoc)) {
auto *argList = ArgumentList::createImplicit(
getASTContext(), {Argument(SourceLoc(), Identifier(), nullptr)},
/*firstTrailingClosureIndex=*/None,
AllocationArena::ConstraintSolver);
ArgumentLists.insert({argumentsLoc, argList});
ArgumentLists.insert({memberLoc, argList});
}

auto *memberTypeLoc = getConstraintLocator(
Expand Down
43 changes: 9 additions & 34 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,43 +424,21 @@ ConstraintLocator *ConstraintSystem::getConstraintLocator(
return getConstraintLocator(anchor, newPath);
}

ConstraintLocator *ConstraintSystem::getImplicitValueConversionLocator(
ConstraintLocatorBuilder root, ConversionRestrictionKind restriction) {
SmallVector<LocatorPathElt, 4> path;
auto anchor = root.getLocatorParts(path);
{
// Drop any value-to-optional conversions that were applied along the
// way to reach this one.
while (!path.empty()) {
if (path.back().is<LocatorPathElt::OptionalPayload>()) {
path.pop_back();
continue;
}
break;
}

// If the conversion is associated with a contextual type e.g.
// `_: Double = CGFloat(1)` then drop `ContextualType` so that
// it's easy to find when the underlying expression has been
// rewritten.
if (!path.empty() && path.back().is<LocatorPathElt::ContextualType>()) {
anchor = ASTNode();
path.clear();
}
}

return getConstraintLocator(/*base=*/getConstraintLocator(anchor, path),
LocatorPathElt::ImplicitConversion(restriction));
}

ConstraintLocator *ConstraintSystem::getCalleeLocator(
ConstraintLocator *locator, bool lookThroughApply,
llvm::function_ref<Type(Expr *)> getType,
llvm::function_ref<Type(Type)> simplifyType,
llvm::function_ref<Optional<SelectedOverload>(ConstraintLocator *)>
getOverloadFor) {
if (locator->findLast<LocatorPathElt::ImplicitConversion>())
return locator;
if (auto conversion =
locator->findLast<LocatorPathElt::ImplicitConversion>()) {
if (conversion->is(ConversionRestrictionKind::DoubleToCGFloat) ||
conversion->is(ConversionRestrictionKind::CGFloatToDouble)) {
return getConstraintLocator(
ASTNode(), {*conversion, ConstraintLocator::ApplyFunction,
ConstraintLocator::ConstructorMember});
}
}

auto anchor = locator->getAnchor();
auto path = locator->getPath();
Expand Down Expand Up @@ -5391,9 +5369,6 @@ ConstraintSystem::getArgumentInfoLocator(ConstraintLocator *locator) {
if (anchor.isNull() && locator->getPath().empty())
return nullptr;

if (locator->findLast<LocatorPathElt::ImplicitConversion>())
return locator;

// Applies and unresolved member exprs can have callee locators that are
// dependent on the type of their function, which may not have been resolved
// yet. Therefore we need to handle them specially.
Expand Down
15 changes: 4 additions & 11 deletions test/Constraints/implicit_double_cgfloat_conversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,25 +194,18 @@ func test_no_ambiguity_with_unary_operators(width: CGFloat, height: CGFloat) {
}

func test_conversions_with_optional_promotion(d: Double, cgf: CGFloat) {
func test_double(_: Double??, _: Double???) {}
func test_cgfloat(_: CGFloat??, _: CGFloat???) {}
func test_double(_: Double??) {}
func test_cgfloat(_: CGFloat??) {}

// CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC
// CHECK-NEXT: apply
// CHECK-NEXT: enum $Optional<Double>, #Optional.some!enumelt
// CHECK-NEXT: enum $Optional<Optional<Double>>, #Optional.some!enumelt
test_double(cgf, cgf)
test_double(cgf)

// CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC
// CHECK-NEXT: apply
// CHECK-NEXT: enum $Optional<CGFloat>, #Optional.some!enumelt
// CHECK-NEXT: enum $Optional<Optional<CGFloat>>, #Optional.some!enumelt
test_cgfloat(d, d)
}

// https://github.com/apple/swift/issues/59374
func test_multi_argument_conversion_with_optional(d: Double, cgf: CGFloat) {
func test(_: Double, _: CGFloat?) {}

test(cgf, d) // Ok (CGFloat -> Double and Double? -> CGFloat?)
test_cgfloat(d)
}