diff --git a/src/librustc/ty/print.rs b/src/librustc/ty/print.rs index 45762460f2db..77fed9cf1d15 100644 --- a/src/librustc/ty/print.rs +++ b/src/librustc/ty/print.rs @@ -173,10 +173,9 @@ fn path_qualified( self: &mut PrintCx<'_, '_, 'tcx, Self>, self_ty: Ty<'tcx>, trait_ref: Option>, + ns: Namespace, ) -> Self::Path; #[must_use] - fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path; - #[must_use] fn path_append( self: &mut PrintCx<'_, '_, '_, Self>, path: Self::Path, @@ -291,7 +290,7 @@ pub fn default_print_def_path( parent_generics.has_self && parent_generics.parent_count == 0; if let (Some(substs), true) = (substs, parent_has_own_self) { let trait_ref = ty::TraitRef::new(parent_def_id, substs); - self.path_qualified(trait_ref.self_ty(), Some(trait_ref)) + self.path_qualified(trait_ref.self_ty(), Some(trait_ref), ns) } else { self.print_def_path(parent_def_id, substs, ns, iter::empty()) } @@ -367,35 +366,7 @@ fn default_print_impl_path( // Otherwise, try to give a good form that would be valid language // syntax. Preferably using associated item notation. - - if let Some(trait_ref) = impl_trait_ref { - // Trait impls. - return self.path_qualified(self_ty, Some(trait_ref)); - } - - // Inherent impls. Try to print `Foo::bar` for an inherent - // impl on `Foo`, but fallback to `::bar` if self-type is - // anything other than a simple path. - match self_ty.sty { - ty::Adt(adt_def, substs) => { - self.print_def_path(adt_def.did, Some(substs), ns, iter::empty()) - } - - ty::Foreign(did) => self.print_def_path(did, None, ns, iter::empty()), - - ty::Bool | - ty::Char | - ty::Int(_) | - ty::Uint(_) | - ty::Float(_) | - ty::Str => { - self.path_impl(&self_ty.to_string()) - } - - _ => { - self.path_qualified(self_ty, None) - } - } + self.path_qualified(self_ty, impl_trait_ref, ns) } } @@ -587,7 +558,30 @@ pub fn pretty_path_qualified( &mut self, self_ty: Ty<'tcx>, trait_ref: Option>, + ns: Namespace, ) -> P::Path { + 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 + // anything other than a simple path. + match self_ty.sty { + ty::Adt(adt_def, substs) => { + return self.print_def_path(adt_def.did, Some(substs), ns, iter::empty()); + } + ty::Foreign(did) => { + return self.print_def_path(did, None, ns, iter::empty()); + } + + ty::Bool | ty::Char | ty::Str | + ty::Int(_) | ty::Uint(_) | ty::Float(_) => { + self_ty.print_display(self)?; + return Ok(PrettyPath { empty: false }); + } + + _ => {} + } + } + write!(self.printer, "<")?; self_ty.print_display(self)?; if let Some(trait_ref) = trait_ref { @@ -781,12 +775,9 @@ fn path_qualified( self: &mut PrintCx<'_, '_, 'tcx, Self>, self_ty: Ty<'tcx>, trait_ref: Option>, + ns: Namespace, ) -> Self::Path { - self.pretty_path_qualified(self_ty, trait_ref) - } - fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path { - write!(self.printer, "{}", text)?; - Ok(PrettyPath { empty: false }) + self.pretty_path_qualified(self_ty, trait_ref, ns) } fn path_append( self: &mut PrintCx<'_, '_, '_, Self>, diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index 2c38c437cf6e..3c34d2540990 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -1007,7 +1007,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { Ok(()) } debug { - let _ = cx.path_qualified(self.self_ty(), Some(*self))?; + let _ = cx.path_qualified(self.self_ty(), Some(*self), Namespace::TypeNS)?; Ok(()) } } diff --git a/src/librustc_codegen_utils/symbol_names.rs b/src/librustc_codegen_utils/symbol_names.rs index 4c7b00ae0780..0ea141b6574b 100644 --- a/src/librustc_codegen_utils/symbol_names.rs +++ b/src/librustc_codegen_utils/symbol_names.rs @@ -416,16 +416,24 @@ fn path_qualified( self: &mut PrintCx<'_, '_, 'tcx, Self>, self_ty: Ty<'tcx>, trait_ref: Option>, + ns: Namespace, ) -> Self::Path { + // HACK(eddyb) avoid `keep_within_component` for the cases + // that print without `<...>` around `self_ty`. + match self_ty.sty { + ty::Adt(..) | ty::Foreign(_) | + ty::Bool | ty::Char | ty::Str | + ty::Int(_) | ty::Uint(_) | ty::Float(_) if trait_ref.is_none() => { + return self.pretty_path_qualified(self_ty, trait_ref, ns); + } + _ => {} + } + let kept_within_component = mem::replace(&mut self.printer.keep_within_component, true); - let r = self.pretty_path_qualified(self_ty, trait_ref); + let r = self.pretty_path_qualified(self_ty, trait_ref, ns); self.printer.keep_within_component = kept_within_component; r } - fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path { - self.printer.write_str(text)?; - Ok(PrettyPath { empty: false }) - } fn path_append( self: &mut PrintCx<'_, '_, '_, Self>, _: Self::Path, diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index f629447fc64f..4cc18125e049 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -4239,6 +4239,7 @@ fn path_qualified( self: &mut PrintCx<'_, '_, 'tcx, Self>, self_ty: Ty<'tcx>, trait_ref: Option>, + _ns: Namespace, ) -> Self::Path { // This shouldn't ever be needed, but just in case: if let Some(trait_ref) = trait_ref { @@ -4247,9 +4248,6 @@ fn path_qualified( vec![format!("<{}>", self_ty)] } } - fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path { - vec![text.to_string()] - } fn path_append( self: &mut PrintCx<'_, '_, '_, Self>, mut path: Self::Path,