Skip to content

Commit 184e238

Browse files
committed
[Diagnostics] Improve diagnostics for mutating callAsFunction used on r-value
1 parent 84e23cc commit 184e238

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ Expr *FailureDiagnostic::getBaseExprFor(Expr *anchor) const {
121121
return SE->getBase();
122122
else if (auto *MRE = dyn_cast<MemberRefExpr>(anchor))
123123
return MRE->getBase();
124+
else if (auto *call = dyn_cast<CallExpr>(anchor)) {
125+
auto fnType = getType(call->getFn());
126+
if (fnType->isCallableNominalType(getDC())) {
127+
return call->getFn();
128+
}
129+
}
124130

125131
return nullptr;
126132
}

test/Sema/call_as_function_simple.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ struct Mutating {
102102
}
103103
}
104104
func testMutating(_ x: Mutating, _ y: inout Mutating) {
105-
// TODO(SR-11378): Improve this error to match the error using a direct `callAsFunction` member reference.
106-
// expected-error @+2 {{cannot call value of non-function type 'Mutating'}}
107-
// expected-error @+1 {{cannot invoke 'x' with no arguments}}
105+
// expected-error @+1 {{cannot use mutating member on immutable value: 'x' is a 'let' constant}}
108106
_ = x()
109107
// expected-error @+1 {{cannot use mutating member on immutable value: 'x' is a 'let' constant}}
110108
_ = x.callAsFunction()

0 commit comments

Comments
 (0)