Skip to content

Commit 87d5894

Browse files
committed
Add comments to Printer.
These details took me some time to work out.
1 parent d76eef4 commit 87d5894

File tree

1 file changed

+39
-17
lines changed
  • compiler/rustc_middle/src/ty/print

1 file changed

+39
-17
lines changed

compiler/rustc_middle/src/ty/print/mod.rs

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,16 @@ pub trait Print<'tcx, P> {
2121
fn print(&self, p: &mut P) -> Result<(), PrintError>;
2222
}
2323

24-
/// Interface for outputting user-facing "type-system entities"
25-
/// (paths, types, lifetimes, constants, etc.) as a side-effect
26-
/// (e.g. formatting, like `PrettyPrinter` implementors do) or by
27-
/// constructing some alternative representation (e.g. an AST),
28-
/// which the associated types allow passing through the methods.
29-
///
30-
/// For pretty-printing/formatting in particular, see `PrettyPrinter`.
31-
//
32-
// FIXME(eddyb) find a better name; this is more general than "printing".
24+
/// A trait that "prints" user-facing type system entities: paths, types, lifetimes, constants,
25+
/// etc. "Printing" here means building up a representation of the entity's path, usually as a
26+
/// `String` (e.g. "std::io::Read") or a `Vec<Symbol>` (e.g. `[sym::std, sym::io, sym::Read]`). The
27+
/// representation is built up by appending one or more pieces. The specific details included in
28+
/// the built-up representation depend on the purpose of the printer. The more advanced printers
29+
/// also rely on the `PrettyPrinter` sub-trait.
3330
pub trait Printer<'tcx>: Sized {
3431
fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
3532

33+
/// Appends a representation of an entity with a normal path, e.g. "std::io::Read".
3634
fn print_def_path(
3735
&mut self,
3836
def_id: DefId,
@@ -41,6 +39,7 @@ pub trait Printer<'tcx>: Sized {
4139
self.default_print_def_path(def_id, args)
4240
}
4341

42+
/// Like `print_def_path`, but for `DefPathData::Impl`.
4443
fn print_impl_path(
4544
&mut self,
4645
impl_def_id: DefId,
@@ -66,42 +65,65 @@ pub trait Printer<'tcx>: Sized {
6665
self.default_print_impl_path(impl_def_id, self_ty, impl_trait_ref)
6766
}
6867

68+
/// Appends a representation of a region.
6969
fn print_region(&mut self, region: ty::Region<'tcx>) -> Result<(), PrintError>;
7070

71+
/// Appends a representation of a type.
7172
fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError>;
7273

74+
/// Appends a representation of a list of `PolyExistentialPredicate`s.
7375
fn print_dyn_existential(
7476
&mut self,
7577
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
7678
) -> Result<(), PrintError>;
7779

80+
/// Appends a representation of a const.
7881
fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError>;
7982

83+
/// Appends a representation of a crate name, e.g. "std", or even "".
8084
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError>;
8185

82-
fn path_qualified(
86+
/// Appends a representation of a (full or partial) simple path, in two parts. `print_prefix`,
87+
/// when called, appends the representation of the leading segments. The rest of the method
88+
/// appends the representation of the final segment, the details of which are in
89+
/// `disambiguated_data`.
90+
///
91+
/// E.g. "std::io" + "Read" -> "std::io::Read".
92+
fn path_append(
8393
&mut self,
84-
self_ty: Ty<'tcx>,
85-
trait_ref: Option<ty::TraitRef<'tcx>>,
94+
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
95+
disambiguated_data: &DisambiguatedDefPathData,
8696
) -> Result<(), PrintError>;
8797

98+
/// Similar to `path_append`, but the final segment is an `impl` segment.
99+
///
100+
/// E.g. "slice" + "<impl [T]>" -> "slice::<impl [T]>", which may then be further appended to,
101+
/// giving a longer path representation such as "slice::<impl [T]>::to_vec_in::ConvertVec".
88102
fn path_append_impl(
89103
&mut self,
90104
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
91105
self_ty: Ty<'tcx>,
92106
trait_ref: Option<ty::TraitRef<'tcx>>,
93107
) -> Result<(), PrintError>;
94108

95-
fn path_append(
109+
/// Appends a representation of a path ending in generic args, in two parts. `print_prefix`,
110+
/// when called, appends the leading segments. The rest of the method appends the
111+
/// representation of the generic args. (Some printers choose to skip appending the generic
112+
/// args.)
113+
///
114+
/// E.g. "ImplementsTraitForUsize" + "<usize>" -> "ImplementsTraitForUsize<usize>".
115+
fn path_generic_args(
96116
&mut self,
97117
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
98-
disambiguated_data: &DisambiguatedDefPathData,
118+
args: &[GenericArg<'tcx>],
99119
) -> Result<(), PrintError>;
100120

101-
fn path_generic_args(
121+
/// Appends a representation of a qualified path segment, e.g. "<OsString as From<&T>>".
122+
/// If `trait_ref` is `None`, it may fall back to simpler forms, e.g. "<Vec<T>>" or just "Foo".
123+
fn path_qualified(
102124
&mut self,
103-
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
104-
args: &[GenericArg<'tcx>],
125+
self_ty: Ty<'tcx>,
126+
trait_ref: Option<ty::TraitRef<'tcx>>,
105127
) -> Result<(), PrintError>;
106128

107129
fn should_truncate(&mut self) -> bool {

0 commit comments

Comments
 (0)