3838using namespace swift ;
3939using namespace ast_scope ;
4040
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-
4641#pragma mark source range utilities
4742static bool rangeableIsIgnored (const Decl *d) { return d->isImplicit (); }
4843static bool rangeableIsIgnored (const Expr *d) {
@@ -746,11 +741,11 @@ class NodeAdder
746741 if (auto *var = patternBinding->getSingleVar ())
747742 scopeCreator.addChildrenForKnownAttributes (var, parentScope);
748743
749- const bool isInTypeDecl = parentScope-> isATypeDeclScope ();
744+ const bool isLocalBinding = patternBinding-> getDeclContext ()-> isLocalContext ();
750745
751746 const DeclVisibilityKind vis =
752- isInTypeDecl ? DeclVisibilityKind::MemberOfCurrentNominal
753- : DeclVisibilityKind::LocalVariable ;
747+ isLocalBinding ? DeclVisibilityKind::LocalVariable
748+ : DeclVisibilityKind::MemberOfCurrentNominal ;
754749 auto *insertionPoint = parentScope;
755750 for (auto i : range (patternBinding->getNumPatternEntries ())) {
756751 insertionPoint =
@@ -759,9 +754,12 @@ class NodeAdder
759754 insertionPoint, patternBinding, i, vis)
760755 .getPtrOr (insertionPoint);
761756 }
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;
765763 }
766764
767765 NullablePtr<ASTScopeImpl> visitEnumElementDecl (EnumElementDecl *eed,
@@ -1041,11 +1039,13 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
10411039 scopeCreator.addChildrenForAllLocalizableAccessorsInSourceOrder (var, this );
10421040 });
10431041
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" };
10461047
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" };
10491049}
10501050
10511051void
0 commit comments