Skip to content

Commit 76802dd

Browse files
committed
ASTScope: FunctionBodyScope should not be nested inside ParameterListScope
1 parent 7b967a8 commit 76802dd

File tree

4 files changed

+16
-82
lines changed

4 files changed

+16
-82
lines changed

include/swift/AST/ASTScope.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,6 @@ class ASTScopeImpl {
298298
bool verifyThatChildrenAreContainedWithin(SourceRange) const;
299299
bool verifyThatThisNodeComeAfterItsPriorSibling() const;
300300

301-
virtual SourceRange
302-
getSourceRangeOfEnclosedParamsOfASTNode(bool omitAssertions) const;
303-
304301
private:
305302
bool checkSourceRangeAfterExpansion(const ASTContext &) const;
306303

@@ -991,13 +988,6 @@ class AbstractFunctionDeclScope final : public ASTScopeImpl {
991988

992989
NullablePtr<const void> getReferrent() const override;
993990

994-
protected:
995-
SourceRange
996-
getSourceRangeOfEnclosedParamsOfASTNode(bool omitAssertions) const override;
997-
998-
private:
999-
static SourceLoc getParmsSourceLocOfAFD(AbstractFunctionDecl *);
1000-
1001991
protected:
1002992
NullablePtr<const GenericParamList> genericParams() const override;
1003993
};
@@ -1507,9 +1497,6 @@ class SubscriptDeclScope final : public ASTScopeImpl {
15071497
NullablePtr<const void> getReferrent() const override;
15081498

15091499
protected:
1510-
SourceRange
1511-
getSourceRangeOfEnclosedParamsOfASTNode(bool omitAssertions) const override;
1512-
15131500
NullablePtr<const GenericParamList> genericParams() const override;
15141501
NullablePtr<AbstractStorageDecl>
15151502
getEnclosingAbstractStorageDecl() const override {
@@ -1566,10 +1553,6 @@ class EnumElementScope : public ASTScopeImpl {
15661553
NullablePtr<Decl> getDeclIfAny() const override { return decl; }
15671554
Decl *getDecl() const { return decl; }
15681555

1569-
protected:
1570-
SourceRange
1571-
getSourceRangeOfEnclosedParamsOfASTNode(bool omitAssertions) const override;
1572-
15731556
private:
15741557
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
15751558
};

lib/AST/ASTScopeCreation.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,24 +1298,23 @@ void AbstractFunctionDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
12981298
.ifUniqueConstructExpandAndInsert<DifferentiableAttributeScope>(
12991299
this, diffAttr, decl);
13001300
});
1301+
13011302
// Create scopes for generic and ordinary parameters.
13021303
// For a subscript declaration, the generic and ordinary parameters are in an
13031304
// ancestor scope, so don't make them here.
13041305
ASTScopeImpl *leaf = this;
1306+
13051307
if (!isa<AccessorDecl>(decl)) {
13061308
leaf = scopeCreator.addNestedGenericParamScopesToTree(
13071309
decl, decl->getGenericParams(), leaf);
1308-
if (isLocalizable(decl) && getParmsSourceLocOfAFD(decl).isValid()) {
1309-
// swift::createDesignatedInitOverride just clones the parameters, so they
1310-
// end up with a bogus SourceRange, maybe *before* the start of the
1311-
// function.
1312-
if (!decl->isImplicit()) {
1313-
leaf = scopeCreator
1314-
.constructExpandAndInsertUncheckable<ParameterListScope>(
1315-
leaf, decl->getParameters(), nullptr);
1316-
}
1310+
1311+
auto *params = decl->getParameters();
1312+
if (params->size() > 0) {
1313+
scopeCreator.constructExpandAndInsertUncheckable<ParameterListScope>(
1314+
leaf, params, nullptr);
13171315
}
13181316
}
1317+
13191318
// Create scope for the body.
13201319
// We create body scopes when there is no body for source kit to complete
13211320
// erroneous code in bodies.

lib/AST/ASTScopeSourceRange.cpp

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,7 @@ SourceRange AbstractFunctionDeclScope::getSourceRangeOfThisASTNode(
362362

363363
SourceRange ParameterListScope::getSourceRangeOfThisASTNode(
364364
const bool omitAssertions) const {
365-
const auto rangeForGoodInput =
366-
getSourceRangeOfEnclosedParamsOfASTNode(omitAssertions);
365+
auto rangeForGoodInput = params->getSourceRange();
367366
auto r = SourceRange(rangeForGoodInput.Start,
368367
fixupEndForBadInput(rangeForGoodInput));
369368
ASTScopeAssert(getSourceManager().rangeContains(
@@ -672,52 +671,6 @@ void ASTScopeImpl::widenSourceRangeForIgnoredASTNode(const ASTNode n) {
672671
sourceRangeOfIgnoredASTNodes.widen(r);
673672
}
674673

675-
#pragma mark getSourceRangeOfEnclosedParamsOfASTNode
676-
677-
SourceRange ASTScopeImpl::getSourceRangeOfEnclosedParamsOfASTNode(
678-
const bool omitAssertions) const {
679-
return getParent().get()->getSourceRangeOfEnclosedParamsOfASTNode(
680-
omitAssertions);
681-
}
682-
683-
SourceRange EnumElementScope::getSourceRangeOfEnclosedParamsOfASTNode(
684-
bool omitAssertions) const {
685-
auto *pl = decl->getParameterList();
686-
return pl ? pl->getSourceRange() : SourceRange();
687-
}
688-
689-
SourceRange SubscriptDeclScope::getSourceRangeOfEnclosedParamsOfASTNode(
690-
const bool omitAssertions) const {
691-
auto r = SourceRange(decl->getIndices()->getLParenLoc(), decl->getEndLoc());
692-
// Because of "subscript(x: MyStruct#^PARAM_1^#) -> Int { return 0 }"
693-
// Cannot just use decl->getEndLoc()
694-
r.widen(decl->getIndices()->getRParenLoc());
695-
return r;
696-
}
697-
698-
SourceRange AbstractFunctionDeclScope::getSourceRangeOfEnclosedParamsOfASTNode(
699-
const bool omitAssertions) const {
700-
const auto s = getParmsSourceLocOfAFD(decl);
701-
const auto e = getSourceRangeOfThisASTNode(omitAssertions).End;
702-
return s.isInvalid() || e.isInvalid() ? SourceRange() : SourceRange(s, e);
703-
}
704-
705-
SourceLoc
706-
AbstractFunctionDeclScope::getParmsSourceLocOfAFD(AbstractFunctionDecl *decl) {
707-
if (auto *c = dyn_cast<ConstructorDecl>(decl))
708-
return c->getParameters()->getLParenLoc();
709-
710-
if (auto *dd = dyn_cast<DestructorDecl>(decl))
711-
return dd->getNameLoc();
712-
713-
auto *fd = cast<FuncDecl>(decl);
714-
// clang-format off
715-
return isa<AccessorDecl>(fd) ? fd->getLoc()
716-
: fd->isDeferBody() ? fd->getNameLoc()
717-
: fd->getParameters()->getLParenLoc();
718-
// clang-format on
719-
}
720-
721674
SourceLoc getLocAfterExtendedNominal(const ExtensionDecl *const ext) {
722675
const auto *const etr = ext->getExtendedTypeRepr();
723676
if (!etr)

test/NameLookup/scope_map_top_level.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let a: Int? = 1
88
guard let b = a else {
99
}
1010

11-
func foo() {} // to interrupt the TopLevelCodeDecl
11+
func foo(x: Int) {} // to interrupt the TopLevelCodeDecl
1212

1313
let c = b
1414

@@ -40,10 +40,10 @@ var i: Int = b.my_identity()
4040
// CHECK-EXPANDED-NEXT: `-ConditionalClausePatternUseScope, [8:22 - 8:22] let b{{\??}}
4141
// CHECK-EXPANDED-NEXT: |-BraceStmtScope {{.*}}, [8:22 - 9:1]
4242
// CHECK-EXPANDED-NEXT: `-LookupParentDiversionScope, [9:1 - 21:28]
43-
// CHECK-EXPANDED-NEXT: |-AbstractFunctionDeclScope {{.*}}, [11:1 - 11:13] 'foo()'
44-
// CHECK-EXPANDED-NEXT: `-ParameterListScope {{.*}}, [11:9 - 11:13]
45-
// CHECK-EXPANDED-NEXT: `-FunctionBodyScope {{.*}}, [11:12 - 11:13]
46-
// CHECK-EXPANDED-NEXT: `-BraceStmtScope {{.*}}, [11:12 - 11:13]
43+
// CHECK-EXPANDED-NEXT: |-AbstractFunctionDeclScope {{.*}}, [11:1 - 11:19] 'foo(x:)'
44+
// CHECK-EXPANDED-NEXT: |-ParameterListScope {{.*}}, [11:9 - 11:16]
45+
// CHECK-EXPANDED-NEXT: `-FunctionBodyScope {{.*}}, [11:18 - 11:19]
46+
// CHECK-EXPANDED-NEXT: `-BraceStmtScope {{.*}}, [11:18 - 11:19]
4747
// CHECK-EXPANDED-NEXT: `-TopLevelCodeScope {{.*}}, [13:1 - 21:28]
4848
// CHECK-EXPANDED-NEXT: `-BraceStmtScope {{.*}}, [13:1 - 21:28]
4949
// CHECK-EXPANDED-NEXT: |-PatternEntryDeclScope {{.*}}, [13:5 - 13:9] entry 0 'c'
@@ -52,9 +52,8 @@ var i: Int = b.my_identity()
5252
// CHECK-EXPANDED-NEXT: |-ExtensionDeclScope {{.*}}, [17:14 - 19:1]
5353
// CHECK-EXPANDED-NEXT: `-ExtensionBodyScope {{.*}}, [17:15 - 19:1]
5454
// CHECK-EXPANDED-NEXT: `-AbstractFunctionDeclScope {{.*}}, [18:3 - 18:43] 'my_identity()'
55-
// CHECK-EXPANDED-NEXT: `-ParameterListScope {{.*}}, [18:19 - 18:43]
56-
// CHECK-EXPANDED-NEXT: `-FunctionBodyScope {{.*}}, [18:29 - 18:43]
57-
// CHECK-EXPANDED-NEXT: `-BraceStmtScope {{.*}}, [18:29 - 18:43]
55+
// CHECK-EXPANDED-NEXT: `-FunctionBodyScope {{.*}}, [18:29 - 18:43]
56+
// CHECK-EXPANDED-NEXT: `-BraceStmtScope {{.*}}, [18:29 - 18:43]
5857
// CHECK-EXPANDED-NEXT: `-TopLevelCodeScope {{.*}}, [21:1 - 21:28]
5958
// CHECK-EXPANDED-NEXT: `-BraceStmtScope {{.*}}, [21:1 - 21:28]
6059
// CHECK-EXPANDED-NEXT: `-PatternEntryDeclScope {{.*}}, [21:5 - 21:28] entry 0 'i'

0 commit comments

Comments
 (0)