diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -17159,6 +17159,9 @@ if (getCurFunction()) getCurFunction()->addBlock(BD); + if (BD->isInvalidDecl()) + return CreateRecoveryExpr(Result->getBeginLoc(), Result->getEndLoc(), + {Result}, Result->getType()); return Result; } diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -3730,6 +3730,13 @@ if (FunctionScopes.back()->FirstReturnLoc.isInvalid()) FunctionScopes.back()->FirstReturnLoc = ReturnLoc; + BlockScopeInfo *CurBlock = dyn_cast(CurCap); + if (CurBlock && CurCap->HasImplicitReturnType) { + BlockDecl *BD = CurBlock->TheDecl; + if (!BD->isInvalidDecl() && RetValExp && RetValExp->containsErrors()) + BD->setInvalidDecl(); + } + return Result; } diff --git a/clang/test/SemaObjC/crash-on-val-dep-block-expr.m b/clang/test/SemaObjC/crash-on-val-dep-block-expr.m new file mode 100644 --- /dev/null +++ b/clang/test/SemaObjC/crash-on-val-dep-block-expr.m @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -fblocks -fsyntax-only -verify %s +// no crash + +int (^a)() = ^() { + return c; // expected-error {{use of undeclared identifier 'c'}} +}; + +int (^b)() = (^() { + return c; // expected-error {{use of undeclared identifier 'c'}} +});