Skip to content

[CSApply] A couple of locator adjustments to support updated Double<->CGFloat conversion #59902

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 5 commits into from
Jul 7, 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
41 changes: 28 additions & 13 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3481,9 +3481,11 @@ namespace {

auto elementType = expr->getElementType();

for (auto &element : expr->getElements()) {
element = coerceToType(element, elementType,
cs.getConstraintLocator(element));
for (unsigned i = 0, n = expr->getNumElements(); i != n; ++i) {
expr->setElement(
i, coerceToType(expr->getElement(i), elementType,
cs.getConstraintLocator(
expr, {LocatorPathElt::TupleElement(i)})));
}

return expr;
Expand Down Expand Up @@ -3526,9 +3528,12 @@ namespace {
expr->setInitializer(witness);

auto elementType = expr->getElementType();
for (auto &element : expr->getElements()) {
element = coerceToType(element, elementType,
cs.getConstraintLocator(element));

for (unsigned i = 0, n = expr->getNumElements(); i != n; ++i) {
expr->setElement(
i, coerceToType(expr->getElement(i), elementType,
cs.getConstraintLocator(
expr, {LocatorPathElt::TupleElement(i)})));
}

return expr;
Expand Down Expand Up @@ -3730,10 +3735,12 @@ namespace {
expr->setCondExpr(cond);

// Coerce the then/else branches to the common type.
expr->setThenExpr(coerceToType(expr->getThenExpr(), resultTy,
cs.getConstraintLocator(expr->getThenExpr())));
expr->setElseExpr(coerceToType(expr->getElseExpr(), resultTy,
cs.getConstraintLocator(expr->getElseExpr())));
expr->setThenExpr(coerceToType(
expr->getThenExpr(), resultTy,
cs.getConstraintLocator(expr, LocatorPathElt::TernaryBranch(true))));
expr->setElseExpr(coerceToType(
expr->getElseExpr(), resultTy,
cs.getConstraintLocator(expr, LocatorPathElt::TernaryBranch(false))));

return expr;
}
Expand Down Expand Up @@ -4305,9 +4312,12 @@ namespace {
Expr *visitAssignExpr(AssignExpr *expr) {
// Convert the source to the simplified destination type.
auto destTy = simplifyType(cs.getType(expr->getDest()));
auto locator =
ConstraintLocatorBuilder(cs.getConstraintLocator(expr->getSrc()));
Expr *src = coerceToType(expr->getSrc(), destTy->getRValueType(), locator);
// Conversion is recorded as anchored on an assignment itself by
// constraint generator and that has to be preserved here in case
// anything depends on the locator (i.e. Double<->CGFloat implicit
// conversion).
Expr *src = coerceToType(expr->getSrc(), destTy->getRValueType(),
cs.getConstraintLocator(expr));
if (!src)
return nullptr;

Expand Down Expand Up @@ -6752,6 +6762,11 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
auto *argExpr = locator.trySimplifyToExpr();
assert(argExpr);

// Source requires implicit conversion to match destination
// type but the conversion itself is recorded on assignment.
if (auto *assignment = dyn_cast<AssignExpr>(argExpr))
argExpr = assignment->getSrc();

// Load the value for conversion.
argExpr = cs.coerceToRValue(argExpr);

Expand Down
5 changes: 5 additions & 0 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,11 @@ ConstraintLocator *ConstraintSystem::getImplicitValueConversionLocator(
SmallVector<LocatorPathElt, 4> path;
auto anchor = root.getLocatorParts(path);
{
if (isExpr<DictionaryExpr>(anchor) && path.size() > 1) {
// Drop everything except for first `tuple element #`.
path.pop_back_n(path.size() - 1);
}

// Drop any value-to-optional conversions that were applied along the
// way to reach this one.
while (!path.empty()) {
Expand Down
46 changes: 46 additions & 0 deletions test/Constraints/implicit_double_cgfloat_conversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,49 @@ func test_multi_argument_conversion_with_optional(d: Double, cgf: CGFloat) {

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

extension CGFloat: Hashable {
public func hash(into hasher: inout Hasher) { fatalError() }
}

func test_collection_literals_as_call_arguments() {
enum E {
case test_arr([CGFloat])
case test_dict_key([CGFloat: String])
case test_dict_value([String: CGFloat])
case test_arr_nested([String: [[CGFloat]: String]])
case test_dict_nested([String: [String: CGFloat]])
}

struct Container {
var prop: E
}

struct Point {
var x: Double
var y: Double
}

func test(cont: inout Container, point: Point) {
cont.prop = .test_arr([point.x]) // Ok
cont.prop = .test_dict_key([point.y: ""]) // Ok
cont.prop = .test_dict_value(["": point.y]) // Ok
cont.prop = .test_arr_nested(["": [[point.x]: ""]]) // Ok
cont.prop = .test_dict_nested(["": ["": point.x]]) // Ok
}
}

func assignments_with_and_without_optionals() {
class C {
var prop: CGFloat = 0
}

func test(c: C?, v: Double, cgf: CGFloat) {
c?.prop = v / 2.0 // Ok
c?.prop = (false ? cgf : v)

let copy = c!
copy.prop = Optional(v) ?? 0 // Ok
copy.prop = (true ? cgf : (false ? v : cgf))
}
}