Skip to content

Commit 556ab45

Browse files
committed
[Index] Record references to global actors in closures and function types.
1 parent 6066418 commit 556ab45

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

lib/IDE/SourceEntityWalker.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class SemaAnnotator : public ASTWalker {
8989

9090
bool handleImports(ImportDecl *Import);
9191
bool handleCustomAttributes(Decl *D);
92+
bool handleCustomTypeAttribute(const CustomAttr *customAttr);
93+
bool handleClosureAttributes(ClosureExpr *E);
94+
bool handleTypeAttributes(AttributedTypeRepr *T);
9295
bool passModulePathElements(ImportPath::Module Path,
9396
const clang::Module *ClangMod);
9497

@@ -603,6 +606,10 @@ ASTWalker::PreWalkResult<Expr *> SemaAnnotator::walkToExprPre(Expr *E) {
603606
return Action::Stop();
604607
}
605608
}
609+
} else if (auto CE = dyn_cast<ClosureExpr>(E)) {
610+
if (!handleClosureAttributes(CE))
611+
return Action::Stop();
612+
return Action::Continue(E);
606613
}
607614

608615
return Action::Continue(E);
@@ -655,6 +662,9 @@ ASTWalker::PreWalkAction SemaAnnotator::walkToTypeReprPre(TypeRepr *T) {
655662
ST->getSourceRange(), Data);
656663
return Action::StopIf(!Continue);
657664
}
665+
} else if (auto AT = dyn_cast<AttributedTypeRepr>(T)) {
666+
auto Continue = handleTypeAttributes(AT);
667+
return Action::StopIf(!Continue);
658668
}
659669

660670
return Action::Continue();
@@ -762,6 +772,39 @@ bool SemaAnnotator::handleCustomAttributes(Decl *D) {
762772
return true;
763773
}
764774

775+
bool SemaAnnotator::handleCustomTypeAttribute(const CustomAttr *customAttr) {
776+
if (auto *Repr = customAttr->getTypeRepr())
777+
if (!Repr->walk(*this))
778+
return false;
779+
780+
if (auto *Args = customAttr->getArgs())
781+
if (!Args->walk(*this))
782+
return false;
783+
784+
return true;
785+
}
786+
787+
bool SemaAnnotator::handleClosureAttributes(ClosureExpr *E) {
788+
for (auto *customAttr : E->getAttrs().getAttributes<CustomAttr, true>())
789+
if (!handleCustomTypeAttribute(customAttr))
790+
return false;
791+
792+
return true;
793+
}
794+
795+
bool SemaAnnotator::handleTypeAttributes(AttributedTypeRepr *T) {
796+
for (auto attr : T->getAttrs()) {
797+
if (!attr.is<CustomAttr *>())
798+
continue;
799+
800+
CustomAttr *customAttr = attr.get<CustomAttr *>();
801+
if (!handleCustomTypeAttribute(customAttr))
802+
return false;
803+
}
804+
805+
return true;
806+
}
807+
765808
bool SemaAnnotator::handleImports(ImportDecl *Import) {
766809
auto Mod = Import->getModule();
767810
if (!Mod)

test/Index/index_global_actors.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %target-swift-ide-test -print-indexed-symbols -include-locals -source-filename %s | %FileCheck %s
2+
// REQUIRES: concurrency
3+
4+
@globalActor
5+
actor CustomActor {
6+
static let shared = CustomActor()
7+
}
8+
9+
// Closure attributes
10+
11+
func f() {
12+
_ = { @MainActor in }
13+
// CHECK: [[@LINE-1]]:10 | class/Swift | MainActor | s:ScM | Ref,RelCont | rel: 1
14+
// CHECK-NEXT: RelCont | function/Swift | f() | s:14swift_ide_test1fyyF
15+
_ = { @CustomActor in }
16+
// CHECK: [[@LINE-1]]:10 | class/Swift | CustomActor | s:14swift_ide_test11CustomActorC | Ref,RelCont | rel: 1
17+
// CHECK-NEXT: RelCont | function/Swift | f() | s:14swift_ide_test1fyyF
18+
}
19+
20+
// Function type attributes
21+
22+
typealias MAIsolated = @MainActor (Int) -> ()
23+
// CHECK: [[@LINE-1]]:25 | class/Swift | MainActor | s:ScM | Ref | rel: 0
24+
// CHECK: [[@LINE-2]]:36 | struct/Swift | Int | s:Si | Ref | rel: 0
25+
typealias CAIsolated = @CustomActor () -> Int
26+
// CHECK: [[@LINE-1]]:25 | class/Swift | CustomActor | s:14swift_ide_test11CustomActorC | Ref | rel: 0
27+
// CHECK: [[@LINE-2]]:43 | struct/Swift | Int | s:Si | Ref | rel: 0
28+
29+
// Declaration attributes
30+
31+
@CustomActor
32+
// CHECK: [[@LINE-1]]:2 | class/Swift | CustomActor | s:14swift_ide_test11CustomActorC | Ref | rel: 0
33+
class CustomIsolated {
34+
@CustomActor func customIsolated() {}
35+
// CHECK: [[@LINE-1]]:4 | class/Swift | CustomActor | s:14swift_ide_test11CustomActorC | Ref,RelCont | rel: 1
36+
// CHECK-NEXT: RelCont | instance-method/Swift | customIsolated() | s:14swift_ide_test14CustomIsolatedC06customE0yyF
37+
}

0 commit comments

Comments
 (0)