rustc: provide DisambiguatedDefPathData in ty::print.

This commit is contained in:
Eduard-Mihai Burtescu 2019-02-03 12:59:37 +02:00
parent 8619edede1
commit a54a41ce47
16 changed files with 127 additions and 71 deletions

View file

@ -679,13 +679,13 @@ impl DefPathData {
return name
}
// note that this does not show up in user printouts
CrateRoot => "{{root}}",
CrateRoot => "{{crate}}",
Impl => "{{impl}}",
Misc => "{{?}}",
Misc => "{{misc}}",
ClosureExpr => "{{closure}}",
StructCtor => "{{constructor}}",
AnonConst => "{{constant}}",
ImplTrait => "{{impl-Trait}}",
ImplTrait => "{{opaque}}",
};
Symbol::intern(s).as_interned_str()

View file

@ -445,6 +445,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
sp: Span,
) {
use hir::def_id::CrateNum;
use hir::map::DisambiguatedDefPathData;
use ty::print::Printer;
use ty::subst::Kind;
@ -504,6 +505,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
fn path_append_impl(
self,
_print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
_disambiguated_data: &DisambiguatedDefPathData,
_self_ty: Ty<'tcx>,
_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
@ -512,10 +514,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
fn path_append(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
text: &str,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error> {
let mut path = print_prefix(self)?;
path.push(text.to_string());
path.push(disambiguated_data.data.as_interned_str().to_string());
Ok(path)
}
fn path_generic_args(

View file

@ -1,4 +1,4 @@
use crate::hir::map::DefPathData;
use crate::hir::map::{DefPathData, DisambiguatedDefPathData};
use crate::hir::def_id::{CrateNum, DefId};
use crate::ty::{self, DefIdTree, Ty, TyCtxt};
use crate::ty::subst::{Kind, Subst};
@ -71,13 +71,14 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized {
fn path_append_impl(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
disambiguated_data: &DisambiguatedDefPathData,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error>;
fn path_append(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
text: &str,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error>;
fn path_generic_args(
self,
@ -156,7 +157,7 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized {
} else {
cx.print_def_path(parent_def_id, parent_substs)
},
&key.disambiguated_data.data.as_interned_str().as_str(),
&key.disambiguated_data,
)
}
}
@ -200,12 +201,14 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized {
debug!("default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}",
impl_def_id, self_ty, impl_trait_ref);
let key = self.tcx().def_key(impl_def_id);
let parent_def_id = DefId { index: key.parent.unwrap(), ..impl_def_id };
// Decide whether to print the parent path for the impl.
// Logically, since impls are global, it's never needed, but
// users may find it useful. Currently, we omit the parent if
// the impl is either in the same module as the self-type or
// as the trait.
let parent_def_id = self.tcx().parent(impl_def_id).unwrap();
let in_self_mod = match characteristic_def_id_of_type(self_ty) {
None => false,
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 {
// the module more clearly.
self.path_append_impl(
|cx| cx.print_def_path(parent_def_id, &[]),
&key.disambiguated_data,
self_ty,
impl_trait_ref,
)

View file

@ -1,6 +1,6 @@
use crate::hir;
use crate::hir::def::Namespace;
use crate::hir::map::DefPathData;
use crate::hir::map::{DefPathData, DisambiguatedDefPathData};
use crate::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use crate::middle::cstore::{ExternCrate, ExternCrateSource};
use crate::middle::region;
@ -313,13 +313,13 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
visible_parent, actual_parent,
);
let data = cur_def_key.disambiguated_data.data;
let mut data = cur_def_key.disambiguated_data.data;
debug!(
"try_print_visible_def_path: data={:?} visible_parent={:?} actual_parent={:?}",
data, visible_parent, actual_parent,
);
let symbol = match data {
match data {
// In order to output a path that could actually be imported (valid and visible),
// we need to handle re-exports correctly.
//
@ -351,27 +351,30 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
// the children of the visible parent (as was done when computing
// `visible_parent_map`), looking for the specific child we currently have and then
// have access to the re-exported name.
DefPathData::Module(actual_name) |
DefPathData::TypeNs(actual_name) if Some(visible_parent) != actual_parent => {
self.tcx().item_children(visible_parent)
DefPathData::Module(ref mut name) |
DefPathData::TypeNs(ref mut name) if Some(visible_parent) != actual_parent => {
let reexport = self.tcx().item_children(visible_parent)
.iter()
.find(|child| child.def.def_id() == def_id)
.map(|child| child.ident.as_str())
.unwrap_or_else(|| actual_name.as_str())
.map(|child| child.ident.as_interned_str());
if let Some(reexport) = reexport {
*name = reexport;
}
}
_ => {
data.get_opt_name().map(|n| n.as_str()).unwrap_or_else(|| {
// Re-exported `extern crate` (#43189).
if let DefPathData::CrateRoot = data {
self.tcx().original_crate_name(def_id.krate).as_str()
} else {
Symbol::intern("<unnamed>").as_str()
}
})
},
};
debug!("try_print_visible_def_path: symbol={:?}", symbol);
Ok((self.path_append(Ok, &symbol)?, true))
// Re-exported `extern crate` (#43189).
DefPathData::CrateRoot => {
data = DefPathData::Module(
self.tcx().original_crate_name(def_id.krate).as_interned_str(),
);
}
_ => {}
}
debug!("try_print_visible_def_path: data={:?}", data);
Ok((self.path_append(Ok, &DisambiguatedDefPathData {
data,
disambiguator: 0,
})?, true))
}
fn pretty_path_qualified(
@ -932,10 +935,18 @@ impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> {
// only occur very early in the compiler pipeline.
let parent_def_id = DefId { index: key.parent.unwrap(), ..def_id };
let span = self.tcx.def_span(def_id);
return self.path_append(
|cx| cx.print_def_path(parent_def_id, &[]),
&format!("<impl at {:?}>", span),
);
self = self.print_def_path(parent_def_id, &[])?;
// HACK(eddyb) copy of `path_append` to avoid
// constructing a `DisambiguatedDefPathData`.
if !self.empty_path {
write!(self, "::")?;
}
write!(self, "<impl at {:?}>", span)?;
self.empty_path = false;
return Ok(self);
}
}
@ -995,6 +1006,7 @@ impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> {
fn path_append_impl(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
_disambiguated_data: &DisambiguatedDefPathData,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
@ -1012,17 +1024,35 @@ impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> {
fn path_append(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
text: &str,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error> {
self = print_prefix(self)?;
// FIXME(eddyb) `text` should never be empty, but it
// Skip `::{{constructor}}` on tuple/unit structs.
match disambiguated_data.data {
DefPathData::StructCtor => return Ok(self),
_ => {}
}
// FIXME(eddyb) `name` should never be empty, but it
// currently is for `extern { ... }` "foreign modules".
if !text.is_empty() {
let name = disambiguated_data.data.as_interned_str().as_str();
if !name.is_empty() {
if !self.empty_path {
write!(self, "::")?;
}
write!(self, "{}", text)?;
write!(self, "{}", name)?;
// FIXME(eddyb) this will print e.g. `{{closure}}#3`, but it
// might be nicer to use something else, e.g. `{closure#3}`.
let dis = disambiguated_data.disambiguator;
let print_dis =
disambiguated_data.data.get_opt_name().is_none() ||
dis != 0 && self.tcx.sess.verbose();
if print_dis {
write!(self, "#{}", dis)?;
}
self.empty_path = false;
}

View file

@ -90,7 +90,7 @@
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use rustc::hir::Node;
use rustc::hir::CodegenFnAttrFlags;
use rustc::hir::map::definitions::DefPathData;
use rustc::hir::map::{DefPathData, DisambiguatedDefPathData};
use rustc::ich::NodeIdHashingMode;
use rustc::ty::print::{PrettyPrinter, Printer, Print};
use rustc::ty::query::Providers;
@ -492,11 +492,23 @@ impl Printer<'tcx, 'tcx> for SymbolPrinter<'_, 'tcx> {
fn path_append_impl(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
_disambiguated_data: &DisambiguatedDefPathData,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
self.pretty_path_append_impl(
|cx| cx.path_append(print_prefix, ""),
|mut cx| {
cx = print_prefix(cx)?;
if cx.keep_within_component {
// HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
cx.write_str("::")?;
} else {
cx.path.finalize_pending_component();
}
Ok(cx)
},
self_ty,
trait_ref,
)
@ -504,10 +516,16 @@ impl Printer<'tcx, 'tcx> for SymbolPrinter<'_, 'tcx> {
fn path_append(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
text: &str,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error> {
self = print_prefix(self)?;
// Skip `::{{constructor}}` on tuple/unit structs.
match disambiguated_data.data {
DefPathData::StructCtor => return Ok(self),
_ => {}
}
if self.keep_within_component {
// HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
self.write_str("::")?;
@ -515,7 +533,7 @@ impl Printer<'tcx, 'tcx> for SymbolPrinter<'_, 'tcx> {
self.path.finalize_pending_component();
}
self.write_str(text)?;
self.write_str(&disambiguated_data.data.as_interned_str().as_str())?;
Ok(self)
}
fn path_generic_args(

View file

@ -20,6 +20,7 @@ use rustc::mir::interpret::GlobalId;
use rustc::hir::{self, GenericArg, HirVec};
use rustc::hir::def::{self, Def, CtorKind};
use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc::hir::map::DisambiguatedDefPathData;
use rustc::ty::subst::{Kind, InternalSubsts, SubstsRef};
use rustc::ty::{self, DefIdTree, TyCtxt, Region, RegionVid, Ty, AdtKind};
use rustc::ty::fold::TypeFolder;
@ -4288,6 +4289,7 @@ pub fn get_path_for_type(
fn path_append_impl(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
_disambiguated_data: &DisambiguatedDefPathData,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
@ -4306,10 +4308,10 @@ pub fn get_path_for_type(
fn path_append(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
text: &str,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error> {
let mut path = print_prefix(self)?;
path.push(text.to_string());
path.push(disambiguated_data.data.as_interned_str().to_string());
Ok(path)
}
fn path_generic_args(

View file

@ -98,7 +98,7 @@ fn main() {
// }
// END rustc.main.EraseRegions.after.mir
// START rustc.main-{{closure}}.EraseRegions.after.mir
// fn main::{{closure}}(_1: &[closure@HirId { owner: DefIndex(0:7), local_id: 70 }], _2: &i32) -> &i32 {
// fn main::{{closure}}#0(_1: &[closure@HirId { owner: DefIndex(0:7), local_id: 70 }], _2: &i32) -> &i32 {
// ...
// bb0: {
// Retag([fn entry] _1);

View file

@ -1,22 +1,22 @@
error[E0391]: cycle detected when const-evaluating + checking `Foo::bytes::{{constant}}`
error[E0391]: cycle detected when const-evaluating + checking `Foo::bytes::{{constant}}#0`
--> $DIR/const-size_of-cycle.rs:6:17
|
LL | bytes: [u8; std::mem::size_of::<Foo>()]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: ...which requires const-evaluating `Foo::bytes::{{constant}}`...
note: ...which requires const-evaluating `Foo::bytes::{{constant}}#0`...
--> $SRC_DIR/libcore/mem.rs:LL:COL
|
LL | intrinsics::size_of::<T>()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires computing layout of `Foo`...
= note: ...which requires normalizing `ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: All, def_id: None }, value: [u8; _] }`...
note: ...which requires const-evaluating + checking `Foo::bytes::{{constant}}`...
note: ...which requires const-evaluating + checking `Foo::bytes::{{constant}}#0`...
--> $DIR/const-size_of-cycle.rs:6:17
|
LL | bytes: [u8; std::mem::size_of::<Foo>()]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which again requires const-evaluating + checking `Foo::bytes::{{constant}}`, completing the cycle
= note: ...which again requires const-evaluating + checking `Foo::bytes::{{constant}}#0`, completing the cycle
note: cycle used when processing `Foo`
--> $DIR/const-size_of-cycle.rs:5:1
|

View file

@ -314,7 +314,7 @@ mod this_crate {
let _ = || {
#[deprecated]
fn bar() { }
bar(); //~ ERROR use of deprecated item 'this_crate::test_fn_closure_body::{{closure}}::bar'
bar(); //~ ERROR use of deprecated item 'this_crate::test_fn_closure_body::{{closure}}#0::bar'
};
}

View file

@ -298,7 +298,7 @@ error: use of deprecated item 'this_crate::Trait::trait_deprecated_text': text
LL | <Foo as Trait>::trait_deprecated_text(&foo);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: use of deprecated item 'this_crate::test_fn_closure_body::{{closure}}::bar'
error: use of deprecated item 'this_crate::test_fn_closure_body::{{closure}}#0::bar'
--> $DIR/deprecation-lint.rs:317:13
|
LL | bar();

View file

@ -1,4 +1,4 @@
error[E0391]: cycle detected when processing `cycle1::{{impl-Trait}}`
error[E0391]: cycle detected when processing `cycle1::{{opaque}}#0`
--> $DIR/auto-trait-leak.rs:14:16
|
LL | fn cycle1() -> impl Clone {
@ -10,7 +10,7 @@ note: ...which requires processing `cycle1`...
LL | fn cycle1() -> impl Clone {
| ^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires evaluating trait selection obligation `impl std::clone::Clone: std::marker::Send`...
note: ...which requires processing `cycle2::{{impl-Trait}}`...
note: ...which requires processing `cycle2::{{opaque}}#0`...
--> $DIR/auto-trait-leak.rs:23:16
|
LL | fn cycle2() -> impl Clone {
@ -21,7 +21,7 @@ note: ...which requires processing `cycle2`...
LL | fn cycle2() -> impl Clone {
| ^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires evaluating trait selection obligation `impl std::clone::Clone: std::marker::Send`...
= note: ...which again requires processing `cycle1::{{impl-Trait}}`, completing the cycle
= note: ...which again requires processing `cycle1::{{opaque}}#0`, completing the cycle
note: cycle used when checking item types in top-level module
--> $DIR/auto-trait-leak.rs:3:1
|
@ -34,7 +34,7 @@ LL | | Rc::new(String::from("foo"))
LL | | }
| |_^
error[E0391]: cycle detected when processing `cycle1::{{impl-Trait}}`
error[E0391]: cycle detected when processing `cycle1::{{opaque}}#0`
--> $DIR/auto-trait-leak.rs:14:16
|
LL | fn cycle1() -> impl Clone {
@ -46,7 +46,7 @@ note: ...which requires processing `cycle1`...
LL | fn cycle1() -> impl Clone {
| ^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires evaluating trait selection obligation `impl std::clone::Clone: std::marker::Send`...
note: ...which requires processing `cycle2::{{impl-Trait}}`...
note: ...which requires processing `cycle2::{{opaque}}#0`...
--> $DIR/auto-trait-leak.rs:23:16
|
LL | fn cycle2() -> impl Clone {
@ -56,7 +56,7 @@ note: ...which requires processing `cycle2`...
|
LL | fn cycle2() -> impl Clone {
| ^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which again requires processing `cycle1::{{impl-Trait}}`, completing the cycle
= note: ...which again requires processing `cycle1::{{opaque}}#0`, completing the cycle
note: cycle used when checking item types in top-level module
--> $DIR/auto-trait-leak.rs:3:1
|

View file

@ -5,7 +5,7 @@ LL | const FOO: usize = FOO;
| ^^^
|
= note: ...which again requires processing `FOO`, completing the cycle
note: cycle used when processing `main::{{constant}}`
note: cycle used when processing `main::{{constant}}#0`
--> $DIR/issue-17252.rs:4:18
|
LL | let _x: [u8; FOO]; // caused stack overflow prior to fix

View file

@ -1,11 +1,11 @@
error[E0391]: cycle detected when processing `X::A::{{constant}}`
error[E0391]: cycle detected when processing `X::A::{{constant}}#0`
--> $DIR/issue-23302-1.rs:4:9
|
LL | A = X::A as isize,
| ^^^^^^^^^^^^^
|
= note: ...which again requires processing `X::A::{{constant}}`, completing the cycle
note: cycle used when const-evaluating `X::A::{{constant}}`
= note: ...which again requires processing `X::A::{{constant}}#0`, completing the cycle
note: cycle used when const-evaluating `X::A::{{constant}}#0`
--> $DIR/issue-23302-1.rs:4:9
|
LL | A = X::A as isize,

View file

@ -1,11 +1,11 @@
error[E0391]: cycle detected when processing `Y::A::{{constant}}`
error[E0391]: cycle detected when processing `Y::A::{{constant}}#0`
--> $DIR/issue-23302-2.rs:4:9
|
LL | A = Y::B as isize,
| ^^^^^^^^^^^^^
|
= note: ...which again requires processing `Y::A::{{constant}}`, completing the cycle
note: cycle used when const-evaluating `Y::A::{{constant}}`
= note: ...which again requires processing `Y::A::{{constant}}#0`, completing the cycle
note: cycle used when const-evaluating `Y::A::{{constant}}#0`
--> $DIR/issue-23302-2.rs:4:9
|
LL | A = Y::B as isize,

View file

@ -1,4 +1,4 @@
error[E0391]: cycle detected when processing `Foo::B::{{constant}}`
error[E0391]: cycle detected when processing `Foo::B::{{constant}}#0`
--> $DIR/issue-36163.rs:4:9
|
LL | B = A,
@ -9,8 +9,8 @@ note: ...which requires processing `A`...
|
LL | const A: isize = Foo::B as isize;
| ^^^^^^^^^^^^^^^
= note: ...which again requires processing `Foo::B::{{constant}}`, completing the cycle
note: cycle used when const-evaluating `Foo::B::{{constant}}`
= note: ...which again requires processing `Foo::B::{{constant}}#0`, completing the cycle
note: cycle used when const-evaluating `Foo::B::{{constant}}#0`
--> $DIR/issue-36163.rs:4:9
|
LL | B = A,

View file

@ -1,22 +1,22 @@
error[E0391]: cycle detected when const-evaluating + checking `Foo::bytes::{{constant}}`
error[E0391]: cycle detected when const-evaluating + checking `Foo::bytes::{{constant}}#0`
--> $DIR/issue-44415.rs:6:17
|
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
| ^^^^^^
|
note: ...which requires const-evaluating `Foo::bytes::{{constant}}`...
note: ...which requires const-evaluating `Foo::bytes::{{constant}}#0`...
--> $DIR/issue-44415.rs:6:26
|
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires computing layout of `Foo`...
= note: ...which requires normalizing `ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: All, def_id: None }, value: [u8; _] }`...
note: ...which requires const-evaluating + checking `Foo::bytes::{{constant}}`...
note: ...which requires const-evaluating + checking `Foo::bytes::{{constant}}#0`...
--> $DIR/issue-44415.rs:6:17
|
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
| ^^^^^^
= note: ...which again requires const-evaluating + checking `Foo::bytes::{{constant}}`, completing the cycle
= note: ...which again requires const-evaluating + checking `Foo::bytes::{{constant}}#0`, completing the cycle
note: cycle used when processing `Foo`
--> $DIR/issue-44415.rs:5:1
|