From 238616813974babc53cb85aaa8e102df119d7f0f Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Wed, 12 Dec 2018 15:12:48 +0200 Subject: [PATCH] rustc: explicitly pass the namespace to PrintCx::parameterized. --- src/librustc/mir/mod.rs | 4 +- src/librustc/ty/instance.rs | 3 +- src/librustc/util/ppaux.rs | 79 +++++++++++++++------------ src/test/mir-opt/basic_assignment.rs | 2 +- src/test/mir-opt/match_false_edges.rs | 6 +- src/test/mir-opt/storage_ranges.rs | 2 +- 6 files changed, 54 insertions(+), 42 deletions(-) diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index 7d2050a7c68..ec42809ff65 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -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(()), diff --git a/src/librustc/ty/instance.rs b/src/librustc/ty/instance.rs index 0a49dea7ec1..fd0b97e98a3 100644 --- a/src/librustc/ty/instance.rs +++ b/src/librustc/ty/instance.rs @@ -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(_) => { diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index 780ff8a61e4..94560606cad 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -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>, ) -> 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::` will be printed, - // instead of the less pretty `Struct::{{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: &mut F, did: DefId, substs: SubstsRef<'_>) -> fmt::Result { +pub fn parameterized( + 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")) } } diff --git a/src/test/mir-opt/basic_assignment.rs b/src/test/mir-opt/basic_assignment.rs index 1bbbe67a12c..3ce43cc4a22 100644 --- a/src/test/mir-opt/basic_assignment.rs +++ b/src/test/mir-opt/basic_assignment.rs @@ -35,7 +35,7 @@ fn main() { // _2 = move _3; // StorageDead(_3); // StorageLive(_4); -// _4 = std::option::Option>::None; +// _4 = std::option::Option::>::None; // FakeRead(ForLet, _4); // AscribeUserType(_4, o, UserTypeProjection { base: UserType(1), projs: [] }); // StorageLive(_5); diff --git a/src/test/mir-opt/match_false_edges.rs b/src/test/mir-opt/match_false_edges.rs index ab6de71d289..9eeef8570a3 100644 --- a/src/test/mir-opt/match_false_edges.rs +++ b/src/test/mir-opt/match_false_edges.rs @@ -42,7 +42,7 @@ fn main() { // START rustc.full_tested_match.QualifyAndPromoteConstants.after.mir // bb0: { // ... -// _2 = std::option::Option::Some(const 42i32,); +// _2 = std::option::Option::::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::Some(const 42i32,); +// _2 = std::option::Option::::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::Some(const 1i32,); +// _2 = std::option::Option::::Some(const 1i32,); // FakeRead(ForMatchedPlace, _2); // _3 = discriminant(_2); // switchInt(move _3) -> [1isize: bb2, otherwise: bb3]; diff --git a/src/test/mir-opt/storage_ranges.rs b/src/test/mir-opt/storage_ranges.rs index a5d6ced2b17..9a22f57116e 100644 --- a/src/test/mir-opt/storage_ranges.rs +++ b/src/test/mir-opt/storage_ranges.rs @@ -18,7 +18,7 @@ fn main() { // StorageLive(_4); // StorageLive(_5); // _5 = _1; -// _4 = std::option::Option::Some(move _5,); +// _4 = std::option::Option::::Some(move _5,); // StorageDead(_5); // _3 = &_4; // FakeRead(ForLet, _3); -- 2.44.0