Rename some Printer methods.

I find these name clearer, and starting them all with `print_` makes
things more consistent.
This commit is contained in:
Nicholas Nethercote 2025-08-03 21:02:28 +10:00
parent 69bcd79ab7
commit b8adda6194
8 changed files with 71 additions and 61 deletions

View file

@ -73,12 +73,12 @@ impl<'tcx> Printer<'tcx> for TypeNamePrinter<'tcx> {
self.pretty_print_dyn_existential(predicates)
}
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
self.path.push_str(self.tcx.crate_name(cnum).as_str());
Ok(())
}
fn path_qualified(
fn print_path_with_qualified(
&mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
@ -86,7 +86,7 @@ impl<'tcx> Printer<'tcx> for TypeNamePrinter<'tcx> {
self.pretty_path_qualified(self_ty, trait_ref)
}
fn path_append_impl(
fn print_path_with_impl(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
self_ty: Ty<'tcx>,
@ -105,7 +105,7 @@ impl<'tcx> Printer<'tcx> for TypeNamePrinter<'tcx> {
)
}
fn path_append(
fn print_path_with_simple(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
disambiguated_data: &DisambiguatedDefPathData,
@ -117,7 +117,7 @@ impl<'tcx> Printer<'tcx> for TypeNamePrinter<'tcx> {
Ok(())
}
fn path_generic_args(
fn print_path_with_generic_args(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
args: &[GenericArg<'tcx>],

View file

@ -774,12 +774,12 @@ impl<'tcx> LateContext<'tcx> {
unreachable!(); // because `path_generic_args` ignores the `GenericArgs`
}
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
self.path = vec![self.tcx.crate_name(cnum)];
Ok(())
}
fn path_qualified(
fn print_path_with_qualified(
&mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
@ -800,7 +800,7 @@ impl<'tcx> LateContext<'tcx> {
})
}
fn path_append_impl(
fn print_path_with_impl(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
self_ty: Ty<'tcx>,
@ -825,7 +825,7 @@ impl<'tcx> LateContext<'tcx> {
Ok(())
}
fn path_append(
fn print_path_with_simple(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
disambiguated_data: &DisambiguatedDefPathData,
@ -844,7 +844,7 @@ impl<'tcx> LateContext<'tcx> {
Ok(())
}
fn path_generic_args(
fn print_path_with_generic_args(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
_args: &[GenericArg<'tcx>],

View file

@ -81,7 +81,7 @@ pub trait Printer<'tcx>: Sized {
fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError>;
/// Appends a representation of a crate name, e.g. `std`, or even ``.
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError>;
fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError>;
/// Appends a representation of a (full or partial) simple path, in two parts. `print_prefix`,
/// when called, appends the representation of the leading segments. The rest of the method
@ -89,17 +89,17 @@ pub trait Printer<'tcx>: Sized {
/// `disambiguated_data`.
///
/// E.g. `std::io` + `Read` -> `std::io::Read`.
fn path_append(
fn print_path_with_simple(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<(), PrintError>;
/// Similar to `path_append`, but the final segment is an `impl` segment.
/// Similar to `print_path_with_simple`, but the final segment is an `impl` segment.
///
/// E.g. `slice` + `<impl [T]>` -> `slice::<impl [T]>`, which may then be further appended to,
/// giving a longer path representation such as `slice::<impl [T]>::to_vec_in::ConvertVec`.
fn path_append_impl(
fn print_path_with_impl(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
self_ty: Ty<'tcx>,
@ -112,7 +112,7 @@ pub trait Printer<'tcx>: Sized {
/// args.)
///
/// E.g. `ImplementsTraitForUsize` + `<usize>` -> `ImplementsTraitForUsize<usize>`.
fn path_generic_args(
fn print_path_with_generic_args(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
args: &[GenericArg<'tcx>],
@ -120,7 +120,7 @@ pub trait Printer<'tcx>: Sized {
/// Appends a representation of a qualified path segment, e.g. `<OsString as From<&T>>`.
/// If `trait_ref` is `None`, it may fall back to simpler forms, e.g. `<Vec<T>>` or just `Foo`.
fn path_qualified(
fn print_path_with_qualified(
&mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
@ -140,7 +140,7 @@ pub trait Printer<'tcx>: Sized {
match key.disambiguated_data.data {
DefPathData::CrateRoot => {
assert!(key.parent.is_none());
self.path_crate(def_id.krate)
self.print_crate_name(def_id.krate)
}
DefPathData::Impl => self.print_impl_path(def_id, args),
@ -164,7 +164,7 @@ pub trait Printer<'tcx>: Sized {
)) = self.tcx().coroutine_kind(def_id)
&& args.len() > parent_args.len()
{
return self.path_generic_args(
return self.print_path_with_generic_args(
|p| p.print_def_path(def_id, parent_args),
&args[..parent_args.len() + 1][..1],
);
@ -186,7 +186,7 @@ pub trait Printer<'tcx>: Sized {
_ => {
if !generics.is_own_empty() && args.len() >= generics.count() {
let args = generics.own_args_no_defaults(self.tcx(), args);
return self.path_generic_args(
return self.print_path_with_generic_args(
|p| p.print_def_path(def_id, parent_args),
args,
);
@ -202,7 +202,7 @@ pub trait Printer<'tcx>: Sized {
&& self.tcx().generics_of(parent_def_id).parent_count == 0;
}
self.path_append(
self.print_path_with_simple(
|p: &mut Self| {
if trait_qualify_parent {
let trait_ref = ty::TraitRef::new(
@ -210,7 +210,7 @@ pub trait Printer<'tcx>: Sized {
parent_def_id,
parent_args.iter().copied(),
);
p.path_qualified(trait_ref.self_ty(), Some(trait_ref))
p.print_path_with_qualified(trait_ref.self_ty(), Some(trait_ref))
} else {
p.print_def_path(parent_def_id, parent_args)
}
@ -253,11 +253,15 @@ pub trait Printer<'tcx>: Sized {
// If the impl is not co-located with either self-type or
// trait-type, then fallback to a format that identifies
// the module more clearly.
self.path_append_impl(|p| p.print_def_path(parent_def_id, &[]), self_ty, impl_trait_ref)
self.print_path_with_impl(
|p| p.print_def_path(parent_def_id, &[]),
self_ty,
impl_trait_ref,
)
} else {
// Otherwise, try to give a good form that would be valid language
// syntax. Preferably using associated item notation.
self.path_qualified(self_ty, impl_trait_ref)
self.print_path_with_qualified(self_ty, impl_trait_ref)
}
}
}

View file

@ -474,7 +474,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
// path to the crate followed by the path to the item within the crate.
if let Some(cnum) = def_id.as_crate_root() {
if cnum == LOCAL_CRATE {
self.path_crate(cnum)?;
self.print_crate_name(cnum)?;
return Ok(true);
}
@ -498,7 +498,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
// or avoid ending up with `ExternCrateSource::Extern`,
// for the injected `std`/`core`.
if span.is_dummy() {
self.path_crate(cnum)?;
self.print_crate_name(cnum)?;
return Ok(true);
}
@ -512,13 +512,13 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
return Ok(true);
}
(ExternCrateSource::Path, LOCAL_CRATE) => {
self.path_crate(cnum)?;
self.print_crate_name(cnum)?;
return Ok(true);
}
_ => {}
},
None => {
self.path_crate(cnum)?;
self.print_crate_name(cnum)?;
return Ok(true);
}
}
@ -628,7 +628,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
return Ok(false);
}
callers.push(visible_parent);
// HACK(eddyb) this bypasses `path_append`'s prefix printing to avoid
// HACK(eddyb) this bypasses `print_path_with_simple`'s prefix printing to avoid
// knowing ahead of time whether the entire path will succeed or not.
// To support printers that do not implement `PrettyPrinter`, a `Vec` or
// linked list on the stack would need to be built, before any printing.
@ -637,7 +637,10 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
true => {}
}
callers.pop();
self.path_append(|_| Ok(()), &DisambiguatedDefPathData { data, disambiguator: 0 })?;
self.print_path_with_simple(
|_| Ok(()),
&DisambiguatedDefPathData { data, disambiguator: 0 },
)?;
Ok(true)
}
@ -1312,10 +1315,10 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
alias_ty: ty::AliasTerm<'tcx>,
) -> Result<(), PrintError> {
let def_key = self.tcx().def_key(alias_ty.def_id);
self.path_generic_args(
self.print_path_with_generic_args(
|p| {
p.path_append(
|p| p.path_qualified(alias_ty.self_ty(), None),
p.print_path_with_simple(
|p| p.print_path_with_qualified(alias_ty.self_ty(), None),
&def_key.disambiguated_data,
)
},
@ -1400,7 +1403,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
}
}
// HACK(eddyb) this duplicates `FmtPrinter`'s `path_generic_args`,
// HACK(eddyb) this duplicates `FmtPrinter`'s `print_path_with_generic_args`,
// in order to place the projections inside the `<...>`.
if !resugared {
let principal_with_self =
@ -2234,7 +2237,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
self.print_def_path(parent_def_id, &[])?;
// HACK(eddyb) copy of `path_append` to avoid
// HACK(eddyb) copy of `print_path_with_simple` to avoid
// constructing a `DisambiguatedDefPathData`.
if !self.empty_path {
write!(self, "::")?;
@ -2310,7 +2313,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
self.pretty_print_const(ct, false)
}
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
self.empty_path = true;
if cnum == LOCAL_CRATE {
if self.tcx.sess.at_least_rust_2018() {
@ -2327,7 +2330,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
Ok(())
}
fn path_qualified(
fn print_path_with_qualified(
&mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
@ -2337,7 +2340,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
Ok(())
}
fn path_append_impl(
fn print_path_with_impl(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
self_ty: Ty<'tcx>,
@ -2359,7 +2362,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
Ok(())
}
fn path_append(
fn print_path_with_simple(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
disambiguated_data: &DisambiguatedDefPathData,
@ -2390,7 +2393,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
Ok(())
}
fn path_generic_args(
fn print_path_with_generic_args(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
args: &[GenericArg<'tcx>],
@ -3229,7 +3232,7 @@ define_print! {
// The args don't contain the self ty (as it has been erased) but the corresp.
// generics do as the trait always has a self ty param. We need to offset.
let args = &self.args[p.tcx().generics_of(self.def_id).parent_count - 1..];
p.path_generic_args(|p| write!(p, "{name}"), args)?;
p.print_path_with_generic_args(|p| write!(p, "{name}"), args)?;
write!(p, " = ")?;
self.term.print(p)?;
}

View file

@ -218,8 +218,8 @@ struct LegacySymbolMangler<'tcx> {
path: SymbolPath,
// When `true`, `finalize_pending_component` isn't used.
// This is needed when recursing into `path_qualified`,
// or `path_generic_args`, as any nested paths are
// This is needed when recursing into `print_path_with_qualified`,
// or `print_path_with_generic_args`, as any nested paths are
// logically within one component.
keep_within_component: bool,
}
@ -303,11 +303,12 @@ impl<'tcx> Printer<'tcx> for LegacySymbolMangler<'tcx> {
Ok(())
}
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
self.write_str(self.tcx.crate_name(cnum).as_str())?;
Ok(())
}
fn path_qualified(
fn print_path_with_qualified(
&mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
@ -329,7 +330,7 @@ impl<'tcx> Printer<'tcx> for LegacySymbolMangler<'tcx> {
}
}
fn path_append_impl(
fn print_path_with_impl(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
self_ty: Ty<'tcx>,
@ -352,7 +353,8 @@ impl<'tcx> Printer<'tcx> for LegacySymbolMangler<'tcx> {
trait_ref,
)
}
fn path_append(
fn print_path_with_simple(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
disambiguated_data: &DisambiguatedDefPathData,
@ -375,7 +377,8 @@ impl<'tcx> Printer<'tcx> for LegacySymbolMangler<'tcx> {
Ok(())
}
fn path_generic_args(
fn print_path_with_generic_args(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
args: &[GenericArg<'tcx>],

View file

@ -365,7 +365,7 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> {
// Encode impl generic params if the generic parameters contain non-region parameters
// and this isn't an inherent impl.
if impl_trait_ref.is_some() && args.iter().any(|a| a.has_non_region_param()) {
self.path_generic_args(
self.print_path_with_generic_args(
|this| {
this.path_append_ns(
|p| p.print_def_path(parent_def_id, &[]),
@ -786,7 +786,7 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> {
None => {
self.push("S");
for (field_def, field) in iter::zip(&variant_def.fields, fields) {
// HACK(eddyb) this mimics `path_append`,
// HACK(eddyb) this mimics `print_path_with_simple`,
// instead of simply using `field_def.ident`,
// just to be able to handle disambiguators.
let disambiguated_field =
@ -819,7 +819,7 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> {
Ok(())
}
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
self.push("C");
if !self.is_exportable {
let stable_crate_id = self.tcx.def_path_hash(cnum.as_def_id()).stable_crate_id();
@ -830,7 +830,7 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> {
Ok(())
}
fn path_qualified(
fn print_path_with_qualified(
&mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
@ -843,7 +843,7 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> {
self.print_def_path(trait_ref.def_id, trait_ref.args)
}
fn path_append_impl(
fn print_path_with_impl(
&mut self,
_: impl FnOnce(&mut Self) -> Result<(), PrintError>,
_: Ty<'tcx>,
@ -853,7 +853,7 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> {
unreachable!()
}
fn path_append(
fn print_path_with_simple(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
disambiguated_data: &DisambiguatedDefPathData,
@ -873,7 +873,7 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> {
DefPathData::SyntheticCoroutineBody => 's',
DefPathData::NestedStatic => 'n',
// These should never show up as `path_append` arguments.
// These should never show up as `print_path_with_simple` arguments.
DefPathData::CrateRoot
| DefPathData::Use
| DefPathData::GlobalAsm
@ -896,7 +896,7 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> {
)
}
fn path_generic_args(
fn print_path_with_generic_args(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
args: &[GenericArg<'tcx>],

View file

@ -253,12 +253,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
unreachable!(); // because `path_generic_args` ignores the `GenericArgs`
}
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
self.segments = vec![self.tcx.crate_name(cnum)];
Ok(())
}
fn path_qualified(
fn print_path_with_qualified(
&mut self,
_self_ty: Ty<'tcx>,
_trait_ref: Option<ty::TraitRef<'tcx>>,
@ -266,7 +266,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
Err(fmt::Error)
}
fn path_append_impl(
fn print_path_with_impl(
&mut self,
_print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
_self_ty: Ty<'tcx>,
@ -275,7 +275,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
Err(fmt::Error)
}
fn path_append(
fn print_path_with_simple(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
disambiguated_data: &DisambiguatedDefPathData,
@ -285,7 +285,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
Ok(())
}
fn path_generic_args(
fn print_path_with_generic_args(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
_args: &[GenericArg<'tcx>],

View file

@ -947,7 +947,7 @@ fn foo(&self) -> Self::T { String::new() }
pub fn format_generic_args(&self, args: &[ty::GenericArg<'tcx>]) -> String {
FmtPrinter::print_string(self.tcx, hir::def::Namespace::TypeNS, |p| {
p.path_generic_args(|_| Ok(()), args)
p.print_path_with_generic_args(|_| Ok(()), args)
})
.expect("could not write to `String`.")
}