Skip to content

Commit e8261fb

Browse files
Rollup merge of rust-lang#144949 - nnethercote:more-Printer-cleanups, r=davidtwco
More `Printer` cleanups A sequel to rust-lang#144776. r? `@davidtwco`
2 parents 2da75af + 42a1042 commit e8261fb

File tree

9 files changed

+186
-155
lines changed

9 files changed

+186
-155
lines changed

compiler/rustc_const_eval/src/util/type_name.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ use rustc_middle::bug;
77
use rustc_middle::ty::print::{PrettyPrinter, PrintError, Printer};
88
use rustc_middle::ty::{self, GenericArg, GenericArgKind, Ty, TyCtxt};
99

10-
struct AbsolutePathPrinter<'tcx> {
10+
struct TypeNamePrinter<'tcx> {
1111
tcx: TyCtxt<'tcx>,
1212
path: String,
1313
}
1414

15-
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
15+
impl<'tcx> Printer<'tcx> for TypeNamePrinter<'tcx> {
1616
fn tcx(&self) -> TyCtxt<'tcx> {
1717
self.tcx
1818
}
@@ -75,26 +75,26 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
7575
self.pretty_print_dyn_existential(predicates)
7676
}
7777

78-
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
78+
fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
7979
self.path.push_str(self.tcx.crate_name(cnum).as_str());
8080
Ok(())
8181
}
8282

83-
fn path_qualified(
83+
fn print_path_with_qualified(
8484
&mut self,
8585
self_ty: Ty<'tcx>,
8686
trait_ref: Option<ty::TraitRef<'tcx>>,
8787
) -> Result<(), PrintError> {
88-
self.pretty_path_qualified(self_ty, trait_ref)
88+
self.pretty_print_path_with_qualified(self_ty, trait_ref)
8989
}
9090

91-
fn path_append_impl(
91+
fn print_path_with_impl(
9292
&mut self,
9393
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
9494
self_ty: Ty<'tcx>,
9595
trait_ref: Option<ty::TraitRef<'tcx>>,
9696
) -> Result<(), PrintError> {
97-
self.pretty_path_append_impl(
97+
self.pretty_print_path_with_impl(
9898
|cx| {
9999
print_prefix(cx)?;
100100

@@ -107,7 +107,7 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
107107
)
108108
}
109109

110-
fn path_append(
110+
fn print_path_with_simple(
111111
&mut self,
112112
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
113113
disambiguated_data: &DisambiguatedDefPathData,
@@ -119,7 +119,7 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
119119
Ok(())
120120
}
121121

122-
fn path_generic_args(
122+
fn print_path_with_generic_args(
123123
&mut self,
124124
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
125125
args: &[GenericArg<'tcx>],
@@ -135,7 +135,7 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
135135
}
136136
}
137137

138-
impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {
138+
impl<'tcx> PrettyPrinter<'tcx> for TypeNamePrinter<'tcx> {
139139
fn should_print_region(&self, _region: ty::Region<'_>) -> bool {
140140
false
141141
}
@@ -159,15 +159,15 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {
159159
}
160160
}
161161

162-
impl Write for AbsolutePathPrinter<'_> {
162+
impl Write for TypeNamePrinter<'_> {
163163
fn write_str(&mut self, s: &str) -> std::fmt::Result {
164164
self.path.push_str(s);
165165
Ok(())
166166
}
167167
}
168168

169169
pub fn type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> String {
170-
let mut p = AbsolutePathPrinter { tcx, path: String::new() };
170+
let mut p = TypeNamePrinter { tcx, path: String::new() };
171171
p.print_type(ty).unwrap();
172172
p.path
173173
}

compiler/rustc_lint/src/context.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -745,12 +745,12 @@ impl<'tcx> LateContext<'tcx> {
745745
/// }
746746
/// ```
747747
pub fn get_def_path(&self, def_id: DefId) -> Vec<Symbol> {
748-
struct AbsolutePathPrinter<'tcx> {
748+
struct LintPathPrinter<'tcx> {
749749
tcx: TyCtxt<'tcx>,
750750
path: Vec<Symbol>,
751751
}
752752

753-
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
753+
impl<'tcx> Printer<'tcx> for LintPathPrinter<'tcx> {
754754
fn tcx(&self) -> TyCtxt<'tcx> {
755755
self.tcx
756756
}
@@ -774,12 +774,12 @@ impl<'tcx> LateContext<'tcx> {
774774
unreachable!(); // because `path_generic_args` ignores the `GenericArgs`
775775
}
776776

777-
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
777+
fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
778778
self.path = vec![self.tcx.crate_name(cnum)];
779779
Ok(())
780780
}
781781

782-
fn path_qualified(
782+
fn print_path_with_qualified(
783783
&mut self,
784784
self_ty: Ty<'tcx>,
785785
trait_ref: Option<ty::TraitRef<'tcx>>,
@@ -800,7 +800,7 @@ impl<'tcx> LateContext<'tcx> {
800800
})
801801
}
802802

803-
fn path_append_impl(
803+
fn print_path_with_impl(
804804
&mut self,
805805
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
806806
self_ty: Ty<'tcx>,
@@ -825,7 +825,7 @@ impl<'tcx> LateContext<'tcx> {
825825
Ok(())
826826
}
827827

828-
fn path_append(
828+
fn print_path_with_simple(
829829
&mut self,
830830
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
831831
disambiguated_data: &DisambiguatedDefPathData,
@@ -844,7 +844,7 @@ impl<'tcx> LateContext<'tcx> {
844844
Ok(())
845845
}
846846

847-
fn path_generic_args(
847+
fn print_path_with_generic_args(
848848
&mut self,
849849
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
850850
_args: &[GenericArg<'tcx>],
@@ -853,7 +853,7 @@ impl<'tcx> LateContext<'tcx> {
853853
}
854854
}
855855

856-
let mut p = AbsolutePathPrinter { tcx: self.tcx, path: vec![] };
856+
let mut p = LintPathPrinter { tcx: self.tcx, path: vec![] };
857857
p.print_def_path(def_id, &[]).unwrap();
858858
p.path
859859
}

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,7 +1932,7 @@ fn pretty_print_const_value_tcx<'tcx>(
19321932
let args = tcx.lift(args).unwrap();
19331933
let mut p = FmtPrinter::new(tcx, Namespace::ValueNS);
19341934
p.print_alloc_ids = true;
1935-
p.print_value_path(variant_def.def_id, args)?;
1935+
p.pretty_print_value_path(variant_def.def_id, args)?;
19361936
fmt.write_str(&p.into_buffer())?;
19371937

19381938
match variant_def.ctor_kind() {
@@ -1974,7 +1974,7 @@ fn pretty_print_const_value_tcx<'tcx>(
19741974
(ConstValue::ZeroSized, ty::FnDef(d, s)) => {
19751975
let mut p = FmtPrinter::new(tcx, Namespace::ValueNS);
19761976
p.print_alloc_ids = true;
1977-
p.print_value_path(*d, s)?;
1977+
p.pretty_print_value_path(*d, s)?;
19781978
fmt.write_str(&p.into_buffer())?;
19791979
return Ok(());
19801980
}

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

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,16 @@ pub trait Print<'tcx, P> {
1919
fn print(&self, p: &mut P) -> Result<(), PrintError>;
2020
}
2121

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

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

40+
/// Like `print_def_path`, but for `DefPathData::Impl`.
4241
fn print_impl_path(
4342
&mut self,
4443
impl_def_id: DefId,
@@ -64,48 +63,67 @@ pub trait Printer<'tcx>: Sized {
6463
self.default_print_impl_path(impl_def_id, self_ty, impl_trait_ref)
6564
}
6665

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

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

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

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

78-
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError>;
81+
/// Appends a representation of a crate name, e.g. `std`, or even ``.
82+
fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError>;
7983

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

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

93-
fn path_append(
107+
/// Appends a representation of a path ending in generic args, in two parts. `print_prefix`,
108+
/// when called, appends the leading segments. The rest of the method appends the
109+
/// representation of the generic args. (Some printers choose to skip appending the generic
110+
/// args.)
111+
///
112+
/// E.g. `ImplementsTraitForUsize` + `<usize>` -> `ImplementsTraitForUsize<usize>`.
113+
fn print_path_with_generic_args(
94114
&mut self,
95115
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
96-
disambiguated_data: &DisambiguatedDefPathData,
116+
args: &[GenericArg<'tcx>],
97117
) -> Result<(), PrintError>;
98118

99-
fn path_generic_args(
119+
/// Appends a representation of a qualified path segment, e.g. `<OsString as From<&T>>`.
120+
/// If `trait_ref` is `None`, it may fall back to simpler forms, e.g. `<Vec<T>>` or just `Foo`.
121+
fn print_path_with_qualified(
100122
&mut self,
101-
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
102-
args: &[GenericArg<'tcx>],
123+
self_ty: Ty<'tcx>,
124+
trait_ref: Option<ty::TraitRef<'tcx>>,
103125
) -> Result<(), PrintError>;
104126

105-
fn should_truncate(&mut self) -> bool {
106-
false
107-
}
108-
109127
// Defaults (should not be overridden):
110128

111129
#[instrument(skip(self), level = "debug")]
@@ -120,7 +138,7 @@ pub trait Printer<'tcx>: Sized {
120138
match key.disambiguated_data.data {
121139
DefPathData::CrateRoot => {
122140
assert!(key.parent.is_none());
123-
self.path_crate(def_id.krate)
141+
self.print_crate_name(def_id.krate)
124142
}
125143

126144
DefPathData::Impl => self.print_impl_path(def_id, args),
@@ -144,7 +162,7 @@ pub trait Printer<'tcx>: Sized {
144162
)) = self.tcx().coroutine_kind(def_id)
145163
&& args.len() > parent_args.len()
146164
{
147-
return self.path_generic_args(
165+
return self.print_path_with_generic_args(
148166
|p| p.print_def_path(def_id, parent_args),
149167
&args[..parent_args.len() + 1][..1],
150168
);
@@ -166,7 +184,7 @@ pub trait Printer<'tcx>: Sized {
166184
_ => {
167185
if !generics.is_own_empty() && args.len() >= generics.count() {
168186
let args = generics.own_args_no_defaults(self.tcx(), args);
169-
return self.path_generic_args(
187+
return self.print_path_with_generic_args(
170188
|p| p.print_def_path(def_id, parent_args),
171189
args,
172190
);
@@ -182,15 +200,15 @@ pub trait Printer<'tcx>: Sized {
182200
&& self.tcx().generics_of(parent_def_id).parent_count == 0;
183201
}
184202

185-
self.path_append(
203+
self.print_path_with_simple(
186204
|p: &mut Self| {
187205
if trait_qualify_parent {
188206
let trait_ref = ty::TraitRef::new(
189207
p.tcx(),
190208
parent_def_id,
191209
parent_args.iter().copied(),
192210
);
193-
p.path_qualified(trait_ref.self_ty(), Some(trait_ref))
211+
p.print_path_with_qualified(trait_ref.self_ty(), Some(trait_ref))
194212
} else {
195213
p.print_def_path(parent_def_id, parent_args)
196214
}
@@ -233,11 +251,15 @@ pub trait Printer<'tcx>: Sized {
233251
// If the impl is not co-located with either self-type or
234252
// trait-type, then fallback to a format that identifies
235253
// the module more clearly.
236-
self.path_append_impl(|p| p.print_def_path(parent_def_id, &[]), self_ty, impl_trait_ref)
254+
self.print_path_with_impl(
255+
|p| p.print_def_path(parent_def_id, &[]),
256+
self_ty,
257+
impl_trait_ref,
258+
)
237259
} else {
238260
// Otherwise, try to give a good form that would be valid language
239261
// syntax. Preferably using associated item notation.
240-
self.path_qualified(self_ty, impl_trait_ref)
262+
self.print_path_with_qualified(self_ty, impl_trait_ref)
241263
}
242264
}
243265
}

0 commit comments

Comments
 (0)