Skip to content

[6.1] Revert https://github.com/swiftlang/swift/pull/77550 #78519

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 1 commit into from
Jan 9, 2025
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
4 changes: 0 additions & 4 deletions include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,6 @@ struct PrintOptions {
OpaqueReturnTypePrintingMode OpaqueReturnTypePrinting =
OpaqueReturnTypePrintingMode::WithOpaqueKeyword;

/// If non-null, opaque types that have this naming decl should be printed as
/// `some P1` instead of as a stable reference.
const ValueDecl *OpaqueReturnTypeNamingDecl = nullptr;

/// Whether to print decl attributes that are only used internally,
/// such as _silgen_name, transparent, etc.
bool PrintUserInaccessibleAttrs = true;
Expand Down
66 changes: 38 additions & 28 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ bool PrintOptions::excludeAttr(const DeclAttribute *DA) const {
return false;
}

/// Forces printing types with the `some` keyword, instead of the full stable
/// reference.
struct PrintWithOpaqueResultTypeKeywordRAII {
PrintWithOpaqueResultTypeKeywordRAII(PrintOptions &Options)
: Options(Options) {
SavedMode = Options.OpaqueReturnTypePrinting;
Options.OpaqueReturnTypePrinting =
PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword;
}
~PrintWithOpaqueResultTypeKeywordRAII() {
Options.OpaqueReturnTypePrinting = SavedMode;
}

private:
PrintOptions &Options;
PrintOptions::OpaqueReturnTypePrintingMode SavedMode;
};

PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
bool preferTypeRepr,
bool printFullConvention,
Expand Down Expand Up @@ -941,19 +959,10 @@ class PrintAST : public ASTVisitor<PrintAST> {
printTypeLocWithOptions(TL, Options, printBeforeType);
}

void printTypeLocForImplicitlyUnwrappedOptional(
TypeLoc TL, bool IUO, const ValueDecl *opaqueTypeNamingDecl) {
auto savedIOU = Options.PrintOptionalAsImplicitlyUnwrapped;
Options.PrintOptionalAsImplicitlyUnwrapped = IUO;

auto savedOpaqueTypeNamingDecl = Options.OpaqueReturnTypeNamingDecl;
if (opaqueTypeNamingDecl)
Options.OpaqueReturnTypeNamingDecl = opaqueTypeNamingDecl;

printTypeLocWithOptions(TL, Options);

Options.PrintOptionalAsImplicitlyUnwrapped = savedIOU;
Options.OpaqueReturnTypeNamingDecl = savedOpaqueTypeNamingDecl;
void printTypeLocForImplicitlyUnwrappedOptional(TypeLoc TL, bool IUO) {
PrintOptions options = Options;
options.PrintOptionalAsImplicitlyUnwrapped = IUO;
printTypeLocWithOptions(TL, options);
}

void printContextIfNeeded(const Decl *decl) {
Expand Down Expand Up @@ -1357,17 +1366,18 @@ void PrintAST::printTypedPattern(const TypedPattern *TP) {
printPattern(TP->getSubPattern());
Printer << ": ";

VarDecl *varDecl = nullptr;
PrintWithOpaqueResultTypeKeywordRAII x(Options);

// Make sure to check if the underlying var decl is an implicitly unwrapped
// optional.
bool isIUO = false;
if (auto *named = dyn_cast<NamedPattern>(TP->getSubPattern()))
if (auto decl = named->getDecl())
varDecl = decl;
isIUO = decl->isImplicitlyUnwrappedOptional();

const auto TyLoc = TypeLoc(TP->getTypeRepr(),
TP->hasType() ? TP->getType() : Type());

printTypeLocForImplicitlyUnwrappedOptional(
TyLoc, varDecl ? varDecl->isImplicitlyUnwrappedOptional() : false,
varDecl);
printTypeLocForImplicitlyUnwrappedOptional(TyLoc, isIUO);
}

/// Determines if we are required to print the name of a property declaration,
Expand Down Expand Up @@ -3789,8 +3799,9 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
}
Printer.printDeclResultTypePre(decl, tyLoc);

PrintWithOpaqueResultTypeKeywordRAII x(Options);
printTypeLocForImplicitlyUnwrappedOptional(
tyLoc, decl->isImplicitlyUnwrappedOptional(), decl);
tyLoc, decl->isImplicitlyUnwrappedOptional());
}

printAccessors(decl);
Expand Down Expand Up @@ -3872,7 +3883,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
}

printTypeLocForImplicitlyUnwrappedOptional(
TheTypeLoc, param->isImplicitlyUnwrappedOptional(), nullptr);
TheTypeLoc, param->isImplicitlyUnwrappedOptional());
}

if (param->isDefaultArgument() && Options.PrintDefaultArgumentValue) {
Expand Down Expand Up @@ -4162,6 +4173,8 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
}
}

PrintWithOpaqueResultTypeKeywordRAII x(Options);

// Check if we would go down the type repr path... in such a case, see if
// we can find a type repr and if that type has a sending type repr. In
// such a case, look through the sending type repr since we handle it here
Expand All @@ -4188,7 +4201,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
// If we printed using type repr printing, do not print again.
if (!usedTypeReprPrinting) {
printTypeLocForImplicitlyUnwrappedOptional(
ResultTyLoc, decl->isImplicitlyUnwrappedOptional(), decl);
ResultTyLoc, decl->isImplicitlyUnwrappedOptional());
}
Printer.printStructurePost(PrintStructureKind::FunctionReturnType);
}
Expand Down Expand Up @@ -4337,8 +4350,9 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
Printer.printDeclResultTypePre(decl, elementTy);
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);

PrintWithOpaqueResultTypeKeywordRAII x(Options);
printTypeLocForImplicitlyUnwrappedOptional(
elementTy, decl->isImplicitlyUnwrappedOptional(), decl);
elementTy, decl->isImplicitlyUnwrappedOptional());
Printer.printStructurePost(PrintStructureKind::FunctionReturnType);
}

Expand Down Expand Up @@ -7151,11 +7165,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
auto genericSig = namingDecl->getInnermostDeclContext()
->getGenericSignatureOfContext();

auto mode = Options.OpaqueReturnTypePrinting;
if (Options.OpaqueReturnTypeNamingDecl == T->getDecl()->getNamingDecl())
mode = PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword;

switch (mode) {
switch (Options.OpaqueReturnTypePrinting) {
case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
if (printNamedOpaque())
return;
Expand Down
26 changes: 0 additions & 26 deletions test/ModuleInterface/opaque-result-types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public protocol AssocTypeInference {
subscript() -> AssocSubscript { get }
}

// CHECK-LABEL: public struct Bar<T> : OpaqueResultTypes.AssocTypeInference
@available(SwiftStdlib 5.1, *)
public struct Bar<T>: AssocTypeInference {
public init() {}
Expand Down Expand Up @@ -260,28 +259,3 @@ public struct Zim: AssocTypeInference {
return 123
}
}

public protocol PrimaryAssociatedTypeInference<Assoc> {
associatedtype Assoc

func foo(_: Int) -> Assoc
}

// CHECK-LABEL: public struct Baz : OpaqueResultTypes.PrimaryAssociatedTypeInference

public struct Baz: PrimaryAssociatedTypeInference {
// CHECK-LABEL: public func foo(_: Swift.Int) -> some OpaqueResultTypes.Foo
public func foo(_: Int) -> some Foo {
return 123
}

// CHECK-LABEL: public func callsFoo() -> @_opaqueReturnTypeOf("$s17OpaqueResultTypes3BazV3fooyQrSiF", 0) __
public func callsFoo() -> Assoc {
return foo(123)
}

// CHECK-LABEL: public func identity() -> some OpaqueResultTypes.PrimaryAssociatedTypeInference<@_opaqueReturnTypeOf("$s17OpaqueResultTypes3BazV3fooyQrSiF", 0) __>
public func identity() -> some PrimaryAssociatedTypeInference<Assoc> {
return self
}
}