-
Notifications
You must be signed in to change notification settings - Fork 10.5k
ASTScope: Take start location into account when modeling local pattern bindings #34038
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
81fc342
cecbf21
c5edc45
5461508
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,11 +38,6 @@ | |
using namespace swift; | ||
using namespace ast_scope; | ||
|
||
/// If true, nest scopes so a variable is out of scope before its declaration | ||
/// Does not handle capture rules for local functions properly. | ||
/// If false don't push uses down into subscopes after decls. | ||
static const bool handleUseBeforeDef = false; | ||
|
||
#pragma mark source range utilities | ||
static bool rangeableIsIgnored(const Decl *d) { return d->isImplicit(); } | ||
static bool rangeableIsIgnored(const Expr *d) { | ||
|
@@ -746,11 +741,11 @@ class NodeAdder | |
if (auto *var = patternBinding->getSingleVar()) | ||
scopeCreator.addChildrenForKnownAttributes(var, parentScope); | ||
|
||
const bool isInTypeDecl = parentScope->isATypeDeclScope(); | ||
const bool isLocalBinding = patternBinding->getDeclContext()->isLocalContext(); | ||
|
||
const DeclVisibilityKind vis = | ||
isInTypeDecl ? DeclVisibilityKind::MemberOfCurrentNominal | ||
: DeclVisibilityKind::LocalVariable; | ||
isLocalBinding ? DeclVisibilityKind::LocalVariable | ||
: DeclVisibilityKind::MemberOfCurrentNominal; | ||
auto *insertionPoint = parentScope; | ||
for (auto i : range(patternBinding->getNumPatternEntries())) { | ||
insertionPoint = | ||
|
@@ -759,9 +754,12 @@ class NodeAdder | |
insertionPoint, patternBinding, i, vis) | ||
.getPtrOr(insertionPoint); | ||
} | ||
// If in a type decl, the type search will find these, | ||
// but if in a brace stmt, must continue under the last binding. | ||
return isInTypeDecl ? parentScope : insertionPoint; | ||
|
||
ASTScopeAssert(isLocalBinding || insertionPoint == parentScope, | ||
"Bindings at the top-level or members of types should " | ||
"not change the insertion point"); | ||
Comment on lines
+758
to
+760
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
|
||
return insertionPoint; | ||
} | ||
|
||
NullablePtr<ASTScopeImpl> visitEnumElementDecl(EnumElementDecl *eed, | ||
|
@@ -1041,11 +1039,13 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint( | |
scopeCreator.addChildrenForAllLocalizableAccessorsInSourceOrder(var, this); | ||
}); | ||
|
||
ASTScopeAssert(!handleUseBeforeDef, | ||
"next line is wrong otherwise; would need a use scope"); | ||
// In local context, the PatternEntryDeclScope becomes the insertion point, so | ||
// that all any bindings introduecd by the pattern are in scope for subsequent | ||
// lookups. | ||
Comment on lines
+1042
to
+1044
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice and clear! |
||
if (vis == DeclVisibilityKind::LocalVariable) | ||
return {this, "All code that follows is inside this scope"}; | ||
|
||
return {getParent().get(), "When not handling use-before-def, succeeding " | ||
"code just goes in the same scope as this one"}; | ||
return {getParent().get(), "Global and type members do not introduce scopes"}; | ||
} | ||
|
||
void | ||
|
@@ -1424,11 +1424,6 @@ AbstractPatternEntryScope::AbstractPatternEntryScope( | |
"out of bounds"); | ||
} | ||
|
||
bool ASTScopeImpl::isATypeDeclScope() const { | ||
Decl *const pd = getDeclIfAny().getPtrOrNull(); | ||
return pd && (isa<NominalTypeDecl>(pd) || isa<ExtensionDecl>(pd)); | ||
} | ||
|
||
#pragma mark new operators | ||
void *ASTScopeImpl::operator new(size_t bytes, const ASTContext &ctx, | ||
unsigned alignment) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,8 +31,8 @@ var i: Int = b.my_identity() | |
// CHECK-EXPANDED-NEXT: `-NominalTypeBodyScope {{.*}}, [4:11 - 4:13] | ||
// CHECK-EXPANDED-NEXT: `-TopLevelCodeScope {{.*}}, [6:1 - 21:28] | ||
// CHECK-EXPANDED-NEXT: `-BraceStmtScope {{.*}}, [6:1 - 21:28] | ||
// CHECK-EXPANDED-NEXT: |-PatternEntryDeclScope {{.*}}, [6:5 - 6:15] entry 0 'a' | ||
// CHECK-EXPANDED-NEXT: `-PatternEntryInitializerScope {{.*}}, [6:15 - 6:15] entry 0 'a' | ||
// CHECK-EXPANDED-NEXT: `-PatternEntryDeclScope {{.*}}, [6:5 - 21:28] entry 0 'a' | ||
// CHECK-EXPANDED-NEXT: |-PatternEntryInitializerScope {{.*}}, [6:15 - 6:15] entry 0 'a' | ||
// CHECK-EXPANDED-NEXT: `-TopLevelCodeScope {{.*}}, [8:1 - 21:28] | ||
// CHECK-EXPANDED-NEXT: `-BraceStmtScope {{.*}}, [8:1 - 21:28] | ||
// CHECK-EXPANDED-NEXT: `-GuardStmtScope {{.*}}, [8:1 - 21:28] | ||
|
@@ -46,9 +46,9 @@ var i: Int = b.my_identity() | |
// CHECK-EXPANDED-NEXT: `-BraceStmtScope {{.*}}, [11:18 - 11:19] | ||
// CHECK-EXPANDED-NEXT: `-TopLevelCodeScope {{.*}}, [13:1 - 21:28] | ||
// CHECK-EXPANDED-NEXT: `-BraceStmtScope {{.*}}, [13:1 - 21:28] | ||
// CHECK-EXPANDED-NEXT: |-PatternEntryDeclScope {{.*}}, [13:5 - 13:9] entry 0 'c' | ||
// CHECK-EXPANDED-NEXT: `-PatternEntryInitializerScope {{.*}}, [13:9 - 13:9] entry 0 'c' | ||
// CHECK-EXPANDED-NEXT: |-TypeAliasDeclScope {{.*}}, [15:1 - 15:15] | ||
// CHECK-EXPANDED-NEXT: `-PatternEntryDeclScope {{.*}}, [13:5 - 21:28] entry 0 'c' | ||
// CHECK-EXPANDED-NEXT: |-PatternEntryInitializerScope {{.*}}, [13:9 - 13:9] entry 0 'c' | ||
// CHECK-EXPANDED-NEXT: |-TypeAliasDeclScope {{.*}}, [15:1 - 15:15] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: It seems like the TypeAliasDeclScope got overindented along the way. |
||
// CHECK-EXPANDED-NEXT: |-ExtensionDeclScope {{.*}}, [17:14 - 19:1] | ||
// CHECK-EXPANDED-NEXT: `-ExtensionBodyScope {{.*}}, [17:15 - 19:1] | ||
// CHECK-EXPANDED-NEXT: `-AbstractFunctionDeclScope {{.*}}, [18:3 - 18:43] 'my_identity()' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the flag I've been talking about!