@@ -2612,7 +2612,6 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
2612
2612
// non-function/non-metatype type, then we cannot call it!
2613
2613
if (!isUnresolvedOrTypeVarType (fnType) &&
2614
2614
!fnType->is <AnyFunctionType>() && !fnType->is <MetatypeType>()) {
2615
-
2616
2615
auto arg = callExpr->getArg ();
2617
2616
auto isDynamicCallable =
2618
2617
CS.DynamicCallableCache [fnType->getCanonicalType ()].isValid ();
@@ -2625,7 +2624,7 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
2625
2624
return funcDecl && funcDecl->isCallAsFunctionMethod ();
2626
2625
});
2627
2626
2628
- // Diagnose @dynamicCallable errors.
2627
+ // Diagnose specific @dynamicCallable errors.
2629
2628
if (isDynamicCallable) {
2630
2629
auto dynamicCallableMethods =
2631
2630
CS.DynamicCallableCache [fnType->getCanonicalType ()];
@@ -2644,13 +2643,12 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
2644
2643
}
2645
2644
}
2646
2645
2647
- if (fnType->is <ExistentialMetatypeType>()) {
2646
+ auto isExistentialMetatypeType = fnType->is <ExistentialMetatypeType>();
2647
+ if (isExistentialMetatypeType) {
2648
2648
auto diag = diagnose (arg->getStartLoc (),
2649
2649
diag::missing_init_on_metatype_initialization);
2650
2650
diag.highlight (fnExpr->getSourceRange ());
2651
- }
2652
-
2653
- if (!fnType->is <ExistentialMetatypeType>()) {
2651
+ } else if (!isDynamicCallable) {
2654
2652
auto diag = diagnose (arg->getStartLoc (),
2655
2653
diag::cannot_call_non_function_value, fnType);
2656
2654
diag.highlight (fnExpr->getSourceRange ());
@@ -2669,7 +2667,7 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
2669
2667
// the line after the callee, then it's likely the user forgot to
2670
2668
// write "do" before their brace stmt.
2671
2669
// Note that line differences of more than 1 are diagnosed during parsing.
2672
- if (auto *PE = dyn_cast<ParenExpr>(arg))
2670
+ if (auto *PE = dyn_cast<ParenExpr>(arg)) {
2673
2671
if (PE->hasTrailingClosure () && isa<ClosureExpr>(PE->getSubExpr ())) {
2674
2672
auto *closure = cast<ClosureExpr>(PE->getSubExpr ());
2675
2673
auto &SM = CS.getASTContext ().SourceMgr ;
@@ -2681,9 +2679,14 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
2681
2679
.fixItInsert (closure->getStartLoc (), " do " );
2682
2680
}
2683
2681
}
2682
+ }
2684
2683
2685
- if (!isDynamicCallable && !hasCallAsFunctionMethods)
2684
+ // Use the existing machinery to provide more useful diagnostics for
2685
+ // @dynamicCallable calls, rather than cannot_call_non_function_value.
2686
+ if ((isExistentialMetatypeType || !isDynamicCallable) &&
2687
+ !hasCallAsFunctionMethods) {
2686
2688
return true ;
2689
+ }
2687
2690
}
2688
2691
2689
2692
bool hasTrailingClosure = callArgHasTrailingClosure (callExpr->getArg ());
0 commit comments