Skip to content

Introduce non-recursive print options and use them to handle IUO printing more elegantly #82327

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
Jun 19, 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
16 changes: 9 additions & 7 deletions include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "swift/AST/AttrKind.h"
#include "swift/AST/Identifier.h"
#include "swift/AST/TypeOrExtensionDecl.h"
#include "swift/Basic/OptionSet.h"
#include "swift/Basic/STLExtras.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallSet.h"
Expand Down Expand Up @@ -124,6 +125,14 @@ struct ShouldPrintChecker {
virtual ~ShouldPrintChecker() = default;
};

/// Type-printing options which should only be applied to the outermost
/// type.
enum class NonRecursivePrintOption: uint32_t {
/// Print `Optional<T>` as `T!`.
ImplicitlyUnwrappedOptional = 1 << 0,
};
using NonRecursivePrintOptions = OptionSet<NonRecursivePrintOption>;

/// Options for printing AST nodes.
///
/// A default-constructed PrintOptions is suitable for printing to users;
Expand Down Expand Up @@ -586,13 +595,6 @@ struct PrintOptions {
/// yet.
llvm::SmallSet<StringRef, 4> *AliasModuleNamesTargets = nullptr;

/// When printing an Optional<T>, rather than printing 'T?', print
/// 'T!'. Used as a modifier only when we know we're printing
/// something that was declared as an implicitly unwrapped optional
/// at the top level. This is stripped out of the printing options
/// for optionals that are nested within other optionals.
bool PrintOptionalAsImplicitlyUnwrapped = false;

/// Replaces the name of private and internal properties of types with '_'.
bool OmitNameOfInaccessibleProperties = false;

Expand Down
12 changes: 8 additions & 4 deletions include/swift/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,14 @@ class Type {
SWIFT_DEBUG_DUMP;
void dump(raw_ostream &os, unsigned indent = 0) const;

void print(raw_ostream &OS, const PrintOptions &PO = PrintOptions()) const;
void print(ASTPrinter &Printer, const PrintOptions &PO) const;
void print(raw_ostream &OS, const PrintOptions &PO = PrintOptions(),
NonRecursivePrintOptions OPO = std::nullopt) const;
void print(ASTPrinter &Printer, const PrintOptions &PO,
NonRecursivePrintOptions OPO = std::nullopt) const;

/// Return the name of the type as a string, for use in diagnostics only.
std::string getString(const PrintOptions &PO = PrintOptions()) const;
std::string getString(const PrintOptions &PO = PrintOptions(),
NonRecursivePrintOptions OPO = std::nullopt) const;

/// Return the name of the type, adding parens in cases where
/// appending or prepending text to the result would cause that text
Expand All @@ -360,7 +363,8 @@ class Type {
/// the type would make it appear that it's appended to "Float" as
/// opposed to the entire type.
std::string
getStringAsComponent(const PrintOptions &PO = PrintOptions()) const;
getStringAsComponent(const PrintOptions &PO = PrintOptions(),
NonRecursivePrintOptions OPO = std::nullopt) const;

/// Computes the join between two types.
///
Expand Down
95 changes: 63 additions & 32 deletions include/swift/AST/TypeRepr.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@ class alignas(1 << TypeReprAlignInBits) TypeRepr

//*** Allocation Routines ************************************************/

void print(raw_ostream &OS, const PrintOptions &Opts = PrintOptions()) const;
void print(ASTPrinter &Printer, const PrintOptions &Opts) const;
void print(raw_ostream &OS, const PrintOptions &opts = PrintOptions(),
NonRecursivePrintOptions nrOpts = std::nullopt) const;
void print(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts = std::nullopt) const;
SWIFT_DEBUG_DUMP;
void dump(raw_ostream &OS, unsigned indent = 0) const;
};
Expand Down Expand Up @@ -252,7 +254,8 @@ class ErrorTypeRepr : public TypeRepr {
private:
SourceLoc getStartLocImpl() const { return Range.Start; }
SourceLoc getEndLocImpl() const { return Range.End; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -303,7 +306,7 @@ class AttributedTypeRepr final
ReferenceOwnership getSILOwnership() const;

void printAttrs(llvm::raw_ostream &OS) const;
void printAttrs(ASTPrinter &Printer, const PrintOptions &Options) const;
void printAttrs(ASTPrinter &Printer, const PrintOptions &options) const;

static bool classof(const TypeRepr *T) {
return T->getKind() == TypeReprKind::Attributed;
Expand All @@ -321,7 +324,8 @@ class AttributedTypeRepr final
}
SourceLoc getEndLocImpl() const { return Ty->getEndLoc(); }
SourceLoc getLocImpl() const { return Ty->getLoc(); }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -417,7 +421,8 @@ class DeclRefTypeRepr : public TypeRepr {
SourceLoc getLocImpl() const;
SourceLoc getEndLocImpl() const;

void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts,
NonRecursivePrintOptions nrOpts) const;

friend class TypeRepr;
};
Expand Down Expand Up @@ -610,7 +615,8 @@ class FunctionTypeRepr : public TypeRepr {
SourceLoc getEndLocImpl() const { return RetTy->getEndLoc(); }
SourceLoc getLocImpl() const { return ArrowLoc; }

void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand All @@ -637,7 +643,8 @@ class ArrayTypeRepr : public TypeRepr {
private:
SourceLoc getStartLocImpl() const { return Brackets.Start; }
SourceLoc getEndLocImpl() const { return Brackets.End; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -666,7 +673,8 @@ class InlineArrayTypeRepr : public TypeRepr {
private:
SourceLoc getStartLocImpl() const { return Brackets.Start; }
SourceLoc getEndLocImpl() const { return Brackets.End; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -699,7 +707,8 @@ class DictionaryTypeRepr : public TypeRepr {
private:
SourceLoc getStartLocImpl() const { return Brackets.Start; }
SourceLoc getEndLocImpl() const { return Brackets.End; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -731,7 +740,8 @@ class OptionalTypeRepr : public TypeRepr {
SourceLoc getLocImpl() const {
return QuestionLoc.isValid() ? QuestionLoc : Base->getLoc();
}
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -760,7 +770,8 @@ class ImplicitlyUnwrappedOptionalTypeRepr : public TypeRepr {
SourceLoc getStartLocImpl() const { return Base->getStartLoc(); }
SourceLoc getEndLocImpl() const { return ExclamationLoc; }
SourceLoc getLocImpl() const { return ExclamationLoc; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -801,7 +812,8 @@ class VarargTypeRepr final : public TypeRepr {
SourceLoc getStartLocImpl() const { return Element->getEndLoc(); }
SourceLoc getEndLocImpl() const { return EllipsisLoc; }
SourceLoc getLocImpl() const { return EllipsisLoc; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -832,7 +844,8 @@ class PackExpansionTypeRepr final : public TypeRepr {
SourceLoc getStartLocImpl() const { return RepeatLoc; }
SourceLoc getEndLocImpl() const { return Pattern->getEndLoc(); }
SourceLoc getLocImpl() const { return RepeatLoc; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -880,7 +893,8 @@ class PackTypeRepr final
SourceLoc getStartLocImpl() const { return KeywordLoc; }
SourceLoc getEndLocImpl() const { return BraceLocs.End; }
SourceLoc getLocImpl() const { return KeywordLoc; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -915,7 +929,8 @@ class PackElementTypeRepr: public TypeRepr {
SourceLoc getStartLocImpl() const { return EachLoc; }
SourceLoc getEndLocImpl() const { return PackType->getEndLoc(); }
SourceLoc getLocImpl() const { return EachLoc; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -1010,7 +1025,8 @@ class TupleTypeRepr final : public TypeRepr,
private:
SourceLoc getStartLocImpl() const { return Parens.Start; }
SourceLoc getEndLocImpl() const { return Parens.End; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -1066,7 +1082,8 @@ class CompositionTypeRepr final : public TypeRepr,
SourceLoc getStartLocImpl() const { return FirstTypeLoc; }
SourceLoc getLocImpl() const { return CompositionRange.Start; }
SourceLoc getEndLocImpl() const { return CompositionRange.End; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -1095,7 +1112,8 @@ class MetatypeTypeRepr : public TypeRepr {
SourceLoc getStartLocImpl() const { return Base->getStartLoc(); }
SourceLoc getEndLocImpl() const { return MetaLoc; }
SourceLoc getLocImpl() const { return MetaLoc; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -1124,7 +1142,8 @@ class ProtocolTypeRepr : public TypeRepr {
SourceLoc getStartLocImpl() const { return Base->getStartLoc(); }
SourceLoc getEndLocImpl() const { return ProtocolLoc; }
SourceLoc getLocImpl() const { return ProtocolLoc; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -1155,7 +1174,8 @@ class SpecifierTypeRepr : public TypeRepr {
private:
SourceLoc getStartLocImpl() const { return SpecifierLoc; }
SourceLoc getEndLocImpl() const { return Base->getEndLoc(); }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -1274,7 +1294,8 @@ class CallerIsolatedTypeRepr : public TypeRepr {
SourceLoc getStartLocImpl() const { return Loc; }
SourceLoc getEndLocImpl() const { return Base->getEndLoc(); }
SourceLoc getLocImpl() const { return Base->getLoc(); }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -1311,7 +1332,8 @@ class FixedTypeRepr : public TypeRepr {
private:
SourceLoc getStartLocImpl() const { return Loc; }
SourceLoc getEndLocImpl() const { return Loc; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand All @@ -1338,7 +1360,8 @@ class SelfTypeRepr : public TypeRepr {
private:
SourceLoc getStartLocImpl() const { return Loc; }
SourceLoc getEndLocImpl() const { return Loc; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -1391,7 +1414,8 @@ class OpaqueReturnTypeRepr : public TypeRepr {
SourceLoc getStartLocImpl() const { return OpaqueLoc; }
SourceLoc getEndLocImpl() const { return Constraint->getEndLoc(); }
SourceLoc getLocImpl() const { return OpaqueLoc; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -1426,7 +1450,8 @@ class NamedOpaqueReturnTypeRepr : public TypeRepr {
SourceLoc getStartLocImpl() const;
SourceLoc getEndLocImpl() const;
SourceLoc getLocImpl() const;
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -1455,7 +1480,8 @@ class ExistentialTypeRepr: public TypeRepr {
SourceLoc getStartLocImpl() const { return AnyLoc; }
SourceLoc getEndLocImpl() const { return Constraint->getEndLoc(); }
SourceLoc getLocImpl() const { return AnyLoc; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -1483,7 +1509,8 @@ class InverseTypeRepr : public TypeRepr {
SourceLoc getStartLocImpl() const { return TildeLoc; }
SourceLoc getEndLocImpl() const { return Constraint->getEndLoc(); }
SourceLoc getLocImpl() const { return TildeLoc; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -1511,7 +1538,8 @@ class PlaceholderTypeRepr: public TypeRepr {
SourceLoc getStartLocImpl() const { return UnderscoreLoc; }
SourceLoc getEndLocImpl() const { return UnderscoreLoc; }
SourceLoc getLocImpl() const { return UnderscoreLoc; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -1600,7 +1628,8 @@ class SILBoxTypeRepr final : public TypeRepr,
SourceLoc getStartLocImpl() const;
SourceLoc getEndLocImpl() const;
SourceLoc getLocImpl() const;
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend TypeRepr;
};

Expand All @@ -1627,7 +1656,8 @@ class LifetimeDependentTypeRepr final : public SpecifierTypeRepr {
SourceLoc getStartLocImpl() const;
SourceLoc getEndLocImpl() const;
SourceLoc getLocImpl() const;
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down Expand Up @@ -1668,7 +1698,8 @@ class IntegerTypeRepr final : public TypeRepr {

SourceLoc getEndLocImpl() const { return Loc; }
SourceLoc getLocImpl() const { return Loc; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
NonRecursivePrintOptions nrOpts) const;
friend class TypeRepr;
};

Expand Down
Loading