@@ -296,11 +296,27 @@ void StmtEmitter::visitBraceStmt(BraceStmt *S) {
296296 SGF.LocalAuxiliaryDecls .clear ();
297297 }
298298
299+ bool didDiagnoseUnreachableElements = false ;
299300 for (auto &ESD : S->getElements ()) {
300301
301- if (auto D = ESD.dyn_cast <Decl*>())
302+ if (auto D = ESD.dyn_cast <Decl*>()) {
302303 if (isa<IfConfigDecl>(D))
303304 continue ;
305+
306+ // Hoisted declarations are emitted at the top level by emitSourceFile().
307+ if (D->isHoisted ())
308+ continue ;
309+
310+ // PatternBindingBecls represent local variable bindings that execute
311+ // as part of the function's execution.
312+ if (!isa<PatternBindingDecl>(D)) {
313+ // Other decls define entities that may be used by the program, such as
314+ // local function declarations. So handle them here, before checking for
315+ // reachability, and then continue looping.
316+ SGF.visit (D);
317+ continue ;
318+ }
319+ }
304320
305321 // If we ever reach an unreachable point, stop emitting statements and issue
306322 // an unreachable code diagnostic.
@@ -350,6 +366,10 @@ void StmtEmitter::visitBraceStmt(BraceStmt *S) {
350366 continue ;
351367 }
352368
369+ if (didDiagnoseUnreachableElements)
370+ continue ;
371+ didDiagnoseUnreachableElements = true ;
372+
353373 if (StmtType != UnknownStmtType) {
354374 diagnose (getASTContext (), ESD.getStartLoc (),
355375 diag::unreachable_code_after_stmt, StmtType);
@@ -376,7 +396,7 @@ void StmtEmitter::visitBraceStmt(BraceStmt *S) {
376396 }
377397 }
378398 }
379- return ;
399+ continue ;
380400 }
381401
382402 // Process children.
@@ -395,11 +415,11 @@ void StmtEmitter::visitBraceStmt(BraceStmt *S) {
395415 } else {
396416 auto *D = ESD.get <Decl*>();
397417
398- // Hoisted declarations are emitted at the top level by emitSourceFile() .
399- if (D-> isHoisted ())
400- continue ;
418+ // Only PatternBindingDecls should be emitted here .
419+ // Other decls were handled above.
420+ auto PBD = cast<PatternBindingDecl>(D) ;
401421
402- SGF.visit (D );
422+ SGF.visit (PBD );
403423 }
404424 }
405425}
0 commit comments