]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/ty/print/pretty.rs
Don't use spaces before type ascription like colons
[rust.git] / src / librustc / ty / print / pretty.rs
index 16d89343596878c0a042bec8052c3f5b57fdf33d..db539f9195c9d399f8aa4b4427c96968865445e2 100644 (file)
@@ -831,7 +831,11 @@ fn pretty_fn_sig(
         Ok(self)
     }
 
-    fn pretty_print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
+    fn pretty_print_const(
+        mut self,
+        ct: &'tcx ty::Const<'tcx>,
+        print_ty: bool,
+    ) -> Result<Self::Const, Self::Error> {
         define_scoped_cx!(self);
 
         if self.tcx().sess.verbose() {
@@ -839,32 +843,54 @@ fn pretty_print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const
             return Ok(self);
         }
 
+        macro_rules! print_underscore {
+            () => {{
+                p!(write("_"));
+                if print_ty {
+                    p!(write(": "), print(ct.ty));
+                }
+            }};
+        }
+
         match (ct.val, &ct.ty.kind) {
             (_, ty::FnDef(did, substs)) => p!(print_value_path(*did, substs)),
-            (ty::ConstKind::Unevaluated(did, substs), _) => match self.tcx().def_kind(did) {
-                Some(DefKind::Static) | Some(DefKind::Const) | Some(DefKind::AssocConst) => {
-                    p!(print_value_path(did, substs))
-                }
-                _ => {
-                    if did.is_local() {
-                        let span = self.tcx().def_span(did);
-                        if let Ok(snip) = self.tcx().sess.source_map().span_to_snippet(span) {
-                            p!(write("{}", snip))
-                        } else {
-                            p!(write("_: "), print(ct.ty))
+            (ty::ConstKind::Unevaluated(did, substs, promoted), _) => {
+                if let Some(promoted) = promoted {
+                    p!(print_value_path(did, substs));
+                    p!(write("::{:?}", promoted));
+                } else {
+                    match self.tcx().def_kind(did) {
+                        Some(DefKind::Static)
+                        | Some(DefKind::Const)
+                        | Some(DefKind::AssocConst) => p!(print_value_path(did, substs)),
+                        _ => {
+                            if did.is_local() {
+                                let span = self.tcx().def_span(did);
+                                if let Ok(snip) = self.tcx().sess.source_map().span_to_snippet(span)
+                                {
+                                    p!(write("{}", snip))
+                                } else {
+                                    print_underscore!()
+                                }
+                            } else {
+                                print_underscore!()
+                            }
                         }
-                    } else {
-                        p!(write("_: "), print(ct.ty))
                     }
                 }
-            },
-            (ty::ConstKind::Infer(..), _) => p!(write("_: "), print(ct.ty)),
+            }
+            (ty::ConstKind::Infer(..), _) => print_underscore!(),
             (ty::ConstKind::Param(ParamConst { name, .. }), _) => p!(write("{}", name)),
-            (ty::ConstKind::Value(value), _) => return self.pretty_print_const_value(value, ct.ty),
+            (ty::ConstKind::Value(value), _) => {
+                return self.pretty_print_const_value(value, ct.ty, print_ty);
+            }
 
             _ => {
                 // fallback
-                p!(write("{:?} : ", ct.val), print(ct.ty))
+                p!(write("{:?}", ct.val));
+                if print_ty {
+                    p!(write(": "), print(ct.ty));
+                }
             }
         };
         Ok(self)
@@ -874,6 +900,7 @@ fn pretty_print_const_value(
         mut self,
         ct: ConstValue<'tcx>,
         ty: Ty<'tcx>,
+        print_ty: bool,
     ) -> Result<Self::Const, Self::Error> {
         define_scoped_cx!(self);
 
@@ -980,7 +1007,10 @@ fn pretty_print_const_value(
                 };
                 if !printed {
                     // fallback
-                    p!(write("{:?} : ", ct), print(ty))
+                    p!(write("{:?}", ct));
+                    if print_ty {
+                        p!(write(": "), print(ty));
+                    }
                 }
             }
         };
@@ -1154,7 +1184,7 @@ fn print_dyn_existential(
     }
 
     fn print_const(self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
-        self.pretty_print_const(ct)
+        self.pretty_print_const(ct, true)
     }
 
     fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
@@ -1580,7 +1610,7 @@ impl<'tcx, T, U, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::OutlivesPredicat
     type Error = P::Error;
     fn print(&self, mut cx: P) -> Result<Self::Output, Self::Error> {
         define_scoped_cx!(cx);
-        p!(print(self.0), write(" : "), print(self.1));
+        p!(print(self.0), write(": "), print(self.1));
         Ok(cx)
     }
 }
@@ -1783,7 +1813,12 @@ pub fn print_only_trait_path(self) -> ty::Binder<TraitRefPrintOnlyTraitPath<'tcx
 
     ty::Predicate<'tcx> {
         match *self {
-            ty::Predicate::Trait(ref data) => p!(print(data)),
+            ty::Predicate::Trait(ref data, constness) => {
+                if let ast::Constness::Const = constness {
+                    p!(write("const "));
+                }
+                p!(print(data))
+            }
             ty::Predicate::Subtype(ref predicate) => p!(print(predicate)),
             ty::Predicate::RegionOutlives(ref predicate) => p!(print(predicate)),
             ty::Predicate::TypeOutlives(ref predicate) => p!(print(predicate)),