Skip to content

Commit ae9c290

Browse files
committed
[Scope map] Local types and functions are visible in the enclosing braces.
As in non-local scopes, local types and functions are generally visible anywhere within enclosing braces, and it is up to capture analysis to determine whether these entities attempt to capture an entity that isn't guaranteed to be available.
1 parent dd7ae44 commit ae9c290

File tree

6 files changed

+35
-37
lines changed

6 files changed

+35
-37
lines changed

lib/AST/ASTScope.cpp

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,8 @@ bool ASTScope::canStealContinuation() const {
12471247
case ASTScopeKind::ForStmt:
12481248
case ASTScopeKind::ForStmtInitializer:
12491249
case ASTScopeKind::Closure:
1250+
case ASTScopeKind::TypeDecl:
1251+
case ASTScopeKind::AbstractFunctionDecl:
12501252
// These node kinds don't introduce names that would be visible in a
12511253
// continuation.
12521254
return false;
@@ -1261,8 +1263,6 @@ bool ASTScope::canStealContinuation() const {
12611263
return getParent()->getKind() == ASTScopeKind::TopLevelCode;
12621264

12631265
case ASTScopeKind::AfterPatternBinding:
1264-
case ASTScopeKind::TypeDecl:
1265-
case ASTScopeKind::AbstractFunctionDecl:
12661266
case ASTScopeKind::PatternBinding:
12671267
// Declarations always steal continuations.
12681268
return true;
@@ -1789,11 +1789,12 @@ SmallVector<ValueDecl *, 4> ASTScope::getLocalBindings() const {
17891789
switch (getKind()) {
17901790
case ASTScopeKind::Preexpanded:
17911791
case ASTScopeKind::SourceFile:
1792+
case ASTScopeKind::AbstractFunctionDecl:
1793+
case ASTScopeKind::TypeDecl:
17921794
case ASTScopeKind::TypeOrExtensionBody:
17931795
case ASTScopeKind::DefaultArgument:
17941796
case ASTScopeKind::AbstractFunctionBody:
17951797
case ASTScopeKind::PatternBinding:
1796-
case ASTScopeKind::BraceStmt:
17971798
case ASTScopeKind::IfStmt:
17981799
case ASTScopeKind::GuardStmt:
17991800
case ASTScopeKind::RepeatWhileStmt:
@@ -1806,11 +1807,6 @@ SmallVector<ValueDecl *, 4> ASTScope::getLocalBindings() const {
18061807
// No local declarations.
18071808
break;
18081809

1809-
case ASTScopeKind::TypeDecl:
1810-
if (getHistoricalContinuation())
1811-
result.push_back(typeDecl);
1812-
break;
1813-
18141810
case ASTScopeKind::ExtensionGenericParams:
18151811
// Bind this extension, if we haven't done so already.
18161812
if (!extension->getExtendedType())
@@ -1840,22 +1836,22 @@ SmallVector<ValueDecl *, 4> ASTScope::getLocalBindings() const {
18401836
handlePattern(patternBinding.decl->getPattern(patternBinding.entry));
18411837
break;
18421838

1843-
case ASTScopeKind::AbstractFunctionDecl:
1844-
// FIXME: This allows overloading to work in top-level code, but is probably
1845-
// incorrect. We should likely use
1846-
//
1847-
// if (getHistoricalContext())
1848-
//
1849-
// and name lookup should apply the normal name-hiding rules.
1850-
if (abstractFunction->getDeclContext()->isLocalContext())
1851-
result.push_back(abstractFunction);
1852-
break;
1853-
18541839
case ASTScopeKind::ConditionalClause:
18551840
handlePattern(conditionalClause.stmt->getCond()[conditionalClause.index]
18561841
.getPatternOrNull());
18571842
break;
18581843

1844+
case ASTScopeKind::BraceStmt:
1845+
// All types and functions are visible anywhere within their brace
1846+
// statements. It's up to capture analysis to determine what is usable.
1847+
for (auto element : braceStmt.stmt->getElements()) {
1848+
if (auto decl = element.dyn_cast<Decl *>()) {
1849+
if (isa<AbstractFunctionDecl>(decl) || isa<TypeDecl>(decl))
1850+
result.push_back(cast<ValueDecl>(decl));
1851+
}
1852+
}
1853+
break;
1854+
18591855
case ASTScopeKind::ForEachPattern:
18601856
handlePattern(forEach->getPattern());
18611857
break;

lib/AST/NameLookup.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,12 @@ static DeclVisibilityKind getLocalDeclVisibilityKind(const ASTScope *scope) {
402402
switch (scope->getKind()) {
403403
case ASTScopeKind::Preexpanded:
404404
case ASTScopeKind::SourceFile:
405+
case ASTScopeKind::TypeDecl:
406+
case ASTScopeKind::AbstractFunctionDecl:
405407
case ASTScopeKind::TypeOrExtensionBody:
406408
case ASTScopeKind::AbstractFunctionBody:
407409
case ASTScopeKind::DefaultArgument:
408410
case ASTScopeKind::PatternBinding:
409-
case ASTScopeKind::BraceStmt:
410411
case ASTScopeKind::IfStmt:
411412
case ASTScopeKind::GuardStmt:
412413
case ASTScopeKind::RepeatWhileStmt:
@@ -427,11 +428,10 @@ static DeclVisibilityKind getLocalDeclVisibilityKind(const ASTScope *scope) {
427428
case ASTScopeKind::PatternInitializer: // lazy var 'self'
428429
return DeclVisibilityKind::FunctionParameter;
429430

430-
case ASTScopeKind::TypeDecl:
431-
case ASTScopeKind::AbstractFunctionDecl:
432431
case ASTScopeKind::AfterPatternBinding:
433432
case ASTScopeKind::ConditionalClause:
434433
case ASTScopeKind::ForEachPattern:
434+
case ASTScopeKind::BraceStmt:
435435
case ASTScopeKind::CatchStmt:
436436
case ASTScopeKind::CaseStmt:
437437
case ASTScopeKind::ForStmtInitializer:

test/NameBinding/scope_map.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,14 @@ class LazyProperties {
271271
// CHECK-EXPANDED-NEXT: {{^}} |-PatternInitializer {{.*}} entry 0 [54:14 - 54:14] expanded
272272
// CHECK-EXPANDED-NEXT: {{^}} `-AfterPatternBinding {{.*}} entry 0 [54:14 - 56:3] expanded
273273
// CHECK-EXPANDED: {{^}} `-AfterPatternBinding {{.*}} entry 0 [55:14 - 56:3] expanded
274-
// CHECK-EXPANDED-NEXT: {{^}} `-AbstractFunctionDecl {{.*}} f(_:) [57:3 - 100:1] expanded
275-
// CHECK-EXPANDED-NEXT: {{^}} |-AbstractFunctionParams {{.*}} f(_:) param 0:0 [57:15 - 57:38] expanded
274+
// CHECK-EXPANDED-NEXT: {{^}} |-AbstractFunctionDecl {{.*}} f(_:) [57:3 - 57:38] expanded
275+
// CHECK-EXPANDED-NEXT: {{^}} `-AbstractFunctionParams {{.*}} f(_:) param 0:0 [57:15 - 57:38] expanded
276276
// CHECK-EXPANDED-NEXT: `-AbstractFunctionBody {{.*}} f(_:) [57:27 - 57:38] expanded
277277
// CHECK-EXPANDED-NEXT: {{^}} `-BraceStmt {{.*}} [57:27 - 57:38] expanded
278278
// CHECK-EXPANDED: {{^}} `-AfterPatternBinding {{.*}} entry 0 [58:16 - 100:1] expanded
279-
// CHECK-EXPANDED-NEXT: {{^}} `-TypeDecl {{.*}} S7 [59:3 - 100:1] expanded
280-
// CHECK-EXPANDED-NEXT: {{^}} |-TypeOrExtensionBody {{.*}} 'S7' [59:13 - 59:15] expanded
281-
// CHECK-EXPANDED-NEXT: {{^}} `-TypeDecl {{.*}} S7Alias [60:3 - 100:1] expanded
279+
// CHECK-EXPANDED-NEXT: {{^}} |-TypeDecl {{.*}} S7 [59:3 - 59:15] expanded
280+
// CHECK-EXPANDED-NEXT: {{^}} `-TypeOrExtensionBody {{.*}} 'S7' [59:13 - 59:15] expanded
281+
// CHECK-EXPANDED-NEXT: {{^}} |-TypeDecl {{.*}} S7Alias [60:3 - 60:23] expanded
282282
// CHECK-EXPANDED-NEXT: {{^}} |-IfStmt {{.*}} [62:3 - 66:3] expanded
283283
// CHECK-EXPANDED-NEXT: {{^}} |-ConditionalClause {{.*}} index 0 [62:18 - 64:3] expanded
284284
// CHECK-EXPANDED-NEXT: {{^}} `-ConditionalClause {{.*}} index 1 [62:29 - 64:3] expanded
@@ -376,7 +376,7 @@ class LazyProperties {
376376
// CHECK-EXPANDED: {{^}} `-BraceStmt {{.*}} [146:9 - 148:5] expanded
377377
// CHECK-EXPANDED: {{^}} `-AfterPatternBinding {{.*}} entry 1 [149:36 - 155:1] expanded
378378
// CHECK-EXPANDED: {{^}} `-AfterPatternBinding {{.*}} entry 2 [150:21 - 155:1] expanded
379-
// CHECK-EXPANDED-NEXT: {{^}} `-AbstractFunctionDecl {{.*}} _ [150:25 - 155:1] expanded
379+
// CHECK-EXPANDED-NEXT: {{^}} |-AbstractFunctionDecl {{.*}} _ [150:25 - 152:3] expanded
380380
// CHECK-EXPANDED: {{^}} `-BraceStmt {{.*}} [150:25 - 152:3] expanded
381381
// CHECK-EXPANDED-NEXT: {{^}} `-BraceStmt {{.*}} [154:6 - 154:8] expanded
382382

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

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

415-
// CHECK-EXPANDED: -AbstractFunctionDecl {{.*}} localPatternsWithSharedType() [186:1 - [[EOF]]] expanded
415+
// CHECK-EXPANDED: -AbstractFunctionDecl {{.*}} localPatternsWithSharedType() [186:1 - 188:1] expanded
416416
// CHECK-EXPANDED: `-BraceStmt {{.*}} [186:36 - 188:1] expanded
417417
// CHECK-EXPANDED-NEXT: `-PatternBinding {{.*}} entry 0 [187:7 - 188:1] expanded
418418
// CHECK-EXPANDED-NEXT: `-AfterPatternBinding {{.*}} entry 0 [187:7 - 188:1] expanded
@@ -478,19 +478,19 @@ class LazyProperties {
478478
// CHECK-SEARCHES: |-AbstractFunctionDecl {{.*}} functionBodies1(a:b:) [41:1 - 100:1] expanded
479479
// CHECK-SEARCHES: `-AbstractFunctionParams {{.*}} functionBodies1(a:b:) param 0:0 [41:25 - 100:1] expanded
480480
// CHECK-SEARCHES: |-AbstractFunctionDecl {{.*}} throwing() [102:1 - 102:26] unexpanded
481-
// CHECK-SEARCHES: -AbstractFunctionDecl {{.*}} defaultArguments(i:j:) [166:1 - [[EOF]]] expanded
481+
// CHECK-SEARCHES: -AbstractFunctionDecl {{.*}} defaultArguments(i:j:) [166:1 - 175:1] expanded
482482
// CHECK-SEARCHES: DefaultArgument {{.*}} [166:32 - 166:32] expanded
483483
// CHECK-SEARCHES-NOT: {{ expanded}}
484-
// CHECK-SEARCHES: `-TypeDecl {{.*}} PatternInitializers [177:1 - [[EOF]]] expanded
484+
// CHECK-SEARCHES: |-TypeDecl {{.*}} PatternInitializers [177:1 - 180:1] expanded
485485
// CHECK-SEARCHES: -TypeOrExtensionBody {{.*}} 'PatternInitializers' [177:28 - 180:1] expanded
486486
// CHECK-SEARCHES: |-PatternBinding {{.*}} entry 0 [178:7 - 178:21] unexpanded
487487
// CHECK-SEARCHES: `-PatternBinding {{.*}} entry 1 [179:7 - 179:25] expanded
488488
// CHECK-SEARCHES: `-PatternInitializer {{.*}} entry 1 [179:16 - 179:25] expanded
489489
// CHECK-SEARCHES-NOT: {{ expanded}}
490-
// CHECK-SEARCHES: `-TypeDecl {{.*}} ProtoWithSubscript [182:1 - [[EOF]]] expanded
490+
// CHECK-SEARCHES: |-TypeDecl {{.*}} ProtoWithSubscript [182:1 - 184:1] unexpanded
491491
// CHECK-SEARCHES-NOT: {{ expanded}}
492-
// CHECK-SEARCHES: `-AbstractFunctionDecl {{.*}} localPatternsWithSharedType() [186:1 - [[EOF]]] expanded
493-
// CHECK-SEARCHES: `-TypeDecl {{.*}} LazyProperties [190:1 - [[EOF]]] expanded
492+
// CHECK-SEARCHES: |-AbstractFunctionDecl {{.*}} localPatternsWithSharedType() [186:1 - 188:1] unexpanded
493+
// CHECK-SEARCHES: `-TypeDecl {{.*}} LazyProperties [190:1 - 194:1] expanded
494494
// CHECK-SEARCHES: -TypeOrExtensionBody {{.*}} 'LazyProperties' [190:22 - 194:1] expanded
495495
// CHECK-SEARCHES-NEXT: |-PatternBinding {{.*}} entry 0 [191:7 - 191:20] unexpanded
496496
// CHECK-SEARCHES-NEXT: `-PatternBinding {{.*}} entry 0 [193:12 - 193:29] expanded

test/NameBinding/scope_map_top_level.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ var i: Int = b.my_identity()
3232
// CHECK-EXPANDED-NEXT: `-AfterPatternBinding {{.*}} entry 0 [6:15 - [[EOF]]] expanded
3333
// CHECK-EXPANDED-NEXT: `-TopLevelCode {{.*}} [8:1 - [[EOF]]] expanded
3434
// CHECK-EXPANDED: `-ConditionalClause {{.*}} index 0 guard-continuation [9:1 - [[EOF]]] expanded
35-
// CHECK-EXPANDED-NEXT: `-AbstractFunctionDecl {{.*}} foo() [11:1 - [[EOF]]] expanded
35+
// CHECK-EXPANDED-NEXT: |-AbstractFunctionDecl {{.*}} foo() [11:1 - 11:13] expanded
3636
// CHECK-EXPANDED: `-AfterPatternBinding {{.*}} entry 0 [13:9 - [[EOF]]] expanded
37-
// CHECK-EXPANDED-NEXT: `-TypeDecl {{.*}} T [15:1 - [[EOF]]] expanded
37+
// CHECK-EXPANDED-NEXT: |-TypeDecl {{.*}} T [15:1 - 15:15] expanded

test/Parse/type_expr.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %target-parse-verify-swift
2+
// RUN: %target-parse-verify-swift -enable-astscope-lookup
23

34
// Types in expression contexts must be followed by a member access or
45
// constructor call.

test/SILGen/local_recursion.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %target-swift-frontend -parse-as-library -emit-silgen %s | %FileCheck %s
2+
// RUN: %target-swift-frontend -enable-astscope-lookup -parse-as-library -emit-silgen %s | %FileCheck %s
23

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

0 commit comments

Comments
 (0)