Skip to content

Commit a2ded36

Browse files
authored
Merge pull request #82327 from rjmccall/non-recursive-print-options
Introduce non-recursive print options and use them to handle IUO printing more elegantly
2 parents a0d3ad7 + 5c20b19 commit a2ded36

File tree

10 files changed

+368
-251
lines changed

10 files changed

+368
-251
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/AttrKind.h"
1717
#include "swift/AST/Identifier.h"
1818
#include "swift/AST/TypeOrExtensionDecl.h"
19+
#include "swift/Basic/OptionSet.h"
1920
#include "swift/Basic/STLExtras.h"
2021
#include "llvm/ADT/DenseMap.h"
2122
#include "llvm/ADT/SmallSet.h"
@@ -124,6 +125,14 @@ struct ShouldPrintChecker {
124125
virtual ~ShouldPrintChecker() = default;
125126
};
126127

128+
/// Type-printing options which should only be applied to the outermost
129+
/// type.
130+
enum class NonRecursivePrintOption: uint32_t {
131+
/// Print `Optional<T>` as `T!`.
132+
ImplicitlyUnwrappedOptional = 1 << 0,
133+
};
134+
using NonRecursivePrintOptions = OptionSet<NonRecursivePrintOption>;
135+
127136
/// Options for printing AST nodes.
128137
///
129138
/// A default-constructed PrintOptions is suitable for printing to users;
@@ -586,13 +595,6 @@ struct PrintOptions {
586595
/// yet.
587596
llvm::SmallSet<StringRef, 4> *AliasModuleNamesTargets = nullptr;
588597

589-
/// When printing an Optional<T>, rather than printing 'T?', print
590-
/// 'T!'. Used as a modifier only when we know we're printing
591-
/// something that was declared as an implicitly unwrapped optional
592-
/// at the top level. This is stripped out of the printing options
593-
/// for optionals that are nested within other optionals.
594-
bool PrintOptionalAsImplicitlyUnwrapped = false;
595-
596598
/// Replaces the name of private and internal properties of types with '_'.
597599
bool OmitNameOfInaccessibleProperties = false;
598600

include/swift/AST/Type.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,14 @@ class Type {
347347
SWIFT_DEBUG_DUMP;
348348
void dump(raw_ostream &os, unsigned indent = 0) const;
349349

350-
void print(raw_ostream &OS, const PrintOptions &PO = PrintOptions()) const;
351-
void print(ASTPrinter &Printer, const PrintOptions &PO) const;
350+
void print(raw_ostream &OS, const PrintOptions &PO = PrintOptions(),
351+
NonRecursivePrintOptions OPO = std::nullopt) const;
352+
void print(ASTPrinter &Printer, const PrintOptions &PO,
353+
NonRecursivePrintOptions OPO = std::nullopt) const;
352354

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

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

365369
/// Computes the join between two types.
366370
///

include/swift/AST/TypeRepr.h

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,10 @@ class alignas(1 << TypeReprAlignInBits) TypeRepr
205205

206206
//*** Allocation Routines ************************************************/
207207

208-
void print(raw_ostream &OS, const PrintOptions &Opts = PrintOptions()) const;
209-
void print(ASTPrinter &Printer, const PrintOptions &Opts) const;
208+
void print(raw_ostream &OS, const PrintOptions &opts = PrintOptions(),
209+
NonRecursivePrintOptions nrOpts = std::nullopt) const;
210+
void print(ASTPrinter &Printer, const PrintOptions &opts,
211+
NonRecursivePrintOptions nrOpts = std::nullopt) const;
210212
SWIFT_DEBUG_DUMP;
211213
void dump(raw_ostream &OS, unsigned indent = 0) const;
212214
};
@@ -252,7 +254,8 @@ class ErrorTypeRepr : public TypeRepr {
252254
private:
253255
SourceLoc getStartLocImpl() const { return Range.Start; }
254256
SourceLoc getEndLocImpl() const { return Range.End; }
255-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
257+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
258+
NonRecursivePrintOptions nrOpts) const;
256259
friend class TypeRepr;
257260
};
258261

@@ -303,7 +306,7 @@ class AttributedTypeRepr final
303306
ReferenceOwnership getSILOwnership() const;
304307

305308
void printAttrs(llvm::raw_ostream &OS) const;
306-
void printAttrs(ASTPrinter &Printer, const PrintOptions &Options) const;
309+
void printAttrs(ASTPrinter &Printer, const PrintOptions &options) const;
307310

308311
static bool classof(const TypeRepr *T) {
309312
return T->getKind() == TypeReprKind::Attributed;
@@ -321,7 +324,8 @@ class AttributedTypeRepr final
321324
}
322325
SourceLoc getEndLocImpl() const { return Ty->getEndLoc(); }
323326
SourceLoc getLocImpl() const { return Ty->getLoc(); }
324-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
327+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
328+
NonRecursivePrintOptions nrOpts) const;
325329
friend class TypeRepr;
326330
};
327331

@@ -417,7 +421,8 @@ class DeclRefTypeRepr : public TypeRepr {
417421
SourceLoc getLocImpl() const;
418422
SourceLoc getEndLocImpl() const;
419423

420-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
424+
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts,
425+
NonRecursivePrintOptions nrOpts) const;
421426

422427
friend class TypeRepr;
423428
};
@@ -610,7 +615,8 @@ class FunctionTypeRepr : public TypeRepr {
610615
SourceLoc getEndLocImpl() const { return RetTy->getEndLoc(); }
611616
SourceLoc getLocImpl() const { return ArrowLoc; }
612617

613-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
618+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
619+
NonRecursivePrintOptions nrOpts) const;
614620
friend class TypeRepr;
615621
};
616622

@@ -637,7 +643,8 @@ class ArrayTypeRepr : public TypeRepr {
637643
private:
638644
SourceLoc getStartLocImpl() const { return Brackets.Start; }
639645
SourceLoc getEndLocImpl() const { return Brackets.End; }
640-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
646+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
647+
NonRecursivePrintOptions nrOpts) const;
641648
friend class TypeRepr;
642649
};
643650

@@ -666,7 +673,8 @@ class InlineArrayTypeRepr : public TypeRepr {
666673
private:
667674
SourceLoc getStartLocImpl() const { return Brackets.Start; }
668675
SourceLoc getEndLocImpl() const { return Brackets.End; }
669-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
676+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
677+
NonRecursivePrintOptions nrOpts) const;
670678
friend class TypeRepr;
671679
};
672680

@@ -699,7 +707,8 @@ class DictionaryTypeRepr : public TypeRepr {
699707
private:
700708
SourceLoc getStartLocImpl() const { return Brackets.Start; }
701709
SourceLoc getEndLocImpl() const { return Brackets.End; }
702-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
710+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
711+
NonRecursivePrintOptions nrOpts) const;
703712
friend class TypeRepr;
704713
};
705714

@@ -731,7 +740,8 @@ class OptionalTypeRepr : public TypeRepr {
731740
SourceLoc getLocImpl() const {
732741
return QuestionLoc.isValid() ? QuestionLoc : Base->getLoc();
733742
}
734-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
743+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
744+
NonRecursivePrintOptions nrOpts) const;
735745
friend class TypeRepr;
736746
};
737747

@@ -760,7 +770,8 @@ class ImplicitlyUnwrappedOptionalTypeRepr : public TypeRepr {
760770
SourceLoc getStartLocImpl() const { return Base->getStartLoc(); }
761771
SourceLoc getEndLocImpl() const { return ExclamationLoc; }
762772
SourceLoc getLocImpl() const { return ExclamationLoc; }
763-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
773+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
774+
NonRecursivePrintOptions nrOpts) const;
764775
friend class TypeRepr;
765776
};
766777

@@ -801,7 +812,8 @@ class VarargTypeRepr final : public TypeRepr {
801812
SourceLoc getStartLocImpl() const { return Element->getEndLoc(); }
802813
SourceLoc getEndLocImpl() const { return EllipsisLoc; }
803814
SourceLoc getLocImpl() const { return EllipsisLoc; }
804-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
815+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
816+
NonRecursivePrintOptions nrOpts) const;
805817
friend class TypeRepr;
806818
};
807819

@@ -832,7 +844,8 @@ class PackExpansionTypeRepr final : public TypeRepr {
832844
SourceLoc getStartLocImpl() const { return RepeatLoc; }
833845
SourceLoc getEndLocImpl() const { return Pattern->getEndLoc(); }
834846
SourceLoc getLocImpl() const { return RepeatLoc; }
835-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
847+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
848+
NonRecursivePrintOptions nrOpts) const;
836849
friend class TypeRepr;
837850
};
838851

@@ -880,7 +893,8 @@ class PackTypeRepr final
880893
SourceLoc getStartLocImpl() const { return KeywordLoc; }
881894
SourceLoc getEndLocImpl() const { return BraceLocs.End; }
882895
SourceLoc getLocImpl() const { return KeywordLoc; }
883-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
896+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
897+
NonRecursivePrintOptions nrOpts) const;
884898
friend class TypeRepr;
885899
};
886900

@@ -915,7 +929,8 @@ class PackElementTypeRepr: public TypeRepr {
915929
SourceLoc getStartLocImpl() const { return EachLoc; }
916930
SourceLoc getEndLocImpl() const { return PackType->getEndLoc(); }
917931
SourceLoc getLocImpl() const { return EachLoc; }
918-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
932+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
933+
NonRecursivePrintOptions nrOpts) const;
919934
friend class TypeRepr;
920935
};
921936

@@ -1010,7 +1025,8 @@ class TupleTypeRepr final : public TypeRepr,
10101025
private:
10111026
SourceLoc getStartLocImpl() const { return Parens.Start; }
10121027
SourceLoc getEndLocImpl() const { return Parens.End; }
1013-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1028+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1029+
NonRecursivePrintOptions nrOpts) const;
10141030
friend class TypeRepr;
10151031
};
10161032

@@ -1066,7 +1082,8 @@ class CompositionTypeRepr final : public TypeRepr,
10661082
SourceLoc getStartLocImpl() const { return FirstTypeLoc; }
10671083
SourceLoc getLocImpl() const { return CompositionRange.Start; }
10681084
SourceLoc getEndLocImpl() const { return CompositionRange.End; }
1069-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1085+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1086+
NonRecursivePrintOptions nrOpts) const;
10701087
friend class TypeRepr;
10711088
};
10721089

@@ -1095,7 +1112,8 @@ class MetatypeTypeRepr : public TypeRepr {
10951112
SourceLoc getStartLocImpl() const { return Base->getStartLoc(); }
10961113
SourceLoc getEndLocImpl() const { return MetaLoc; }
10971114
SourceLoc getLocImpl() const { return MetaLoc; }
1098-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1115+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1116+
NonRecursivePrintOptions nrOpts) const;
10991117
friend class TypeRepr;
11001118
};
11011119

@@ -1124,7 +1142,8 @@ class ProtocolTypeRepr : public TypeRepr {
11241142
SourceLoc getStartLocImpl() const { return Base->getStartLoc(); }
11251143
SourceLoc getEndLocImpl() const { return ProtocolLoc; }
11261144
SourceLoc getLocImpl() const { return ProtocolLoc; }
1127-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1145+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1146+
NonRecursivePrintOptions nrOpts) const;
11281147
friend class TypeRepr;
11291148
};
11301149

@@ -1155,7 +1174,8 @@ class SpecifierTypeRepr : public TypeRepr {
11551174
private:
11561175
SourceLoc getStartLocImpl() const { return SpecifierLoc; }
11571176
SourceLoc getEndLocImpl() const { return Base->getEndLoc(); }
1158-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1177+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1178+
NonRecursivePrintOptions nrOpts) const;
11591179
friend class TypeRepr;
11601180
};
11611181

@@ -1274,7 +1294,8 @@ class CallerIsolatedTypeRepr : public TypeRepr {
12741294
SourceLoc getStartLocImpl() const { return Loc; }
12751295
SourceLoc getEndLocImpl() const { return Base->getEndLoc(); }
12761296
SourceLoc getLocImpl() const { return Base->getLoc(); }
1277-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1297+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1298+
NonRecursivePrintOptions nrOpts) const;
12781299
friend class TypeRepr;
12791300
};
12801301

@@ -1311,7 +1332,8 @@ class FixedTypeRepr : public TypeRepr {
13111332
private:
13121333
SourceLoc getStartLocImpl() const { return Loc; }
13131334
SourceLoc getEndLocImpl() const { return Loc; }
1314-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1335+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1336+
NonRecursivePrintOptions nrOpts) const;
13151337
friend class TypeRepr;
13161338
};
13171339

@@ -1338,7 +1360,8 @@ class SelfTypeRepr : public TypeRepr {
13381360
private:
13391361
SourceLoc getStartLocImpl() const { return Loc; }
13401362
SourceLoc getEndLocImpl() const { return Loc; }
1341-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1363+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1364+
NonRecursivePrintOptions nrOpts) const;
13421365
friend class TypeRepr;
13431366
};
13441367

@@ -1391,7 +1414,8 @@ class OpaqueReturnTypeRepr : public TypeRepr {
13911414
SourceLoc getStartLocImpl() const { return OpaqueLoc; }
13921415
SourceLoc getEndLocImpl() const { return Constraint->getEndLoc(); }
13931416
SourceLoc getLocImpl() const { return OpaqueLoc; }
1394-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1417+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1418+
NonRecursivePrintOptions nrOpts) const;
13951419
friend class TypeRepr;
13961420
};
13971421

@@ -1426,7 +1450,8 @@ class NamedOpaqueReturnTypeRepr : public TypeRepr {
14261450
SourceLoc getStartLocImpl() const;
14271451
SourceLoc getEndLocImpl() const;
14281452
SourceLoc getLocImpl() const;
1429-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1453+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1454+
NonRecursivePrintOptions nrOpts) const;
14301455
friend class TypeRepr;
14311456
};
14321457

@@ -1455,7 +1480,8 @@ class ExistentialTypeRepr: public TypeRepr {
14551480
SourceLoc getStartLocImpl() const { return AnyLoc; }
14561481
SourceLoc getEndLocImpl() const { return Constraint->getEndLoc(); }
14571482
SourceLoc getLocImpl() const { return AnyLoc; }
1458-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1483+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1484+
NonRecursivePrintOptions nrOpts) const;
14591485
friend class TypeRepr;
14601486
};
14611487

@@ -1483,7 +1509,8 @@ class InverseTypeRepr : public TypeRepr {
14831509
SourceLoc getStartLocImpl() const { return TildeLoc; }
14841510
SourceLoc getEndLocImpl() const { return Constraint->getEndLoc(); }
14851511
SourceLoc getLocImpl() const { return TildeLoc; }
1486-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1512+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1513+
NonRecursivePrintOptions nrOpts) const;
14871514
friend class TypeRepr;
14881515
};
14891516

@@ -1511,7 +1538,8 @@ class PlaceholderTypeRepr: public TypeRepr {
15111538
SourceLoc getStartLocImpl() const { return UnderscoreLoc; }
15121539
SourceLoc getEndLocImpl() const { return UnderscoreLoc; }
15131540
SourceLoc getLocImpl() const { return UnderscoreLoc; }
1514-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1541+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1542+
NonRecursivePrintOptions nrOpts) const;
15151543
friend class TypeRepr;
15161544
};
15171545

@@ -1600,7 +1628,8 @@ class SILBoxTypeRepr final : public TypeRepr,
16001628
SourceLoc getStartLocImpl() const;
16011629
SourceLoc getEndLocImpl() const;
16021630
SourceLoc getLocImpl() const;
1603-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1631+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1632+
NonRecursivePrintOptions nrOpts) const;
16041633
friend TypeRepr;
16051634
};
16061635

@@ -1627,7 +1656,8 @@ class LifetimeDependentTypeRepr final : public SpecifierTypeRepr {
16271656
SourceLoc getStartLocImpl() const;
16281657
SourceLoc getEndLocImpl() const;
16291658
SourceLoc getLocImpl() const;
1630-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1659+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1660+
NonRecursivePrintOptions nrOpts) const;
16311661
friend class TypeRepr;
16321662
};
16331663

@@ -1668,7 +1698,8 @@ class IntegerTypeRepr final : public TypeRepr {
16681698

16691699
SourceLoc getEndLocImpl() const { return Loc; }
16701700
SourceLoc getLocImpl() const { return Loc; }
1671-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1701+
void printImpl(ASTPrinter &Printer, const PrintOptions &opts,
1702+
NonRecursivePrintOptions nrOpts) const;
16721703
friend class TypeRepr;
16731704
};
16741705

0 commit comments

Comments
 (0)