Skip to content

Commit 5698b6c

Browse files
authored
Merge pull request #5289 from DougGregor/scope-map-fixes
Scope map fixes
2 parents 0443a4d + 861d5d3 commit 5698b6c

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

lib/AST/ASTScope.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,9 +1420,14 @@ SourceRange ASTScope::getSourceRangeImpl() const {
14201420
return typeDecl->getSourceRange();
14211421

14221422
case ASTScopeKind::ExtensionGenericParams: {
1423-
// The generic parameters of an extension are available from the trailing
1423+
// The generic parameters of an extension are available from the ':' of
1424+
// the inheritance clause (if available), or else that from the
14241425
// 'where' (if present) or from the start of the body.
1426+
// FIXME: Approximating the ':' with the start of the first inherited entry.
14251427
SourceLoc startLoc;
1428+
if (!extension->getInherited().empty() &&
1429+
extension->getInherited().front().getSourceRange().Start.isValid())
1430+
startLoc = extension->getInherited().front().getSourceRange().Start;
14261431
if (auto trailingWhere = extension->getTrailingWhereClause())
14271432
startLoc = trailingWhere->getWhereLoc();
14281433
else
@@ -1807,7 +1812,13 @@ SmallVector<ValueDecl *, 4> ASTScope::getLocalBindings() const {
18071812
// No local declarations.
18081813
break;
18091814

1810-
case ASTScopeKind::ExtensionGenericParams:
1815+
case ASTScopeKind::ExtensionGenericParams: {
1816+
// If the source range containing the extension parameters is empty,
1817+
// do nothing.
1818+
SourceRange range = getSourceRangeImpl();
1819+
if (range.Start == range.End)
1820+
break;
1821+
18111822
// Bind this extension, if we haven't done so already.
18121823
if (!extension->getExtendedType())
18131824
if (auto resolver = extension->getASTContext().getLazyResolver())
@@ -1821,6 +1832,7 @@ SmallVector<ValueDecl *, 4> ASTScope::getLocalBindings() const {
18211832
result.push_back(param);
18221833
}
18231834
break;
1835+
}
18241836

18251837
case ASTScopeKind::GenericParams:
18261838
result.push_back(genericParams.params->getParams()[genericParams.index]);
@@ -1873,6 +1885,8 @@ SmallVector<ValueDecl *, 4> ASTScope::getLocalBindings() const {
18731885
break;
18741886

18751887
case ASTScopeKind::PatternInitializer:
1888+
// FIXME: This causes recursion that we cannot yet handle.
1889+
#if false
18761890
// 'self' is available within the pattern initializer of a 'lazy' variable.
18771891
if (auto singleVar = patternBinding.decl->getSingleVar()) {
18781892
if (singleVar->getAttrs().hasAttribute<LazyAttr>() &&
@@ -1890,6 +1904,7 @@ SmallVector<ValueDecl *, 4> ASTScope::getLocalBindings() const {
18901904
result.push_back(self);
18911905
}
18921906
}
1907+
#endif
18931908
break;
18941909

18951910
case ASTScopeKind::Closure:

lib/Parse/ParseStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2213,7 +2213,7 @@ ParserResult<Stmt> Parser::parseStmtForCStyle(SourceLoc ForLoc,
22132213
// If we're missing a semicolon, try to recover.
22142214
if (Tok.isNot(tok::semi)) {
22152215
// Provide a reasonable default location for the first semicolon.
2216-
Semi1Loc = Tok.getLoc();
2216+
Semi1Loc = PreviousLoc;
22172217

22182218
if (auto *BS = ConvertClosureToBraceStmt(First.getPtrOrNull(), Context)) {
22192219
// We have seen:

test/NameBinding/scope_map.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,9 @@ class LazyProperties {
459459
// CHECK-SEARCHES-NEXT: FileUnit file="{{.*}}scope_map.swift"
460460
// CHECK-SEARCHES-NEXT: ClassDecl name=LazyProperties
461461
// CHECK-SEARCHES-NEXT: Initializer PatternBinding {{.*}} #0
462-
// CHECK-SEARCHES-NEXT: Local bindings: self
462+
463+
// FIXME: Re-enable the binding below
464+
// CHECK-SEARCHES-NOT: Local bindings: self
463465

464466
// CHECK-SEARCHES-LABEL: ***Complete scope map***
465467
// CHECK-SEARCHES-NEXT: SourceFile {{.*}} '{{.*}}scope_map.swift' [1:1 - [[EOF:[0-9]+:[0-9]+]]] unexpanded

test/NameBinding/scope_map_lookup.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ extension P2 {
3737
}
3838
}
3939

40+
#if false
4041
// Lazy properties
4142
class LazyProperties {
4243
init() {
43-
lazy var localvar = 42 // expected-error {{lazy is only valid for members of a struct or class}} {{5-10=}}
44+
lazy var localvar = 42 // FIXME: should error {{lazy is only valid for members of a struct or class}} {{5-10=}}
4445
localvar += 1
4546
_ = localvar
4647
}
@@ -57,6 +58,7 @@ class LazyProperties {
5758

5859
lazy var prop5: Int = { self.value + 1 }()
5960
}
61+
#endif
6062

6163
// Protocol extensions.
6264
// Extending via a superclass constraint.
@@ -110,6 +112,10 @@ protocol Fooable {
110112
var foo: Foo { get }
111113
}
112114

115+
// The extension below once caused infinite recursion.
116+
struct S<T> // expected-error{{expected '{' in struct}}
117+
extension S // expected-error{{expected '{' in extension}}
118+
113119
let a = b ; let b = a // expected-error{{could not infer type for 'a'}}
114120
// expected-error@-1 {{'a' used within its own type}}
115121
// FIXME: That second error is bogus.

test/Parse/toplevel_library_invalid.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 -parse-as-library
2+
// RUN: %target-parse-verify-swift -parse-as-library -enable-astscope-lookup
23

34
let x = 42 // expected-note{{did you mean 'x'?}}
45
x + x; // expected-error {{expressions are not allowed at the top level}} expected-warning {{result of operator '+' is unused}}

0 commit comments

Comments
 (0)