Skip to content

Commit 4c347d2

Browse files
authored
Merge pull request #60801 from ahoppen/ahoppen-5.7/rdar98623438
[5.7][SourceKit] Workaround a bug that parameterized protocols without 'any' cannot be mangled
2 parents 7e4a43b + 3a80fa7 commit 4c347d2

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
@@ -194,6 +194,16 @@ const USRBasedType *USRBasedType::fromType(Type Ty, USRBasedTypeArena &Arena) {
194194
return USRBasedType::null(Arena);
195195
}
196196

197+
// ParameterizedProtocolType should always be wrapped in ExistentialType and
198+
// cannot be mangled on its own.
199+
// But ParameterizedProtocolType can currently occur in 'typealias'
200+
// declarations. rdar://99176683
201+
// To avoid crashing in USR generation, simply return a null type until the
202+
// underlying issue has been fixed.
203+
if (Ty->is<ParameterizedProtocolType>()) {
204+
return USRBasedType::null(Arena);
205+
}
206+
197207
SmallString<32> USR;
198208
llvm::raw_svector_ostream OS(USR);
199209
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)