From 3895f0e9affaab800f5f2cdc06726aeee9a7ff10 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 16 Oct 2023 20:26:12 +0200 Subject: [PATCH 1/4] 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. --- .../rustc_const_eval/src/util/type_name.rs | 30 +++--- .../src/infer/error_reporting/mod.rs | 49 +++++----- compiler/rustc_lint/src/context.rs | 62 ++++++------ compiler/rustc_middle/src/ty/print/mod.rs | 46 ++++----- compiler/rustc_middle/src/ty/print/pretty.rs | 98 +++++++------------ compiler/rustc_symbol_mangling/src/legacy.rs | 30 +++--- compiler/rustc_symbol_mangling/src/v0.rs | 34 +++---- 7 files changed, 149 insertions(+), 200 deletions(-) diff --git a/compiler/rustc_const_eval/src/util/type_name.rs b/compiler/rustc_const_eval/src/util/type_name.rs index a924afda6f07a..5a068feb2d2f2 100644 --- a/compiler/rustc_const_eval/src/util/type_name.rs +++ b/compiler/rustc_const_eval/src/util/type_name.rs @@ -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 { + fn print_region(self, _region: ty::Region<'_>) -> Result { Ok(self) } - fn print_type(mut self, ty: Ty<'tcx>) -> Result { + fn print_type(mut self, ty: Ty<'tcx>) -> Result { 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 { + fn print_const(self, ct: ty::Const<'tcx>) -> Result { self.pretty_print_const(ct, false) } fn print_dyn_existential( self, predicates: &'tcx ty::List>, - ) -> Result { + ) -> Result { self.pretty_print_dyn_existential(predicates) } - fn path_crate(mut self, cnum: CrateNum) -> Result { + fn path_crate(mut self, cnum: CrateNum) -> Result { 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>, - ) -> Result { + ) -> Result { self.pretty_path_qualified(self_ty, trait_ref) } fn path_append_impl( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, _disambiguated_data: &DisambiguatedDefPathData, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { 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, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, - ) -> Result { + ) -> Result { 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, + print_prefix: impl FnOnce(Self) -> Result, args: &[GenericArg<'tcx>], - ) -> Result { + ) -> Result { self = print_prefix(self)?; let args = args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_))); diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index d35b318e258d9..5a3188b02eebe 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -580,6 +580,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { struct AbsolutePathPrinter<'tcx> { tcx: TyCtxt<'tcx>, + segments: Vec, } struct NonTrivialPath; @@ -587,69 +588,64 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> { type Error = NonTrivialPath; - type Path = Vec; - 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 { + fn print_region(self, _region: ty::Region<'_>) -> Result { Err(NonTrivialPath) } - fn print_type(self, _ty: Ty<'tcx>) -> Result { + fn print_type(self, _ty: Ty<'tcx>) -> Result { Err(NonTrivialPath) } fn print_dyn_existential( self, _predicates: &'tcx ty::List>, - ) -> Result { + ) -> Result { Err(NonTrivialPath) } - fn print_const(self, _ct: ty::Const<'tcx>) -> Result { + fn print_const(self, _ct: ty::Const<'tcx>) -> Result { Err(NonTrivialPath) } - fn path_crate(self, cnum: CrateNum) -> Result { - Ok(vec![self.tcx.crate_name(cnum).to_string()]) + fn path_crate(mut self, cnum: CrateNum) -> Result { + self.segments = vec![self.tcx.crate_name(cnum).to_string()]; + Ok(self) } fn path_qualified( self, _self_ty: Ty<'tcx>, _trait_ref: Option>, - ) -> Result { + ) -> Result { Err(NonTrivialPath) } fn path_append_impl( self, - _print_prefix: impl FnOnce(Self) -> Result, + _print_prefix: impl FnOnce(Self) -> Result, _disambiguated_data: &DisambiguatedDefPathData, _self_ty: Ty<'tcx>, _trait_ref: Option>, - ) -> Result { + ) -> Result { Err(NonTrivialPath) } fn path_append( - self, - print_prefix: impl FnOnce(Self) -> Result, + mut self, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, - ) -> Result { - let mut path = print_prefix(self)?; - path.push(disambiguated_data.to_string()); - Ok(path) + ) -> Result { + self = print_prefix(self)?; + self.segments.push(disambiguated_data.to_string()); + Ok(self) } fn path_generic_args( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, _args: &[GenericArg<'tcx>], - ) -> Result { + ) -> Result { 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 diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index eb1c6be48e648..af7f2bcf66903 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -1200,51 +1200,47 @@ impl<'tcx> LateContext<'tcx> { /// } /// ``` pub fn get_def_path(&self, def_id: DefId) -> Vec { - pub struct AbsolutePathPrinter<'tcx> { - pub tcx: TyCtxt<'tcx>, + struct AbsolutePathPrinter<'tcx> { + tcx: TyCtxt<'tcx>, + path: Vec, } impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> { type Error = !; - type Path = Vec; - type Region = (); - type Type = (); - type DynExistential = (); - type Const = (); - fn tcx(&self) -> TyCtxt<'tcx> { self.tcx } - fn print_region(self, _region: ty::Region<'_>) -> Result { - Ok(()) + fn print_region(self, _region: ty::Region<'_>) -> Result { + Ok(self) } - fn print_type(self, _ty: Ty<'tcx>) -> Result { - Ok(()) + fn print_type(self, _ty: Ty<'tcx>) -> Result { + Ok(self) } fn print_dyn_existential( self, _predicates: &'tcx ty::List>, - ) -> Result { - Ok(()) + ) -> Result { + Ok(self) } - fn print_const(self, _ct: ty::Const<'tcx>) -> Result { - Ok(()) + fn print_const(self, _ct: ty::Const<'tcx>) -> Result { + Ok(self) } - fn path_crate(self, cnum: CrateNum) -> Result { - Ok(vec![self.tcx.crate_name(cnum)]) + fn path_crate(mut self, cnum: CrateNum) -> Result { + self.path = vec![self.tcx.crate_name(cnum)]; + Ok(self) } fn path_qualified( - self, + mut self, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { 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, + print_prefix: impl FnOnce(Self) -> Result, _disambiguated_data: &DisambiguatedDefPathData, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { 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!( "", @@ -1288,9 +1285,9 @@ impl<'tcx> LateContext<'tcx> { fn path_append( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, - ) -> Result { + ) -> Result { 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, + print_prefix: impl FnOnce(Self) -> Result, _args: &[GenericArg<'tcx>], - ) -> Result { + ) -> Result { 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`. diff --git a/compiler/rustc_middle/src/ty/print/mod.rs b/compiler/rustc_middle/src/ty/print/mod.rs index aa8e2e30715eb..263f192b280f4 100644 --- a/compiler/rustc_middle/src/ty/print/mod.rs +++ b/compiler/rustc_middle/src/ty/print/mod.rs @@ -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 { + ) -> Result { 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>, - ) -> Result { + ) -> Result { self.default_print_impl_path(impl_def_id, args, self_ty, trait_ref) } - fn print_region(self, region: ty::Region<'tcx>) -> Result; + fn print_region(self, region: ty::Region<'tcx>) -> Result; - fn print_type(self, ty: Ty<'tcx>) -> Result; + fn print_type(self, ty: Ty<'tcx>) -> Result; fn print_dyn_existential( self, predicates: &'tcx ty::List>, - ) -> Result; + ) -> Result; - fn print_const(self, ct: ty::Const<'tcx>) -> Result; + fn print_const(self, ct: ty::Const<'tcx>) -> Result; - fn path_crate(self, cnum: CrateNum) -> Result; + fn path_crate(self, cnum: CrateNum) -> Result; fn path_qualified( self, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result; + ) -> Result; fn path_append_impl( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result; + ) -> Result; fn path_append( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, - ) -> Result; + ) -> Result; fn path_generic_args( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, args: &[GenericArg<'tcx>], - ) -> Result; + ) -> Result; // Defaults (should not be overridden): @@ -103,7 +97,7 @@ pub trait Printer<'tcx>: Sized { self, def_id: DefId, args: &'tcx [GenericArg<'tcx>], - ) -> Result { + ) -> Result { 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>, - ) -> Result { + ) -> Result { 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 { } 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 { 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 { @@ -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> { - type Output = P::DynExistential; + type Output = P; type Error = P::Error; fn print(&self, cx: P) -> Result { cx.print_dyn_existential(self) @@ -320,7 +314,7 @@ impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for &'tcx ty::List> Print<'tcx, P> for ty::Const<'tcx> { - type Output = P::Const; + type Output = P; type Error = P::Error; fn print(&self, cx: P) -> Result { cx.print_const(*self) diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index e321f0a7b7f29..31d6b65111fe0 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -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 { + ) -> Result { self.print_def_path(def_id, args) } @@ -264,7 +254,7 @@ pub trait PrettyPrinter<'tcx>: f: impl FnOnce(Self) -> Result, t: impl FnOnce(Self) -> Result, conversion: &str, - ) -> Result { + ) -> Result { 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>, - ) -> Result { + ) -> Result { if trait_ref.is_none() { // Inherent impls. Try to print `Foo::bar` for an inherent // impl on `Foo`, but fallback to `::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, + print_prefix: impl FnOnce(Self) -> Result, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { 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 { + fn pretty_print_type(mut self, ty: Ty<'tcx>) -> Result { define_scoped_cx!(self); match *ty.kind() { @@ -919,7 +903,7 @@ pub trait PrettyPrinter<'tcx>: mut self, def_id: DefId, args: &'tcx ty::List>, - ) -> Result { + ) -> Result { 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 { + ) -> Result { 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>, - ) -> Result { + ) -> Result { // 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 { + ) -> Result { 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 { + fn pretty_print_const_scalar(self, scalar: Scalar, ty: Ty<'tcx>) -> Result { 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 { + ) -> Result { 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 { + ) -> Result { define_scoped_cx!(self); match ty.kind() { @@ -1545,7 +1525,7 @@ pub trait PrettyPrinter<'tcx>: self, _: Pointer, ty: Ty<'tcx>, - ) -> Result { + ) -> Result { 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 { + fn pretty_print_byte_str(mut self, byte_str: &'tcx [u8]) -> Result { 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 { + ) -> Result { 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 { + ) -> Result { 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 { + ) -> Result { 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 { + fn print_region(self, region: ty::Region<'tcx>) -> Result { self.pretty_print_region(region) } - fn print_type(mut self, ty: Ty<'tcx>) -> Result { + fn print_type(mut self, ty: Ty<'tcx>) -> Result { 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>, - ) -> Result { + ) -> Result { self.pretty_print_dyn_existential(predicates) } - fn print_const(self, ct: ty::Const<'tcx>) -> Result { + fn print_const(self, ct: ty::Const<'tcx>) -> Result { self.pretty_print_const(ct, false) } - fn path_crate(mut self, cnum: CrateNum) -> Result { + fn path_crate(mut self, cnum: CrateNum) -> Result { 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>, - ) -> Result { + ) -> Result { 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, + print_prefix: impl FnOnce(Self) -> Result, _disambiguated_data: &DisambiguatedDefPathData, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { 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, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, - ) -> Result { + ) -> Result { 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, + print_prefix: impl FnOnce(Self) -> Result, args: &[GenericArg<'tcx>], - ) -> Result { + ) -> Result { 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 { + ) -> Result { 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, t: impl FnOnce(Self) -> Result, conversion: &str, - ) -> Result { + ) -> Result { self.write_str("{")?; self = f(self)?; self.write_str(conversion)?; @@ -2206,7 +2180,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> { self, p: Pointer, ty: Ty<'tcx>, - ) -> Result { + ) -> Result { let print = |mut this: Self| { define_scoped_cx!(this); if this.print_alloc_ids { diff --git a/compiler/rustc_symbol_mangling/src/legacy.rs b/compiler/rustc_symbol_mangling/src/legacy.rs index 2fc102bda1344..06b3f91ebef63 100644 --- a/compiler/rustc_symbol_mangling/src/legacy.rs +++ b/compiler/rustc_symbol_mangling/src/legacy.rs @@ -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 { + fn print_region(self, _region: ty::Region<'_>) -> Result { Ok(self) } - fn print_type(mut self, ty: Ty<'tcx>) -> Result { + fn print_type(mut self, ty: Ty<'tcx>) -> Result { 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>, - ) -> Result { + ) -> Result { 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 { + fn print_const(self, ct: ty::Const<'tcx>) -> Result { // 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 { + fn path_crate(self, cnum: CrateNum) -> Result { 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>, - ) -> Result { + ) -> Result { // 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, + print_prefix: impl FnOnce(Self) -> Result, _disambiguated_data: &DisambiguatedDefPathData, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { 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, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, - ) -> Result { + ) -> Result { 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, + print_prefix: impl FnOnce(Self) -> Result, args: &[GenericArg<'tcx>], - ) -> Result { + ) -> Result { self = print_prefix(self)?; let args = diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index 82b1a772e3d7b..249cdf7bdfabd 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -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 { + ) -> Result { 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>, - ) -> Result { + ) -> Result { 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 { + fn print_region(self, region: ty::Region<'_>) -> Result { 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 { + fn print_type(mut self, ty: Ty<'tcx>) -> Result { // 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>, - ) -> Result { + ) -> Result { // 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 { + fn print_const(mut self, ct: ty::Const<'tcx>) -> Result { // 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 { + fn path_crate(self, cnum: CrateNum) -> Result { 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>, - ) -> Result { + ) -> Result { 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, + _: impl FnOnce(Self) -> Result, _: &DisambiguatedDefPathData, _: Ty<'tcx>, _: Option>, - ) -> Result { + ) -> Result { // Inlined into `print_impl_path` unreachable!() } fn path_append( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, - ) -> Result { + ) -> Result { 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, + print_prefix: impl FnOnce(Self) -> Result, args: &[GenericArg<'tcx>], - ) -> Result { + ) -> Result { // 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(), From 0b5a4c1adfc4cede0fa06243ef9e7447e286affb Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 16 Oct 2023 20:36:56 +0200 Subject: [PATCH 2/4] Remove `Print::Output` Now that `Printer` doesn't have subprinters anymore, the output of a printing operation is always the same. --- .../rustc_const_eval/src/util/type_name.rs | 2 +- .../nice_region_error/placeholder_error.rs | 4 +-- compiler/rustc_middle/src/ty/print/mod.rs | 15 +++------ compiler/rustc_middle/src/ty/print/pretty.rs | 31 +++++++++---------- compiler/rustc_symbol_mangling/src/legacy.rs | 2 +- .../error_reporting/type_err_ctxt_ext.rs | 16 +++------- 6 files changed, 27 insertions(+), 43 deletions(-) diff --git a/compiler/rustc_const_eval/src/util/type_name.rs b/compiler/rustc_const_eval/src/util/type_name.rs index 5a068feb2d2f2..fba8121ead031 100644 --- a/compiler/rustc_const_eval/src/util/type_name.rs +++ b/compiler/rustc_const_eval/src/util/type_name.rs @@ -140,7 +140,7 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> { } fn comma_sep(mut self, mut elems: impl Iterator) -> Result where - T: Print<'tcx, Self, Output = Self, Error = Self::Error>, + T: Print<'tcx, Self, Error = Self::Error>, { if let Some(first) = elems.next() { self = first.print(self)?; diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs index 4aec28b051f14..17a265fd4f472 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs @@ -28,7 +28,7 @@ pub struct Highlighted<'tcx, T> { impl<'tcx, T> IntoDiagnosticArg for Highlighted<'tcx, T> where - T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>, Error = fmt::Error, Output = FmtPrinter<'a, 'tcx>>, + T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>, Error = fmt::Error>, { fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> { rustc_errors::DiagnosticArgValue::Str(self.to_string().into()) @@ -43,7 +43,7 @@ impl<'tcx, T> Highlighted<'tcx, T> { impl<'tcx, T> fmt::Display for Highlighted<'tcx, T> where - T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>, Error = fmt::Error, Output = FmtPrinter<'a, 'tcx>>, + T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>, Error = fmt::Error>, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS); diff --git a/compiler/rustc_middle/src/ty/print/mod.rs b/compiler/rustc_middle/src/ty/print/mod.rs index 263f192b280f4..e789374fdd63c 100644 --- a/compiler/rustc_middle/src/ty/print/mod.rs +++ b/compiler/rustc_middle/src/ty/print/mod.rs @@ -13,10 +13,9 @@ pub use self::pretty::*; // FIXME(eddyb) false positive, the lifetime parameters are used with `P: Printer<...>`. #[allow(unused_lifetimes)] pub trait Print<'tcx, P> { - type Output; type Error; - fn print(&self, cx: P) -> Result; + fn print(&self, cx: P) -> Result; } /// Interface for outputting user-facing "type-system entities" @@ -289,34 +288,30 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option { } impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Region<'tcx> { - type Output = P; type Error = P::Error; - fn print(&self, cx: P) -> Result { + fn print(&self, cx: P) -> Result { cx.print_region(*self) } } impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for Ty<'tcx> { - type Output = P; type Error = P::Error; - fn print(&self, cx: P) -> Result { + fn print(&self, cx: P) -> Result { cx.print_type(*self) } } impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for &'tcx ty::List> { - type Output = P; type Error = P::Error; - fn print(&self, cx: P) -> Result { + fn print(&self, cx: P) -> Result { cx.print_dyn_existential(self) } } impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Const<'tcx> { - type Output = P; type Error = P::Error; - fn print(&self, cx: P) -> Result { + fn print(&self, cx: P) -> Result { cx.print_const(*self) } } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 31d6b65111fe0..ae3e6885b759a 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -217,7 +217,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { fn in_binder(self, value: &ty::Binder<'tcx, T>) -> Result where - T: Print<'tcx, Self, Output = Self, Error = Self::Error> + TypeFoldable>, + T: Print<'tcx, Self, Error = Self::Error> + TypeFoldable>, { value.as_ref().skip_binder().print(self) } @@ -228,7 +228,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { f: F, ) -> Result where - T: Print<'tcx, Self, Output = Self, Error = Self::Error> + TypeFoldable>, + T: Print<'tcx, Self, Error = Self::Error> + TypeFoldable>, { f(value.as_ref().skip_binder(), self) } @@ -236,7 +236,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { /// Prints comma-separated elements. fn comma_sep(mut self, mut elems: impl Iterator) -> Result where - T: Print<'tcx, Self, Output = Self, Error = Self::Error>, + T: Print<'tcx, Self, Error = Self::Error>, { if let Some(first) = elems.next() { self = first.print(self)?; @@ -2085,7 +2085,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> { fn in_binder(self, value: &ty::Binder<'tcx, T>) -> Result where - T: Print<'tcx, Self, Output = Self, Error = Self::Error> + TypeFoldable>, + T: Print<'tcx, Self, Error = Self::Error> + TypeFoldable>, { self.pretty_in_binder(value) } @@ -2096,7 +2096,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> { f: C, ) -> Result where - T: Print<'tcx, Self, Output = Self, Error = Self::Error> + TypeFoldable>, + T: Print<'tcx, Self, Error = Self::Error> + TypeFoldable>, { self.pretty_wrap_binder(value, f) } @@ -2345,7 +2345,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> { value: &ty::Binder<'tcx, T>, ) -> Result<(Self, T, BTreeMap>), fmt::Error> where - T: Print<'tcx, Self, Output = Self, Error = fmt::Error> + TypeFoldable>, + T: Print<'tcx, Self, Error = fmt::Error> + TypeFoldable>, { fn name_by_region_index( index: usize, @@ -2515,7 +2515,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> { pub fn pretty_in_binder(self, value: &ty::Binder<'tcx, T>) -> Result where - T: Print<'tcx, Self, Output = Self, Error = fmt::Error> + TypeFoldable>, + T: Print<'tcx, Self, Error = fmt::Error> + TypeFoldable>, { let old_region_index = self.region_index; let (new, new_value, _) = self.name_all_regions(value)?; @@ -2531,7 +2531,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> { f: C, ) -> Result where - T: Print<'tcx, Self, Output = Self, Error = fmt::Error> + TypeFoldable>, + T: Print<'tcx, Self, Error = fmt::Error> + TypeFoldable>, { let old_region_index = self.region_index; let (new, new_value, _) = self.name_all_regions(value)?; @@ -2596,24 +2596,22 @@ impl<'tcx> FmtPrinter<'_, 'tcx> { impl<'tcx, T, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::Binder<'tcx, T> where - T: Print<'tcx, P, Output = P, Error = P::Error> + TypeFoldable>, + T: Print<'tcx, P, Error = P::Error> + TypeFoldable>, { - type Output = P; type Error = P::Error; - fn print(&self, cx: P) -> Result { + fn print(&self, cx: P) -> Result { cx.in_binder(self) } } impl<'tcx, T, U, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::OutlivesPredicate where - T: Print<'tcx, P, Output = P, Error = P::Error>, - U: Print<'tcx, P, Output = P, Error = P::Error>, + T: Print<'tcx, P, Error = P::Error>, + U: Print<'tcx, P, Error = P::Error>, { - type Output = P; type Error = P::Error; - fn print(&self, mut cx: P) -> Result { + fn print(&self, mut cx: P) -> Result { define_scoped_cx!(cx); p!(print(self.0), ": ", print(self.1)); Ok(cx) @@ -2640,9 +2638,8 @@ macro_rules! forward_display_to_print { macro_rules! define_print_and_forward_display { (($self:ident, $cx:ident): $($ty:ty $print:block)+) => { $(impl<'tcx, P: PrettyPrinter<'tcx>> Print<'tcx, P> for $ty { - type Output = P; type Error = fmt::Error; - fn print(&$self, $cx: P) -> Result { + fn print(&$self, $cx: P) -> Result { #[allow(unused_mut)] let mut $cx = $cx; define_scoped_cx!($cx); diff --git a/compiler/rustc_symbol_mangling/src/legacy.rs b/compiler/rustc_symbol_mangling/src/legacy.rs index 06b3f91ebef63..9b8646a7751fe 100644 --- a/compiler/rustc_symbol_mangling/src/legacy.rs +++ b/compiler/rustc_symbol_mangling/src/legacy.rs @@ -367,7 +367,7 @@ impl<'tcx> PrettyPrinter<'tcx> for &mut SymbolPrinter<'tcx> { } fn comma_sep(mut self, mut elems: impl Iterator) -> Result where - T: Print<'tcx, Self, Output = Self, Error = Self::Error>, + T: Print<'tcx, Self, Error = Self::Error>, { if let Some(first) = elems.next() { self = first.print(self)?; diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs index 1020144a01b23..1133d4302fe47 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs @@ -60,9 +60,7 @@ pub trait TypeErrCtxtExt<'tcx> { suggest_increasing_limit: bool, ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> where - T: fmt::Display - + TypeFoldable> - + Print<'tcx, FmtPrinter<'tcx, 'tcx>, Output = FmtPrinter<'tcx, 'tcx>>, + T: fmt::Display + TypeFoldable> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>, >>::Error: std::fmt::Debug; fn report_overflow_error( @@ -73,9 +71,7 @@ pub trait TypeErrCtxtExt<'tcx> { mutate: impl FnOnce(&mut Diagnostic), ) -> ! where - T: fmt::Display - + TypeFoldable> - + Print<'tcx, FmtPrinter<'tcx, 'tcx>, Output = FmtPrinter<'tcx, 'tcx>>, + T: fmt::Display + TypeFoldable> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>, >>::Error: std::fmt::Debug; fn report_overflow_no_abort(&self, obligation: PredicateObligation<'tcx>) -> ErrorGuaranteed; @@ -227,9 +223,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { mutate: impl FnOnce(&mut Diagnostic), ) -> ! where - T: fmt::Display - + TypeFoldable> - + Print<'tcx, FmtPrinter<'tcx, 'tcx>, Output = FmtPrinter<'tcx, 'tcx>>, + T: fmt::Display + TypeFoldable> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>, >>::Error: std::fmt::Debug, { let mut err = self.build_overflow_error(predicate, span, suggest_increasing_limit); @@ -247,9 +241,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { suggest_increasing_limit: bool, ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> where - T: fmt::Display - + TypeFoldable> - + Print<'tcx, FmtPrinter<'tcx, 'tcx>, Output = FmtPrinter<'tcx, 'tcx>>, + T: fmt::Display + TypeFoldable> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>, >>::Error: std::fmt::Debug, { let predicate = self.resolve_vars_if_possible(predicate.clone()); From 6038888118d88ed4175ccb49e9e429865432f253 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 16 Oct 2023 20:46:24 +0200 Subject: [PATCH 3/4] Remove `Printer::Error` It's always a `fmt::Error` except in some cases where it was `!`, but we're not really winning anything in that case. --- .../rustc_const_eval/src/util/type_name.rs | 36 +++-- .../src/infer/error_reporting/mod.rs | 44 +++--- compiler/rustc_lint/src/context.rs | 28 ++-- compiler/rustc_middle/src/ty/print/mod.rs | 54 +++---- compiler/rustc_middle/src/ty/print/pretty.rs | 134 +++++++++--------- compiler/rustc_symbol_mangling/src/legacy.rs | 36 +++-- compiler/rustc_symbol_mangling/src/v0.rs | 42 +++--- 7 files changed, 180 insertions(+), 194 deletions(-) diff --git a/compiler/rustc_const_eval/src/util/type_name.rs b/compiler/rustc_const_eval/src/util/type_name.rs index fba8121ead031..06944a93d3b74 100644 --- a/compiler/rustc_const_eval/src/util/type_name.rs +++ b/compiler/rustc_const_eval/src/util/type_name.rs @@ -3,7 +3,7 @@ use rustc_hir::def_id::CrateNum; use rustc_hir::definitions::DisambiguatedDefPathData; use rustc_middle::ty::{ self, - print::{PrettyPrinter, Print, Printer}, + print::{PrettyPrinter, Print, PrintError, Printer}, GenericArg, GenericArgKind, Ty, TyCtxt, }; use std::fmt::Write; @@ -14,17 +14,15 @@ struct AbsolutePathPrinter<'tcx> { } impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> { - type Error = std::fmt::Error; - fn tcx(&self) -> TyCtxt<'tcx> { self.tcx } - fn print_region(self, _region: ty::Region<'_>) -> Result { + fn print_region(self, _region: ty::Region<'_>) -> Result { Ok(self) } - fn print_type(mut self, ty: Ty<'tcx>) -> Result { + fn print_type(mut self, ty: Ty<'tcx>) -> Result { match *ty.kind() { // Types without identity. ty::Bool @@ -62,18 +60,18 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> { } } - fn print_const(self, ct: ty::Const<'tcx>) -> Result { + fn print_const(self, ct: ty::Const<'tcx>) -> Result { self.pretty_print_const(ct, false) } fn print_dyn_existential( self, predicates: &'tcx ty::List>, - ) -> Result { + ) -> Result { self.pretty_print_dyn_existential(predicates) } - fn path_crate(mut self, cnum: CrateNum) -> Result { + fn path_crate(mut self, cnum: CrateNum) -> Result { self.path.push_str(self.tcx.crate_name(cnum).as_str()); Ok(self) } @@ -82,17 +80,17 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> { self, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { self.pretty_path_qualified(self_ty, trait_ref) } fn path_append_impl( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, _disambiguated_data: &DisambiguatedDefPathData, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { self.pretty_path_append_impl( |mut cx| { cx = print_prefix(cx)?; @@ -108,9 +106,9 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> { fn path_append( mut self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, - ) -> Result { + ) -> Result { self = print_prefix(self)?; write!(self.path, "::{}", disambiguated_data.data).unwrap(); @@ -120,9 +118,9 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> { fn path_generic_args( mut self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, args: &[GenericArg<'tcx>], - ) -> Result { + ) -> Result { self = print_prefix(self)?; let args = args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_))); @@ -138,9 +136,9 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> { fn should_print_region(&self, _region: ty::Region<'_>) -> bool { false } - fn comma_sep(mut self, mut elems: impl Iterator) -> Result + fn comma_sep(mut self, mut elems: impl Iterator) -> Result where - T: Print<'tcx, Self, Error = Self::Error>, + T: Print<'tcx, Self, Error = PrintError>, { if let Some(first) = elems.next() { self = first.print(self)?; @@ -154,8 +152,8 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> { fn generic_delimiters( mut self, - f: impl FnOnce(Self) -> Result, - ) -> Result { + f: impl FnOnce(Self) -> Result, + ) -> Result { write!(self, "<")?; self = f(self)?; diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 5a3188b02eebe..496bb1766a71f 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -67,7 +67,7 @@ use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::intravisit::Visitor; use rustc_hir::lang_items::LangItem; use rustc_middle::dep_graph::DepContext; -use rustc_middle::ty::print::with_forced_trimmed_paths; +use rustc_middle::ty::print::{with_forced_trimmed_paths, PrintError}; use rustc_middle::ty::relate::{self, RelateResult, TypeRelation}; use rustc_middle::ty::{ self, error::TypeError, IsSuggestable, List, Region, Ty, TyCtxt, TypeFoldable, @@ -583,35 +583,31 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { segments: Vec, } - struct NonTrivialPath; - impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> { - type Error = NonTrivialPath; - fn tcx<'a>(&'a self) -> TyCtxt<'tcx> { self.tcx } - fn print_region(self, _region: ty::Region<'_>) -> Result { - Err(NonTrivialPath) + fn print_region(self, _region: ty::Region<'_>) -> Result { + Err(fmt::Error) } - fn print_type(self, _ty: Ty<'tcx>) -> Result { - Err(NonTrivialPath) + fn print_type(self, _ty: Ty<'tcx>) -> Result { + Err(fmt::Error) } fn print_dyn_existential( self, _predicates: &'tcx ty::List>, - ) -> Result { - Err(NonTrivialPath) + ) -> Result { + Err(fmt::Error) } - fn print_const(self, _ct: ty::Const<'tcx>) -> Result { - Err(NonTrivialPath) + fn print_const(self, _ct: ty::Const<'tcx>) -> Result { + Err(fmt::Error) } - fn path_crate(mut self, cnum: CrateNum) -> Result { + fn path_crate(mut self, cnum: CrateNum) -> Result { self.segments = vec![self.tcx.crate_name(cnum).to_string()]; Ok(self) } @@ -619,33 +615,33 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { self, _self_ty: Ty<'tcx>, _trait_ref: Option>, - ) -> Result { - Err(NonTrivialPath) + ) -> Result { + Err(fmt::Error) } fn path_append_impl( self, - _print_prefix: impl FnOnce(Self) -> Result, + _print_prefix: impl FnOnce(Self) -> Result, _disambiguated_data: &DisambiguatedDefPathData, _self_ty: Ty<'tcx>, _trait_ref: Option>, - ) -> Result { - Err(NonTrivialPath) + ) -> Result { + Err(fmt::Error) } fn path_append( mut self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, - ) -> Result { + ) -> Result { self = print_prefix(self)?; self.segments.push(disambiguated_data.to_string()); Ok(self) } fn path_generic_args( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, _args: &[GenericArg<'tcx>], - ) -> Result { + ) -> Result { print_prefix(self) } } @@ -663,7 +659,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { // We compare strings because DefPath can be different // for imported and non-imported crates - let same_path = || -> Result<_, NonTrivialPath> { + let same_path = || -> Result<_, PrintError> { Ok(self.tcx.def_path_str(did1) == self.tcx.def_path_str(did2) || abs_path(did1)? == abs_path(did2)?) }; diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index af7f2bcf66903..1f08db3086076 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -31,7 +31,7 @@ use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData}; use rustc_middle::middle::privacy::EffectiveVisibilities; use rustc_middle::middle::stability; use rustc_middle::ty::layout::{LayoutError, LayoutOfHelpers, TyAndLayout}; -use rustc_middle::ty::print::with_no_trimmed_paths; +use rustc_middle::ty::print::{with_no_trimmed_paths, PrintError}; use rustc_middle::ty::{self, print::Printer, GenericArg, RegisteredTools, Ty, TyCtxt}; use rustc_session::config::ExpectedValues; use rustc_session::lint::{BuiltinLintDiagnostics, LintExpectationId}; @@ -1206,32 +1206,30 @@ impl<'tcx> LateContext<'tcx> { } impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> { - type Error = !; - fn tcx(&self) -> TyCtxt<'tcx> { self.tcx } - fn print_region(self, _region: ty::Region<'_>) -> Result { + fn print_region(self, _region: ty::Region<'_>) -> Result { Ok(self) } - fn print_type(self, _ty: Ty<'tcx>) -> Result { + fn print_type(self, _ty: Ty<'tcx>) -> Result { Ok(self) } fn print_dyn_existential( self, _predicates: &'tcx ty::List>, - ) -> Result { + ) -> Result { Ok(self) } - fn print_const(self, _ct: ty::Const<'tcx>) -> Result { + fn print_const(self, _ct: ty::Const<'tcx>) -> Result { Ok(self) } - fn path_crate(mut self, cnum: CrateNum) -> Result { + fn path_crate(mut self, cnum: CrateNum) -> Result { self.path = vec![self.tcx.crate_name(cnum)]; Ok(self) } @@ -1240,7 +1238,7 @@ impl<'tcx> LateContext<'tcx> { mut self, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { if trait_ref.is_none() { if let ty::Adt(def, args) = self_ty.kind() { return self.print_def_path(def.did(), args); @@ -1259,11 +1257,11 @@ impl<'tcx> LateContext<'tcx> { fn path_append_impl( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, _disambiguated_data: &DisambiguatedDefPathData, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { let mut path = print_prefix(self)?; // This shouldn't ever be needed, but just in case: @@ -1285,9 +1283,9 @@ impl<'tcx> LateContext<'tcx> { fn path_append( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, - ) -> Result { + ) -> Result { let mut path = print_prefix(self)?; // Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs. @@ -1301,9 +1299,9 @@ impl<'tcx> LateContext<'tcx> { fn path_generic_args( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, _args: &[GenericArg<'tcx>], - ) -> Result { + ) -> Result { print_prefix(self) } } diff --git a/compiler/rustc_middle/src/ty/print/mod.rs b/compiler/rustc_middle/src/ty/print/mod.rs index e789374fdd63c..a1384c1616f58 100644 --- a/compiler/rustc_middle/src/ty/print/mod.rs +++ b/compiler/rustc_middle/src/ty/print/mod.rs @@ -10,12 +10,14 @@ use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData}; mod pretty; pub use self::pretty::*; +pub type PrintError = std::fmt::Error; + // FIXME(eddyb) false positive, the lifetime parameters are used with `P: Printer<...>`. #[allow(unused_lifetimes)] pub trait Print<'tcx, P> { type Error; - fn print(&self, cx: P) -> Result; + fn print(&self, cx: P) -> Result; } /// Interface for outputting user-facing "type-system entities" @@ -28,15 +30,13 @@ pub trait Print<'tcx, P> { // // FIXME(eddyb) find a better name; this is more general than "printing". pub trait Printer<'tcx>: Sized { - type Error; - fn tcx<'a>(&'a self) -> TyCtxt<'tcx>; fn print_def_path( self, def_id: DefId, args: &'tcx [GenericArg<'tcx>], - ) -> Result { + ) -> Result { self.default_print_def_path(def_id, args) } @@ -46,48 +46,48 @@ pub trait Printer<'tcx>: Sized { args: &'tcx [GenericArg<'tcx>], self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { self.default_print_impl_path(impl_def_id, args, self_ty, trait_ref) } - fn print_region(self, region: ty::Region<'tcx>) -> Result; + fn print_region(self, region: ty::Region<'tcx>) -> Result; - fn print_type(self, ty: Ty<'tcx>) -> Result; + fn print_type(self, ty: Ty<'tcx>) -> Result; fn print_dyn_existential( self, predicates: &'tcx ty::List>, - ) -> Result; + ) -> Result; - fn print_const(self, ct: ty::Const<'tcx>) -> Result; + fn print_const(self, ct: ty::Const<'tcx>) -> Result; - fn path_crate(self, cnum: CrateNum) -> Result; + fn path_crate(self, cnum: CrateNum) -> Result; fn path_qualified( self, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result; + ) -> Result; fn path_append_impl( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result; + ) -> Result; fn path_append( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, - ) -> Result; + ) -> Result; fn path_generic_args( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, args: &[GenericArg<'tcx>], - ) -> Result; + ) -> Result; // Defaults (should not be overridden): @@ -96,7 +96,7 @@ pub trait Printer<'tcx>: Sized { self, def_id: DefId, args: &'tcx [GenericArg<'tcx>], - ) -> Result { + ) -> Result { let key = self.tcx().def_key(def_id); debug!(?key); @@ -187,7 +187,7 @@ pub trait Printer<'tcx>: Sized { _args: &'tcx [GenericArg<'tcx>], self_ty: Ty<'tcx>, impl_trait_ref: Option>, - ) -> Result { + ) -> Result { debug!( "default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}", impl_def_id, self_ty, impl_trait_ref @@ -288,30 +288,30 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option { } impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Region<'tcx> { - type Error = P::Error; - fn print(&self, cx: P) -> Result { + type Error = PrintError; + fn print(&self, cx: P) -> Result { cx.print_region(*self) } } impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for Ty<'tcx> { - type Error = P::Error; + type Error = PrintError; - fn print(&self, cx: P) -> Result { + fn print(&self, cx: P) -> Result { cx.print_type(*self) } } impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for &'tcx ty::List> { - type Error = P::Error; - fn print(&self, cx: P) -> Result { + type Error = PrintError; + fn print(&self, cx: P) -> Result { cx.print_dyn_existential(self) } } impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Const<'tcx> { - type Error = P::Error; - fn print(&self, cx: P) -> Result { + type Error = PrintError; + fn print(&self, cx: P) -> Result { cx.print_const(*self) } } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index ae3e6885b759a..f1b17ddd2300c 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -205,19 +205,19 @@ 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> + fmt::Write { +pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { /// Like `print_def_path` but for value paths. fn print_value_path( self, def_id: DefId, args: &'tcx [GenericArg<'tcx>], - ) -> Result { + ) -> Result { self.print_def_path(def_id, args) } - fn in_binder(self, value: &ty::Binder<'tcx, T>) -> Result + fn in_binder(self, value: &ty::Binder<'tcx, T>) -> Result where - T: Print<'tcx, Self, Error = Self::Error> + TypeFoldable>, + T: Print<'tcx, Self, Error = PrintError> + TypeFoldable>, { value.as_ref().skip_binder().print(self) } @@ -226,17 +226,17 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { self, value: &ty::Binder<'tcx, T>, f: F, - ) -> Result + ) -> Result where - T: Print<'tcx, Self, Error = Self::Error> + TypeFoldable>, + T: Print<'tcx, Self, Error = PrintError> + TypeFoldable>, { f(value.as_ref().skip_binder(), self) } /// Prints comma-separated elements. - fn comma_sep(mut self, mut elems: impl Iterator) -> Result + fn comma_sep(mut self, mut elems: impl Iterator) -> Result where - T: Print<'tcx, Self, Error = Self::Error>, + T: Print<'tcx, Self, Error = PrintError>, { if let Some(first) = elems.next() { self = first.print(self)?; @@ -251,10 +251,10 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { /// Prints `{f: t}` or `{f as t}` depending on the `cast` argument fn typed_value( mut self, - f: impl FnOnce(Self) -> Result, - t: impl FnOnce(Self) -> Result, + f: impl FnOnce(Self) -> Result, + t: impl FnOnce(Self) -> Result, conversion: &str, - ) -> Result { + ) -> Result { self.write_str("{")?; self = f(self)?; self.write_str(conversion)?; @@ -266,8 +266,8 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { /// Prints `<...>` around what `f` prints. fn generic_delimiters( self, - f: impl FnOnce(Self) -> Result, - ) -> Result; + f: impl FnOnce(Self) -> Result, + ) -> Result; /// Returns `true` if the region should be printed in /// optional positions, e.g., `&'a T` or `dyn Tr + 'b`. @@ -281,7 +281,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { /// If possible, this returns a global path resolving to `def_id` that is visible /// from at least one local module, and returns `true`. If the crate defining `def_id` is /// declared with an `extern crate`, the path is guaranteed to use the `extern crate`. - fn try_print_visible_def_path(self, def_id: DefId) -> Result<(Self, bool), Self::Error> { + fn try_print_visible_def_path(self, def_id: DefId) -> Result<(Self, bool), PrintError> { if NO_VISIBLE_PATH.with(|flag| flag.get()) { return Ok((self, false)); } @@ -295,7 +295,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { // 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, bool), Self::Error> { + fn force_print_trimmed_def_path(mut self, def_id: DefId) -> Result<(Self, bool), PrintError> { 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); @@ -365,7 +365,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { } /// 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, bool), Self::Error> { + fn try_print_trimmed_def_path(mut self, def_id: DefId) -> Result<(Self, bool), PrintError> { if FORCE_TRIMMED_PATH.with(|flag| flag.get()) { let (s, trimmed) = self.force_print_trimmed_def_path(def_id)?; if trimmed { @@ -407,7 +407,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { mut self, def_id: DefId, callers: &mut Vec, - ) -> Result<(Self, bool), Self::Error> { + ) -> Result<(Self, bool), PrintError> { define_scoped_cx!(self); debug!("try_print_visible_def_path: def_id={:?}", def_id); @@ -579,7 +579,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { self, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { if trait_ref.is_none() { // Inherent impls. Try to print `Foo::bar` for an inherent // impl on `Foo`, but fallback to `::bar` if self-type is @@ -613,10 +613,10 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { fn pretty_path_append_impl( mut self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { self = print_prefix(self)?; self.generic_delimiters(|mut cx| { @@ -632,7 +632,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { }) } - fn pretty_print_type(mut self, ty: Ty<'tcx>) -> Result { + fn pretty_print_type(mut self, ty: Ty<'tcx>) -> Result { define_scoped_cx!(self); match *ty.kind() { @@ -903,7 +903,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { mut self, def_id: DefId, args: &'tcx ty::List>, - ) -> Result { + ) -> Result { let tcx = self.tcx(); // Grab the "TraitA + TraitB" from `impl TraitA + TraitB`, @@ -1173,7 +1173,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { fn pretty_print_inherent_projection( self, alias_ty: &ty::AliasTy<'tcx>, - ) -> Result { + ) -> Result { let def_key = self.tcx().def_key(alias_ty.def_id); self.path_generic_args( |cx| { @@ -1197,7 +1197,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { fn pretty_print_dyn_existential( mut self, predicates: &'tcx ty::List>, - ) -> Result { + ) -> Result { // Generate the main trait ref, including associated types. let mut first = true; @@ -1290,7 +1290,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { inputs: &[Ty<'tcx>], c_variadic: bool, output: Ty<'tcx>, - ) -> Result { + ) -> Result { define_scoped_cx!(self); p!("(", comma_sep(inputs.iter().copied())); @@ -1312,7 +1312,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { mut self, ct: ty::Const<'tcx>, print_ty: bool, - ) -> Result { + ) -> Result { define_scoped_cx!(self); if self.should_print_verbose() { @@ -1388,7 +1388,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { Ok(self) } - fn pretty_print_const_scalar(self, scalar: Scalar, ty: Ty<'tcx>) -> Result { + fn pretty_print_const_scalar(self, scalar: Scalar, ty: Ty<'tcx>) -> Result { match scalar { Scalar::Ptr(ptr, _size) => self.pretty_print_const_scalar_ptr(ptr, ty), Scalar::Int(int) => { @@ -1401,7 +1401,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { mut self, ptr: Pointer, ty: Ty<'tcx>, - ) -> Result { + ) -> Result { define_scoped_cx!(self); let (alloc_id, offset) = ptr.into_parts(); @@ -1463,7 +1463,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { int: ScalarInt, ty: Ty<'tcx>, print_ty: bool, - ) -> Result { + ) -> Result { define_scoped_cx!(self); match ty.kind() { @@ -1525,7 +1525,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { self, _: Pointer, ty: Ty<'tcx>, - ) -> Result { + ) -> Result { self.typed_value( |mut this| { this.write_str("&_")?; @@ -1536,7 +1536,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { ) } - fn pretty_print_byte_str(mut self, byte_str: &'tcx [u8]) -> Result { + fn pretty_print_byte_str(mut self, byte_str: &'tcx [u8]) -> Result { write!(self, "b\"{}\"", byte_str.escape_ascii())?; Ok(self) } @@ -1546,7 +1546,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { valtree: ty::ValTree<'tcx>, ty: Ty<'tcx>, print_ty: bool, - ) -> Result { + ) -> Result { define_scoped_cx!(self); if self.should_print_verbose() { @@ -1669,7 +1669,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx, Error = fmt::Error> + fmt::Write { fn pretty_closure_as_impl( mut self, closure: ty::ClosureArgs<'tcx>, - ) -> Result { + ) -> Result { let sig = closure.sig(); let kind = closure.kind_ty().to_opt_closure_kind().unwrap_or(ty::ClosureKind::Fn); @@ -1842,8 +1842,6 @@ impl fmt::Write for FmtPrinter<'_, '_> { } impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { - type Error = fmt::Error; - fn tcx<'a>(&'a self) -> TyCtxt<'tcx> { self.tcx } @@ -1852,7 +1850,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { mut self, def_id: DefId, args: &'tcx [GenericArg<'tcx>], - ) -> Result { + ) -> Result { define_scoped_cx!(self); if args.is_empty() { @@ -1907,11 +1905,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 { + fn print_region(self, region: ty::Region<'tcx>) -> Result { self.pretty_print_region(region) } - fn print_type(mut self, ty: Ty<'tcx>) -> Result { + fn print_type(mut self, ty: Ty<'tcx>) -> Result { if self.type_length_limit.value_within_limit(self.printed_type_count) { self.printed_type_count += 1; self.pretty_print_type(ty) @@ -1925,15 +1923,15 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { fn print_dyn_existential( self, predicates: &'tcx ty::List>, - ) -> Result { + ) -> Result { self.pretty_print_dyn_existential(predicates) } - fn print_const(self, ct: ty::Const<'tcx>) -> Result { + fn print_const(self, ct: ty::Const<'tcx>) -> Result { self.pretty_print_const(ct, false) } - fn path_crate(mut self, cnum: CrateNum) -> Result { + fn path_crate(mut self, cnum: CrateNum) -> Result { self.empty_path = true; if cnum == LOCAL_CRATE { if self.tcx.sess.at_least_rust_2018() { @@ -1954,7 +1952,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { mut self, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { self = self.pretty_path_qualified(self_ty, trait_ref)?; self.empty_path = false; Ok(self) @@ -1962,11 +1960,11 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { fn path_append_impl( mut self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, _disambiguated_data: &DisambiguatedDefPathData, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { self = self.pretty_path_append_impl( |mut cx| { cx = print_prefix(cx)?; @@ -1985,9 +1983,9 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { fn path_append( mut self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, - ) -> Result { + ) -> Result { self = print_prefix(self)?; // Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs. @@ -2016,9 +2014,9 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { fn path_generic_args( mut self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, args: &[GenericArg<'tcx>], - ) -> Result { + ) -> Result { self = print_prefix(self)?; let tcx = self.tcx; @@ -2075,7 +2073,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> { mut self, def_id: DefId, args: &'tcx [GenericArg<'tcx>], - ) -> Result { + ) -> Result { 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; @@ -2083,30 +2081,30 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> { Ok(self) } - fn in_binder(self, value: &ty::Binder<'tcx, T>) -> Result + fn in_binder(self, value: &ty::Binder<'tcx, T>) -> Result where - T: Print<'tcx, Self, Error = Self::Error> + TypeFoldable>, + T: Print<'tcx, Self, Error = PrintError> + TypeFoldable>, { self.pretty_in_binder(value) } - fn wrap_binder Result>( + fn wrap_binder Result>( self, value: &ty::Binder<'tcx, T>, f: C, - ) -> Result + ) -> Result where - T: Print<'tcx, Self, Error = Self::Error> + TypeFoldable>, + T: Print<'tcx, Self, Error = PrintError> + TypeFoldable>, { self.pretty_wrap_binder(value, f) } fn typed_value( mut self, - f: impl FnOnce(Self) -> Result, - t: impl FnOnce(Self) -> Result, + f: impl FnOnce(Self) -> Result, + t: impl FnOnce(Self) -> Result, conversion: &str, - ) -> Result { + ) -> Result { self.write_str("{")?; self = f(self)?; self.write_str(conversion)?; @@ -2119,8 +2117,8 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> { fn generic_delimiters( mut self, - f: impl FnOnce(Self) -> Result, - ) -> Result { + f: impl FnOnce(Self) -> Result, + ) -> Result { write!(self, "<")?; let was_in_value = std::mem::replace(&mut self.in_value, false); @@ -2180,7 +2178,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> { self, p: Pointer, ty: Ty<'tcx>, - ) -> Result { + ) -> Result { let print = |mut this: Self| { define_scoped_cx!(this); if this.print_alloc_ids { @@ -2596,22 +2594,22 @@ impl<'tcx> FmtPrinter<'_, 'tcx> { impl<'tcx, T, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::Binder<'tcx, T> where - T: Print<'tcx, P, Error = P::Error> + TypeFoldable>, + T: Print<'tcx, P, Error = PrintError> + TypeFoldable>, { - type Error = P::Error; + type Error = PrintError; - fn print(&self, cx: P) -> Result { + fn print(&self, cx: P) -> Result { cx.in_binder(self) } } impl<'tcx, T, U, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::OutlivesPredicate where - T: Print<'tcx, P, Error = P::Error>, - U: Print<'tcx, P, Error = P::Error>, + T: Print<'tcx, P, Error = PrintError>, + U: Print<'tcx, P, Error = PrintError>, { - type Error = P::Error; - fn print(&self, mut cx: P) -> Result { + type Error = PrintError; + fn print(&self, mut cx: P) -> Result { define_scoped_cx!(cx); p!(print(self.0), ": ", print(self.1)); Ok(cx) @@ -2639,7 +2637,7 @@ macro_rules! define_print_and_forward_display { (($self:ident, $cx:ident): $($ty:ty $print:block)+) => { $(impl<'tcx, P: PrettyPrinter<'tcx>> Print<'tcx, P> for $ty { type Error = fmt::Error; - fn print(&$self, $cx: P) -> Result { + fn print(&$self, $cx: P) -> Result { #[allow(unused_mut)] let mut $cx = $cx; define_scoped_cx!($cx); diff --git a/compiler/rustc_symbol_mangling/src/legacy.rs b/compiler/rustc_symbol_mangling/src/legacy.rs index 9b8646a7751fe..5e7dba63ff719 100644 --- a/compiler/rustc_symbol_mangling/src/legacy.rs +++ b/compiler/rustc_symbol_mangling/src/legacy.rs @@ -1,7 +1,7 @@ use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher}; use rustc_hir::def_id::CrateNum; use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData}; -use rustc_middle::ty::print::{PrettyPrinter, Print, Printer}; +use rustc_middle::ty::print::{PrettyPrinter, Print, PrintError, Printer}; use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeVisitableExt}; use rustc_middle::ty::{GenericArg, GenericArgKind}; use rustc_middle::util::common::record_time; @@ -200,17 +200,15 @@ struct SymbolPrinter<'tcx> { // symbol names should have their own printing machinery. impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> { - type Error = fmt::Error; - fn tcx(&self) -> TyCtxt<'tcx> { self.tcx } - fn print_region(self, _region: ty::Region<'_>) -> Result { + fn print_region(self, _region: ty::Region<'_>) -> Result { Ok(self) } - fn print_type(mut self, ty: Ty<'tcx>) -> Result { + fn print_type(mut self, ty: Ty<'tcx>) -> Result { match *ty.kind() { // Print all nominal types as paths (unlike `pretty_print_type`). ty::FnDef(def_id, args) @@ -244,7 +242,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> { fn print_dyn_existential( mut self, predicates: &'tcx ty::List>, - ) -> Result { + ) -> Result { let mut first = true; for p in predicates { if !first { @@ -256,7 +254,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> { Ok(self) } - fn print_const(self, ct: ty::Const<'tcx>) -> Result { + fn print_const(self, ct: ty::Const<'tcx>) -> Result { // only print integers match (ct.kind(), ct.ty().kind()) { (ty::ConstKind::Value(ty::ValTree::Leaf(scalar)), ty::Int(_) | ty::Uint(_)) => { @@ -274,7 +272,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> { Ok(self) } - fn path_crate(self, cnum: CrateNum) -> Result { + fn path_crate(self, cnum: CrateNum) -> Result { self.write_str(self.tcx.crate_name(cnum).as_str())?; Ok(self) } @@ -282,7 +280,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> { self, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { // Similar to `pretty_path_qualified`, but for the other // types that are printed as paths (see `print_type` above). match self_ty.kind() { @@ -298,11 +296,11 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> { fn path_append_impl( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, _disambiguated_data: &DisambiguatedDefPathData, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { self.pretty_path_append_impl( |mut cx| { cx = print_prefix(cx)?; @@ -322,9 +320,9 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> { } fn path_append( mut self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, - ) -> Result { + ) -> Result { self = print_prefix(self)?; // Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs. @@ -345,9 +343,9 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> { } fn path_generic_args( mut self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, args: &[GenericArg<'tcx>], - ) -> Result { + ) -> Result { self = print_prefix(self)?; let args = @@ -365,9 +363,9 @@ impl<'tcx> PrettyPrinter<'tcx> for &mut SymbolPrinter<'tcx> { fn should_print_region(&self, _region: ty::Region<'_>) -> bool { false } - fn comma_sep(mut self, mut elems: impl Iterator) -> Result + fn comma_sep(mut self, mut elems: impl Iterator) -> Result where - T: Print<'tcx, Self, Error = Self::Error>, + T: Print<'tcx, Self, Error = PrintError>, { if let Some(first) = elems.next() { self = first.print(self)?; @@ -381,8 +379,8 @@ impl<'tcx> PrettyPrinter<'tcx> for &mut SymbolPrinter<'tcx> { fn generic_delimiters( mut self, - f: impl FnOnce(Self) -> Result, - ) -> Result { + f: impl FnOnce(Self) -> Result, + ) -> Result { write!(self, "<")?; let kept_within_component = mem::replace(&mut self.keep_within_component, true); diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index 249cdf7bdfabd..99c5d0164399b 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -6,7 +6,7 @@ use rustc_hir::def::CtorKind; use rustc_hir::def_id::{CrateNum, DefId}; use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData}; use rustc_middle::ty::layout::IntegerExt; -use rustc_middle::ty::print::{Print, Printer}; +use rustc_middle::ty::print::{Print, PrintError, Printer}; use rustc_middle::ty::{ self, EarlyBinder, FloatTy, Instance, IntTy, Ty, TyCtxt, TypeVisitable, TypeVisitableExt, UintTy, @@ -181,11 +181,11 @@ impl<'tcx> SymbolMangler<'tcx> { fn path_append_ns<'a>( mut self: &'a mut Self, - print_prefix: impl FnOnce(&'a mut Self) -> Result<&'a mut Self, !>, + print_prefix: impl FnOnce(&'a mut Self) -> Result<&'a mut Self, PrintError>, ns: char, disambiguator: u64, name: &str, - ) -> Result<&'a mut Self, !> { + ) -> Result<&'a mut Self, PrintError> { self.push("N"); self.out.push(ns); self = print_prefix(self)?; @@ -194,7 +194,7 @@ impl<'tcx> SymbolMangler<'tcx> { Ok(self) } - fn print_backref(&mut self, i: usize) -> Result<&mut Self, !> { + fn print_backref(&mut self, i: usize) -> Result<&mut Self, PrintError> { self.push("B"); self.push_integer_62((i - self.start_offset) as u64); Ok(self) @@ -203,8 +203,8 @@ impl<'tcx> SymbolMangler<'tcx> { fn in_binder<'a, T>( mut self: &'a mut Self, value: &ty::Binder<'tcx, T>, - print_value: impl FnOnce(&'a mut Self, &T) -> Result<&'a mut Self, !>, - ) -> Result<&'a mut Self, !> + print_value: impl FnOnce(&'a mut Self, &T) -> Result<&'a mut Self, PrintError>, + ) -> Result<&'a mut Self, PrintError> where T: TypeVisitable>, { @@ -230,8 +230,6 @@ impl<'tcx> SymbolMangler<'tcx> { } impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> { - type Error = !; - fn tcx(&self) -> TyCtxt<'tcx> { self.tcx } @@ -240,7 +238,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> { mut self, def_id: DefId, args: &'tcx [GenericArg<'tcx>], - ) -> Result { + ) -> Result { if let Some(&i) = self.paths.get(&(def_id, args)) { return self.print_backref(i); } @@ -262,7 +260,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> { args: &'tcx [GenericArg<'tcx>], mut self_ty: Ty<'tcx>, mut impl_trait_ref: Option>, - ) -> Result { + ) -> Result { let key = self.tcx.def_key(impl_def_id); let parent_def_id = DefId { index: key.parent.unwrap(), ..impl_def_id }; @@ -315,7 +313,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> { Ok(self) } - fn print_region(self, region: ty::Region<'_>) -> Result { + fn print_region(self, region: ty::Region<'_>) -> Result { let i = match *region { // Erased lifetimes use the index 0, for a // shorter mangling of `L_`. @@ -337,7 +335,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> { Ok(self) } - fn print_type(mut self, ty: Ty<'tcx>) -> Result { + fn print_type(mut self, ty: Ty<'tcx>) -> Result { // Basic types, never cached (single-character). let basic_type = match ty.kind() { ty::Bool => "b", @@ -492,7 +490,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> { fn print_dyn_existential( mut self, predicates: &'tcx ty::List>, - ) -> Result { + ) -> Result { // 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 @@ -553,7 +551,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> { Ok(self) } - fn print_const(mut self, ct: ty::Const<'tcx>) -> Result { + fn print_const(mut self, ct: ty::Const<'tcx>) -> Result { // 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() { @@ -725,7 +723,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> { Ok(self) } - fn path_crate(self, cnum: CrateNum) -> Result { + fn path_crate(self, cnum: CrateNum) -> Result { 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()); @@ -738,7 +736,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> { mut self, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { assert!(trait_ref.is_some()); let trait_ref = trait_ref.unwrap(); @@ -749,20 +747,20 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> { fn path_append_impl( self, - _: impl FnOnce(Self) -> Result, + _: impl FnOnce(Self) -> Result, _: &DisambiguatedDefPathData, _: Ty<'tcx>, _: Option>, - ) -> Result { + ) -> Result { // Inlined into `print_impl_path` unreachable!() } fn path_append( self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, - ) -> Result { + ) -> Result { let ns = match disambiguated_data.data { // Extern block segments can be skipped, names from extern blocks // are effectively living in their parent modules. @@ -800,9 +798,9 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> { fn path_generic_args( mut self, - print_prefix: impl FnOnce(Self) -> Result, + print_prefix: impl FnOnce(Self) -> Result, args: &[GenericArg<'tcx>], - ) -> Result { + ) -> Result { // 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(), From 6fc6a6d7831e6b7b23e9e7b7c15c6461ecbac16c Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 16 Oct 2023 20:50:46 +0200 Subject: [PATCH 4/4] Remove `Print::Error` All printing goes through `fmt::Error` now. --- .../rustc_const_eval/src/util/type_name.rs | 2 +- .../nice_region_error/placeholder_error.rs | 4 +-- compiler/rustc_middle/src/ty/print/mod.rs | 7 ----- compiler/rustc_middle/src/ty/print/pretty.rs | 26 ++++++++----------- compiler/rustc_symbol_mangling/src/legacy.rs | 2 +- .../error_reporting/type_err_ctxt_ext.rs | 8 ++---- 6 files changed, 17 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_const_eval/src/util/type_name.rs b/compiler/rustc_const_eval/src/util/type_name.rs index 06944a93d3b74..54eb14ae8fc47 100644 --- a/compiler/rustc_const_eval/src/util/type_name.rs +++ b/compiler/rustc_const_eval/src/util/type_name.rs @@ -138,7 +138,7 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> { } fn comma_sep(mut self, mut elems: impl Iterator) -> Result where - T: Print<'tcx, Self, Error = PrintError>, + T: Print<'tcx, Self>, { if let Some(first) = elems.next() { self = first.print(self)?; diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs index 17a265fd4f472..d6a3bc32cc937 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs @@ -28,7 +28,7 @@ pub struct Highlighted<'tcx, T> { impl<'tcx, T> IntoDiagnosticArg for Highlighted<'tcx, T> where - T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>, Error = fmt::Error>, + T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>>, { fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> { rustc_errors::DiagnosticArgValue::Str(self.to_string().into()) @@ -43,7 +43,7 @@ impl<'tcx, T> Highlighted<'tcx, T> { impl<'tcx, T> fmt::Display for Highlighted<'tcx, T> where - T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>, Error = fmt::Error>, + T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>>, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS); diff --git a/compiler/rustc_middle/src/ty/print/mod.rs b/compiler/rustc_middle/src/ty/print/mod.rs index a1384c1616f58..107b44285ac0e 100644 --- a/compiler/rustc_middle/src/ty/print/mod.rs +++ b/compiler/rustc_middle/src/ty/print/mod.rs @@ -15,8 +15,6 @@ pub type PrintError = std::fmt::Error; // FIXME(eddyb) false positive, the lifetime parameters are used with `P: Printer<...>`. #[allow(unused_lifetimes)] pub trait Print<'tcx, P> { - type Error; - fn print(&self, cx: P) -> Result; } @@ -288,29 +286,24 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option { } impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Region<'tcx> { - type Error = PrintError; fn print(&self, cx: P) -> Result { cx.print_region(*self) } } impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for Ty<'tcx> { - type Error = PrintError; - fn print(&self, cx: P) -> Result { cx.print_type(*self) } } impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for &'tcx ty::List> { - type Error = PrintError; fn print(&self, cx: P) -> Result { cx.print_dyn_existential(self) } } impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Const<'tcx> { - type Error = PrintError; fn print(&self, cx: P) -> Result { cx.print_const(*self) } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index f1b17ddd2300c..d8010c714970d 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -217,7 +217,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { fn in_binder(self, value: &ty::Binder<'tcx, T>) -> Result where - T: Print<'tcx, Self, Error = PrintError> + TypeFoldable>, + T: Print<'tcx, Self> + TypeFoldable>, { value.as_ref().skip_binder().print(self) } @@ -228,7 +228,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { f: F, ) -> Result where - T: Print<'tcx, Self, Error = PrintError> + TypeFoldable>, + T: Print<'tcx, Self> + TypeFoldable>, { f(value.as_ref().skip_binder(), self) } @@ -236,7 +236,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { /// Prints comma-separated elements. fn comma_sep(mut self, mut elems: impl Iterator) -> Result where - T: Print<'tcx, Self, Error = PrintError>, + T: Print<'tcx, Self>, { if let Some(first) = elems.next() { self = first.print(self)?; @@ -2083,7 +2083,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> { fn in_binder(self, value: &ty::Binder<'tcx, T>) -> Result where - T: Print<'tcx, Self, Error = PrintError> + TypeFoldable>, + T: Print<'tcx, Self> + TypeFoldable>, { self.pretty_in_binder(value) } @@ -2094,7 +2094,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> { f: C, ) -> Result where - T: Print<'tcx, Self, Error = PrintError> + TypeFoldable>, + T: Print<'tcx, Self> + TypeFoldable>, { self.pretty_wrap_binder(value, f) } @@ -2343,7 +2343,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> { value: &ty::Binder<'tcx, T>, ) -> Result<(Self, T, BTreeMap>), fmt::Error> where - T: Print<'tcx, Self, Error = fmt::Error> + TypeFoldable>, + T: Print<'tcx, Self> + TypeFoldable>, { fn name_by_region_index( index: usize, @@ -2513,7 +2513,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> { pub fn pretty_in_binder(self, value: &ty::Binder<'tcx, T>) -> Result where - T: Print<'tcx, Self, Error = fmt::Error> + TypeFoldable>, + T: Print<'tcx, Self> + TypeFoldable>, { let old_region_index = self.region_index; let (new, new_value, _) = self.name_all_regions(value)?; @@ -2529,7 +2529,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> { f: C, ) -> Result where - T: Print<'tcx, Self, Error = fmt::Error> + TypeFoldable>, + T: Print<'tcx, Self> + TypeFoldable>, { let old_region_index = self.region_index; let (new, new_value, _) = self.name_all_regions(value)?; @@ -2594,10 +2594,8 @@ impl<'tcx> FmtPrinter<'_, 'tcx> { impl<'tcx, T, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::Binder<'tcx, T> where - T: Print<'tcx, P, Error = PrintError> + TypeFoldable>, + T: Print<'tcx, P> + TypeFoldable>, { - type Error = PrintError; - fn print(&self, cx: P) -> Result { cx.in_binder(self) } @@ -2605,10 +2603,9 @@ where impl<'tcx, T, U, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::OutlivesPredicate where - T: Print<'tcx, P, Error = PrintError>, - U: Print<'tcx, P, Error = PrintError>, + T: Print<'tcx, P>, + U: Print<'tcx, P>, { - type Error = PrintError; fn print(&self, mut cx: P) -> Result { define_scoped_cx!(cx); p!(print(self.0), ": ", print(self.1)); @@ -2636,7 +2633,6 @@ macro_rules! forward_display_to_print { macro_rules! define_print_and_forward_display { (($self:ident, $cx:ident): $($ty:ty $print:block)+) => { $(impl<'tcx, P: PrettyPrinter<'tcx>> Print<'tcx, P> for $ty { - type Error = fmt::Error; fn print(&$self, $cx: P) -> Result { #[allow(unused_mut)] let mut $cx = $cx; diff --git a/compiler/rustc_symbol_mangling/src/legacy.rs b/compiler/rustc_symbol_mangling/src/legacy.rs index 5e7dba63ff719..5290da9a25b90 100644 --- a/compiler/rustc_symbol_mangling/src/legacy.rs +++ b/compiler/rustc_symbol_mangling/src/legacy.rs @@ -365,7 +365,7 @@ impl<'tcx> PrettyPrinter<'tcx> for &mut SymbolPrinter<'tcx> { } fn comma_sep(mut self, mut elems: impl Iterator) -> Result where - T: Print<'tcx, Self, Error = PrintError>, + T: Print<'tcx, Self>, { if let Some(first) = elems.next() { self = first.print(self)?; diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs index 1133d4302fe47..640bd3fad7ca5 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs @@ -60,8 +60,7 @@ pub trait TypeErrCtxtExt<'tcx> { suggest_increasing_limit: bool, ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> where - T: fmt::Display + TypeFoldable> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>, - >>::Error: std::fmt::Debug; + T: fmt::Display + TypeFoldable> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>; fn report_overflow_error( &self, @@ -71,8 +70,7 @@ pub trait TypeErrCtxtExt<'tcx> { mutate: impl FnOnce(&mut Diagnostic), ) -> ! where - T: fmt::Display + TypeFoldable> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>, - >>::Error: std::fmt::Debug; + T: fmt::Display + TypeFoldable> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>; fn report_overflow_no_abort(&self, obligation: PredicateObligation<'tcx>) -> ErrorGuaranteed; @@ -224,7 +222,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { ) -> ! where T: fmt::Display + TypeFoldable> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>, - >>::Error: std::fmt::Debug, { let mut err = self.build_overflow_error(predicate, span, suggest_increasing_limit); mutate(&mut err); @@ -242,7 +239,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> where T: fmt::Display + TypeFoldable> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>, - >>::Error: std::fmt::Debug, { let predicate = self.resolve_vars_if_possible(predicate.clone()); let mut pred_str = predicate.to_string();