Skip to content

Commit 5a881e9

Browse files
Make sure that projection bounds in ty::TraitObject are sorted in a way that is stable across compilation sessions and crate boundaries.
1 parent c87ba3f commit 5a881e9

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

src/librustc/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13031303
}
13041304

13051305
pub fn mk_trait(self, mut obj: TraitObject<'tcx>) -> Ty<'tcx> {
1306-
obj.projection_bounds.sort_by(|a, b| a.sort_key().cmp(&b.sort_key()));
1306+
obj.projection_bounds.sort_by_key(|b| b.sort_key(self));
13071307
self.mk_ty(TyTrait(box obj))
13081308
}
13091309

src/librustc/ty/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,10 +1018,6 @@ impl<'tcx> PolyProjectionPredicate<'tcx> {
10181018
pub fn item_name(&self) -> Name {
10191019
self.0.projection_ty.item_name // safe to skip the binder to access a name
10201020
}
1021-
1022-
pub fn sort_key(&self) -> (DefId, Name) {
1023-
self.0.projection_ty.sort_key()
1024-
}
10251021
}
10261022

10271023
pub trait ToPolyTraitRef<'tcx> {

src/librustc/ty/sty.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::mem;
2323
use std::ops;
2424
use syntax::abi;
2525
use syntax::ast::{self, Name};
26-
use syntax::parse::token::keywords;
26+
use syntax::parse::token::{keywords, InternedString};
2727

2828
use serialize::{Decodable, Decoder, Encodable, Encoder};
2929

@@ -440,12 +440,6 @@ pub struct ProjectionTy<'tcx> {
440440
pub item_name: Name,
441441
}
442442

443-
impl<'tcx> ProjectionTy<'tcx> {
444-
pub fn sort_key(&self) -> (DefId, Name) {
445-
(self.trait_ref.def_id, self.item_name)
446-
}
447-
}
448-
449443
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
450444
pub struct BareFnTy<'tcx> {
451445
pub unsafety: hir::Unsafety,
@@ -738,8 +732,17 @@ impl<'a, 'tcx, 'gcx> PolyExistentialProjection<'tcx> {
738732
self.0.item_name // safe to skip the binder to access a name
739733
}
740734

741-
pub fn sort_key(&self) -> (DefId, Name) {
742-
(self.0.trait_ref.def_id, self.0.item_name)
735+
pub fn sort_key(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> (u64, InternedString) {
736+
// We want something here that is stable across crate boundaries.
737+
// The DefId isn't but the `deterministic_hash` of the corresponding
738+
// DefPath is.
739+
let trait_def = tcx.lookup_trait_def(self.0.trait_ref.def_id);
740+
let def_path_hash = trait_def.def_path_hash;
741+
742+
// An `ast::Name` is also not stable (it's just an index into an
743+
// interning table), so map to the corresponding `InternedString`.
744+
let item_name = self.0.item_name.as_str();
745+
(def_path_hash, item_name)
743746
}
744747

745748
pub fn with_self_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,

src/librustc/ty/trait_def.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,20 @@ pub struct TraitDef<'tcx> {
7070
pub specialization_graph: RefCell<traits::specialization_graph::Graph>,
7171

7272
/// Various flags
73-
pub flags: Cell<TraitFlags>
73+
pub flags: Cell<TraitFlags>,
74+
75+
/// The ICH of this trait's DefPath, cached here so it doesn't have to be
76+
/// recomputed all the time.
77+
pub def_path_hash: u64,
7478
}
7579

7680
impl<'a, 'gcx, 'tcx> TraitDef<'tcx> {
7781
pub fn new(unsafety: hir::Unsafety,
7882
paren_sugar: bool,
7983
generics: &'tcx ty::Generics<'tcx>,
8084
trait_ref: ty::TraitRef<'tcx>,
81-
associated_type_names: Vec<Name>)
85+
associated_type_names: Vec<Name>,
86+
def_path_hash: u64)
8287
-> TraitDef<'tcx> {
8388
TraitDef {
8489
paren_sugar: paren_sugar,
@@ -90,6 +95,7 @@ impl<'a, 'gcx, 'tcx> TraitDef<'tcx> {
9095
blanket_impls: RefCell::new(vec![]),
9196
flags: Cell::new(ty::TraitFlags::NO_TRAIT_FLAGS),
9297
specialization_graph: RefCell::new(traits::specialization_graph::Graph::new()),
98+
def_path_hash: def_path_hash,
9399
}
94100
}
95101

src/librustc_metadata/decoder.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,14 @@ pub fn get_trait_def<'a, 'tcx>(cdata: Cmd,
385385
let unsafety = parse_unsafety(item_doc);
386386
let associated_type_names = parse_associated_type_names(item_doc);
387387
let paren_sugar = parse_paren_sugar(item_doc);
388+
let def_path = def_path(cdata, item_id);
388389

389390
ty::TraitDef::new(unsafety,
390391
paren_sugar,
391392
generics,
392393
item_trait_ref(item_doc, tcx, cdata),
393-
associated_type_names)
394+
associated_type_names,
395+
def_path.deterministic_hash(tcx))
394396
}
395397

396398
pub fn get_adt_def<'a, 'tcx>(cdata: Cmd,

src/librustc_typeck/collect.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,12 +1290,15 @@ fn trait_def_of_item<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
12901290
}
12911291
}).collect();
12921292

1293+
let def_path_hash = tcx.def_path(def_id).deterministic_hash(tcx);
1294+
12931295
let trait_ref = ty::TraitRef::new(def_id, substs);
12941296
let trait_def = ty::TraitDef::new(unsafety,
12951297
paren_sugar,
12961298
ty_generics,
12971299
trait_ref,
1298-
associated_type_names);
1300+
associated_type_names,
1301+
def_path_hash);
12991302

13001303
tcx.intern_trait_def(trait_def)
13011304
}

0 commit comments

Comments
 (0)