Skip to content

Commit ca4abab

Browse files
authored
Merge pull request #58746 from ahoppen/pr/enum-assoc-value-is-call
[Index] Consider construction of enums with associated values as a function call
2 parents 1843f22 + 74cd03b commit ca4abab

File tree

9 files changed

+92
-6
lines changed

9 files changed

+92
-6
lines changed

lib/IDE/Refactoring.cpp

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

871871
auto *Scope = VD->getDeclContext();
872-
// If the context is a top-level code decl, there may be other sibling
873-
// decls that the renamed symbol is visible from
874-
if (isa<TopLevelCodeDecl>(Scope))
872+
// There may be sibling decls that the renamed symbol is visible from.
873+
switch (Scope->getContextKind()) {
874+
case DeclContextKind::GenericTypeDecl:
875+
case DeclContextKind::ExtensionDecl:
876+
case DeclContextKind::TopLevelCodeDecl:
877+
case DeclContextKind::SubscriptDecl:
878+
case DeclContextKind::EnumElementDecl:
879+
case DeclContextKind::AbstractFunctionDecl:
875880
Scope = Scope->getParent();
881+
break;
882+
case DeclContextKind::AbstractClosureExpr:
883+
case DeclContextKind::Initializer:
884+
case DeclContextKind::SerializedLocal:
885+
case DeclContextKind::Module:
886+
case DeclContextKind::FileUnit:
887+
break;
888+
}
876889

877890
Scopes.push_back(Scope);
878891
}

lib/Index/Index.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,7 @@ bool IndexSwiftASTWalker::reportRef(ValueDecl *D, SourceLoc Loc,
15321532
if (!shouldIndex(D, /*IsRef=*/true))
15331533
return true; // keep walking
15341534

1535-
if (isa<AbstractFunctionDecl>(D)) {
1535+
if (isa<AbstractFunctionDecl>(D) || isa<EnumElementDecl>(D)) {
15361536
if (initFuncRefIndexSymbol(D, Loc, Info))
15371537
return true;
15381538
} else if (isa<AbstractStorageDecl>(D)) {

test/Index/local.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func foo(a: Int, b: Int, c: Int) {
3939
let _ = LocalEnum.foo(x: LocalStruct())
4040
// LOCAL: [[@LINE-1]]:13 | enum(local)/Swift | LocalEnum | [[LocalEnum_USR]] | Ref,RelCont | rel: 1
4141
// CHECK-NOT: [[@LINE-2]]:13 | enum(local)/Swift | LocalEnum | {{.*}} | Ref,RelCont | rel: 1
42-
// LOCAL: [[@LINE-3]]:23 | enumerator(local)/Swift | foo(x:) | [[LocalEnum_foo_USR]] | Ref,RelCont | rel: 1
42+
// LOCAL: [[@LINE-3]]:23 | enumerator(local)/Swift | foo(x:) | [[LocalEnum_foo_USR]] | Ref,Call,RelRec,RelCall,RelCont | rel: 2
4343
// CHECK-NOT: [[@LINE-4]]:23 | enumerator(local)/Swift | foo(x:) | {{.*}} | Ref,RelCont | rel: 1
4444
// LOCAL: [[@LINE-5]]:30 | struct(local)/Swift | LocalStruct | [[LocalStruct_USR]] | Ref,RelCont | rel: 1
4545
// CHECK-NOT: [[@LINE-6]]:30 | struct(local)/Swift | LocalStruct | {{.*}} | Ref,RelCont | rel: 1

test/SourceKit/Indexing/index_enum_case.swift.response

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@
139139
key.name: "two(a:)",
140140
key.usr: "s:15index_enum_case1EO3twoyACSS_tcACmF",
141141
key.line: 21,
142-
key.column: 13
142+
key.column: 13,
143+
key.receiver_usr: "s:15index_enum_case1EO"
143144
},
144145
{
145146
key.kind: source.lang.swift.decl.function.free,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
enum Foo {
2+
case primary(with: Int)
3+
case second
4+
}
5+
6+
func test() {
7+
let _ = Foo.primary(with: 1)
8+
let _ = Foo.primary
9+
let _ = Foo.second
10+
}
11+
12+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
enum Foo {
2+
case first(associated: Int)
3+
case secondary
4+
}
5+
6+
func test() {
7+
let _ = Foo.first(associated: 1)
8+
let _ = Foo.first
9+
let _ = Foo.secondary
10+
}
11+
12+
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+

test/refactoring/rename/enum.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
enum Foo {
2+
case first(associated: Int)
3+
case second
4+
}
5+
6+
func test() {
7+
let _ = Foo.first(associated: 1)
8+
let _ = Foo.first
9+
let _ = Foo.second
10+
}
11+
12+
// RUN: %empty-directory(%t.result)
13+
// RUN: %refactor -rename -source-filename %s -pos=2:8 -new-name 'primary(with:)' > %t.result/first_def.swift
14+
// RUN: diff -u %S/Outputs/enum/first.swift.expected %t.result/first_def.swift
15+
// RUN: %refactor -rename -source-filename %s -pos=7:15 -new-name 'primary(with:)' > %t.result/first_ref.swift
16+
// RUN: diff -u %S/Outputs/enum/first.swift.expected %t.result/first_ref.swift
17+
// RUN: %refactor -rename -source-filename %s -pos=7:21 -new-name 'primary(with:)' > %t.result/first_ref_assoc.swift
18+
// RUN: diff -u %S/Outputs/enum/first.swift.expected %t.result/first_ref_assoc.swift
19+
// RUN: %refactor -rename -source-filename %s -pos=8:15 -new-name 'primary(with:)' > %t.result/first_ref_base.swift
20+
// RUN: diff -u %S/Outputs/enum/first.swift.expected %t.result/first_ref_assoc.swift
21+
22+
// RUN: %refactor -rename -source-filename %s -pos=3:8 -new-name 'secondary' > %t.result/second_def.swift
23+
// RUN: diff -u %S/Outputs/enum/second.swift.expected %t.result/second_def.swift
24+
// RUN: %refactor -rename -source-filename %s -pos=9:15 -new-name 'secondary' > %t.result/second_ref.swift
25+
// RUN: diff -u %S/Outputs/enum/second.swift.expected %t.result/second_ref.swift
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)