]> git.lizzy.rs Git - rust.git/commitdiff
rustc: explicitly pass the namespace to PrintCx::parameterized.
authorEduard-Mihai Burtescu <edy.burt@gmail.com>
Wed, 12 Dec 2018 13:12:48 +0000 (15:12 +0200)
committerEduard-Mihai Burtescu <edy.burt@gmail.com>
Fri, 15 Mar 2019 07:26:13 +0000 (09:26 +0200)
src/librustc/mir/mod.rs
src/librustc/ty/instance.rs
src/librustc/util/ppaux.rs
src/test/mir-opt/basic_assignment.rs
src/test/mir-opt/match_false_edges.rs
src/test/mir-opt/storage_ranges.rs

index 7d2050a7c68a889ae6fc75694e6ccf5bbe26b641..ec42809ff65699fcf4443a55e089211a766939c9 100644 (file)
@@ -2,7 +2,7 @@
 //!
 //! [rustc guide]: https://rust-lang.github.io/rustc-guide/mir/index.html
 
-use crate::hir::def::CtorKind;
+use crate::hir::def::{CtorKind, Namespace};
 use crate::hir::def_id::DefId;
 use crate::hir::{self, HirId, InlineAsm};
 use crate::mir::interpret::{ConstValue, EvalErrorKind, Scalar};
@@ -2405,7 +2405,7 @@ fn fmt_tuple(fmt: &mut Formatter<'_>, places: &[Operand<'_>]) -> fmt::Result {
                     AggregateKind::Adt(adt_def, variant, substs, _user_ty, _) => {
                         let variant_def = &adt_def.variants[variant];
 
-                        ppaux::parameterized(fmt, variant_def.did, substs)?;
+                        ppaux::parameterized(fmt, variant_def.did, substs, Namespace::ValueNS)?;
 
                         match variant_def.ctor_kind {
                             CtorKind::Const => Ok(()),
index 0a49dea7ec1bce319d55da74f211767ddd19edd0..fd0b97e98a3f933e4950148bff2aee2e92c7ccc0 100644 (file)
@@ -1,4 +1,5 @@
 use crate::hir::Unsafety;
+use crate::hir::def::Namespace;
 use crate::hir::def_id::DefId;
 use crate::ty::{self, Ty, PolyFnSig, TypeFoldable, SubstsRef, TyCtxt};
 use crate::traits;
@@ -175,7 +176,7 @@ pub fn requires_local<'a>(
 
 impl<'tcx> fmt::Display for Instance<'tcx> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        ppaux::parameterized(f, self.def_id(), self.substs)?;
+        ppaux::parameterized(f, self.def_id(), self.substs, Namespace::ValueNS)?;
         match self.def {
             InstanceDef::Item(_) => Ok(()),
             InstanceDef::VtableShim(_) => {
index 780ff8a61e41c55a96d482b85840b3b8950cdccc..94560606cad2ad147b368d35a5868a445e9b9987 100644 (file)
@@ -1,3 +1,4 @@
+use crate::hir::def::Namespace;
 use crate::hir::def_id::DefId;
 use crate::hir::map::definitions::DefPathData;
 use crate::middle::region;
@@ -285,26 +286,12 @@ fn fn_sig(
 
     fn parameterized(
         &mut self,
-        mut def_id: DefId,
+        def_id: DefId,
         substs: SubstsRef<'tcx>,
+        ns: Namespace,
         projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
     ) -> fmt::Result {
-        let mut key = self.tcx.def_key(def_id);
-        let is_value_ns = match key.disambiguated_data.data {
-            DefPathData::ValueNs(_) |
-            DefPathData::EnumVariant(_) => true,
-
-            // Skip `StructCtor` so that `Struct::<T>` will be printed,
-            // instead of the less pretty `Struct<T>::{{constructor}}`.
-            DefPathData::StructCtor => {
-                def_id.index = key.parent.unwrap();
-                key = self.tcx.def_key(def_id);
-                true
-            }
-
-            _ => false,
-        };
-
+        let key = self.tcx.def_key(def_id);
         let generics = self.tcx.generics_of(def_id);
 
         if let Some(parent_def_id) = generics.parent {
@@ -315,13 +302,20 @@ fn parameterized(
                 parent_generics.has_self && parent_generics.parent_count == 0;
             if parent_has_own_self {
                 print!(self, write("<"), print_display(substs.type_at(0)), write(" as "))?;
-            }
-            self.parameterized(parent_def_id, substs, iter::empty())?;
-            if parent_has_own_self {
+                self.parameterized(parent_def_id, substs, Namespace::TypeNS, iter::empty())?;
                 print!(self, write(">"))?;
+            } else {
+                self.parameterized(parent_def_id, substs, ns, iter::empty())?;
             }
 
-            print!(self, write("::{}", key.disambiguated_data.data.as_interned_str()))?;
+            // Skip `::{{constructor}}` on tuple/unit structs.
+            match key.disambiguated_data.data {
+                DefPathData::StructCtor => {}
+
+                _ => {
+                    print!(self, write("::{}", key.disambiguated_data.data.as_interned_str()))?;
+                }
+            }
         } else {
             // Try to print `impl`s more like how you'd refer to their associated items.
             if let DefPathData::Impl = key.disambiguated_data.data {
@@ -352,7 +346,7 @@ fn parameterized(
             }
         };
 
-        let start = if is_value_ns { "::<" } else { "<" };
+        let start = if ns == Namespace::ValueNS { "::<" } else { "<" };
 
         let has_own_self = generics.has_self && generics.parent_count == 0;
         let params = &generics.params[has_own_self as usize..];
@@ -496,10 +490,15 @@ fn is_name_used(&self, name: &InternedString) -> bool {
     }
 }
 
-pub fn parameterized<F: fmt::Write>(f: &mut F, did: DefId, substs: SubstsRef<'_>) -> fmt::Result {
+pub fn parameterized<F: fmt::Write>(
+    f: &mut F,
+    did: DefId,
+    substs: SubstsRef<'_>,
+    ns: Namespace,
+) -> fmt::Result {
     PrintCx::with(FmtPrinter { fmt: f }, |mut cx| {
         let substs = cx.tcx.lift(&substs).expect("could not lift for printing");
-        cx.parameterized(did, substs, iter::empty())
+        cx.parameterized(did, substs, ns, iter::empty())
     })
 }
 
@@ -538,6 +537,7 @@ fn print(&self, cx: &mut PrintCx<'_, '_, 'tcx, P>) -> Self::Output {
                     cx.parameterized(
                         principal.def_id,
                         principal.substs,
+                        Namespace::TypeNS,
                         self.projection_bounds(),
                     )?;
                 }
@@ -663,7 +663,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
             let trait_ref = *ty::Binder::bind(*self)
                 .with_self_ty(cx.tcx, dummy_self)
                 .skip_binder();
-            cx.parameterized(trait_ref.def_id, trait_ref.substs, iter::empty())
+            cx.parameterized(trait_ref.def_id, trait_ref.substs, Namespace::TypeNS, iter::empty())
         }
         debug {
             self.print_display(cx)
@@ -1112,12 +1112,16 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 define_print! {
     ('tcx) ty::TraitRef<'tcx>, (self, cx) {
         display {
-            cx.parameterized(self.def_id, self.substs, iter::empty())
+            cx.parameterized(self.def_id, self.substs, Namespace::TypeNS, iter::empty())
         }
         debug {
-            print!(cx, write("<"), print(self.self_ty()), write(" as "))?;
-            cx.parameterized(self.def_id, self.substs, iter::empty())?;
-            print!(cx, write(">"))
+            print!(cx,
+                write("<"),
+                print(self.self_ty()),
+                write(" as "),
+                print_display(self),
+                write(">")
+            )
         }
     }
 }
@@ -1163,7 +1167,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                 FnDef(def_id, substs) => {
                     let sig = cx.tcx.fn_sig(def_id).subst(cx.tcx, substs);
                     print!(cx, print(sig), write(" {{"))?;
-                    cx.parameterized(def_id, substs, iter::empty())?;
+                    cx.parameterized(def_id, substs, Namespace::ValueNS, iter::empty())?;
                     print!(cx, write("}}"))
                 }
                 FnPtr(ref bare_fn) => {
@@ -1185,7 +1189,9 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                         ty::BoundTyKind::Param(p) => print!(cx, write("{}", p)),
                     }
                 }
-                Adt(def, substs) => cx.parameterized(def.did, substs, iter::empty()),
+                Adt(def, substs) => {
+                    cx.parameterized(def.did, substs, Namespace::TypeNS, iter::empty())
+                }
                 Dynamic(data, r) => {
                     let print_r = r.display_outputs_anything(cx);
                     if print_r {
@@ -1199,7 +1205,12 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                     Ok(())
                 }
                 Foreign(def_id) => {
-                    cx.parameterized(def_id, subst::InternalSubsts::empty(), iter::empty())
+                    cx.parameterized(
+                        def_id,
+                        subst::InternalSubsts::empty(),
+                        Namespace::TypeNS,
+                        iter::empty(),
+                    )
                 }
                 Projection(ref data) => data.print(cx),
                 UnnormalizedProjection(ref data) => {
@@ -1481,7 +1492,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 define_print! {
     ('tcx) ty::ProjectionTy<'tcx>, (self, cx) {
         display {
-            cx.parameterized(self.item_def_id, self.substs, iter::empty())
+            cx.parameterized(self.item_def_id, self.substs, Namespace::TypeNS, iter::empty())
         }
     }
 }
@@ -1518,7 +1529,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                 }
                 ty::Predicate::ConstEvaluatable(def_id, substs) => {
                     print!(cx, write("the constant `"))?;
-                    cx.parameterized(def_id, substs, iter::empty())?;
+                    cx.parameterized(def_id, substs, Namespace::ValueNS, iter::empty())?;
                     print!(cx, write("` can be evaluated"))
                 }
             }
index 1bbbe67a12cb8f990274ed7818be22877fba0f56..3ce43cc4a224fa1f443145bbffba5aaaca0246b7 100644 (file)
@@ -35,7 +35,7 @@ fn main() {
 //        _2 = move _3;
 //        StorageDead(_3);
 //        StorageLive(_4);
-//        _4 = std::option::Option<std::boxed::Box<u32>>::None;
+//        _4 = std::option::Option::<std::boxed::Box<u32>>::None;
 //        FakeRead(ForLet, _4);
 //        AscribeUserType(_4, o, UserTypeProjection { base: UserType(1), projs: [] });
 //        StorageLive(_5);
index ab6de71d2894d49488ebbbcbd3e89dc8da918a97..9eeef8570a37a307f86bd0e0f0fb90d075c6cabc 100644 (file)
@@ -42,7 +42,7 @@ fn main() {
 // START rustc.full_tested_match.QualifyAndPromoteConstants.after.mir
 //  bb0: {
 //      ...
-//      _2 = std::option::Option<i32>::Some(const 42i32,);
+//      _2 = std::option::Option::<i32>::Some(const 42i32,);
 //      FakeRead(ForMatchedPlace, _2);
 //      _3 = discriminant(_2);
 //      switchInt(move _3) -> [0isize: bb4, 1isize: bb2, otherwise: bb7];
@@ -111,7 +111,7 @@ fn main() {
 // START rustc.full_tested_match2.QualifyAndPromoteConstants.before.mir
 //  bb0: {
 //      ...
-//      _2 = std::option::Option<i32>::Some(const 42i32,);
+//      _2 = std::option::Option::<i32>::Some(const 42i32,);
 //      FakeRead(ForMatchedPlace, _2);
 //      _3 = discriminant(_2);
 //      switchInt(move _3) -> [0isize: bb3, 1isize: bb2, otherwise: bb7];
@@ -180,7 +180,7 @@ fn main() {
 // START rustc.main.QualifyAndPromoteConstants.before.mir
 // bb0: {
 //     ...
-//      _2 = std::option::Option<i32>::Some(const 1i32,);
+//      _2 = std::option::Option::<i32>::Some(const 1i32,);
 //      FakeRead(ForMatchedPlace, _2);
 //      _3 = discriminant(_2);
 //      switchInt(move _3) -> [1isize: bb2, otherwise: bb3];
index a5d6ced2b1772a29e26c2a66baf9aec536a35849..9a22f57116ed89f7a0997e2d32a494a096f39daf 100644 (file)
@@ -18,7 +18,7 @@ fn main() {
 //         StorageLive(_4);
 //         StorageLive(_5);
 //         _5 = _1;
-//         _4 = std::option::Option<i32>::Some(move _5,);
+//         _4 = std::option::Option::<i32>::Some(move _5,);
 //         StorageDead(_5);
 //         _3 = &_4;
 //         FakeRead(ForLet, _3);