1
Fork 0

Remove "subprinter" types from Printer

These are `Self` in almost all printers except one, which can just store
the state as a field instead. This simplifies the printer and allows for
further simplifications, for example using `&mut self` instead of
passing around the printer.
This commit is contained in:
Nilstrieb 2023-10-16 20:26:12 +02:00
parent 98c1e3d95b
commit 3895f0e9af
7 changed files with 149 additions and 200 deletions

View file

@ -1200,51 +1200,47 @@ impl<'tcx> LateContext<'tcx> {
/// }
/// ```
pub fn get_def_path(&self, def_id: DefId) -> Vec<Symbol> {
pub struct AbsolutePathPrinter<'tcx> {
pub tcx: TyCtxt<'tcx>,
struct AbsolutePathPrinter<'tcx> {
tcx: TyCtxt<'tcx>,
path: Vec<Symbol>,
}
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
type Error = !;
type Path = Vec<Symbol>;
type Region = ();
type Type = ();
type DynExistential = ();
type Const = ();
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
Ok(())
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
Ok(self)
}
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
Ok(())
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, Self::Error> {
Ok(self)
}
fn print_dyn_existential(
self,
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error> {
Ok(())
) -> Result<Self, Self::Error> {
Ok(self)
}
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
Ok(())
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
Ok(self)
}
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
Ok(vec![self.tcx.crate_name(cnum)])
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
self.path = vec![self.tcx.crate_name(cnum)];
Ok(self)
}
fn path_qualified(
self,
mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
if trait_ref.is_none() {
if let ty::Adt(def, args) = self_ty.kind() {
return self.print_def_path(def.did(), args);
@ -1253,24 +1249,25 @@ impl<'tcx> LateContext<'tcx> {
// This shouldn't ever be needed, but just in case:
with_no_trimmed_paths!({
Ok(vec![match trait_ref {
self.path = vec![match trait_ref {
Some(trait_ref) => Symbol::intern(&format!("{trait_ref:?}")),
None => Symbol::intern(&format!("<{self_ty}>")),
}])
}];
Ok(self)
})
}
fn path_append_impl(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
_disambiguated_data: &DisambiguatedDefPathData,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
let mut path = print_prefix(self)?;
// This shouldn't ever be needed, but just in case:
path.push(match trait_ref {
path.path.push(match trait_ref {
Some(trait_ref) => {
with_no_trimmed_paths!(Symbol::intern(&format!(
"<impl {} for {}>",
@ -1288,9 +1285,9 @@ impl<'tcx> LateContext<'tcx> {
fn path_append(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
let mut path = print_prefix(self)?;
// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
@ -1298,20 +1295,23 @@ impl<'tcx> LateContext<'tcx> {
return Ok(path);
}
path.push(Symbol::intern(&disambiguated_data.data.to_string()));
path.path.push(Symbol::intern(&disambiguated_data.data.to_string()));
Ok(path)
}
fn path_generic_args(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
_args: &[GenericArg<'tcx>],
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
print_prefix(self)
}
}
AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]).unwrap()
AbsolutePathPrinter { tcx: self.tcx, path: vec![] }
.print_def_path(def_id, &[])
.unwrap()
.path
}
/// Returns the associated type `name` for `self_ty` as an implementation of `trait_id`.