Skip to content

Commit 87c27fd

Browse files
committed
rustc: explicitly pass the namespace to PrintCx::parameterized.
1 parent b3073f4 commit 87c27fd

File tree

6 files changed

+51
-42
lines changed

6 files changed

+51
-42
lines changed

src/librustc/mir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! [rustc guide]: https://rust-lang.github.io/rustc-guide/mir/index.html
44
5-
use hir::def::CtorKind;
5+
use hir::def::{CtorKind, Namespace};
66
use hir::def_id::DefId;
77
use hir::{self, HirId, InlineAsm};
88
use mir::interpret::{ConstValue, EvalErrorKind, Scalar};
@@ -2377,7 +2377,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
23772377
AggregateKind::Adt(adt_def, variant, substs, _user_ty, _) => {
23782378
let variant_def = &adt_def.variants[variant];
23792379

2380-
ppaux::parameterized(fmt, variant_def.did, substs)?;
2380+
ppaux::parameterized(fmt, variant_def.did, substs, Namespace::ValueNS)?;
23812381

23822382
match variant_def.ctor_kind {
23832383
CtorKind::Const => Ok(()),

src/librustc/ty/instance.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use hir::Unsafety;
2+
use hir::def::Namespace;
23
use hir::def_id::DefId;
34
use ty::{self, Ty, PolyFnSig, TypeFoldable, Substs, TyCtxt};
45
use traits;
@@ -174,7 +175,7 @@ impl<'tcx> InstanceDef<'tcx> {
174175

175176
impl<'tcx> fmt::Display for Instance<'tcx> {
176177
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
177-
ppaux::parameterized(f, self.def_id(), self.substs)?;
178+
ppaux::parameterized(f, self.def_id(), self.substs, Namespace::ValueNS)?;
178179
match self.def {
179180
InstanceDef::Item(_) => Ok(()),
180181
InstanceDef::VtableShim(_) => {

src/librustc/util/ppaux.rs

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use hir::def::Namespace;
12
use hir::def_id::DefId;
23
use hir::map::definitions::DefPathData;
34
use middle::region;
@@ -285,26 +286,12 @@ impl<F: fmt::Write> PrintCx<'a, 'gcx, 'tcx, FmtPrinter<F>> {
285286

286287
fn parameterized(
287288
&mut self,
288-
mut def_id: DefId,
289+
def_id: DefId,
289290
substs: &Substs<'tcx>,
291+
ns: Namespace,
290292
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
291293
) -> fmt::Result {
292-
let mut key = self.tcx.def_key(def_id);
293-
let is_value_ns = match key.disambiguated_data.data {
294-
DefPathData::ValueNs(_) |
295-
DefPathData::EnumVariant(_) => true,
296-
297-
// Skip `StructCtor` so that `Struct::<T>` will be printed,
298-
// instead of the less pretty `Struct<T>::{{constructor}}`.
299-
DefPathData::StructCtor => {
300-
def_id.index = key.parent.unwrap();
301-
key = self.tcx.def_key(def_id);
302-
true
303-
}
304-
305-
_ => false,
306-
};
307-
294+
let key = self.tcx.def_key(def_id);
308295
let generics = self.tcx.generics_of(def_id);
309296

310297
if let Some(parent_def_id) = generics.parent {
@@ -315,13 +302,20 @@ impl<F: fmt::Write> PrintCx<'a, 'gcx, 'tcx, FmtPrinter<F>> {
315302
parent_generics.has_self && parent_generics.parent_count == 0;
316303
if parent_has_own_self {
317304
print!(self, write("<"), print_display(substs.type_at(0)), write(" as "))?;
318-
}
319-
self.parameterized(parent_def_id, substs, iter::empty())?;
320-
if parent_has_own_self {
305+
self.parameterized(parent_def_id, substs, Namespace::TypeNS, iter::empty())?;
321306
print!(self, write(">"))?;
307+
} else {
308+
self.parameterized(parent_def_id, substs, ns, iter::empty())?;
322309
}
323310

324-
print!(self, write("::{}", key.disambiguated_data.data.as_interned_str()))?;
311+
// Skip `::{{constructor}}` on tuple/unit structs.
312+
match key.disambiguated_data.data {
313+
DefPathData::StructCtor => {}
314+
315+
_ => {
316+
print!(self, write("::{}", key.disambiguated_data.data.as_interned_str()))?;
317+
}
318+
}
325319
} else {
326320
// Try to print `impl`s more like how you'd refer to their associated items.
327321
if let DefPathData::Impl = key.disambiguated_data.data {
@@ -352,7 +346,7 @@ impl<F: fmt::Write> PrintCx<'a, 'gcx, 'tcx, FmtPrinter<F>> {
352346
}
353347
};
354348

355-
let start = if is_value_ns { "::<" } else { "<" };
349+
let start = if ns == Namespace::ValueNS { "::<" } else { "<" };
356350

357351
let has_own_self = generics.has_self && generics.parent_count == 0;
358352
let params = &generics.params[has_own_self as usize..];
@@ -491,10 +485,15 @@ impl<F: fmt::Write> PrintCx<'a, 'gcx, 'tcx, FmtPrinter<F>> {
491485
}
492486
}
493487

494-
pub fn parameterized<F: fmt::Write>(f: &mut F, did: DefId, substs: &Substs<'_>) -> fmt::Result {
488+
pub fn parameterized<F: fmt::Write>(
489+
f: &mut F,
490+
did: DefId,
491+
substs: &Substs<'_>,
492+
ns: Namespace,
493+
) -> fmt::Result {
495494
PrintCx::with(FmtPrinter { fmt: f }, |mut cx| {
496495
let substs = cx.tcx.lift(&substs).expect("could not lift for printing");
497-
cx.parameterized(did, substs, iter::empty())
496+
cx.parameterized(did, substs, ns, iter::empty())
498497
})
499498
}
500499

@@ -533,6 +532,7 @@ define_print! {
533532
cx.parameterized(
534533
principal.def_id,
535534
principal.substs,
535+
Namespace::TypeNS,
536536
self.projection_bounds(),
537537
)?;
538538
}
@@ -657,7 +657,7 @@ define_print! {
657657
let trait_ref = *ty::Binder::bind(*self)
658658
.with_self_ty(cx.tcx, dummy_self)
659659
.skip_binder();
660-
cx.parameterized(trait_ref.def_id, trait_ref.substs, iter::empty())
660+
cx.parameterized(trait_ref.def_id, trait_ref.substs, Namespace::TypeNS, iter::empty())
661661
}
662662
debug {
663663
self.print_display(cx)
@@ -1100,12 +1100,16 @@ define_print_multi! {
11001100
define_print! {
11011101
('tcx) ty::TraitRef<'tcx>, (self, cx) {
11021102
display {
1103-
cx.parameterized(self.def_id, self.substs, iter::empty())
1103+
cx.parameterized(self.def_id, self.substs, Namespace::TypeNS, iter::empty())
11041104
}
11051105
debug {
1106-
print!(cx, write("<"), print(self.self_ty()), write(" as "))?;
1107-
cx.parameterized(self.def_id, self.substs, iter::empty())?;
1108-
print!(cx, write(">"))
1106+
print!(cx,
1107+
write("<"),
1108+
print(self.self_ty()),
1109+
write(" as "),
1110+
print_display(self),
1111+
write(">")
1112+
)
11091113
}
11101114
}
11111115
}
@@ -1151,7 +1155,7 @@ define_print! {
11511155
FnDef(def_id, substs) => {
11521156
let sig = cx.tcx.fn_sig(def_id).subst(cx.tcx, substs);
11531157
print!(cx, print(sig), write(" {{"))?;
1154-
cx.parameterized(def_id, substs, iter::empty())?;
1158+
cx.parameterized(def_id, substs, Namespace::ValueNS, iter::empty())?;
11551159
print!(cx, write("}}"))
11561160
}
11571161
FnPtr(ref bare_fn) => {
@@ -1173,7 +1177,9 @@ define_print! {
11731177
ty::BoundTyKind::Param(p) => print!(cx, write("{}", p)),
11741178
}
11751179
}
1176-
Adt(def, substs) => cx.parameterized(def.did, substs, iter::empty()),
1180+
Adt(def, substs) => {
1181+
cx.parameterized(def.did, substs, Namespace::TypeNS, iter::empty())
1182+
}
11771183
Dynamic(data, r) => {
11781184
let print_r = r.display_outputs_anything(cx);
11791185
if print_r {
@@ -1186,7 +1192,9 @@ define_print! {
11861192
}
11871193
Ok(())
11881194
}
1189-
Foreign(def_id) => cx.parameterized(def_id, Substs::empty(), iter::empty()),
1195+
Foreign(def_id) => {
1196+
cx.parameterized(def_id, Substs::empty(), Namespace::TypeNS, iter::empty())
1197+
}
11901198
Projection(ref data) => data.print(cx),
11911199
UnnormalizedProjection(ref data) => {
11921200
print!(cx, write("Unnormalized("))?;
@@ -1411,7 +1419,7 @@ define_print! {
14111419
define_print! {
14121420
('tcx) ty::ProjectionTy<'tcx>, (self, cx) {
14131421
display {
1414-
cx.parameterized(self.item_def_id, self.substs, iter::empty())
1422+
cx.parameterized(self.item_def_id, self.substs, Namespace::TypeNS, iter::empty())
14151423
}
14161424
}
14171425
}
@@ -1448,7 +1456,7 @@ define_print! {
14481456
}
14491457
ty::Predicate::ConstEvaluatable(def_id, substs) => {
14501458
print!(cx, write("the constant `"))?;
1451-
cx.parameterized(def_id, substs, iter::empty())?;
1459+
cx.parameterized(def_id, substs, Namespace::ValueNS, iter::empty())?;
14521460
print!(cx, write("` can be evaluated"))
14531461
}
14541462
}

src/test/mir-opt/basic_assignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn main() {
3535
// _2 = move _3;
3636
// StorageDead(_3);
3737
// StorageLive(_4);
38-
// _4 = std::option::Option<std::boxed::Box<u32>>::None;
38+
// _4 = std::option::Option::<std::boxed::Box<u32>>::None;
3939
// FakeRead(ForLet, _4);
4040
// AscribeUserType(_4, o, UserTypeProjection { base: UserType(1), projs: [] });
4141
// StorageLive(_5);

src/test/mir-opt/match_false_edges.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn main() {
4242
// START rustc.full_tested_match.QualifyAndPromoteConstants.after.mir
4343
// bb0: {
4444
// ...
45-
// _2 = std::option::Option<i32>::Some(const 42i32,);
45+
// _2 = std::option::Option::<i32>::Some(const 42i32,);
4646
// FakeRead(ForMatchedPlace, _2);
4747
// _7 = discriminant(_2);
4848
// _9 = &shallow (promoted[2]: std::option::Option<i32>);
@@ -116,7 +116,7 @@ fn main() {
116116
// START rustc.full_tested_match2.QualifyAndPromoteConstants.before.mir
117117
// bb0: {
118118
// ...
119-
// _2 = std::option::Option<i32>::Some(const 42i32,);
119+
// _2 = std::option::Option::<i32>::Some(const 42i32,);
120120
// FakeRead(ForMatchedPlace, _2);
121121
// _7 = discriminant(_2);
122122
// _9 = &shallow _2;
@@ -190,7 +190,7 @@ fn main() {
190190
// START rustc.main.QualifyAndPromoteConstants.before.mir
191191
// bb0: {
192192
// ...
193-
// _2 = std::option::Option<i32>::Some(const 1i32,);
193+
// _2 = std::option::Option::<i32>::Some(const 1i32,);
194194
// FakeRead(ForMatchedPlace, _2);
195195
// _11 = discriminant(_2);
196196
// _16 = &shallow _2;

src/test/mir-opt/storage_ranges.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn main() {
1818
// StorageLive(_4);
1919
// StorageLive(_5);
2020
// _5 = _1;
21-
// _4 = std::option::Option<i32>::Some(move _5,);
21+
// _4 = std::option::Option::<i32>::Some(move _5,);
2222
// StorageDead(_5);
2323
// _3 = &_4;
2424
// FakeRead(ForLet, _3);

0 commit comments

Comments
 (0)