Skip to content

Commit 241ca4f

Browse files
committed
rustc: provide DisambiguatedDefPathData in ty::print.
1 parent d949187 commit 241ca4f

File tree

17 files changed

+129
-73
lines changed

17 files changed

+129
-73
lines changed

src/librustc/hir/map/definitions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,13 +679,13 @@ impl DefPathData {
679679
return name
680680
}
681681
// note that this does not show up in user printouts
682-
CrateRoot => "{{root}}",
682+
CrateRoot => "{{crate}}",
683683
Impl => "{{impl}}",
684-
Misc => "{{?}}",
684+
Misc => "{{misc}}",
685685
ClosureExpr => "{{closure}}",
686686
StructCtor => "{{constructor}}",
687687
AnonConst => "{{constant}}",
688-
ImplTrait => "{{impl-Trait}}",
688+
ImplTrait => "{{opaque}}",
689689
};
690690

691691
Symbol::intern(s).as_interned_str()

src/librustc/infer/error_reporting/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
445445
sp: Span,
446446
) {
447447
use hir::def_id::CrateNum;
448+
use hir::map::DisambiguatedDefPathData;
448449
use ty::print::Printer;
449450
use ty::subst::Kind;
450451

@@ -504,6 +505,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
504505
fn path_append_impl(
505506
self,
506507
_print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
508+
_disambiguated_data: &DisambiguatedDefPathData,
507509
_self_ty: Ty<'tcx>,
508510
_trait_ref: Option<ty::TraitRef<'tcx>>,
509511
) -> Result<Self::Path, Self::Error> {
@@ -512,10 +514,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
512514
fn path_append(
513515
self,
514516
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
515-
text: &str,
517+
disambiguated_data: &DisambiguatedDefPathData,
516518
) -> Result<Self::Path, Self::Error> {
517519
let mut path = print_prefix(self)?;
518-
path.push(text.to_string());
520+
path.push(disambiguated_data.data.as_interned_str().to_string());
519521
Ok(path)
520522
}
521523
fn path_generic_args(

src/librustc/ty/print/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::hir::map::DefPathData;
1+
use crate::hir::map::{DefPathData, DisambiguatedDefPathData};
22
use crate::hir::def_id::{CrateNum, DefId};
33
use crate::ty::{self, DefIdTree, Ty, TyCtxt};
44
use crate::ty::subst::{Kind, Subst};
@@ -71,13 +71,14 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized {
7171
fn path_append_impl(
7272
self,
7373
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
74+
disambiguated_data: &DisambiguatedDefPathData,
7475
self_ty: Ty<'tcx>,
7576
trait_ref: Option<ty::TraitRef<'tcx>>,
7677
) -> Result<Self::Path, Self::Error>;
7778
fn path_append(
7879
self,
7980
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
80-
text: &str,
81+
disambiguated_data: &DisambiguatedDefPathData,
8182
) -> Result<Self::Path, Self::Error>;
8283
fn path_generic_args(
8384
self,
@@ -156,7 +157,7 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized {
156157
} else {
157158
cx.print_def_path(parent_def_id, parent_substs)
158159
},
159-
&key.disambiguated_data.data.as_interned_str().as_str(),
160+
&key.disambiguated_data,
160161
)
161162
}
162163
}
@@ -200,12 +201,14 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized {
200201
debug!("default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}",
201202
impl_def_id, self_ty, impl_trait_ref);
202203

204+
let key = self.tcx().def_key(impl_def_id);
205+
let parent_def_id = DefId { index: key.parent.unwrap(), ..impl_def_id };
206+
203207
// Decide whether to print the parent path for the impl.
204208
// Logically, since impls are global, it's never needed, but
205209
// users may find it useful. Currently, we omit the parent if
206210
// the impl is either in the same module as the self-type or
207211
// as the trait.
208-
let parent_def_id = self.tcx().parent(impl_def_id).unwrap();
209212
let in_self_mod = match characteristic_def_id_of_type(self_ty) {
210213
None => false,
211214
Some(ty_def_id) => self.tcx().parent(ty_def_id) == Some(parent_def_id),
@@ -221,6 +224,7 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized {
221224
// the module more clearly.
222225
self.path_append_impl(
223226
|cx| cx.print_def_path(parent_def_id, &[]),
227+
&key.disambiguated_data,
224228
self_ty,
225229
impl_trait_ref,
226230
)

src/librustc/ty/print/pretty.rs

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::hir;
22
use crate::hir::def::Namespace;
3-
use crate::hir::map::DefPathData;
3+
use crate::hir::map::{DefPathData, DisambiguatedDefPathData};
44
use crate::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
55
use crate::middle::cstore::{ExternCrate, ExternCrateSource};
66
use crate::middle::region;
@@ -313,13 +313,13 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
313313
visible_parent, actual_parent,
314314
);
315315

316-
let data = cur_def_key.disambiguated_data.data;
316+
let mut data = cur_def_key.disambiguated_data.data;
317317
debug!(
318318
"try_print_visible_def_path: data={:?} visible_parent={:?} actual_parent={:?}",
319319
data, visible_parent, actual_parent,
320320
);
321321

322-
let symbol = match data {
322+
match data {
323323
// In order to output a path that could actually be imported (valid and visible),
324324
// we need to handle re-exports correctly.
325325
//
@@ -351,27 +351,30 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
351351
// the children of the visible parent (as was done when computing
352352
// `visible_parent_map`), looking for the specific child we currently have and then
353353
// have access to the re-exported name.
354-
DefPathData::Module(actual_name) |
355-
DefPathData::TypeNs(actual_name) if Some(visible_parent) != actual_parent => {
356-
self.tcx().item_children(visible_parent)
354+
DefPathData::Module(ref mut name) |
355+
DefPathData::TypeNs(ref mut name) if Some(visible_parent) != actual_parent => {
356+
let reexport = self.tcx().item_children(visible_parent)
357357
.iter()
358358
.find(|child| child.def.def_id() == def_id)
359-
.map(|child| child.ident.as_str())
360-
.unwrap_or_else(|| actual_name.as_str())
359+
.map(|child| child.ident.as_interned_str());
360+
if let Some(reexport) = reexport {
361+
*name = reexport;
362+
}
361363
}
362-
_ => {
363-
data.get_opt_name().map(|n| n.as_str()).unwrap_or_else(|| {
364-
// Re-exported `extern crate` (#43189).
365-
if let DefPathData::CrateRoot = data {
366-
self.tcx().original_crate_name(def_id.krate).as_str()
367-
} else {
368-
Symbol::intern("<unnamed>").as_str()
369-
}
370-
})
371-
},
372-
};
373-
debug!("try_print_visible_def_path: symbol={:?}", symbol);
374-
Ok((self.path_append(Ok, &symbol)?, true))
364+
// Re-exported `extern crate` (#43189).
365+
DefPathData::CrateRoot => {
366+
data = DefPathData::Module(
367+
self.tcx().original_crate_name(def_id.krate).as_interned_str(),
368+
);
369+
}
370+
_ => {}
371+
}
372+
debug!("try_print_visible_def_path: data={:?}", data);
373+
374+
Ok((self.path_append(Ok, &DisambiguatedDefPathData {
375+
data,
376+
disambiguator: 0,
377+
})?, true))
375378
}
376379

377380
fn pretty_path_qualified(
@@ -932,10 +935,18 @@ impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> {
932935
// only occur very early in the compiler pipeline.
933936
let parent_def_id = DefId { index: key.parent.unwrap(), ..def_id };
934937
let span = self.tcx.def_span(def_id);
935-
return self.path_append(
936-
|cx| cx.print_def_path(parent_def_id, &[]),
937-
&format!("<impl at {:?}>", span),
938-
);
938+
939+
self = self.print_def_path(parent_def_id, &[])?;
940+
941+
// HACK(eddyb) copy of `path_append` to avoid
942+
// constructing a `DisambiguatedDefPathData`.
943+
if !self.empty_path {
944+
write!(self, "::")?;
945+
}
946+
write!(self, "<impl at {:?}>", span)?;
947+
self.empty_path = false;
948+
949+
return Ok(self);
939950
}
940951
}
941952

@@ -995,6 +1006,7 @@ impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> {
9951006
fn path_append_impl(
9961007
mut self,
9971008
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
1009+
_disambiguated_data: &DisambiguatedDefPathData,
9981010
self_ty: Ty<'tcx>,
9991011
trait_ref: Option<ty::TraitRef<'tcx>>,
10001012
) -> Result<Self::Path, Self::Error> {
@@ -1012,17 +1024,35 @@ impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> {
10121024
fn path_append(
10131025
mut self,
10141026
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
1015-
text: &str,
1027+
disambiguated_data: &DisambiguatedDefPathData,
10161028
) -> Result<Self::Path, Self::Error> {
10171029
self = print_prefix(self)?;
10181030

1019-
// FIXME(eddyb) `text` should never be empty, but it
1031+
// Skip `::{{constructor}}` on tuple/unit structs.
1032+
match disambiguated_data.data {
1033+
DefPathData::StructCtor => return Ok(self),
1034+
_ => {}
1035+
}
1036+
1037+
// FIXME(eddyb) `name` should never be empty, but it
10201038
// currently is for `extern { ... }` "foreign modules".
1021-
if !text.is_empty() {
1039+
let name = disambiguated_data.data.as_interned_str().as_str();
1040+
if !name.is_empty() {
10221041
if !self.empty_path {
10231042
write!(self, "::")?;
10241043
}
1025-
write!(self, "{}", text)?;
1044+
write!(self, "{}", name)?;
1045+
1046+
// FIXME(eddyb) this will print e.g. `{{closure}}#3`, but it
1047+
// might be nicer to use something else, e.g. `{closure#3}`.
1048+
let dis = disambiguated_data.disambiguator;
1049+
let print_dis =
1050+
disambiguated_data.data.get_opt_name().is_none() ||
1051+
dis != 0 && self.tcx.sess.verbose();
1052+
if print_dis {
1053+
write!(self, "#{}", dis)?;
1054+
}
1055+
10261056
self.empty_path = false;
10271057
}
10281058

src/librustc_codegen_utils/symbol_names.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
9191
use rustc::hir::Node;
9292
use rustc::hir::CodegenFnAttrFlags;
93-
use rustc::hir::map::definitions::DefPathData;
93+
use rustc::hir::map::{DefPathData, DisambiguatedDefPathData};
9494
use rustc::ich::NodeIdHashingMode;
9595
use rustc::ty::print::{PrettyPrinter, Printer, Print};
9696
use rustc::ty::query::Providers;
@@ -492,30 +492,48 @@ impl Printer<'tcx, 'tcx> for SymbolPrinter<'_, 'tcx> {
492492
fn path_append_impl(
493493
self,
494494
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
495+
_disambiguated_data: &DisambiguatedDefPathData,
495496
self_ty: Ty<'tcx>,
496497
trait_ref: Option<ty::TraitRef<'tcx>>,
497498
) -> Result<Self::Path, Self::Error> {
498499
self.pretty_path_append_impl(
499-
|cx| cx.path_append(print_prefix, ""),
500+
|mut cx| {
501+
cx = print_prefix(cx)?;
502+
503+
if cx.keep_within_component {
504+
// HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
505+
cx.write_str("::")?;
506+
} else {
507+
cx.path.finalize_pending_component();
508+
}
509+
510+
Ok(cx)
511+
},
500512
self_ty,
501513
trait_ref,
502514
)
503515
}
504516
fn path_append(
505517
mut self,
506518
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
507-
text: &str,
519+
disambiguated_data: &DisambiguatedDefPathData,
508520
) -> Result<Self::Path, Self::Error> {
509521
self = print_prefix(self)?;
510522

523+
// Skip `::{{constructor}}` on tuple/unit structs.
524+
match disambiguated_data.data {
525+
DefPathData::StructCtor => return Ok(self),
526+
_ => {}
527+
}
528+
511529
if self.keep_within_component {
512530
// HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
513531
self.write_str("::")?;
514532
} else {
515533
self.path.finalize_pending_component();
516534
}
517535

518-
self.write_str(text)?;
536+
self.write_str(&disambiguated_data.data.as_interned_str().as_str())?;
519537
Ok(self)
520538
}
521539
fn path_generic_args(

src/librustdoc/clean/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc::mir::interpret::GlobalId;
2020
use rustc::hir::{self, GenericArg, HirVec};
2121
use rustc::hir::def::{self, Def, CtorKind};
2222
use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
23+
use rustc::hir::map::DisambiguatedDefPathData;
2324
use rustc::ty::subst::{Kind, InternalSubsts, SubstsRef};
2425
use rustc::ty::{self, DefIdTree, TyCtxt, Region, RegionVid, Ty, AdtKind};
2526
use rustc::ty::fold::TypeFolder;
@@ -4288,6 +4289,7 @@ pub fn get_path_for_type(
42884289
fn path_append_impl(
42894290
self,
42904291
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
4292+
_disambiguated_data: &DisambiguatedDefPathData,
42914293
self_ty: Ty<'tcx>,
42924294
trait_ref: Option<ty::TraitRef<'tcx>>,
42934295
) -> Result<Self::Path, Self::Error> {
@@ -4306,10 +4308,10 @@ pub fn get_path_for_type(
43064308
fn path_append(
43074309
self,
43084310
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
4309-
text: &str,
4311+
disambiguated_data: &DisambiguatedDefPathData,
43104312
) -> Result<Self::Path, Self::Error> {
43114313
let mut path = print_prefix(self)?;
4312-
path.push(text.to_string());
4314+
path.push(disambiguated_data.data.as_interned_str().to_string());
43134315
Ok(path)
43144316
}
43154317
fn path_generic_args(

src/test/mir-opt/retag.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn main() {
9898
// }
9999
// END rustc.main.EraseRegions.after.mir
100100
// START rustc.main-{{closure}}.EraseRegions.after.mir
101-
// fn main::{{closure}}(_1: &[closure@HirId { owner: DefIndex(0:7), local_id: 70 }], _2: &i32) -> &i32 {
101+
// fn main::{{closure}}#0(_1: &[closure@HirId { owner: DefIndex(0:7), local_id: 70 }], _2: &i32) -> &i32 {
102102
// ...
103103
// bb0: {
104104
// Retag([fn entry] _1);

src/test/ui/consts/const-size_of-cycle.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
error[E0391]: cycle detected when const-evaluating + checking `Foo::bytes::{{constant}}`
1+
error[E0391]: cycle detected when const-evaluating + checking `Foo::bytes::{{constant}}#0`
22
--> $DIR/const-size_of-cycle.rs:6:17
33
|
44
LL | bytes: [u8; std::mem::size_of::<Foo>()]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
note: ...which requires const-evaluating `Foo::bytes::{{constant}}`...
7+
note: ...which requires const-evaluating `Foo::bytes::{{constant}}#0`...
88
--> $SRC_DIR/libcore/mem.rs:LL:COL
99
|
1010
LL | intrinsics::size_of::<T>()
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
= note: ...which requires computing layout of `Foo`...
1313
= note: ...which requires normalizing `ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: All, def_id: None }, value: [u8; _] }`...
14-
note: ...which requires const-evaluating + checking `Foo::bytes::{{constant}}`...
14+
note: ...which requires const-evaluating + checking `Foo::bytes::{{constant}}#0`...
1515
--> $DIR/const-size_of-cycle.rs:6:17
1616
|
1717
LL | bytes: [u8; std::mem::size_of::<Foo>()]
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
19-
= note: ...which again requires const-evaluating + checking `Foo::bytes::{{constant}}`, completing the cycle
19+
= note: ...which again requires const-evaluating + checking `Foo::bytes::{{constant}}#0`, completing the cycle
2020
note: cycle used when processing `Foo`
2121
--> $DIR/const-size_of-cycle.rs:5:1
2222
|

src/test/ui/deprecation/deprecation-lint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ mod this_crate {
314314
let _ = || {
315315
#[deprecated]
316316
fn bar() { }
317-
bar(); //~ ERROR use of deprecated item 'this_crate::test_fn_closure_body::{{closure}}::bar'
317+
bar(); //~ ERROR use of deprecated item 'this_crate::test_fn_closure_body::{{closure}}#0::bar'
318318
};
319319
}
320320

src/test/ui/deprecation/deprecation-lint.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ error: use of deprecated item 'this_crate::Trait::trait_deprecated_text': text
298298
LL | <Foo as Trait>::trait_deprecated_text(&foo);
299299
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
300300

301-
error: use of deprecated item 'this_crate::test_fn_closure_body::{{closure}}::bar'
301+
error: use of deprecated item 'this_crate::test_fn_closure_body::{{closure}}#0::bar'
302302
--> $DIR/deprecation-lint.rs:317:13
303303
|
304304
LL | bar();

0 commit comments

Comments
 (0)