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

@ -16,21 +16,15 @@ struct AbsolutePathPrinter<'tcx> {
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
type Error = std::fmt::Error;
type Path = Self;
type Region = Self;
type Type = Self;
type DynExistential = Self;
type Const = Self;
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
Ok(self)
}
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, Self::Error> {
match *ty.kind() {
// Types without identity.
ty::Bool
@ -68,18 +62,18 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
}
}
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
self.pretty_print_const(ct, false)
}
fn print_dyn_existential(
self,
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error> {
) -> Result<Self, Self::Error> {
self.pretty_print_dyn_existential(predicates)
}
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
self.path.push_str(self.tcx.crate_name(cnum).as_str());
Ok(self)
}
@ -88,17 +82,17 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
self.pretty_path_qualified(self_ty, trait_ref)
}
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> {
self.pretty_path_append_impl(
|mut cx| {
cx = print_prefix(cx)?;
@ -114,9 +108,9 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
fn path_append(
mut 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> {
self = print_prefix(self)?;
write!(self.path, "::{}", disambiguated_data.data).unwrap();
@ -126,9 +120,9 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
fn path_generic_args(
mut 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> {
self = print_prefix(self)?;
let args =
args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_)));

View file

@ -580,6 +580,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
struct AbsolutePathPrinter<'tcx> {
tcx: TyCtxt<'tcx>,
segments: Vec<String>,
}
struct NonTrivialPath;
@ -587,69 +588,64 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
type Error = NonTrivialPath;
type Path = Vec<String>;
type Region = !;
type Type = !;
type DynExistential = !;
type Const = !;
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
self.tcx
}
fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
}
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
}
fn print_dyn_existential(
self,
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error> {
) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
}
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
}
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
Ok(vec![self.tcx.crate_name(cnum).to_string()])
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
self.segments = vec![self.tcx.crate_name(cnum).to_string()];
Ok(self)
}
fn path_qualified(
self,
_self_ty: Ty<'tcx>,
_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
}
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> {
Err(NonTrivialPath)
}
fn path_append(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error> {
let mut path = print_prefix(self)?;
path.push(disambiguated_data.to_string());
Ok(path)
) -> Result<Self, Self::Error> {
self = print_prefix(self)?;
self.segments.push(disambiguated_data.to_string());
Ok(self)
}
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)
}
}
@ -659,8 +655,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
// are from a local module we could have false positives, e.g.
// let _ = [{struct Foo; Foo}, {struct Foo; Foo}];
if did1.krate != did2.krate {
let abs_path =
|def_id| AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]);
let abs_path = |def_id| {
AbsolutePathPrinter { tcx: self.tcx, segments: vec![] }
.print_def_path(def_id, &[])
.map(|p| p.segments)
};
// We compare strings because DefPath can be different
// for imported and non-imported crates

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`.

View file

@ -31,19 +31,13 @@ pub trait Print<'tcx, P> {
pub trait Printer<'tcx>: Sized {
type Error;
type Path;
type Region;
type Type;
type DynExistential;
type Const;
fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
fn print_def_path(
self,
def_id: DefId,
args: &'tcx [GenericArg<'tcx>],
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
self.default_print_def_path(def_id, args)
}
@ -53,48 +47,48 @@ pub trait Printer<'tcx>: Sized {
args: &'tcx [GenericArg<'tcx>],
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
self.default_print_impl_path(impl_def_id, args, self_ty, trait_ref)
}
fn print_region(self, region: ty::Region<'tcx>) -> Result<Self::Region, Self::Error>;
fn print_region(self, region: ty::Region<'tcx>) -> Result<Self, Self::Error>;
fn print_type(self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error>;
fn print_type(self, ty: Ty<'tcx>) -> Result<Self, Self::Error>;
fn print_dyn_existential(
self,
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error>;
) -> Result<Self, Self::Error>;
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error>;
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, Self::Error>;
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error>;
fn path_crate(self, cnum: CrateNum) -> Result<Self, Self::Error>;
fn path_qualified(
self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error>;
) -> Result<Self, Self::Error>;
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>;
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>;
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>;
// Defaults (should not be overridden):
@ -103,7 +97,7 @@ pub trait Printer<'tcx>: Sized {
self,
def_id: DefId,
args: &'tcx [GenericArg<'tcx>],
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
let key = self.tcx().def_key(def_id);
debug!(?key);
@ -194,7 +188,7 @@ pub trait Printer<'tcx>: Sized {
_args: &'tcx [GenericArg<'tcx>],
self_ty: Ty<'tcx>,
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
debug!(
"default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}",
impl_def_id, self_ty, impl_trait_ref
@ -295,7 +289,7 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
}
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Region<'tcx> {
type Output = P::Region;
type Output = P;
type Error = P::Error;
fn print(&self, cx: P) -> Result<Self::Output, Self::Error> {
cx.print_region(*self)
@ -303,7 +297,7 @@ impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Region<'tcx> {
}
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for Ty<'tcx> {
type Output = P::Type;
type Output = P;
type Error = P::Error;
fn print(&self, cx: P) -> Result<Self::Output, Self::Error> {
@ -312,7 +306,7 @@ impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for Ty<'tcx> {
}
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
type Output = P::DynExistential;
type Output = P;
type Error = P::Error;
fn print(&self, cx: P) -> Result<Self::Output, Self::Error> {
cx.print_dyn_existential(self)
@ -320,7 +314,7 @@ impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for &'tcx ty::List<ty::PolyExistenti
}
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Const<'tcx> {
type Output = P::Const;
type Output = P;
type Error = P::Error;
fn print(&self, cx: P) -> Result<Self::Output, Self::Error> {
cx.print_const(*self)

View file

@ -205,23 +205,13 @@ impl<'tcx> RegionHighlightMode<'tcx> {
}
/// Trait for printers that pretty-print using `fmt::Write` to the printer.
pub trait PrettyPrinter<'tcx>:
Printer<
'tcx,
Error = fmt::Error,
Path = Self,
Region = Self,
Type = Self,
DynExistential = Self,
Const = Self,
> + fmt::Write
{
pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write {
/// Like `print_def_path` but for value paths.
fn print_value_path(
self,
def_id: DefId,
args: &'tcx [GenericArg<'tcx>],
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
self.print_def_path(def_id, args)
}
@ -264,7 +254,7 @@ pub trait PrettyPrinter<'tcx>:
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
t: impl FnOnce(Self) -> Result<Self, Self::Error>,
conversion: &str,
) -> Result<Self::Const, Self::Error> {
) -> Result<Self, Self::Error> {
self.write_str("{")?;
self = f(self)?;
self.write_str(conversion)?;
@ -305,10 +295,7 @@ pub trait PrettyPrinter<'tcx>:
// For enum variants, if they have an unique name, then we only print the name, otherwise we
// print the enum name and the variant name. Otherwise, we do not print anything and let the
// caller use the `print_def_path` fallback.
fn force_print_trimmed_def_path(
mut self,
def_id: DefId,
) -> Result<(Self::Path, bool), Self::Error> {
fn force_print_trimmed_def_path(mut self, def_id: DefId) -> Result<(Self, bool), Self::Error> {
let key = self.tcx().def_key(def_id);
let visible_parent_map = self.tcx().visible_parent_map(());
let kind = self.tcx().def_kind(def_id);
@ -378,10 +365,7 @@ pub trait PrettyPrinter<'tcx>:
}
/// Try to see if this path can be trimmed to a unique symbol name.
fn try_print_trimmed_def_path(
mut self,
def_id: DefId,
) -> Result<(Self::Path, bool), Self::Error> {
fn try_print_trimmed_def_path(mut self, def_id: DefId) -> Result<(Self, bool), Self::Error> {
if FORCE_TRIMMED_PATH.with(|flag| flag.get()) {
let (s, trimmed) = self.force_print_trimmed_def_path(def_id)?;
if trimmed {
@ -595,7 +579,7 @@ pub trait PrettyPrinter<'tcx>:
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() {
// Inherent impls. Try to print `Foo::bar` for an inherent
// impl on `Foo`, but fallback to `<Foo>::bar` if self-type is
@ -629,10 +613,10 @@ pub trait PrettyPrinter<'tcx>:
fn pretty_path_append_impl(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
self = print_prefix(self)?;
self.generic_delimiters(|mut cx| {
@ -648,7 +632,7 @@ pub trait PrettyPrinter<'tcx>:
})
}
fn pretty_print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
fn pretty_print_type(mut self, ty: Ty<'tcx>) -> Result<Self, Self::Error> {
define_scoped_cx!(self);
match *ty.kind() {
@ -919,7 +903,7 @@ pub trait PrettyPrinter<'tcx>:
mut self,
def_id: DefId,
args: &'tcx ty::List<ty::GenericArg<'tcx>>,
) -> Result<Self::Type, Self::Error> {
) -> Result<Self, Self::Error> {
let tcx = self.tcx();
// Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
@ -1189,7 +1173,7 @@ pub trait PrettyPrinter<'tcx>:
fn pretty_print_inherent_projection(
self,
alias_ty: &ty::AliasTy<'tcx>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
let def_key = self.tcx().def_key(alias_ty.def_id);
self.path_generic_args(
|cx| {
@ -1213,7 +1197,7 @@ pub trait PrettyPrinter<'tcx>:
fn pretty_print_dyn_existential(
mut self,
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error> {
) -> Result<Self, Self::Error> {
// Generate the main trait ref, including associated types.
let mut first = true;
@ -1328,7 +1312,7 @@ pub trait PrettyPrinter<'tcx>:
mut self,
ct: ty::Const<'tcx>,
print_ty: bool,
) -> Result<Self::Const, Self::Error> {
) -> Result<Self, Self::Error> {
define_scoped_cx!(self);
if self.should_print_verbose() {
@ -1404,11 +1388,7 @@ pub trait PrettyPrinter<'tcx>:
Ok(self)
}
fn pretty_print_const_scalar(
self,
scalar: Scalar,
ty: Ty<'tcx>,
) -> Result<Self::Const, Self::Error> {
fn pretty_print_const_scalar(self, scalar: Scalar, ty: Ty<'tcx>) -> Result<Self, Self::Error> {
match scalar {
Scalar::Ptr(ptr, _size) => self.pretty_print_const_scalar_ptr(ptr, ty),
Scalar::Int(int) => {
@ -1421,7 +1401,7 @@ pub trait PrettyPrinter<'tcx>:
mut self,
ptr: Pointer,
ty: Ty<'tcx>,
) -> Result<Self::Const, Self::Error> {
) -> Result<Self, Self::Error> {
define_scoped_cx!(self);
let (alloc_id, offset) = ptr.into_parts();
@ -1483,7 +1463,7 @@ pub trait PrettyPrinter<'tcx>:
int: ScalarInt,
ty: Ty<'tcx>,
print_ty: bool,
) -> Result<Self::Const, Self::Error> {
) -> Result<Self, Self::Error> {
define_scoped_cx!(self);
match ty.kind() {
@ -1545,7 +1525,7 @@ pub trait PrettyPrinter<'tcx>:
self,
_: Pointer<Prov>,
ty: Ty<'tcx>,
) -> Result<Self::Const, Self::Error> {
) -> Result<Self, Self::Error> {
self.typed_value(
|mut this| {
this.write_str("&_")?;
@ -1556,7 +1536,7 @@ pub trait PrettyPrinter<'tcx>:
)
}
fn pretty_print_byte_str(mut self, byte_str: &'tcx [u8]) -> Result<Self::Const, Self::Error> {
fn pretty_print_byte_str(mut self, byte_str: &'tcx [u8]) -> Result<Self, Self::Error> {
write!(self, "b\"{}\"", byte_str.escape_ascii())?;
Ok(self)
}
@ -1566,7 +1546,7 @@ pub trait PrettyPrinter<'tcx>:
valtree: ty::ValTree<'tcx>,
ty: Ty<'tcx>,
print_ty: bool,
) -> Result<Self::Const, Self::Error> {
) -> Result<Self, Self::Error> {
define_scoped_cx!(self);
if self.should_print_verbose() {
@ -1689,7 +1669,7 @@ pub trait PrettyPrinter<'tcx>:
fn pretty_closure_as_impl(
mut self,
closure: ty::ClosureArgs<'tcx>,
) -> Result<Self::Const, Self::Error> {
) -> Result<Self, Self::Error> {
let sig = closure.sig();
let kind = closure.kind_ty().to_opt_closure_kind().unwrap_or(ty::ClosureKind::Fn);
@ -1864,12 +1844,6 @@ impl fmt::Write for FmtPrinter<'_, '_> {
impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
type Error = fmt::Error;
type Path = Self;
type Region = Self;
type Type = Self;
type DynExistential = Self;
type Const = Self;
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
self.tcx
}
@ -1878,7 +1852,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
mut self,
def_id: DefId,
args: &'tcx [GenericArg<'tcx>],
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
define_scoped_cx!(self);
if args.is_empty() {
@ -1933,11 +1907,11 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
self.default_print_def_path(def_id, args)
}
fn print_region(self, region: ty::Region<'tcx>) -> Result<Self::Region, Self::Error> {
fn print_region(self, region: ty::Region<'tcx>) -> Result<Self, Self::Error> {
self.pretty_print_region(region)
}
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, Self::Error> {
if self.type_length_limit.value_within_limit(self.printed_type_count) {
self.printed_type_count += 1;
self.pretty_print_type(ty)
@ -1951,15 +1925,15 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
fn print_dyn_existential(
self,
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error> {
) -> Result<Self, Self::Error> {
self.pretty_print_dyn_existential(predicates)
}
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
self.pretty_print_const(ct, false)
}
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
self.empty_path = true;
if cnum == LOCAL_CRATE {
if self.tcx.sess.at_least_rust_2018() {
@ -1980,7 +1954,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
self = self.pretty_path_qualified(self_ty, trait_ref)?;
self.empty_path = false;
Ok(self)
@ -1988,11 +1962,11 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
fn path_append_impl(
mut 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> {
self = self.pretty_path_append_impl(
|mut cx| {
cx = print_prefix(cx)?;
@ -2011,9 +1985,9 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
fn path_append(
mut 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> {
self = print_prefix(self)?;
// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
@ -2042,9 +2016,9 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
fn path_generic_args(
mut 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> {
self = print_prefix(self)?;
let tcx = self.tcx;
@ -2101,7 +2075,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
mut self,
def_id: DefId,
args: &'tcx [GenericArg<'tcx>],
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
let was_in_value = std::mem::replace(&mut self.in_value, true);
self = self.print_def_path(def_id, args)?;
self.in_value = was_in_value;
@ -2132,7 +2106,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
t: impl FnOnce(Self) -> Result<Self, Self::Error>,
conversion: &str,
) -> Result<Self::Const, Self::Error> {
) -> Result<Self, Self::Error> {
self.write_str("{")?;
self = f(self)?;
self.write_str(conversion)?;
@ -2206,7 +2180,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
self,
p: Pointer<Prov>,
ty: Ty<'tcx>,
) -> Result<Self::Const, Self::Error> {
) -> Result<Self, Self::Error> {
let print = |mut this: Self| {
define_scoped_cx!(this);
if this.print_alloc_ids {

View file

@ -202,21 +202,15 @@ struct SymbolPrinter<'tcx> {
impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
type Error = fmt::Error;
type Path = Self;
type Region = Self;
type Type = Self;
type DynExistential = Self;
type Const = Self;
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
Ok(self)
}
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, Self::Error> {
match *ty.kind() {
// Print all nominal types as paths (unlike `pretty_print_type`).
ty::FnDef(def_id, args)
@ -250,7 +244,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
fn print_dyn_existential(
mut self,
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error> {
) -> Result<Self, Self::Error> {
let mut first = true;
for p in predicates {
if !first {
@ -262,7 +256,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
Ok(self)
}
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
// only print integers
match (ct.kind(), ct.ty().kind()) {
(ty::ConstKind::Value(ty::ValTree::Leaf(scalar)), ty::Int(_) | ty::Uint(_)) => {
@ -280,7 +274,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
Ok(self)
}
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
fn path_crate(self, cnum: CrateNum) -> Result<Self, Self::Error> {
self.write_str(self.tcx.crate_name(cnum).as_str())?;
Ok(self)
}
@ -288,7 +282,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
// Similar to `pretty_path_qualified`, but for the other
// types that are printed as paths (see `print_type` above).
match self_ty.kind() {
@ -304,11 +298,11 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
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> {
self.pretty_path_append_impl(
|mut cx| {
cx = print_prefix(cx)?;
@ -328,9 +322,9 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
}
fn path_append(
mut 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> {
self = print_prefix(self)?;
// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
@ -351,9 +345,9 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
}
fn path_generic_args(
mut 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> {
self = print_prefix(self)?;
let args =

View file

@ -232,12 +232,6 @@ impl<'tcx> SymbolMangler<'tcx> {
impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
type Error = !;
type Path = Self;
type Region = Self;
type Type = Self;
type DynExistential = Self;
type Const = Self;
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
@ -246,7 +240,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
mut self,
def_id: DefId,
args: &'tcx [GenericArg<'tcx>],
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
if let Some(&i) = self.paths.get(&(def_id, args)) {
return self.print_backref(i);
}
@ -268,7 +262,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
args: &'tcx [GenericArg<'tcx>],
mut self_ty: Ty<'tcx>,
mut impl_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
let key = self.tcx.def_key(impl_def_id);
let parent_def_id = DefId { index: key.parent.unwrap(), ..impl_def_id };
@ -321,7 +315,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
Ok(self)
}
fn print_region(self, region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
fn print_region(self, region: ty::Region<'_>) -> Result<Self, Self::Error> {
let i = match *region {
// Erased lifetimes use the index 0, for a
// shorter mangling of `L_`.
@ -343,7 +337,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
Ok(self)
}
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, Self::Error> {
// Basic types, never cached (single-character).
let basic_type = match ty.kind() {
ty::Bool => "b",
@ -498,7 +492,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
fn print_dyn_existential(
mut self,
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error> {
) -> Result<Self, Self::Error> {
// Okay, so this is a bit tricky. Imagine we have a trait object like
// `dyn for<'a> Foo<'a, Bar = &'a ()>`. When we mangle this, the
// output looks really close to the syntax, where the `Bar = &'a ()` bit
@ -559,7 +553,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
Ok(self)
}
fn print_const(mut self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
fn print_const(mut self, ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
// We only mangle a typed value if the const can be evaluated.
let ct = ct.normalize(self.tcx, ty::ParamEnv::reveal_all());
match ct.kind() {
@ -731,7 +725,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
Ok(self)
}
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
fn path_crate(self, cnum: CrateNum) -> Result<Self, Self::Error> {
self.push("C");
let stable_crate_id = self.tcx.def_path_hash(cnum.as_def_id()).stable_crate_id();
self.push_disambiguator(stable_crate_id.as_u64());
@ -744,7 +738,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
assert!(trait_ref.is_some());
let trait_ref = trait_ref.unwrap();
@ -755,20 +749,20 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
fn path_append_impl(
self,
_: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
_: impl FnOnce(Self) -> Result<Self, Self::Error>,
_: &DisambiguatedDefPathData,
_: Ty<'tcx>,
_: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
// Inlined into `print_impl_path`
unreachable!()
}
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 ns = match disambiguated_data.data {
// Extern block segments can be skipped, names from extern blocks
// are effectively living in their parent modules.
@ -806,9 +800,9 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
fn path_generic_args(
mut 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> {
// Don't print any regions if they're all erased.
let print_regions = args.iter().any(|arg| match arg.unpack() {
GenericArgKind::Lifetime(r) => !r.is_erased(),