@@ -667,10 +667,30 @@ class NodeAdder
667
667
668
668
NullablePtr<ASTScopeImpl> visitBraceStmt (BraceStmt *bs, ASTScopeImpl *p,
669
669
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
+
670
688
auto maybeBraceScope =
671
- scopeCreator.ifUniqueConstructExpandAndInsert <BraceStmtScope>(p, bs);
689
+ scopeCreator.ifUniqueConstructExpandAndInsert <BraceStmtScope>(
690
+ p, bs, std::move (localFuncsAndTypes), std::move (localVars));
672
691
if (auto *s = scopeCreator.getASTContext ().Stats )
673
692
++s->getFrontendCounters ().NumBraceStmtASTScopes ;
693
+
674
694
return maybeBraceScope.getPtrOr (p);
675
695
}
676
696
@@ -681,23 +701,23 @@ class NodeAdder
681
701
if (auto *var = patternBinding->getSingleVar ())
682
702
scopeCreator.addChildrenForKnownAttributes (var, parentScope);
683
703
684
- const bool isLocalBinding = patternBinding->getDeclContext ()->isLocalContext ();
685
-
686
- const DeclVisibilityKind vis =
687
- isLocalBinding ? DeclVisibilityKind::LocalVariable
688
- : DeclVisibilityKind::MemberOfCurrentNominal;
689
704
auto *insertionPoint = parentScope;
690
705
for (auto i : range (patternBinding->getNumPatternEntries ())) {
706
+ bool isLocalBinding = false ;
707
+ if (auto *varDecl = patternBinding->getAnchoringVarDecl (i)) {
708
+ isLocalBinding = varDecl->getDeclContext ()->isLocalContext ();
709
+ }
710
+
691
711
insertionPoint =
692
712
scopeCreator
693
713
.ifUniqueConstructExpandAndInsert <PatternEntryDeclScope>(
694
- insertionPoint, patternBinding, i, vis )
714
+ insertionPoint, patternBinding, i, isLocalBinding )
695
715
.getPtrOr (insertionPoint);
696
- }
697
716
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
+ }
701
721
702
722
return insertionPoint;
703
723
}
@@ -971,7 +991,7 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
971
991
" Original inits are always after the '='" );
972
992
scopeCreator
973
993
.constructExpandAndInsertUncheckable <PatternEntryInitializerScope>(
974
- this , decl, patternEntryIndex, vis );
994
+ this , decl, patternEntryIndex, isLocalBinding );
975
995
}
976
996
977
997
// Add accessors for the variables in this pattern.
@@ -982,7 +1002,7 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
982
1002
// In local context, the PatternEntryDeclScope becomes the insertion point, so
983
1003
// that all any bindings introduecd by the pattern are in scope for subsequent
984
1004
// lookups.
985
- if (vis == DeclVisibilityKind::LocalVariable )
1005
+ if (isLocalBinding )
986
1006
return {this , " All code that follows is inside this scope" };
987
1007
988
1008
return {getParent ().get (), " Global and type members do not introduce scopes" };
@@ -1358,8 +1378,9 @@ ASTScopeImpl *LabeledConditionalStmtScope::createNestedConditionalClauseScopes(
1358
1378
1359
1379
AbstractPatternEntryScope::AbstractPatternEntryScope (
1360
1380
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) {
1363
1384
ASTScopeAssert (entryIndex < declBeingScoped->getPatternList ().size (),
1364
1385
" out of bounds" );
1365
1386
}
0 commit comments