about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEduard-Mihai Burtescu <edy.burt@gmail.com>2018-12-28 06:09:22 +0200
committerEduard-Mihai Burtescu <edy.burt@gmail.com>2019-03-15 13:25:10 +0200
commitdf6650f38cb2bed40fb1b1fe5d6a6e08dab963e2 (patch)
tree191436e32bb20c36daf53c6cae291d07d6d4ea96 /src
parent39fd54a418b08051623505ff5263fe8714f40b4c (diff)
downloadrust-df6650f38cb2bed40fb1b1fe5d6a6e08dab963e2.tar.gz
rust-df6650f38cb2bed40fb1b1fe5d6a6e08dab963e2.zip
rustc: move `...::<impl ...>` printing into `pretty_path_qualified`.
Diffstat (limited to 'src')
-rw-r--r--src/librustc/ty/print.rs39
-rw-r--r--src/librustc/util/ppaux.rs2
-rw-r--r--src/librustc_codegen_utils/symbol_names.rs41
-rw-r--r--src/librustdoc/clean/mod.rs9
4 files changed, 68 insertions, 23 deletions
diff --git a/src/librustc/ty/print.rs b/src/librustc/ty/print.rs
index e0a5a2fcf84..77768975e25 100644
--- a/src/librustc/ty/print.rs
+++ b/src/librustc/ty/print.rs
@@ -173,6 +173,7 @@ pub trait Printer: Sized {
     #[must_use]
     fn path_qualified(
         self: &mut PrintCx<'_, '_, 'tcx, Self>,
+        impl_prefix: Option<Self::Path>,
         self_ty: Ty<'tcx>,
         trait_ref: Option<ty::TraitRef<'tcx>>,
         ns: Namespace,
@@ -301,7 +302,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
                         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), ns)
+                        self.path_qualified(None, trait_ref.self_ty(), Some(trait_ref), ns)
                     } else {
                         self.print_def_path(parent_def_id, substs, ns, iter::empty())
                     }
@@ -357,21 +358,18 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
             Some(trait_ref) => self.tcx.parent(trait_ref.def_id) == Some(parent_def_id),
         };
 
-        if !in_self_mod && !in_trait_mod {
+        let prefix_path = if !in_self_mod && !in_trait_mod {
             // If the impl is not co-located with either self-type or
             // trait-type, then fallback to a format that identifies
             // the module more clearly.
-            let path = self.print_def_path(parent_def_id, None, ns, iter::empty());
-            if let Some(trait_ref) = impl_trait_ref {
-                return self.path_append(path, &format!("<impl {} for {}>", trait_ref, self_ty));
-            } else {
-                return self.path_append(path, &format!("<impl {}>", self_ty));
-            }
-        }
+            Some(self.print_def_path(parent_def_id, None, ns, iter::empty()))
+        } else {
+            // Otherwise, try to give a good form that would be valid language
+            // syntax. Preferably using associated item notation.
+            None
+        };
 
-        // Otherwise, try to give a good form that would be valid language
-        // syntax. Preferably using associated item notation.
-        self.path_qualified(self_ty, impl_trait_ref, ns)
+        self.path_qualified(prefix_path, self_ty, impl_trait_ref, ns)
     }
 }
 
@@ -561,10 +559,24 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
 
     pub fn pretty_path_qualified(
         &mut self,
+        impl_prefix: Option<P::Path>,
         self_ty: Ty<'tcx>,
         trait_ref: Option<ty::TraitRef<'tcx>>,
         ns: Namespace,
     ) -> P::Path {
+        if let Some(prefix) = impl_prefix {
+            // HACK(eddyb) going through `path_append` means symbol name
+            // computation gets to handle its equivalent of `::` correctly.
+            let _ = self.path_append(prefix, "<impl ")?;
+            if let Some(trait_ref) = trait_ref {
+                trait_ref.print_display(self)?;
+                write!(self.printer, " for ")?;
+            }
+            self_ty.print_display(self)?;
+            write!(self.printer, ">")?;
+            return Ok(PrettyPath { empty: false });
+        }
+
         if trait_ref.is_none() {
             // Inherent impls. Try to print `Foo::bar` for an inherent
             // impl on `Foo`, but fallback to `<Foo>::bar` if self-type is
@@ -774,11 +786,12 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
     }
     fn path_qualified(
         self: &mut PrintCx<'_, '_, 'tcx, Self>,
+        impl_prefix: Option<Self::Path>,
         self_ty: Ty<'tcx>,
         trait_ref: Option<ty::TraitRef<'tcx>>,
         ns: Namespace,
     ) -> Self::Path {
-        self.pretty_path_qualified(self_ty, trait_ref, ns)
+        self.pretty_path_qualified(impl_prefix, 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 3c34d254099..6384a2af8ef 100644
--- a/src/librustc/util/ppaux.rs
+++ b/src/librustc/util/ppaux.rs
@@ -1007,7 +1007,7 @@ define_print! {
             Ok(())
         }
         debug {
-            let _ = cx.path_qualified(self.self_ty(), Some(*self), Namespace::TypeNS)?;
+            let _ = cx.path_qualified(None, 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 0ea141b6574..c4de355aa44 100644
--- a/src/librustc_codegen_utils/symbol_names.rs
+++ b/src/librustc_codegen_utils/symbol_names.rs
@@ -387,7 +387,7 @@ impl SymbolPath {
     }
 
     fn finalize_pending_component(&mut self) {
-        if !self.keep_within_component && !self.temp_buf.is_empty() {
+        if !self.temp_buf.is_empty() {
             let _ = write!(self.result, "{}{}", self.temp_buf.len(), self.temp_buf);
             self.temp_buf.clear();
         }
@@ -414,6 +414,7 @@ impl Printer for SymbolPath {
     }
     fn path_qualified(
         self: &mut PrintCx<'_, '_, 'tcx, Self>,
+        impl_prefix: Option<Self::Path>,
         self_ty: Ty<'tcx>,
         trait_ref: Option<ty::TraitRef<'tcx>>,
         ns: Namespace,
@@ -423,25 +424,51 @@ impl Printer for SymbolPath {
         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);
+            ty::Int(_) | ty::Uint(_) | ty::Float(_)
+                if impl_prefix.is_none() && trait_ref.is_none() =>
+            {
+                return self.pretty_path_qualified(None, self_ty, trait_ref, ns);
             }
             _ => {}
         }
 
+        // HACK(eddyb) make sure to finalize the last component of the
+        // `impl` prefix, to avoid it fusing with the following text.
+        let impl_prefix = impl_prefix.map(|prefix| {
+            let mut prefix = self.path_append(prefix, "")?;
+
+            // HACK(eddyb) also avoid an unnecessary `::`.
+            prefix.empty = true;
+
+            Ok(prefix)
+        });
+
         let kept_within_component = mem::replace(&mut self.printer.keep_within_component, true);
-        let r = self.pretty_path_qualified(self_ty, trait_ref, ns);
+        let r = self.pretty_path_qualified(impl_prefix, self_ty, trait_ref, ns);
         self.printer.keep_within_component = kept_within_component;
         r
     }
     fn path_append(
         self: &mut PrintCx<'_, '_, '_, Self>,
-        _: Self::Path,
+        path: Self::Path,
         text: &str,
     ) -> Self::Path {
-        self.printer.finalize_pending_component();
+        let mut path = path?;
+
+        if self.keep_within_component {
+            // HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
+            if !path.empty {
+                self.printer.write_str("::")?;
+            } else {
+                path.empty = text.is_empty();
+            }
+        } else {
+            self.printer.finalize_pending_component();
+            path.empty = false;
+        }
+
         self.printer.write_str(text)?;
-        Ok(PrettyPath { empty: false })
+        Ok(path)
     }
     fn path_generic_args(
         self: &mut PrintCx<'_, '_, 'tcx, Self>,
diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs
index 4cc18125e04..bfc7c7859f5 100644
--- a/src/librustdoc/clean/mod.rs
+++ b/src/librustdoc/clean/mod.rs
@@ -4237,16 +4237,21 @@ where F: Fn(DefId) -> Def {
         }
         fn path_qualified(
             self: &mut PrintCx<'_, '_, 'tcx, Self>,
+            impl_prefix: Option<Self::Path>,
             self_ty: Ty<'tcx>,
             trait_ref: Option<ty::TraitRef<'tcx>>,
             _ns: Namespace,
         ) -> Self::Path {
+            let mut path = impl_prefix.unwrap_or(vec![]);
+
             // This shouldn't ever be needed, but just in case:
             if let Some(trait_ref) = trait_ref {
-                vec![format!("{:?}", trait_ref)]
+                path.push(format!("{:?}", trait_ref));
             } else {
-                vec![format!("<{}>", self_ty)]
+                path.push(format!("<{}>", self_ty));
             }
+
+            path
         }
         fn path_append(
             self: &mut PrintCx<'_, '_, '_, Self>,