Skip to content

[5.7][CSSimplify] Don't fix invalid r-value use for misplaced & #60130

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 20, 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
24 changes: 19 additions & 5 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4579,8 +4579,18 @@ bool ConstraintSystem::repairFailures(
if (lhs->isPlaceholder())
return true;

conversionsOrFixes.push_back(
TreatRValueAsLValue::create(*this, getConstraintLocator(locator)));
auto *loc = getConstraintLocator(locator);
// If this `inout` is in incorrect position, it should be diagnosed
// by other fixes.
if (loc->directlyAt<InOutExpr>()) {
if (!getArgumentLocator(castToExpr(anchor))) {
conversionsOrFixes.push_back(
RemoveAddressOf::create(*this, lhs, rhs, loc));
return true;
}
}

conversionsOrFixes.push_back(TreatRValueAsLValue::create(*this, loc));
return true;
}
}
Expand Down Expand Up @@ -4779,10 +4789,14 @@ bool ConstraintSystem::repairFailures(
if (repairByInsertingExplicitCall(lhs, rhs))
return true;

if (isa<InOutExpr>(AE->getSrc())) {
if (auto *inoutExpr = dyn_cast<InOutExpr>(AE->getSrc())) {
auto *loc = getConstraintLocator(inoutExpr);

if (hasFixFor(loc, FixKind::RemoveAddressOf))
return true;

conversionsOrFixes.push_back(
RemoveAddressOf::create(*this, lhs, rhs,
getConstraintLocator(locator)));
RemoveAddressOf::create(*this, lhs, rhs, loc));
return true;
}

Expand Down
4 changes: 3 additions & 1 deletion lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5510,7 +5510,9 @@ Solution::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
// to figure out exactly where it was used.
if (auto *argExpr = getAsExpr<InOutExpr>(locator->getAnchor())) {
auto *argLoc = getConstraintSystem().getArgumentLocator(argExpr);
assert(argLoc && "Incorrect use of `inout` expression");
if (!argLoc)
return None;

locator = argLoc;
}

Expand Down
15 changes: 15 additions & 0 deletions test/Constraints/lvalues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,18 @@ func look_through_parens_when_checking_inout() {
modifyPoint((&point), 0) // expected-error {{use of extraneous '&}} {{16-17=(}} {{15-16=&}}
modifyPoint((&point), msg: "") // expected-error {{use of extraneous '&}} {{16-17=(}} {{15-16=&}}
}

// rdar://96631324 - compiler crash while producing diagnostics
func test_incorrect_inout_at_assignment_source() {
class S {
var prop: String = ""
}

func test(s: S) {
let str: String = ""
let val: Int = 0

s.prop = &str // expected-error {{use of extraneous '&'}}
s.prop = &val // expected-error {{use of extraneous '&'}}
}
}