Skip to content

Commit

Permalink
rustc: provide DisambiguatedDefPathData in ty::print.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Mar 15, 2019
1 parent 8619ede commit a54a41c
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 71 deletions.
6 changes: 3 additions & 3 deletions src/librustc/hir/map/definitions.rs
Expand Up @@ -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()
Expand Down
6 changes: 4 additions & 2 deletions src/librustc/infer/error_reporting/mod.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -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> {
Expand All @@ -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(
Expand Down
12 changes: 8 additions & 4 deletions src/librustc/ty/print/mod.rs
@@ -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};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
}
}
Expand Down Expand Up @@ -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),
Expand All @@ -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,
)
Expand Down
88 changes: 59 additions & 29 deletions src/librustc/ty/print/pretty.rs
@@ -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;
Expand Down Expand Up @@ -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.
//
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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> {
Expand All @@ -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;
}

Expand Down
26 changes: 22 additions & 4 deletions src/librustc_codegen_utils/symbol_names.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -492,30 +492,48 @@ 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,
)
}
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("::")?;
} else {
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(
Expand Down
6 changes: 4 additions & 2 deletions src/librustdoc/clean/mod.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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> {
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/test/mir-opt/retag.rs
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/consts/const-size_of-cycle.stderr
@@ -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
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/deprecation/deprecation-lint.rs
Expand Up @@ -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'
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/deprecation/deprecation-lint.stderr
Expand Up @@ -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();
Expand Down

0 comments on commit a54a41c

Please sign in to comment.