Skip to content

Commit d93ae06

Browse files
committed
[ASTPrinter] Don't transform type if current type can't have members
Since 9ba892c we always transform `CurrentType` in `ASTPrinter` to be an interface type. This causes issues for variables that whose type is a protocol. Previously, when printing the type, we had `CurrentType` set to an `OpenedArchetypeType`. Now we replace the archetype by a `GenericTypeParamType`, which may not have members, so we are hitting an assertion in `ASTPrinter.cpp:270`. To resolve this, replace any `OpenedArchetypeType`s with their protocol type before calling `mapTypeOutOfContext`. Resolves rdar://76580851 [SR-14479]
1 parent 9ba892c commit d93ae06

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,18 @@ class PrintAST : public ASTVisitor<PrintAST> {
923923
if (Options.TransformContext) {
924924
Type CurrentType = Options.TransformContext->getBaseType();
925925
if (CurrentType && CurrentType->hasArchetype()) {
926+
// OpenedArchetypeTypes get replaced by a GenericTypeParamType without a
927+
// name in mapTypeOutOfContext. The GenericTypeParamType has no children
928+
// so we can't use it for TypeTransformContext.
929+
// To work around this, replace the OpenedArchetypeType with the type of
930+
// the protocol itself.
931+
CurrentType = CurrentType.transform([](Type T) -> Type {
932+
if (auto *Opened = T->getAs<OpenedArchetypeType>()) {
933+
return Opened->getOpenedExistentialType();
934+
} else {
935+
return T;
936+
}
937+
});
926938
CurrentType = CurrentType->mapTypeOutOfContext();
927939
}
928940
setCurrentType(CurrentType);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
protocol MyError {
2+
var myVar: String { get }
3+
}
4+
5+
func foo(error: MyError) {
6+
_ = error.myVar
7+
}
8+
9+
// RUN: %sourcekitd-test -req=cursor -pos=6:15 %s -- %s | %FileCheck %s
10+
// CHECK: myVar
11+
// CHECK-NEXT: s:27cursor_info_existential_var7MyErrorP5myVarSSvp
12+
// CHECK-NEXT: source.lang.swift
13+
// CHECK-NEXT: String

0 commit comments

Comments
 (0)