Skip to content

Commit 9818b55

Browse files
committed
[CSDiagnostics] Add MissingCall failure
It's used to diagnose cases when function/method are used as a property instead of being called.
1 parent f086d41 commit 9818b55

File tree

5 files changed

+53
-24
lines changed

5 files changed

+53
-24
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7392,26 +7392,9 @@ bool FailureDiagnosis::diagnoseMemberFailures(
73927392
// Produce a specific diagnostic + fixit for this situation.
73937393
if (auto baseFTy = baseObjTy->getAs<AnyFunctionType>()) {
73947394
if (baseExpr && baseFTy->getParams().empty()) {
7395-
SourceLoc insertLoc = baseExpr->getEndLoc();
7396-
7397-
if (auto *DRE = dyn_cast<DeclRefExpr>(baseExpr)) {
7398-
diagnose(baseExpr->getLoc(), diag::did_not_call_function,
7399-
DRE->getDecl()->getBaseName().getIdentifier())
7400-
.fixItInsertAfter(insertLoc, "()");
7401-
return true;
7402-
}
7403-
7404-
if (auto *DSCE = dyn_cast<DotSyntaxCallExpr>(baseExpr))
7405-
if (auto *DRE = dyn_cast<DeclRefExpr>(DSCE->getFn())) {
7406-
diagnose(baseExpr->getLoc(), diag::did_not_call_method,
7407-
DRE->getDecl()->getBaseName().getIdentifier())
7408-
.fixItInsertAfter(insertLoc, "()");
7409-
return true;
7410-
}
7411-
7412-
diagnose(baseExpr->getLoc(), diag::did_not_call_function_value)
7413-
.fixItInsertAfter(insertLoc, "()");
7414-
return true;
7395+
auto failure =
7396+
MissingCallFailure(expr, CS, CS.getConstraintLocator(baseExpr));
7397+
return failure.diagnoseAsError();
74157398
}
74167399
}
74177400

lib/Sema/CSDiagnostics.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,3 +1240,38 @@ bool NonOptionalUnwrapFailure::diagnoseAsError() {
12401240

12411241
return true;
12421242
}
1243+
1244+
bool MissingCallFailure::diagnoseAsError() {
1245+
auto *baseExpr = getAnchor();
1246+
SourceLoc insertLoc = baseExpr->getEndLoc();
1247+
1248+
if (auto *FVE = dyn_cast<ForceValueExpr>(baseExpr))
1249+
baseExpr = FVE->getSubExpr();
1250+
1251+
if (auto *DRE = dyn_cast<DeclRefExpr>(baseExpr)) {
1252+
emitDiagnostic(baseExpr->getLoc(), diag::did_not_call_function,
1253+
DRE->getDecl()->getBaseName().getIdentifier())
1254+
.fixItInsertAfter(insertLoc, "()");
1255+
return true;
1256+
}
1257+
1258+
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(baseExpr)) {
1259+
emitDiagnostic(baseExpr->getLoc(), diag::did_not_call_method,
1260+
UDE->getName().getBaseIdentifier())
1261+
.fixItInsertAfter(insertLoc, "()");
1262+
return true;
1263+
}
1264+
1265+
if (auto *DSCE = dyn_cast<DotSyntaxCallExpr>(baseExpr)) {
1266+
if (auto *DRE = dyn_cast<DeclRefExpr>(DSCE->getFn())) {
1267+
emitDiagnostic(baseExpr->getLoc(), diag::did_not_call_method,
1268+
DRE->getDecl()->getBaseName().getIdentifier())
1269+
.fixItInsertAfter(insertLoc, "()");
1270+
return true;
1271+
}
1272+
}
1273+
1274+
emitDiagnostic(baseExpr->getLoc(), diag::did_not_call_function_value)
1275+
.fixItInsertAfter(insertLoc, "()");
1276+
return true;
1277+
}

lib/Sema/CSDiagnostics.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,15 @@ class NonOptionalUnwrapFailure final : public FailureDiagnostic {
630630
bool diagnoseAsError() override;
631631
};
632632

633+
class MissingCallFailure final : public FailureDiagnostic {
634+
public:
635+
MissingCallFailure(Expr *root, ConstraintSystem &cs,
636+
ConstraintLocator *locator)
637+
: FailureDiagnostic(root, cs, locator) {}
638+
639+
bool diagnoseAsError() override;
640+
};
641+
633642
} // end namespace constraints
634643
} // end namespace swift
635644

lib/Sema/CSFix.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ RemoveUnwrap *RemoveUnwrap::create(ConstraintSystem &cs, Type baseType,
227227
}
228228

229229
bool InsertExplicitCall::diagnose(Expr *root, bool asNote) const {
230-
return false;
230+
auto failure = MissingCallFailure(root, getConstraintSystem(), getLocator());
231+
return failure.diagnose(asNote);
231232
}
232233

233234
InsertExplicitCall *InsertExplicitCall::create(ConstraintSystem &cs,

test/Constraints/fixes.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ class U {
104104

105105
class T {
106106
func m1() {
107-
// FIXME: should apply nullary function fixit here. {{function produces expected type 'U'; did you mean to call it with '()'?}}
108-
// <rdar://problem/17741575>
109-
let l = self.m2!.prop1 // expected-error{{cannot force unwrap value of non-optional type '() -> U?'}} {{24-25=}}
107+
// <rdar://problem/17741575>
108+
let l = self.m2!.prop1
109+
// expected-error@-1 {{cannot force unwrap value of non-optional type '() -> U?'}} {{22-23=}}
110+
// expected-error@-2 {{method 'm2' was used as a property; add () to call it}} {{23-23=()}}
110111
}
111112

112113
func m2() -> U! {

0 commit comments

Comments
 (0)