Skip to content

Commit a6c295c

Browse files
committed
Modify repr() so that when -Z verbose is used, at least, it does not
fetch trait definitions. This allows is to be used early in the compiler without triggering ICEs. Also make -Z verbose less horrifyingly ugly.
1 parent 3e88b5b commit a6c295c

File tree

1 file changed

+58
-34
lines changed

1 file changed

+58
-34
lines changed

src/librustc/util/ppaux.rs

+58-34
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ pub fn ty_to_string<'tcx>(cx: &ctxt<'tcx>, typ: &ty::TyS<'tcx>) -> String {
395395
}
396396
ty_enum(did, substs) | ty_struct(did, substs) => {
397397
let base = ty::item_path_str(cx, did);
398-
let generics = ty::lookup_item_type(cx, did).generics;
399-
parameterized(cx, &base, substs, &generics, did, &[])
398+
parameterized(cx, &base, substs, did, &[],
399+
|| ty::lookup_item_type(cx, did).generics)
400400
}
401401
ty_trait(ref data) => {
402402
data.user_string(cx)
@@ -444,23 +444,40 @@ pub fn explicit_self_category_to_str(category: &ty::ExplicitSelfCategory)
444444
}
445445
}
446446

447-
pub fn parameterized<'tcx>(cx: &ctxt<'tcx>,
448-
base: &str,
449-
substs: &subst::Substs<'tcx>,
450-
generics: &ty::Generics<'tcx>,
451-
did: ast::DefId,
452-
projections: &[ty::ProjectionPredicate<'tcx>])
453-
-> String
447+
pub fn parameterized<'tcx,GG>(cx: &ctxt<'tcx>,
448+
base: &str,
449+
substs: &subst::Substs<'tcx>,
450+
did: ast::DefId,
451+
projections: &[ty::ProjectionPredicate<'tcx>],
452+
get_generics: GG)
453+
-> String
454+
where GG : FnOnce() -> ty::Generics<'tcx>
454455
{
455456
if cx.sess.verbose() {
456-
if substs.is_noop() {
457-
return format!("{}", base);
458-
} else {
459-
return format!("{}<{},{}>",
460-
base,
461-
substs.regions.repr(cx),
462-
substs.types.repr(cx));
457+
let mut strings = vec![];
458+
match substs.regions {
459+
subst::ErasedRegions => {
460+
strings.push(format!(".."));
461+
}
462+
subst::NonerasedRegions(ref regions) => {
463+
for region in regions.iter() {
464+
strings.push(region.repr(cx));
465+
}
466+
}
463467
}
468+
for ty in substs.types.iter() {
469+
strings.push(ty.repr(cx));
470+
}
471+
for projection in projections.iter() {
472+
strings.push(format!("{}={}",
473+
projection.projection_ty.item_name.user_string(cx),
474+
projection.ty.user_string(cx)));
475+
}
476+
return if strings.is_empty() {
477+
format!("{}", base)
478+
} else {
479+
format!("{}<{}>", base, strings.connect(","))
480+
};
464481
}
465482

466483
let mut strs = Vec::new();
@@ -484,6 +501,13 @@ pub fn parameterized<'tcx>(cx: &ctxt<'tcx>,
484501
}
485502
}
486503

504+
// It is important to execute this conditionally, only if -Z
505+
// verbose is false. Otherwise, debug logs can sometimes cause
506+
// ICEs trying to fetch the generics early in the pipeline. This
507+
// is kind of a hacky workaround in that -Z verbose is required to
508+
// avoid those ICEs.
509+
let generics = get_generics();
510+
487511
let tps = substs.types.get_slice(subst::TypeSpace);
488512
let ty_params = generics.types.get_slice(subst::TypeSpace);
489513
let has_defaults = ty_params.last().map_or(false, |def| def.default.is_some());
@@ -645,13 +669,12 @@ impl<'tcx> UserString<'tcx> for TraitAndProjections<'tcx> {
645669
fn user_string(&self, tcx: &ctxt<'tcx>) -> String {
646670
let &(ref trait_ref, ref projection_bounds) = self;
647671
let base = ty::item_path_str(tcx, trait_ref.def_id);
648-
let trait_def = ty::lookup_trait_def(tcx, trait_ref.def_id);
649672
parameterized(tcx,
650673
&base,
651674
trait_ref.substs,
652-
&trait_def.generics,
653675
trait_ref.def_id,
654-
&projection_bounds[])
676+
&projection_bounds[],
677+
|| ty::lookup_trait_def(tcx, trait_ref.def_id).generics.clone())
655678
}
656679
}
657680

@@ -687,10 +710,9 @@ impl<'tcx> UserString<'tcx> for ty::TyTrait<'tcx> {
687710
}
688711

689712
impl<'tcx> Repr<'tcx> for ty::TypeParameterDef<'tcx> {
690-
fn repr(&self, tcx: &ctxt<'tcx>) -> String {
691-
format!("TypeParameterDef({:?}, {}, {:?}/{})",
713+
fn repr(&self, _tcx: &ctxt<'tcx>) -> String {
714+
format!("TypeParameterDef({:?}, {:?}/{})",
692715
self.def_id,
693-
self.bounds.repr(tcx),
694716
self.space,
695717
self.index)
696718
}
@@ -781,11 +803,8 @@ impl<'tcx> Repr<'tcx> for ty::TraitRef<'tcx> {
781803
// to enumerate the `for<...>` etc because the debruijn index
782804
// tells you everything you need to know.
783805
let base = ty::item_path_str(tcx, self.def_id);
784-
let trait_def = ty::lookup_trait_def(tcx, self.def_id);
785-
format!("TraitRef({}, {})",
786-
self.substs.self_ty().repr(tcx),
787-
parameterized(tcx, &base, self.substs,
788-
&trait_def.generics, self.def_id, &[]))
806+
parameterized(tcx, &base, self.substs, self.def_id, &[],
807+
|| ty::lookup_trait_def(tcx, self.def_id).generics.clone())
789808
}
790809
}
791810

@@ -987,16 +1006,22 @@ impl<'tcx> Repr<'tcx> for ty::TypeScheme<'tcx> {
9871006

9881007
impl<'tcx> Repr<'tcx> for ty::Generics<'tcx> {
9891008
fn repr(&self, tcx: &ctxt<'tcx>) -> String {
990-
format!("Generics(types: {}, regions: {}, predicates: {})",
1009+
format!("Generics(types: {}, regions: {})",
9911010
self.types.repr(tcx),
992-
self.regions.repr(tcx),
1011+
self.regions.repr(tcx))
1012+
}
1013+
}
1014+
1015+
impl<'tcx> Repr<'tcx> for ty::GenericPredicates<'tcx> {
1016+
fn repr(&self, tcx: &ctxt<'tcx>) -> String {
1017+
format!("GenericPredicates(predicates: {})",
9931018
self.predicates.repr(tcx))
9941019
}
9951020
}
9961021

997-
impl<'tcx> Repr<'tcx> for ty::GenericBounds<'tcx> {
1022+
impl<'tcx> Repr<'tcx> for ty::InstantiatedPredicates<'tcx> {
9981023
fn repr(&self, tcx: &ctxt<'tcx>) -> String {
999-
format!("GenericBounds({})",
1024+
format!("InstantiatedPredicates({})",
10001025
self.predicates.repr(tcx))
10011026
}
10021027
}
@@ -1249,9 +1274,8 @@ impl<'tcx, T> UserString<'tcx> for ty::Binder<T>
12491274
impl<'tcx> UserString<'tcx> for ty::TraitRef<'tcx> {
12501275
fn user_string(&self, tcx: &ctxt<'tcx>) -> String {
12511276
let path_str = ty::item_path_str(tcx, self.def_id);
1252-
let trait_def = ty::lookup_trait_def(tcx, self.def_id);
1253-
parameterized(tcx, &path_str, self.substs,
1254-
&trait_def.generics, self.def_id, &[])
1277+
parameterized(tcx, &path_str, self.substs, self.def_id, &[],
1278+
|| ty::lookup_trait_def(tcx, self.def_id).generics.clone())
12551279
}
12561280
}
12571281

0 commit comments

Comments
 (0)