Skip to content

Commit 3ec4ced

Browse files
committed
ASTScope: Don't handle top-level bindings in a SourceFile for now
1 parent f738a57 commit 3ec4ced

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

include/swift/AST/ASTScope.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#define SWIFT_AST_AST_SCOPE_H
3030

3131
#include "swift/AST/ASTNode.h"
32-
#include "swift/AST/NameLookup.h" // for DeclVisibilityKind
32+
#include "swift/AST/NameLookup.h"
3333
#include "swift/AST/SimpleRequest.h"
3434
#include "swift/Basic/Compiler.h"
3535
#include "swift/Basic/Debug.h"
@@ -1023,10 +1023,10 @@ class AbstractPatternEntryScope : public ASTScopeImpl {
10231023
public:
10241024
PatternBindingDecl *const decl;
10251025
const unsigned patternEntryIndex;
1026-
const DeclVisibilityKind vis;
1026+
const bool isLocalBinding;
10271027

10281028
AbstractPatternEntryScope(PatternBindingDecl *, unsigned entryIndex,
1029-
DeclVisibilityKind);
1029+
bool);
10301030
virtual ~AbstractPatternEntryScope() {}
10311031

10321032
const PatternBindingEntry &getPatternEntry() const;
@@ -1043,8 +1043,8 @@ class AbstractPatternEntryScope : public ASTScopeImpl {
10431043
class PatternEntryDeclScope final : public AbstractPatternEntryScope {
10441044
public:
10451045
PatternEntryDeclScope(PatternBindingDecl *pbDecl, unsigned entryIndex,
1046-
DeclVisibilityKind vis)
1047-
: AbstractPatternEntryScope(pbDecl, entryIndex, vis) {}
1046+
bool isLocalBinding)
1047+
: AbstractPatternEntryScope(pbDecl, entryIndex, isLocalBinding) {}
10481048
virtual ~PatternEntryDeclScope() {}
10491049

10501050
protected:
@@ -1071,8 +1071,8 @@ class PatternEntryInitializerScope final : public AbstractPatternEntryScope {
10711071

10721072
public:
10731073
PatternEntryInitializerScope(PatternBindingDecl *pbDecl, unsigned entryIndex,
1074-
DeclVisibilityKind vis)
1075-
: AbstractPatternEntryScope(pbDecl, entryIndex, vis),
1074+
bool isLocalBinding)
1075+
: AbstractPatternEntryScope(pbDecl, entryIndex, isLocalBinding),
10761076
initAsWrittenWhenCreated(pbDecl->getOriginalInit(entryIndex)) {}
10771077
virtual ~PatternEntryInitializerScope() {}
10781078

lib/AST/ASTScopeCreation.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -701,23 +701,23 @@ class NodeAdder
701701
if (auto *var = patternBinding->getSingleVar())
702702
scopeCreator.addChildrenForKnownAttributes(var, parentScope);
703703

704-
const bool isLocalBinding = patternBinding->getDeclContext()->isLocalContext();
705-
706-
const DeclVisibilityKind vis =
707-
isLocalBinding ? DeclVisibilityKind::LocalVariable
708-
: DeclVisibilityKind::MemberOfCurrentNominal;
709704
auto *insertionPoint = parentScope;
710705
for (auto i : range(patternBinding->getNumPatternEntries())) {
706+
bool isLocalBinding = false;
707+
if (auto *varDecl = patternBinding->getAnchoringVarDecl(i)) {
708+
isLocalBinding = varDecl->getDeclContext()->isLocalContext();
709+
}
710+
711711
insertionPoint =
712712
scopeCreator
713713
.ifUniqueConstructExpandAndInsert<PatternEntryDeclScope>(
714-
insertionPoint, patternBinding, i, vis)
714+
insertionPoint, patternBinding, i, isLocalBinding)
715715
.getPtrOr(insertionPoint);
716-
}
717716

718-
ASTScopeAssert(isLocalBinding || insertionPoint == parentScope,
719-
"Bindings at the top-level or members of types should "
720-
"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+
}
721721

722722
return insertionPoint;
723723
}
@@ -991,7 +991,7 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
991991
"Original inits are always after the '='");
992992
scopeCreator
993993
.constructExpandAndInsertUncheckable<PatternEntryInitializerScope>(
994-
this, decl, patternEntryIndex, vis);
994+
this, decl, patternEntryIndex, isLocalBinding);
995995
}
996996

997997
// Add accessors for the variables in this pattern.
@@ -1002,7 +1002,7 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
10021002
// In local context, the PatternEntryDeclScope becomes the insertion point, so
10031003
// that all any bindings introduecd by the pattern are in scope for subsequent
10041004
// lookups.
1005-
if (vis == DeclVisibilityKind::LocalVariable)
1005+
if (isLocalBinding)
10061006
return {this, "All code that follows is inside this scope"};
10071007

10081008
return {getParent().get(), "Global and type members do not introduce scopes"};
@@ -1378,8 +1378,9 @@ ASTScopeImpl *LabeledConditionalStmtScope::createNestedConditionalClauseScopes(
13781378

13791379
AbstractPatternEntryScope::AbstractPatternEntryScope(
13801380
PatternBindingDecl *declBeingScoped, unsigned entryIndex,
1381-
DeclVisibilityKind vis)
1382-
: decl(declBeingScoped), patternEntryIndex(entryIndex), vis(vis) {
1381+
bool isLocalBinding)
1382+
: decl(declBeingScoped), patternEntryIndex(entryIndex),
1383+
isLocalBinding(isLocalBinding) {
13831384
ASTScopeAssert(entryIndex < declBeingScoped->getPatternList().size(),
13841385
"out of bounds");
13851386
}

lib/AST/ASTScopeLookup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ bool GenericParamScope::lookupLocalsOrMembers(DeclConsumer consumer) const {
293293
}
294294

295295
bool PatternEntryDeclScope::lookupLocalsOrMembers(DeclConsumer consumer) const {
296-
if (vis != DeclVisibilityKind::LocalVariable)
297-
return false; // look in self type will find this later
296+
if (!isLocalBinding)
297+
return false;
298298
return lookupLocalBindingsInPattern(getPattern(), consumer);
299299
}
300300

lib/AST/Decl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,8 @@ void PatternBindingEntry::setInit(Expr *E) {
14471447
VarDecl *PatternBindingEntry::getAnchoringVarDecl() const {
14481448
SmallVector<VarDecl *, 8> variables;
14491449
getPattern()->collectVariables(variables);
1450-
assert(!variables.empty());
1450+
if (variables.empty())
1451+
return nullptr;
14511452
return variables[0];
14521453
}
14531454

test/NameLookup/scope_map_top_level.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ var i: Int = b.my_identity()
3131
// CHECK-EXPANDED-NEXT: `-NominalTypeBodyScope {{.*}}, [4:11 - 4:13]
3232
// CHECK-EXPANDED-NEXT: `-TopLevelCodeScope {{.*}}, [6:1 - 21:28]
3333
// CHECK-EXPANDED-NEXT: `-BraceStmtScope {{.*}}, [6:1 - 21:28]
34-
// CHECK-EXPANDED-NEXT: `-PatternEntryDeclScope {{.*}}, [6:5 - 21:28] entry 0 'a'
35-
// CHECK-EXPANDED-NEXT: |-PatternEntryInitializerScope {{.*}}, [6:15 - 6:15] entry 0 'a'
34+
// CHECK-EXPANDED-NEXT: |-PatternEntryDeclScope {{.*}}, [6:5 - 6:15] entry 0 'a'
35+
// CHECK-EXPANDED-NEXT: `-PatternEntryInitializerScope {{.*}}, [6:15 - 6:15] entry 0 'a'
3636
// CHECK-EXPANDED-NEXT: `-TopLevelCodeScope {{.*}}, [8:1 - 21:28]
3737
// CHECK-EXPANDED-NEXT: `-BraceStmtScope {{.*}}, [8:1 - 21:28]
3838
// CHECK-EXPANDED-NEXT: `-GuardStmtScope {{.*}}, [8:1 - 21:28]
@@ -46,9 +46,9 @@ var i: Int = b.my_identity()
4646
// CHECK-EXPANDED-NEXT: `-BraceStmtScope {{.*}}, [11:18 - 11:19]
4747
// CHECK-EXPANDED-NEXT: `-TopLevelCodeScope {{.*}}, [13:1 - 21:28]
4848
// CHECK-EXPANDED-NEXT: `-BraceStmtScope {{.*}}, [13:1 - 21:28]
49-
// CHECK-EXPANDED-NEXT: `-PatternEntryDeclScope {{.*}}, [13:5 - 21:28] entry 0 'c'
50-
// CHECK-EXPANDED-NEXT: |-PatternEntryInitializerScope {{.*}}, [13:9 - 13:9] entry 0 'c'
51-
// CHECK-EXPANDED-NEXT: |-TypeAliasDeclScope {{.*}}, [15:1 - 15:15]
49+
// CHECK-EXPANDED-NEXT: |-PatternEntryDeclScope {{.*}}, [13:5 - 13:9] entry 0 'c'
50+
// CHECK-EXPANDED-NEXT: `-PatternEntryInitializerScope {{.*}}, [13:9 - 13:9] entry 0 'c'
51+
// CHECK-EXPANDED-NEXT: |-TypeAliasDeclScope {{.*}}, [15:1 - 15:15]
5252
// CHECK-EXPANDED-NEXT: |-ExtensionDeclScope {{.*}}, [17:14 - 19:1]
5353
// CHECK-EXPANDED-NEXT: `-ExtensionBodyScope {{.*}}, [17:15 - 19:1]
5454
// CHECK-EXPANDED-NEXT: `-AbstractFunctionDeclScope {{.*}}, [18:3 - 18:43] 'my_identity()'

0 commit comments

Comments
 (0)