@@ -667,10 +667,30 @@ class NodeAdder
667667
668668 NullablePtr<ASTScopeImpl> visitBraceStmt (BraceStmt *bs, ASTScopeImpl *p,
669669 ScopeCreator &scopeCreator) {
670+ SmallVector<ValueDecl *, 2 > localFuncsAndTypes;
671+ SmallVector<VarDecl *, 2 > localVars;
672+
673+ // All types and functions are visible anywhere within a brace statement
674+ // scope. When ordering matters (i.e. var decl) we will have split the brace
675+ // statement into nested scopes.
676+ for (auto braceElement : bs->getElements ()) {
677+ if (auto localBinding = braceElement.dyn_cast <Decl *>()) {
678+ if (auto *vd = dyn_cast<ValueDecl>(localBinding)) {
679+ if (isa<FuncDecl>(vd) || isa<TypeDecl>(vd)) {
680+ localFuncsAndTypes.push_back (vd);
681+ } else if (auto *var = dyn_cast<VarDecl>(localBinding)) {
682+ localVars.push_back (var);
683+ }
684+ }
685+ }
686+ }
687+
670688 auto maybeBraceScope =
671- scopeCreator.ifUniqueConstructExpandAndInsert <BraceStmtScope>(p, bs);
689+ scopeCreator.ifUniqueConstructExpandAndInsert <BraceStmtScope>(
690+ p, bs, std::move (localFuncsAndTypes), std::move (localVars));
672691 if (auto *s = scopeCreator.getASTContext ().Stats )
673692 ++s->getFrontendCounters ().NumBraceStmtASTScopes ;
693+
674694 return maybeBraceScope.getPtrOr (p);
675695 }
676696
@@ -681,23 +701,23 @@ class NodeAdder
681701 if (auto *var = patternBinding->getSingleVar ())
682702 scopeCreator.addChildrenForKnownAttributes (var, parentScope);
683703
684- const bool isLocalBinding = patternBinding->getDeclContext ()->isLocalContext ();
685-
686- const DeclVisibilityKind vis =
687- isLocalBinding ? DeclVisibilityKind::LocalVariable
688- : DeclVisibilityKind::MemberOfCurrentNominal;
689704 auto *insertionPoint = parentScope;
690705 for (auto i : range (patternBinding->getNumPatternEntries ())) {
706+ bool isLocalBinding = false ;
707+ if (auto *varDecl = patternBinding->getAnchoringVarDecl (i)) {
708+ isLocalBinding = varDecl->getDeclContext ()->isLocalContext ();
709+ }
710+
691711 insertionPoint =
692712 scopeCreator
693713 .ifUniqueConstructExpandAndInsert <PatternEntryDeclScope>(
694- insertionPoint, patternBinding, i, vis )
714+ insertionPoint, patternBinding, i, isLocalBinding )
695715 .getPtrOr (insertionPoint);
696- }
697716
698- ASTScopeAssert (isLocalBinding || insertionPoint == parentScope,
699- " Bindings at the top-level or members of types should "
700- " not change the insertion point" );
717+ ASTScopeAssert (isLocalBinding || insertionPoint == parentScope,
718+ " Bindings at the top-level or members of types should "
719+ " not change the insertion point" );
720+ }
701721
702722 return insertionPoint;
703723 }
@@ -971,7 +991,7 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
971991 " Original inits are always after the '='" );
972992 scopeCreator
973993 .constructExpandAndInsertUncheckable <PatternEntryInitializerScope>(
974- this , decl, patternEntryIndex, vis );
994+ this , decl, patternEntryIndex, isLocalBinding );
975995 }
976996
977997 // Add accessors for the variables in this pattern.
@@ -982,7 +1002,7 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
9821002 // In local context, the PatternEntryDeclScope becomes the insertion point, so
9831003 // that all any bindings introduecd by the pattern are in scope for subsequent
9841004 // lookups.
985- if (vis == DeclVisibilityKind::LocalVariable )
1005+ if (isLocalBinding )
9861006 return {this , " All code that follows is inside this scope" };
9871007
9881008 return {getParent ().get (), " Global and type members do not introduce scopes" };
@@ -1358,8 +1378,9 @@ ASTScopeImpl *LabeledConditionalStmtScope::createNestedConditionalClauseScopes(
13581378
13591379AbstractPatternEntryScope::AbstractPatternEntryScope (
13601380 PatternBindingDecl *declBeingScoped, unsigned entryIndex,
1361- DeclVisibilityKind vis)
1362- : decl(declBeingScoped), patternEntryIndex(entryIndex), vis(vis) {
1381+ bool isLocalBinding)
1382+ : decl(declBeingScoped), patternEntryIndex(entryIndex),
1383+ isLocalBinding(isLocalBinding) {
13631384 ASTScopeAssert (entryIndex < declBeingScoped->getPatternList ().size (),
13641385 " out of bounds" );
13651386}
0 commit comments