mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[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:
@@ -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);
|
||||
|
||||
13
test/SourceKit/CursorInfo/cursor_info_existential_var.swift
Normal file
13
test/SourceKit/CursorInfo/cursor_info_existential_var.swift
Normal 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
|
||||
Reference in New Issue
Block a user