Skip to content

[Scope map] Local types and functions are visible in the enclosing braces #4816

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions lib/AST/ASTScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,8 @@ bool ASTScope::canStealContinuation() const {
case ASTScopeKind::ForStmt:
case ASTScopeKind::ForStmtInitializer:
case ASTScopeKind::Closure:
case ASTScopeKind::TypeDecl:
case ASTScopeKind::AbstractFunctionDecl:
// These node kinds don't introduce names that would be visible in a
// continuation.
return false;
Expand All @@ -1261,8 +1263,6 @@ bool ASTScope::canStealContinuation() const {
return getParent()->getKind() == ASTScopeKind::TopLevelCode;

case ASTScopeKind::AfterPatternBinding:
case ASTScopeKind::TypeDecl:
case ASTScopeKind::AbstractFunctionDecl:
case ASTScopeKind::PatternBinding:
// Declarations always steal continuations.
return true;
Expand Down Expand Up @@ -1789,11 +1789,12 @@ SmallVector<ValueDecl *, 4> ASTScope::getLocalBindings() const {
switch (getKind()) {
case ASTScopeKind::Preexpanded:
case ASTScopeKind::SourceFile:
case ASTScopeKind::AbstractFunctionDecl:
case ASTScopeKind::TypeDecl:
case ASTScopeKind::TypeOrExtensionBody:
case ASTScopeKind::DefaultArgument:
case ASTScopeKind::AbstractFunctionBody:
case ASTScopeKind::PatternBinding:
case ASTScopeKind::BraceStmt:
case ASTScopeKind::IfStmt:
case ASTScopeKind::GuardStmt:
case ASTScopeKind::RepeatWhileStmt:
Expand All @@ -1806,11 +1807,6 @@ SmallVector<ValueDecl *, 4> ASTScope::getLocalBindings() const {
// No local declarations.
break;

case ASTScopeKind::TypeDecl:
if (getHistoricalContinuation())
result.push_back(typeDecl);
break;

case ASTScopeKind::ExtensionGenericParams:
// Bind this extension, if we haven't done so already.
if (!extension->getExtendedType())
Expand Down Expand Up @@ -1840,22 +1836,22 @@ SmallVector<ValueDecl *, 4> ASTScope::getLocalBindings() const {
handlePattern(patternBinding.decl->getPattern(patternBinding.entry));
break;

case ASTScopeKind::AbstractFunctionDecl:
// FIXME: This allows overloading to work in top-level code, but is probably
// incorrect. We should likely use
//
// if (getHistoricalContext())
//
// and name lookup should apply the normal name-hiding rules.
if (abstractFunction->getDeclContext()->isLocalContext())
result.push_back(abstractFunction);
break;

case ASTScopeKind::ConditionalClause:
handlePattern(conditionalClause.stmt->getCond()[conditionalClause.index]
.getPatternOrNull());
break;

case ASTScopeKind::BraceStmt:
// All types and functions are visible anywhere within their brace
// statements. It's up to capture analysis to determine what is usable.
for (auto element : braceStmt.stmt->getElements()) {
if (auto decl = element.dyn_cast<Decl *>()) {
if (isa<AbstractFunctionDecl>(decl) || isa<TypeDecl>(decl))
result.push_back(cast<ValueDecl>(decl));
}
}
break;

case ASTScopeKind::ForEachPattern:
handlePattern(forEach->getPattern());
break;
Expand Down
6 changes: 3 additions & 3 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,12 @@ static DeclVisibilityKind getLocalDeclVisibilityKind(const ASTScope *scope) {
switch (scope->getKind()) {
case ASTScopeKind::Preexpanded:
case ASTScopeKind::SourceFile:
case ASTScopeKind::TypeDecl:
case ASTScopeKind::AbstractFunctionDecl:
case ASTScopeKind::TypeOrExtensionBody:
case ASTScopeKind::AbstractFunctionBody:
case ASTScopeKind::DefaultArgument:
case ASTScopeKind::PatternBinding:
case ASTScopeKind::BraceStmt:
case ASTScopeKind::IfStmt:
case ASTScopeKind::GuardStmt:
case ASTScopeKind::RepeatWhileStmt:
Expand All @@ -427,11 +428,10 @@ static DeclVisibilityKind getLocalDeclVisibilityKind(const ASTScope *scope) {
case ASTScopeKind::PatternInitializer: // lazy var 'self'
return DeclVisibilityKind::FunctionParameter;

case ASTScopeKind::TypeDecl:
case ASTScopeKind::AbstractFunctionDecl:
case ASTScopeKind::AfterPatternBinding:
case ASTScopeKind::ConditionalClause:
case ASTScopeKind::ForEachPattern:
case ASTScopeKind::BraceStmt:
case ASTScopeKind::CatchStmt:
case ASTScopeKind::CaseStmt:
case ASTScopeKind::ForStmtInitializer:
Expand Down
26 changes: 13 additions & 13 deletions test/NameBinding/scope_map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,14 @@ class LazyProperties {
// CHECK-EXPANDED-NEXT: {{^}} |-PatternInitializer {{.*}} entry 0 [54:14 - 54:14] expanded
// CHECK-EXPANDED-NEXT: {{^}} `-AfterPatternBinding {{.*}} entry 0 [54:14 - 56:3] expanded
// CHECK-EXPANDED: {{^}} `-AfterPatternBinding {{.*}} entry 0 [55:14 - 56:3] expanded
// CHECK-EXPANDED-NEXT: {{^}} `-AbstractFunctionDecl {{.*}} f(_:) [57:3 - 100:1] expanded
// CHECK-EXPANDED-NEXT: {{^}} |-AbstractFunctionParams {{.*}} f(_:) param 0:0 [57:15 - 57:38] expanded
// CHECK-EXPANDED-NEXT: {{^}} |-AbstractFunctionDecl {{.*}} f(_:) [57:3 - 57:38] expanded
// CHECK-EXPANDED-NEXT: {{^}} `-AbstractFunctionParams {{.*}} f(_:) param 0:0 [57:15 - 57:38] expanded
// CHECK-EXPANDED-NEXT: `-AbstractFunctionBody {{.*}} f(_:) [57:27 - 57:38] expanded
// CHECK-EXPANDED-NEXT: {{^}} `-BraceStmt {{.*}} [57:27 - 57:38] expanded
// CHECK-EXPANDED: {{^}} `-AfterPatternBinding {{.*}} entry 0 [58:16 - 100:1] expanded
// CHECK-EXPANDED-NEXT: {{^}} `-TypeDecl {{.*}} S7 [59:3 - 100:1] expanded
// CHECK-EXPANDED-NEXT: {{^}} |-TypeOrExtensionBody {{.*}} 'S7' [59:13 - 59:15] expanded
// CHECK-EXPANDED-NEXT: {{^}} `-TypeDecl {{.*}} S7Alias [60:3 - 100:1] expanded
// CHECK-EXPANDED-NEXT: {{^}} |-TypeDecl {{.*}} S7 [59:3 - 59:15] expanded
// CHECK-EXPANDED-NEXT: {{^}} `-TypeOrExtensionBody {{.*}} 'S7' [59:13 - 59:15] expanded
// CHECK-EXPANDED-NEXT: {{^}} |-TypeDecl {{.*}} S7Alias [60:3 - 60:23] expanded
// CHECK-EXPANDED-NEXT: {{^}} |-IfStmt {{.*}} [62:3 - 66:3] expanded
// CHECK-EXPANDED-NEXT: {{^}} |-ConditionalClause {{.*}} index 0 [62:18 - 64:3] expanded
// CHECK-EXPANDED-NEXT: {{^}} `-ConditionalClause {{.*}} index 1 [62:29 - 64:3] expanded
Expand Down Expand Up @@ -376,7 +376,7 @@ class LazyProperties {
// CHECK-EXPANDED: {{^}} `-BraceStmt {{.*}} [146:9 - 148:5] expanded
// CHECK-EXPANDED: {{^}} `-AfterPatternBinding {{.*}} entry 1 [149:36 - 155:1] expanded
// CHECK-EXPANDED: {{^}} `-AfterPatternBinding {{.*}} entry 2 [150:21 - 155:1] expanded
// CHECK-EXPANDED-NEXT: {{^}} `-AbstractFunctionDecl {{.*}} _ [150:25 - 155:1] expanded
// CHECK-EXPANDED-NEXT: {{^}} |-AbstractFunctionDecl {{.*}} _ [150:25 - 152:3] expanded
// CHECK-EXPANDED: {{^}} `-BraceStmt {{.*}} [150:25 - 152:3] expanded
// CHECK-EXPANDED-NEXT: {{^}} `-BraceStmt {{.*}} [154:6 - 154:8] expanded

Expand All @@ -395,7 +395,7 @@ class LazyProperties {
// CHECK-EXPANDED-NEXT: {{^}} |-Closure {{.*}} [164:1 - 164:14] expanded
// CHECK-EXPANDED-NEXT: {{^}} `-BraceStmt {{.*}} [164:1 - 164:14] expanded

// CHECK-EXPANDED: -AbstractFunctionDecl {{.*}} defaultArguments(i:j:) [166:1 - [[EOF]]] expanded
// CHECK-EXPANDED: -AbstractFunctionDecl {{.*}} defaultArguments(i:j:) [166:1 - 175:1] expanded
// CHECK-EXPANDED: {{^}} |-DefaultArgument {{.*}} [166:32 - 166:32] expanded
// CHECK-EXPANDED-NEXT: {{^}} `-AbstractFunctionParams {{.*}} defaultArguments(i:j:) param 0:0 [166:32 - 175:1] expanded
// CHECK-EXPANDED: {{^}} |-DefaultArgument {{.*}} [167:32 - 167:48] expanded
Expand All @@ -412,7 +412,7 @@ class LazyProperties {
// CHECK-EXPANDED-NEXT: `-AbstractFunctionParams {{.*}} _ param 1:0 [183:39 - 183:39] expanded
// CHECK-EXPANDED-NEXT: `-AbstractFunctionParams {{.*}} _ param 1:1 [183:39 - 183:39] expanded

// CHECK-EXPANDED: -AbstractFunctionDecl {{.*}} localPatternsWithSharedType() [186:1 - [[EOF]]] expanded
// CHECK-EXPANDED: -AbstractFunctionDecl {{.*}} localPatternsWithSharedType() [186:1 - 188:1] expanded
// CHECK-EXPANDED: `-BraceStmt {{.*}} [186:36 - 188:1] expanded
// CHECK-EXPANDED-NEXT: `-PatternBinding {{.*}} entry 0 [187:7 - 188:1] expanded
// CHECK-EXPANDED-NEXT: `-AfterPatternBinding {{.*}} entry 0 [187:7 - 188:1] expanded
Expand Down Expand Up @@ -478,19 +478,19 @@ class LazyProperties {
// CHECK-SEARCHES: |-AbstractFunctionDecl {{.*}} functionBodies1(a:b:) [41:1 - 100:1] expanded
// CHECK-SEARCHES: `-AbstractFunctionParams {{.*}} functionBodies1(a:b:) param 0:0 [41:25 - 100:1] expanded
// CHECK-SEARCHES: |-AbstractFunctionDecl {{.*}} throwing() [102:1 - 102:26] unexpanded
// CHECK-SEARCHES: -AbstractFunctionDecl {{.*}} defaultArguments(i:j:) [166:1 - [[EOF]]] expanded
// CHECK-SEARCHES: -AbstractFunctionDecl {{.*}} defaultArguments(i:j:) [166:1 - 175:1] expanded
// CHECK-SEARCHES: DefaultArgument {{.*}} [166:32 - 166:32] expanded
// CHECK-SEARCHES-NOT: {{ expanded}}
// CHECK-SEARCHES: `-TypeDecl {{.*}} PatternInitializers [177:1 - [[EOF]]] expanded
// CHECK-SEARCHES: |-TypeDecl {{.*}} PatternInitializers [177:1 - 180:1] expanded
// CHECK-SEARCHES: -TypeOrExtensionBody {{.*}} 'PatternInitializers' [177:28 - 180:1] expanded
// CHECK-SEARCHES: |-PatternBinding {{.*}} entry 0 [178:7 - 178:21] unexpanded
// CHECK-SEARCHES: `-PatternBinding {{.*}} entry 1 [179:7 - 179:25] expanded
// CHECK-SEARCHES: `-PatternInitializer {{.*}} entry 1 [179:16 - 179:25] expanded
// CHECK-SEARCHES-NOT: {{ expanded}}
// CHECK-SEARCHES: `-TypeDecl {{.*}} ProtoWithSubscript [182:1 - [[EOF]]] expanded
// CHECK-SEARCHES: |-TypeDecl {{.*}} ProtoWithSubscript [182:1 - 184:1] unexpanded
// CHECK-SEARCHES-NOT: {{ expanded}}
// CHECK-SEARCHES: `-AbstractFunctionDecl {{.*}} localPatternsWithSharedType() [186:1 - [[EOF]]] expanded
// CHECK-SEARCHES: `-TypeDecl {{.*}} LazyProperties [190:1 - [[EOF]]] expanded
// CHECK-SEARCHES: |-AbstractFunctionDecl {{.*}} localPatternsWithSharedType() [186:1 - 188:1] unexpanded
// CHECK-SEARCHES: `-TypeDecl {{.*}} LazyProperties [190:1 - 194:1] expanded
// CHECK-SEARCHES: -TypeOrExtensionBody {{.*}} 'LazyProperties' [190:22 - 194:1] expanded
// CHECK-SEARCHES-NEXT: |-PatternBinding {{.*}} entry 0 [191:7 - 191:20] unexpanded
// CHECK-SEARCHES-NEXT: `-PatternBinding {{.*}} entry 0 [193:12 - 193:29] expanded
Expand Down
4 changes: 2 additions & 2 deletions test/NameBinding/scope_map_top_level.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ var i: Int = b.my_identity()
// CHECK-EXPANDED-NEXT: `-AfterPatternBinding {{.*}} entry 0 [6:15 - [[EOF]]] expanded
// CHECK-EXPANDED-NEXT: `-TopLevelCode {{.*}} [8:1 - [[EOF]]] expanded
// CHECK-EXPANDED: `-ConditionalClause {{.*}} index 0 guard-continuation [9:1 - [[EOF]]] expanded
// CHECK-EXPANDED-NEXT: `-AbstractFunctionDecl {{.*}} foo() [11:1 - [[EOF]]] expanded
// CHECK-EXPANDED-NEXT: |-AbstractFunctionDecl {{.*}} foo() [11:1 - 11:13] expanded
// CHECK-EXPANDED: `-AfterPatternBinding {{.*}} entry 0 [13:9 - [[EOF]]] expanded
// CHECK-EXPANDED-NEXT: `-TypeDecl {{.*}} T [15:1 - [[EOF]]] expanded
// CHECK-EXPANDED-NEXT: |-TypeDecl {{.*}} T [15:1 - 15:15] expanded
1 change: 1 addition & 0 deletions test/Parse/type_expr.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %target-parse-verify-swift
// RUN: %target-parse-verify-swift -enable-astscope-lookup

// Types in expression contexts must be followed by a member access or
// constructor call.
Expand Down
1 change: 1 addition & 0 deletions test/SILGen/local_recursion.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %target-swift-frontend -parse-as-library -emit-silgen %s | %FileCheck %s
// RUN: %target-swift-frontend -enable-astscope-lookup -parse-as-library -emit-silgen %s | %FileCheck %s

// CHECK-LABEL: sil hidden @_TF15local_recursion15local_recursionFTSi1ySi_T_ : $@convention(thin) (Int, Int) -> () {
// CHECK: bb0([[X:%0]] : $Int, [[Y:%1]] : $Int):
Expand Down