[ASTPrinter] Don't transform type if current type can't have members

Since 9ba892c5af 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]
This commit is contained in:
Alex Hoppen
2021-04-13 11:30:41 +02:00
parent 9ba892c5af
commit d93ae06f50
2 changed files with 25 additions and 0 deletions

View File

@@ -923,6 +923,18 @@ public:
if (Options.TransformContext) {
Type CurrentType = Options.TransformContext->getBaseType();
if (CurrentType && CurrentType->hasArchetype()) {
// OpenedArchetypeTypes get replaced by a GenericTypeParamType without a
// name in mapTypeOutOfContext. The GenericTypeParamType has no children
// so we can't use it for TypeTransformContext.
// To work around this, replace the OpenedArchetypeType with the type of
// the protocol itself.
CurrentType = CurrentType.transform([](Type T) -> Type {
if (auto *Opened = T->getAs<OpenedArchetypeType>()) {
return Opened->getOpenedExistentialType();
} else {
return T;
}
});
CurrentType = CurrentType->mapTypeOutOfContext();
}
setCurrentType(CurrentType);

View File

@@ -0,0 +1,13 @@
protocol MyError {
var myVar: String { get }
}
func foo(error: MyError) {
_ = error.myVar
}
// RUN: %sourcekitd-test -req=cursor -pos=6:15 %s -- %s | %FileCheck %s
// CHECK: myVar
// CHECK-NEXT: s:27cursor_info_existential_var7MyErrorP5myVarSSvp
// CHECK-NEXT: source.lang.swift
// CHECK-NEXT: String