Skip to content

[Diagnostics] Correctly diagnose situations when immutable value is passed to inout parameter #82053

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 2 commits into from
Jun 9, 2025
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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5177,6 +5177,9 @@ ERROR(assignment_bang_has_immutable_subcomponent,none,
NOTE(candidate_is_not_assignable,none,
"candidate is not assignable: %kind0",
(const ValueDecl *))
NOTE(candidate_expects_inout_argument,none,
"candidate expects in-out value for parameter #%0 but argument is immutable",
(unsigned))

NOTE(change_to_mutating,none,
"mark %select{method|%1}0 'mutating' to make 'self' mutable",
Expand Down
15 changes: 14 additions & 1 deletion lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2049,11 +2049,24 @@ bool RValueTreatedAsLValueFailure::diagnoseAsError() {
}

bool RValueTreatedAsLValueFailure::diagnoseAsNote() {
auto overload = getCalleeOverloadChoiceIfAvailable(getLocator());
auto *locator = getLocator();

auto overload = getCalleeOverloadChoiceIfAvailable(locator);
if (!(overload && overload->choice.isDecl()))
return false;

auto *decl = overload->choice.getDecl();

if (locator->isLastElement<LocatorPathElt::ArgumentAttribute>()) {
auto argConv = locator->findLast<LocatorPathElt::ApplyArgToParam>();
if (!argConv)
return false;

emitDiagnosticAt(decl, diag::candidate_expects_inout_argument,
argConv->getParamIdx() + 1);
return true;
}

emitDiagnosticAt(decl, diag::candidate_is_not_assignable, decl);
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 @@ -730,7 +730,9 @@ ConstraintLocator *ConstraintSystem::getCalleeLocator(
}

if (locator->isLastElement<LocatorPathElt::ArgumentAttribute>()) {
return getConstraintLocator(anchor, path.drop_back());
auto argLoc = getConstraintLocator(anchor, path.drop_back());
return getCalleeLocator(argLoc, lookThroughApply, getType, simplifyType,
getOverloadFor);
}

// If we have a locator that starts with a key path component element, we
Expand Down
28 changes: 28 additions & 0 deletions test/Constraints/overload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,31 @@ do {
}
}
}

do {
struct S {
let x: Int
var y: Int
}

func overloaded(_: String) {}
// expected-note@-1 3 {{candidate expects value of type 'String' for parameter #1 (got 'Int')}}
func overloaded(_: inout Int) {}
// expected-note@-1 3 {{candidate expects in-out value for parameter #1 but argument is immutable}}

func testImmutable(s: S) {
overloaded(s.x) // expected-error {{no exact matches in call to local function 'overloaded'}}
}

func testMutable(s: inout S) {
overloaded(s.x) // expected-error {{no exact matches in call to local function 'overloaded'}}
}

func testImmutableBase(s: S) {
overloaded(s.y) // expected-error {{no exact matches in call to local function 'overloaded'}}
}

func testMissingAddressOf(s: inout S) {
overloaded(s.y) // expected-error {{passing value of type 'Int' to an inout parameter requires explicit '&'}}
}
}