Skip to content

Commit

Permalink
rustc: slice substs in ty::print instead of passing the full ones.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Mar 15, 2019
1 parent 9df7c3f commit 8619ede
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 88 deletions.
2 changes: 1 addition & 1 deletion src/librustc/infer/error_reporting/mod.rs
Expand Up @@ -533,7 +533,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
if !(did1.is_local() || did2.is_local()) && did1.krate != did2.krate {
let abs_path = |def_id| {
AbsolutePathPrinter { tcx: self.tcx }
.print_def_path(def_id, None)
.print_def_path(def_id, &[])
};

// We compare strings because DefPath can be different
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/mir/mod.rs
Expand Up @@ -2410,7 +2410,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
ty::tls::with(|tcx| {
let substs = tcx.lift(&substs).expect("could not lift for printing");
FmtPrinter::new(tcx, f, Namespace::ValueNS)
.print_def_path(variant_def.did, Some(substs))?;
.print_def_path(variant_def.did, substs)?;
Ok(())
})?;

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/instance.rs
Expand Up @@ -179,7 +179,7 @@ impl<'tcx> fmt::Display for Instance<'tcx> {
ty::tls::with(|tcx| {
let substs = tcx.lift(&self.substs).expect("could not lift for printing");
FmtPrinter::new(tcx, &mut *f, Namespace::ValueNS)
.print_def_path(self.def_id(), Some(substs))?;
.print_def_path(self.def_id(), substs)?;
Ok(())
})?;

Expand Down
96 changes: 48 additions & 48 deletions src/librustc/ty/print/mod.rs
@@ -1,7 +1,7 @@
use crate::hir::map::DefPathData;
use crate::hir::def_id::{CrateNum, DefId};
use crate::ty::{self, DefIdTree, Ty, TyCtxt};
use crate::ty::subst::{Kind, Subst, SubstsRef};
use crate::ty::subst::{Kind, Subst};

use rustc_data_structures::fx::FxHashSet;

Expand Down Expand Up @@ -29,14 +29,14 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized {
fn print_def_path(
self,
def_id: DefId,
substs: Option<SubstsRef<'tcx>>,
substs: &'tcx [Kind<'tcx>],
) -> Result<Self::Path, Self::Error> {
self.default_print_def_path(def_id, substs)
}
fn print_impl_path(
self,
impl_def_id: DefId,
substs: Option<SubstsRef<'tcx>>,
substs: &'tcx [Kind<'tcx>],
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
Expand Down Expand Up @@ -90,7 +90,7 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized {
fn default_print_def_path(
self,
def_id: DefId,
substs: Option<SubstsRef<'tcx>>,
substs: &'tcx [Kind<'tcx>],
) -> Result<Self::Path, Self::Error> {
debug!("default_print_def_path: def_id={:?}, substs={:?}", def_id, substs);
let key = self.tcx().def_key(def_id);
Expand All @@ -103,69 +103,69 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized {
}

DefPathData::Impl => {
let generics = self.tcx().generics_of(def_id);
let mut self_ty = self.tcx().type_of(def_id);
if let Some(substs) = substs {
self_ty = self_ty.subst(self.tcx(), substs);
}

let mut impl_trait_ref = self.tcx().impl_trait_ref(def_id);
if let Some(substs) = substs {
if substs.len() >= generics.count() {
self_ty = self_ty.subst(self.tcx(), substs);
impl_trait_ref = impl_trait_ref.subst(self.tcx(), substs);
}
self.print_impl_path(def_id, substs, self_ty, impl_trait_ref)
}

_ => {
let generics = substs.map(|_| self.tcx().generics_of(def_id));
let generics_parent = generics.as_ref().and_then(|g| g.parent);
let parent_def_id = DefId { index: key.parent.unwrap(), ..def_id };
let print_parent_path = |cx: Self| {
if let Some(generics_parent_def_id) = generics_parent {
assert_eq!(parent_def_id, generics_parent_def_id);

// FIXME(eddyb) try to move this into the parent's printing
// logic, instead of doing it when printing the child.
let parent_generics = cx.tcx().generics_of(parent_def_id);
let parent_has_own_self =
parent_generics.has_self && parent_generics.parent_count == 0;
if let (Some(substs), true) = (substs, parent_has_own_self) {
let trait_ref = ty::TraitRef::new(parent_def_id, substs);
cx.path_qualified(trait_ref.self_ty(), Some(trait_ref))
} else {
cx.print_def_path(parent_def_id, substs)
}
} else {
cx.print_def_path(parent_def_id, None)
}
};
let print_path = |cx: Self| {

let mut parent_substs = substs;
let mut trait_qualify_parent = false;
if !substs.is_empty() {
let generics = self.tcx().generics_of(def_id);
parent_substs = &substs[..generics.parent_count.min(substs.len())];

match key.disambiguated_data.data {
// Skip `::{{constructor}}` on tuple/unit structs.
DefPathData::StructCtor => print_parent_path(cx),

_ => {
cx.path_append(
print_parent_path,
&key.disambiguated_data.data.as_interned_str().as_str(),
)
// Closures' own generics are only captures, don't print them.
DefPathData::ClosureExpr => {}

// If we have any generic arguments to print, we do that
// on top of the same path, but without its own generics.
_ => if !generics.params.is_empty() && substs.len() >= generics.count() {
let args = self.generic_args_to_print(generics, substs);
return self.path_generic_args(
|cx| cx.print_def_path(def_id, parent_substs),
args,
);
}
}
};

if let (Some(generics), Some(substs)) = (generics, substs) {
let args = self.generic_args_to_print(generics, substs);
self.path_generic_args(print_path, args)
} else {
print_path(self)
// FIXME(eddyb) try to move this into the parent's printing
// logic, instead of doing it when printing the child.
trait_qualify_parent =
generics.has_self &&
generics.parent == Some(parent_def_id) &&
parent_substs.len() == generics.parent_count &&
self.tcx().generics_of(parent_def_id).parent_count == 0;
}

self.path_append(
|cx: Self| if trait_qualify_parent {
let trait_ref = ty::TraitRef::new(
parent_def_id,
cx.tcx().intern_substs(parent_substs),
);
cx.path_qualified(trait_ref.self_ty(), Some(trait_ref))
} else {
cx.print_def_path(parent_def_id, parent_substs)
},
&key.disambiguated_data.data.as_interned_str().as_str(),
)
}
}
}

fn generic_args_to_print(
&self,
generics: &'tcx ty::Generics,
substs: SubstsRef<'tcx>,
substs: &'tcx [Kind<'tcx>],
) -> &'tcx [Kind<'tcx>] {
let mut own_params = generics.parent_count..generics.count();

Expand Down Expand Up @@ -193,7 +193,7 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized {
fn default_print_impl_path(
self,
impl_def_id: DefId,
_substs: Option<SubstsRef<'tcx>>,
_substs: &'tcx [Kind<'tcx>],
self_ty: Ty<'tcx>,
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
Expand All @@ -220,7 +220,7 @@ pub trait Printer<'gcx: 'tcx, 'tcx>: Sized {
// trait-type, then fallback to a format that identifies
// the module more clearly.
self.path_append_impl(
|cx| cx.print_def_path(parent_def_id, None),
|cx| cx.print_def_path(parent_def_id, &[]),
self_ty,
impl_trait_ref,
)
Expand Down
49 changes: 20 additions & 29 deletions src/librustc/ty/print/pretty.rs
Expand Up @@ -5,7 +5,7 @@ use crate::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use crate::middle::cstore::{ExternCrate, ExternCrateSource};
use crate::middle::region;
use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable};
use crate::ty::subst::{Kind, Subst, SubstsRef, UnpackedKind};
use crate::ty::subst::{Kind, Subst, UnpackedKind};
use crate::mir::interpret::ConstValue;
use syntax::symbol::{keywords, Symbol};

Expand Down Expand Up @@ -178,7 +178,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
fn print_value_path(
self,
def_id: DefId,
substs: Option<SubstsRef<'tcx>>,
substs: &'tcx [Kind<'tcx>],
) -> Result<Self::Path, Self::Error> {
self.print_def_path(def_id, substs)
}
Expand Down Expand Up @@ -264,7 +264,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
}) => {
debug!("try_print_visible_def_path: def_id={:?}", def_id);
return Ok((if !span.is_dummy() {
self.print_def_path(def_id, None)?
self.print_def_path(def_id, &[])?
} else {
self.path_crate(cnum)?
}, true));
Expand Down Expand Up @@ -469,8 +469,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
}
ty::FnDef(def_id, substs) => {
let sig = self.tcx().fn_sig(def_id).subst(self.tcx(), substs);
p!(print(sig),
write(" {{"), print_value_path(def_id, Some(substs)), write("}}"));
p!(print(sig), write(" {{"), print_value_path(def_id, substs), write("}}"));
}
ty::FnPtr(ref bare_fn) => {
p!(print(bare_fn))
Expand All @@ -492,7 +491,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
}
}
ty::Adt(def, substs) => {
p!(print_def_path(def.did, Some(substs)));
p!(print_def_path(def.did, substs));
}
ty::Dynamic(data, r) => {
let print_r = self.region_should_not_be_omitted(r);
Expand All @@ -505,7 +504,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
}
}
ty::Foreign(def_id) => {
p!(print_def_path(def_id, None));
p!(print_def_path(def_id, &[]));
}
ty::Projection(ref data) => p!(print(data)),
ty::UnnormalizedProjection(ref data) => {
Expand Down Expand Up @@ -691,7 +690,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
let mut first = true;

if let Some(principal) = predicates.principal() {
p!(print_def_path(principal.def_id, None));
p!(print_def_path(principal.def_id, &[]));

let mut resugared = false;

Expand Down Expand Up @@ -774,7 +773,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
}
first = false;

p!(print_def_path(def_id, None));
p!(print_def_path(def_id, &[]));
}

Ok(self)
Expand Down Expand Up @@ -879,7 +878,7 @@ impl TyCtxt<'_, '_, '_> {
debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns);
let mut s = String::new();
let _ = FmtPrinter::new(self, &mut s, ns)
.print_def_path(def_id, None);
.print_def_path(def_id, &[]);
s
}
}
Expand All @@ -905,21 +904,13 @@ impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> {
fn print_def_path(
mut self,
def_id: DefId,
substs: Option<SubstsRef<'tcx>>,
substs: &'tcx [Kind<'tcx>],
) -> Result<Self::Path, Self::Error> {
define_scoped_cx!(self);

// FIXME(eddyb) avoid querying `tcx.generics_of` and `tcx.def_key`
// both here and in `default_print_def_path`.
let generics = substs.map(|_| self.tcx.generics_of(def_id));
if generics.as_ref().and_then(|g| g.parent).is_none() {
if substs.is_empty() {
match self.try_print_visible_def_path(def_id)? {
(cx, true) => return if let (Some(generics), Some(substs)) = (generics, substs) {
let args = cx.generic_args_to_print(generics, substs);
cx.path_generic_args(Ok, args)
} else {
Ok(cx)
},
(cx, true) => return Ok(cx),
(cx, false) => self = cx,
}
}
Expand All @@ -942,7 +933,7 @@ impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> {
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, None),
|cx| cx.print_def_path(parent_def_id, &[]),
&format!("<impl at {:?}>", span),
);
}
Expand Down Expand Up @@ -1073,7 +1064,7 @@ impl<F: fmt::Write> PrettyPrinter<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F>
fn print_value_path(
mut self,
def_id: DefId,
substs: Option<SubstsRef<'tcx>>,
substs: &'tcx [Kind<'tcx>],
) -> Result<Self::Path, Self::Error> {
let was_in_value = std::mem::replace(&mut self.in_value, true);
self = self.print_def_path(def_id, substs)?;
Expand Down Expand Up @@ -1476,7 +1467,7 @@ define_print_and_forward_display! {
ty::ExistentialPredicate::Trait(x) => p!(print(x)),
ty::ExistentialPredicate::Projection(x) => p!(print(x)),
ty::ExistentialPredicate::AutoTrait(def_id) => {
p!(print_def_path(def_id, None));
p!(print_def_path(def_id, &[]));
}
}
}
Expand Down Expand Up @@ -1509,7 +1500,7 @@ define_print_and_forward_display! {
}

ty::TraitRef<'tcx> {
p!(print_def_path(self.def_id, Some(self.substs)));
p!(print_def_path(self.def_id, self.substs));
}

ConstValue<'tcx> {
Expand Down Expand Up @@ -1553,7 +1544,7 @@ define_print_and_forward_display! {
}

ty::ProjectionTy<'tcx> {
p!(print_def_path(self.item_def_id, Some(self.substs)));
p!(print_def_path(self.item_def_id, self.substs));
}

ty::ClosureKind {
Expand All @@ -1574,17 +1565,17 @@ define_print_and_forward_display! {
ty::Predicate::WellFormed(ty) => p!(print(ty), write(" well-formed")),
ty::Predicate::ObjectSafe(trait_def_id) => {
p!(write("the trait `"),
print_def_path(trait_def_id, None),
print_def_path(trait_def_id, &[]),
write("` is object-safe"))
}
ty::Predicate::ClosureKind(closure_def_id, _closure_substs, kind) => {
p!(write("the closure `"),
print_value_path(closure_def_id, None),
print_value_path(closure_def_id, &[]),
write("` implements the trait `{}`", kind))
}
ty::Predicate::ConstEvaluatable(def_id, substs) => {
p!(write("the constant `"),
print_value_path(def_id, Some(substs)),
print_value_path(def_id, substs),
write("` can be evaluated"))
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/structural_impls.rs
Expand Up @@ -36,7 +36,7 @@ impl fmt::Debug for ty::TraitDef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
FmtPrinter::new(tcx, f, Namespace::TypeNS)
.print_def_path(self.def_id, None)?;
.print_def_path(self.def_id, &[])?;
Ok(())
})
}
Expand All @@ -46,7 +46,7 @@ impl fmt::Debug for ty::AdtDef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
FmtPrinter::new(tcx, f, Namespace::TypeNS)
.print_def_path(self.did, None)?;
.print_def_path(self.did, &[])?;
Ok(())
})
}
Expand Down

0 comments on commit 8619ede

Please sign in to comment.