38
38
using namespace swift ;
39
39
using namespace ast_scope ;
40
40
41
- // / If true, nest scopes so a variable is out of scope before its declaration
42
- // / Does not handle capture rules for local functions properly.
43
- // / If false don't push uses down into subscopes after decls.
44
- static const bool handleUseBeforeDef = false ;
45
-
46
41
#pragma mark source range utilities
47
42
static bool rangeableIsIgnored (const Decl *d) { return d->isImplicit (); }
48
43
static bool rangeableIsIgnored (const Expr *d) {
@@ -746,11 +741,11 @@ class NodeAdder
746
741
if (auto *var = patternBinding->getSingleVar ())
747
742
scopeCreator.addChildrenForKnownAttributes (var, parentScope);
748
743
749
- const bool isInTypeDecl = parentScope-> isATypeDeclScope ();
744
+ const bool isLocalBinding = patternBinding-> getDeclContext ()-> isLocalContext ();
750
745
751
746
const DeclVisibilityKind vis =
752
- isInTypeDecl ? DeclVisibilityKind::MemberOfCurrentNominal
753
- : DeclVisibilityKind::LocalVariable ;
747
+ isLocalBinding ? DeclVisibilityKind::LocalVariable
748
+ : DeclVisibilityKind::MemberOfCurrentNominal ;
754
749
auto *insertionPoint = parentScope;
755
750
for (auto i : range (patternBinding->getNumPatternEntries ())) {
756
751
insertionPoint =
@@ -759,9 +754,12 @@ class NodeAdder
759
754
insertionPoint, patternBinding, i, vis)
760
755
.getPtrOr (insertionPoint);
761
756
}
762
- // If in a type decl, the type search will find these,
763
- // but if in a brace stmt, must continue under the last binding.
764
- return isInTypeDecl ? parentScope : insertionPoint;
757
+
758
+ ASTScopeAssert (isLocalBinding || insertionPoint == parentScope,
759
+ " Bindings at the top-level or members of types should "
760
+ " not change the insertion point" );
761
+
762
+ return insertionPoint;
765
763
}
766
764
767
765
NullablePtr<ASTScopeImpl> visitEnumElementDecl (EnumElementDecl *eed,
@@ -1041,11 +1039,13 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
1041
1039
scopeCreator.addChildrenForAllLocalizableAccessorsInSourceOrder (var, this );
1042
1040
});
1043
1041
1044
- ASTScopeAssert (!handleUseBeforeDef,
1045
- " next line is wrong otherwise; would need a use scope" );
1042
+ // In local context, the PatternEntryDeclScope becomes the insertion point, so
1043
+ // that all any bindings introduecd by the pattern are in scope for subsequent
1044
+ // lookups.
1045
+ if (vis == DeclVisibilityKind::LocalVariable)
1046
+ return {this , " All code that follows is inside this scope" };
1046
1047
1047
- return {getParent ().get (), " When not handling use-before-def, succeeding "
1048
- " code just goes in the same scope as this one" };
1048
+ return {getParent ().get (), " Global and type members do not introduce scopes" };
1049
1049
}
1050
1050
1051
1051
void
0 commit comments