@@ -6116,6 +6116,24 @@ static void noteArchetypeSource(const TypeLoc &loc, ArchetypeType *archetype,
61166116}
61176117
61186118
6119+ // / Check the specified closure to see if it is a multi-statement closure with
6120+ // / an uninferred type. If so, diagnose the problem with an error and return
6121+ // / true.
6122+ static bool checkMultistatementClosureForAmbiguity (ClosureExpr *closure,
6123+ TypeChecker &tc) {
6124+ if (closure->hasSingleExpressionBody () ||
6125+ closure->hasExplicitResultType ())
6126+ return false ;
6127+
6128+ auto closureType = closure->getType ()->getAs <AnyFunctionType>();
6129+ if (!closureType || !isUnresolvedOrTypeVarType (closureType->getResult ()))
6130+ return false ;
6131+
6132+ tc.diagnose (closure->getLoc (), diag::cannot_infer_closure_result_type);
6133+ return true ;
6134+ }
6135+
6136+
61196137// / Emit an error message about an unbound generic parameter existing, and
61206138// / emit notes referring to the target of a diagnostic, e.g., the function
61216139// / or parameter being used.
@@ -6140,6 +6158,21 @@ static void diagnoseUnboundArchetype(Expr *overallExpr,
61406158 ND->getDeclaredType ());
61416159 return ;
61426160 }
6161+
6162+ // A very common cause of this diagnostic is a situation where a closure expr
6163+ // has no inferred type, due to being a multiline closure. Check to see if
6164+ // this is the case and (if so), speculatively diagnose that as the problem.
6165+ bool didDiagnose = false ;
6166+ overallExpr->forEachChildExpr ([&](Expr *subExpr) -> Expr*{
6167+ auto closure = dyn_cast<ClosureExpr>(subExpr);
6168+ if (!didDiagnose && closure)
6169+ didDiagnose = checkMultistatementClosureForAmbiguity (closure, tc);
6170+
6171+ return subExpr;
6172+ });
6173+
6174+ if (didDiagnose) return ;
6175+
61436176
61446177 // Otherwise, emit an error message on the expr we have, and emit a note
61456178 // about where the archetype came from.
@@ -6225,16 +6258,11 @@ void FailureDiagnosis::diagnoseAmbiguity(Expr *E) {
62256258 // Unresolved/Anonymous ClosureExprs are common enough that we should give
62266259 // them tailored diagnostics.
62276260 if (auto CE = dyn_cast<ClosureExpr>(E->getValueProvidingExpr ())) {
6228- auto CFTy = CE->getType ()->getAs <AnyFunctionType>();
6229-
62306261 // If this is a multi-statement closure with no explicit result type, emit
62316262 // a note to clue the developer in.
6232- if (!CE->hasExplicitResultType () && CFTy &&
6233- isUnresolvedOrTypeVarType (CFTy->getResult ())) {
6234- diagnose (CE->getLoc (), diag::cannot_infer_closure_result_type);
6263+ if (checkMultistatementClosureForAmbiguity (CE, CS->getTypeChecker ()))
62356264 return ;
6236- }
6237-
6265+
62386266 diagnose (E->getLoc (), diag::cannot_infer_closure_type)
62396267 .highlight (E->getSourceRange ());
62406268 return ;
0 commit comments