Skip to content

Commit b6377ff

Browse files
authored
Merge pull request #60765 from ahoppen/pr/rdar98623438
[SourceKit] Workaround a bug that parameterized protocols without 'any' cannot be mangled
2 parents 315ee2a + c8a7e55 commit b6377ff

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

lib/IDE/CodeCompletionResultType.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ const USRBasedType *USRBasedType::fromType(Type Ty, USRBasedTypeArena &Arena) {
195195
return USRBasedType::null(Arena);
196196
}
197197

198+
// ParameterizedProtocolType should always be wrapped in ExistentialType and
199+
// cannot be mangled on its own.
200+
// But ParameterizedProtocolType can currently occur in 'typealias'
201+
// declarations. rdar://99176683
202+
// To avoid crashing in USR generation, simply return a null type until the
203+
// underlying issue has been fixed.
204+
if (Ty->is<ParameterizedProtocolType>()) {
205+
return USRBasedType::null(Arena);
206+
}
207+
198208
SmallString<32> USR;
199209
llvm::raw_svector_ostream OS(USR);
200210
printTypeUSR(Ty, OS);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %empty-directory(%t/split)
2+
// RUN: %empty-directory(%t/build)
3+
// RUN: %{python} %utils/split_file.py -o %t/split %s
4+
5+
// RUN: %target-swift-frontend -emit-module -o %t/build %t/split/pck.swift
6+
7+
// RUN: %target-swift-ide-test -code-completion -source-filename %t/split/test.swift -I %t/build -code-completion-token=COMPLETE | %FileCheck %s
8+
9+
// BEGIN pck.swift
10+
11+
public protocol Foo<Bar> {
12+
associatedtype Bar
13+
}
14+
15+
public typealias Problem = Foo<String>
16+
17+
public protocol EmptyProto {}
18+
public typealias ConstrainedBar<T: EmptyProto> = Foo<T>
19+
20+
// BEGIN test.swift
21+
22+
import pck
23+
24+
#^COMPLETE^#
25+
26+
// CHECK: Begin completions
27+
// CHECK-DAG: Decl[Protocol]/OtherModule[pck]/Flair[RareType]: Foo[#Foo#];
28+
// CHECK-DAG: Decl[TypeAlias]/OtherModule[pck]: Problem[#Foo<String>#];
29+
// CHECK-DAG: Decl[TypeAlias]/OtherModule[pck]: ConstrainedBar[#Foo<T>#];
30+
// CHECK: End completions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Testing that these requests don't crash
2+
3+
public protocol Foo<Bar> {
4+
associatedtype Bar
5+
}
6+
// RUN: %sourcekitd-test -req=cursor -pos=%(line+1):11 %s -- %s
7+
typealias Problem = Foo<String>
8+
9+
protocol EmptyProto {}
10+
// RUN: %sourcekitd-test -req=cursor -pos=%(line+1):11 %s -- %s
11+
typealias ConstrainedBar<T: EmptyProto> = Foo<T>
12+
// RUN: %sourcekitd-test -req=cursor -pos=%(line+1):11 %s -- %s
13+
typealias ConstrainedBarMetatype<T: P> = Foo<T>.Type

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,22 @@ fillSymbolInfo(CursorSymbolInfo &Symbol, const DeclInfo &DInfo,
948948
}
949949
Symbol.TypeName = copyAndClearString(Allocator, Buffer);
950950

951+
// ParameterizedProtocolType should always be wrapped in ExistentialType and
952+
// cannot be mangled on its own.
953+
// But ParameterizedProtocolType can currently occur in 'typealias'
954+
// declarations. rdar://99176683
955+
// To avoid crashing in USR generation, return an error for now.
956+
if (auto Ty = DInfo.VD->getInterfaceType()) {
957+
while (auto MetaTy = Ty->getAs<MetatypeType>()) {
958+
Ty = MetaTy->getInstanceType();
959+
}
960+
if (Ty && Ty->getCanonicalType()->is<ParameterizedProtocolType>()) {
961+
return llvm::createStringError(
962+
llvm::inconvertibleErrorCode(),
963+
"Cannot mangle USR for ParameterizedProtocolType without 'any'.");
964+
}
965+
}
966+
951967
SwiftLangSupport::printDeclTypeUSR(DInfo.VD, OS);
952968
Symbol.TypeUSR = copyAndClearString(Allocator, Buffer);
953969

0 commit comments

Comments
 (0)