Skip to content

[5.1][ASTPrinter] Print opaque type using ArchetypeType::getExistentialType() #24318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3412,20 +3412,18 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
bool isSimple = T->hasSimpleTypeRepr();
if (isSimple && T->is<OpaqueTypeArchetypeType>()) {
auto opaqueTy = T->castTo<OpaqueTypeArchetypeType>();
auto opaqueDecl = opaqueTy->getDecl();
if (!opaqueDecl->hasName()) {
switch (Options.OpaqueReturnTypePrinting) {
case PrintOptions::OpaqueReturnTypePrintingMode::StableReference:
case PrintOptions::OpaqueReturnTypePrintingMode::Description:
isSimple = true;
break;
case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
isSimple = false;
break;
case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword: {
isSimple = opaqueTy->getConformsTo().size() < 2;
}
}
switch (Options.OpaqueReturnTypePrinting) {
case PrintOptions::OpaqueReturnTypePrintingMode::StableReference:
case PrintOptions::OpaqueReturnTypePrintingMode::Description:
isSimple = true;
break;
case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
isSimple = false;
break;
case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword: {
isSimple = opaqueTy->getExistentialType()->hasSimpleTypeRepr();
break;
}
}
}

Expand Down Expand Up @@ -4207,14 +4205,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
Printer << "some ";
LLVM_FALLTHROUGH;
case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword: {
SmallVector<Type, 2> types;
for (auto proto : T->getConformsTo())
types.push_back(proto->TypeDecl::getDeclaredInterfaceType());

// Create and visit temporary ProtocolCompositionType.
auto composition =
ProtocolCompositionType::get(T->getASTContext(), types, false);
visit(composition);
visit(T->getExistentialType());
return;
}
case PrintOptions::OpaqueReturnTypePrintingMode::StableReference: {
Expand Down
12 changes: 12 additions & 0 deletions test/IDE/print_opaque_result_type.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %swift-ide-test -print-swift-file-interface -source-filename %s

public class Base {}
// CHECK: public class Base {
public protocol Proto {}
// CHECK: public protocol Proto {
}
public func foo() -> some Base & Proto {
// CHECK: public func foo() -> some Base & Proto
class Derived: Base, Proto {}
return Derived()
}