Skip to content

Commit d2f7d51

Browse files
committed
[Refactoring] Always walk parent decl context of value decl for local rename
Previously, we only walked the parent scope of the decl context that declared the value to rename if it was a TopLevelCodeDecl. But also functions can have locally declared types that can be used in sibling scopes. Thus, we always need to walk the parent DeclContext of the one declaring the value we are renaming. rdar://89836229
1 parent 2f0ae44 commit d2f7d51

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -874,10 +874,23 @@ static void analyzeRenameScope(ValueDecl *VD, Optional<RenameRefInfo> RefInfo,
874874
}
875875

876876
auto *Scope = VD->getDeclContext();
877-
// If the context is a top-level code decl, there may be other sibling
878-
// decls that the renamed symbol is visible from
879-
if (isa<TopLevelCodeDecl>(Scope))
877+
// There may be sibling decls that the renamed symbol is visible from.
878+
switch (Scope->getContextKind()) {
879+
case DeclContextKind::GenericTypeDecl:
880+
case DeclContextKind::ExtensionDecl:
881+
case DeclContextKind::TopLevelCodeDecl:
882+
case DeclContextKind::SubscriptDecl:
883+
case DeclContextKind::EnumElementDecl:
884+
case DeclContextKind::AbstractFunctionDecl:
880885
Scope = Scope->getParent();
886+
break;
887+
case DeclContextKind::AbstractClosureExpr:
888+
case DeclContextKind::Initializer:
889+
case DeclContextKind::SerializedLocal:
890+
case DeclContextKind::Module:
891+
case DeclContextKind::FileUnit:
892+
break;
893+
}
881894

882895
Scopes.push_back(Scope);
883896
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func boop() {
2+
enum LocalEnum {
3+
case east
4+
case south
5+
}
6+
7+
print(LocalEnum.east)
8+
}
9+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func boop() {
2+
enum LocalEnum {
3+
case north
4+
case south
5+
}
6+
7+
print(LocalEnum.north)
8+
}
9+
10+
// RUN: %empty-directory(%t.result)
11+
// RUN: %refactor -rename -source-filename %s -pos=3:10 -new-name east > %t.result/north_def.swift
12+
// RUN: diff -u %S/Outputs/localEnum/north.swift.expected %t.result/north_def.swift
13+
// RUN: %refactor -rename -source-filename %s -pos=7:19 -new-name east > %t.result/north_ref.swift
14+
// RUN: diff -u %S/Outputs/localEnum/north.swift.expected %t.result/north_ref.swift

0 commit comments

Comments
 (0)