Skip to content

[ASTPrinter] Use TypeLoc printing for extended types. #38014

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
13 changes: 6 additions & 7 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
void printSynthesizedExtension(Type ExtendedType, ExtensionDecl *ExtDecl);

void printExtension(ExtensionDecl* ExtDecl);
void printExtendedTypeName(TypeLoc ExtendedTypeLoc);

public:
PrintAST(ASTPrinter &Printer, const PrintOptions &Options)
Expand Down Expand Up @@ -2331,8 +2332,7 @@ void PrintAST::visitImportDecl(ImportDecl *decl) {
[&] { Printer << "."; });
}

static void printExtendedTypeName(Type ExtendedType, ASTPrinter &Printer,
PrintOptions &Options) {
void PrintAST::printExtendedTypeName(TypeLoc ExtendedTypeLoc) {
bool OldFullyQualifiedTypesIfAmbiguous =
Options.FullyQualifiedTypesIfAmbiguous;
Options.FullyQualifiedTypesIfAmbiguous =
Expand All @@ -2342,9 +2342,8 @@ static void printExtendedTypeName(Type ExtendedType, ASTPrinter &Printer,
};

// Strip off generic arguments, if any.
auto Ty = ExtendedType->getAnyNominal()->getDeclaredType();

Ty->print(Printer, Options);
auto Ty = ExtendedTypeLoc.getType()->getAnyNominal()->getDeclaredType();
printTypeLoc(TypeLoc(ExtendedTypeLoc.getTypeRepr(), Ty));
}


Expand Down Expand Up @@ -2402,7 +2401,7 @@ void PrintAST::printSynthesizedExtension(Type ExtendedType,
printAttributes(ExtDecl);
Printer << tok::kw_extension << " ";

printExtendedTypeName(ExtendedType, Printer, Options);
printExtendedTypeName(TypeLoc::withoutLoc(ExtendedType));
printInherited(ExtDecl);

// We may need to combine requirements from ExtDecl (which has the members
Expand Down Expand Up @@ -2453,7 +2452,7 @@ void PrintAST::printExtension(ExtensionDecl *decl) {
printTypeLoc(TypeLoc::withoutLoc(extendedType));
return;
}
printExtendedTypeName(extendedType, Printer, Options);
printExtendedTypeName(TypeLoc(decl->getExtendedTypeRepr(), extendedType));
});
printInherited(decl);

Expand Down
30 changes: 28 additions & 2 deletions test/ModuleInterface/preserve-type-repr.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path - %s -enable-library-evolution -module-name PreferTypeRepr -module-interface-preserve-types-as-written | %FileCheck %s --check-prefix PREFER
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path - %s -enable-library-evolution -module-name PreferTypeRepr | %FileCheck %s --check-prefix DONTPREFER
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -typecheck -enable-library-evolution -emit-module-interface-path %t/External.swiftinterface -module-name External -DEXTERNAL
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path - %s -enable-library-evolution -module-name PreferTypeRepr -module-interface-preserve-types-as-written -I %t | %FileCheck %s --check-prefix PREFER
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path - %s -enable-library-evolution -module-name PreferTypeRepr -I %t | %FileCheck %s --check-prefix DONTPREFER

#if EXTERNAL
public struct Toy {
public init() {}
}

public struct GenericToy<T> {
public init() {}
}
#else
import External

// PREFER: extension Toy
// DONTPREFER: extension External.Toy
extension Toy {
public static var new: Toy { Toy() }
}

// PREFER: extension GenericToy {
// DONTPREFER: extension External.GenericToy {
extension GenericToy {
public static var new: GenericToy<T> { GenericToy() }
}

public protocol Pet {}

Expand Down Expand Up @@ -32,3 +57,4 @@ extension My where T: Pet {
// PREFER: public func isNoMore(_ pet: Ex<Parrot>) -> Bool
// DONTPREFER: public func isNoMore(_ pet: PreferTypeRepr.Ex<PreferTypeRepr.Parrot>) -> Swift.Bool
public func isNoMore(_ pet: Ex<Parrot>) -> Bool {}
#endif