Skip to content

Commit f17a70c

Browse files
committed
Fix a bug with the printing of optional typealiases.
1 parent b3750a7 commit f17a70c

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3529,24 +3529,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
35293529
return;
35303530
}
35313531

3532-
bool isSimple = T->hasSimpleTypeRepr();
3533-
if (!isSimple && T->is<OpaqueTypeArchetypeType>()) {
3534-
auto opaqueTy = T->castTo<OpaqueTypeArchetypeType>();
3535-
switch (Options.OpaqueReturnTypePrinting) {
3536-
case PrintOptions::OpaqueReturnTypePrintingMode::StableReference:
3537-
case PrintOptions::OpaqueReturnTypePrintingMode::Description:
3538-
isSimple = true;
3539-
break;
3540-
case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
3541-
isSimple = false;
3542-
break;
3543-
case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword: {
3544-
isSimple = opaqueTy->getExistentialType()->hasSimpleTypeRepr();
3545-
break;
3546-
}
3547-
}
3548-
}
3549-
3532+
bool isSimple = isSimpleUnderPrintOptions(T);
35503533
if (isSimple) {
35513534
visit(T);
35523535
} else {
@@ -3556,6 +3539,28 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
35563539
}
35573540
}
35583541

3542+
/// Determinee whether the given type has a simple representation
3543+
/// under the current print options.
3544+
bool isSimpleUnderPrintOptions(Type T) {
3545+
if (auto typealias = dyn_cast<TypeAliasType>(T.getPointer())) {
3546+
if (shouldDesugarTypeAliasType(typealias))
3547+
return isSimpleUnderPrintOptions(typealias->getSinglyDesugaredType());
3548+
} else if (auto opaque =
3549+
dyn_cast<OpaqueTypeArchetypeType>(T.getPointer())) {
3550+
switch (Options.OpaqueReturnTypePrinting) {
3551+
case PrintOptions::OpaqueReturnTypePrintingMode::StableReference:
3552+
case PrintOptions::OpaqueReturnTypePrintingMode::Description:
3553+
return true;
3554+
case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
3555+
return false;
3556+
case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword:
3557+
return isSimpleUnderPrintOptions(opaque->getExistentialType());
3558+
}
3559+
llvm_unreachable("bad opaque-return-type printing mode");
3560+
}
3561+
return T->hasSimpleTypeRepr();
3562+
}
3563+
35593564
template <typename T>
35603565
void printModuleContext(T *Ty) {
35613566
FileUnit *File = cast<FileUnit>(Ty->getDecl()->getModuleScopeContext());
@@ -3693,8 +3698,12 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
36933698
Printer << BUILTIN_TYPE_NAME_SILTOKEN;
36943699
}
36953700

3701+
bool shouldDesugarTypeAliasType(TypeAliasType *T) {
3702+
return Options.PrintForSIL || Options.PrintTypeAliasUnderlyingType;
3703+
}
3704+
36963705
void visitTypeAliasType(TypeAliasType *T) {
3697-
if (Options.PrintForSIL || Options.PrintTypeAliasUnderlyingType) {
3706+
if (shouldDesugarTypeAliasType(T)) {
36983707
visit(T->getSinglyDesugaredType());
36993708
return;
37003709
}

0 commit comments

Comments
 (0)