Skip to content

Commit 1560669

Browse files
authored
Merge pull request #31600 from xedin/rdar-62428353
[Diagnostics] Diagnose passing r-value without `&` to `inout` paramet…
2 parents 5640e67 + 595b198 commit 1560669

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3109,9 +3109,15 @@ bool ConstraintSystem::repairFailures(
31093109
// position (which doesn't require explicit `&`) decays into
31103110
// a `Bind` of involved object types, same goes for explicit
31113111
// `&` conversion from l-value to inout type.
3112+
//
3113+
// In case of regular argument conversion although explicit `&`
3114+
// is required we still want to diagnose the problem as one
3115+
// about mutability instead of suggesting to add `&` which wouldn't
3116+
// be correct.
31123117
auto kind = (isExpr<InOutExpr>(anchor) ||
31133118
(rhs->is<InOutType>() &&
3114-
matchKind == ConstraintKind::OperatorArgumentConversion))
3119+
(matchKind == ConstraintKind::ArgumentConversion ||
3120+
matchKind == ConstraintKind::OperatorArgumentConversion)))
31153121
? ConstraintKind::Bind
31163122
: matchKind;
31173123

test/Constraints/diagnostics.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,3 +1477,9 @@ func generic<T>(_ value: inout T, _ closure: (SR12725<T>) -> Void) {}
14771477

14781478
let arg: Int
14791479
generic(&arg) { (g: SR12725<Double>) -> Void in } // expected-error {{cannot convert value of type '(SR12725<Double>) -> Void' to expected argument type '(SR12725<Int>) -> Void'}}
1480+
1481+
// rdar://problem/62428353 - bad error message for passing `T` where `inout T` was expected
1482+
func rdar62428353<T>(_ t: inout T) {
1483+
let v = t // expected-note {{change 'let' to 'var' to make it mutable}} {{3-6=var}}
1484+
rdar62428353(v) // expected-error {{cannot pass immutable value as inout argument: 'v' is a 'let' constant}}
1485+
}

0 commit comments

Comments
 (0)