Skip to content

Commit 6c959af

Browse files
committed
[ASTScope] Model "do" statements in the scope map
1 parent a6e2f8b commit 6c959af

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

include/swift/AST/ASTScope.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,23 @@ class RepeatWhileScope final : public AbstractStmtScope {
18261826
Stmt *getStmt() const override { return stmt; }
18271827
};
18281828

1829+
class DoStmtScope final : public AbstractStmtScope {
1830+
public:
1831+
DoStmt *const stmt;
1832+
DoStmtScope(DoStmt *e) : stmt(e) {}
1833+
virtual ~DoStmtScope() {}
1834+
1835+
protected:
1836+
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
1837+
1838+
private:
1839+
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
1840+
1841+
public:
1842+
std::string getClassName() const override;
1843+
Stmt *getStmt() const override { return stmt; }
1844+
};
1845+
18291846
class DoCatchStmtScope final : public AbstractStmtScope {
18301847
public:
18311848
DoCatchStmt *const stmt;

lib/AST/ASTScope.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ DEFINE_GET_CLASS_NAME(WhileStmtScope)
237237
DEFINE_GET_CLASS_NAME(GuardStmtScope)
238238
DEFINE_GET_CLASS_NAME(LookupParentDiversionScope)
239239
DEFINE_GET_CLASS_NAME(RepeatWhileScope)
240+
DEFINE_GET_CLASS_NAME(DoStmtScope)
240241
DEFINE_GET_CLASS_NAME(DoCatchStmtScope)
241242
DEFINE_GET_CLASS_NAME(SwitchStmtScope)
242243
DEFINE_GET_CLASS_NAME(ForEachStmtScope)

lib/AST/ASTScopeCreation.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ class NodeAdder
866866
VISIT_AND_CREATE(IfStmt, IfStmtScope)
867867
VISIT_AND_CREATE(WhileStmt, WhileStmtScope)
868868
VISIT_AND_CREATE(RepeatWhileStmt, RepeatWhileScope)
869+
VISIT_AND_CREATE(DoStmt, DoStmtScope)
869870
VISIT_AND_CREATE(DoCatchStmt, DoCatchStmtScope)
870871
VISIT_AND_CREATE(SwitchStmt, SwitchStmtScope)
871872
VISIT_AND_CREATE(ForEachStmt, ForEachStmtScope)
@@ -908,11 +909,6 @@ class NodeAdder
908909
ScopeCreator &scopeCreator) {
909910
return scopeCreator.ifUniqueConstructExpandAndInsert<GuardStmtScope>(p, e);
910911
}
911-
NullablePtr<ASTScopeImpl> visitDoStmt(DoStmt *ds, ASTScopeImpl *p,
912-
ScopeCreator &scopeCreator) {
913-
scopeCreator.addToScopeTreeAndReturnInsertionPoint(ds->getBody(), p);
914-
return p; // Don't put subsequent decls inside the "do"
915-
}
916912
NullablePtr<ASTScopeImpl> visitTopLevelCodeDecl(TopLevelCodeDecl *d,
917913
ASTScopeImpl *p,
918914
ScopeCreator &scopeCreator) {
@@ -1204,6 +1200,7 @@ NO_NEW_INSERTION_POINT(CaptureListScope)
12041200
NO_NEW_INSERTION_POINT(CaseStmtScope)
12051201
NO_NEW_INSERTION_POINT(ClosureBodyScope)
12061202
NO_NEW_INSERTION_POINT(DefaultArgumentInitializerScope)
1203+
NO_NEW_INSERTION_POINT(DoStmtScope)
12071204
NO_NEW_INSERTION_POINT(DoCatchStmtScope)
12081205
NO_NEW_INSERTION_POINT(ForEachPatternScope)
12091206
NO_NEW_INSERTION_POINT(ForEachStmtScope)
@@ -1470,6 +1467,11 @@ void RepeatWhileScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
14701467
scopeCreator.addToScopeTree(stmt->getCond(), this);
14711468
}
14721469

1470+
void DoStmtScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
1471+
ScopeCreator &scopeCreator) {
1472+
scopeCreator.addToScopeTree(stmt->getBody(), this);
1473+
}
1474+
14731475
void DoCatchStmtScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
14741476
ScopeCreator &scopeCreator) {
14751477
scopeCreator.addToScopeTree(stmt->getBody(), this);

test/NameLookup/scope_map-astscopelookup.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ class LazyProperties {
194194
lazy var prop: Int = self.value
195195
}
196196

197+
func HasLabeledDo() {
198+
label:
199+
do {
200+
for x in 0..<100 {
201+
if x % 3 == 0 {
202+
break label
203+
}
204+
}
205+
}
206+
}
207+
197208
// RUN: not %target-swift-frontend -dump-scope-maps expanded %s 2> %t.expanded
198209
// RUN: %FileCheck -check-prefix CHECK-EXPANDED %s < %t.expanded
199210

0 commit comments

Comments
 (0)