Skip to content

Commit

Permalink
Fixed issue rust-lang#28.
Browse files Browse the repository at this point in the history
Trait objects weren't translated correctly in some edge cases. Combined
with an ordering bug in the filtering logic, this led to crashes when
sentinel values were passed to the analysis mechanisms.
  • Loading branch information
ibabushkin committed Aug 31, 2017
1 parent 54cb2cf commit 4835764
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/semcheck/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,31 @@ impl<'a, 'gcx, 'tcx> TranslationContext<'a, 'gcx, 'tcx> {
TyDynamic(preds, region) => {
// hacky error catching mechanism
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
let mut success = true;
use std::cell::Cell;

let success = Cell::new(true);
let err_pred = AutoTrait(DefId::local(CRATE_DEF_INDEX));

let target_preds = self.tcx.mk_existential_predicates(preds.iter().map(|p| {
let res: Vec<_> = preds.iter().map(|p| {
debug!("pred: {:?}", p);
match *p.skip_binder() {
Trait(ExistentialTraitRef { def_id: did, substs }) => {
Trait(existential_trait_ref) => {
let trait_ref = Binder(existential_trait_ref)
.with_self_ty(self.tcx, self.tcx.types.err);
let did = trait_ref.skip_binder().def_id;
let substs = trait_ref.skip_binder().substs;

if let Some((target_def_id, target_substs)) =
self.translate_orig_substs(index_map, did, substs)
{
Trait(ExistentialTraitRef {
let target_trait_ref = TraitRef {
def_id: target_def_id,
substs: target_substs
})
} else {
success = false;
substs: target_substs,
};
Trait(ExistentialTraitRef::erase_self_ty(self.tcx,
target_trait_ref))
} else {
success.set(false);
err_pred
}
},
Expand All @@ -202,17 +212,18 @@ impl<'a, 'gcx, 'tcx> TranslationContext<'a, 'gcx, 'tcx> {
ty: ty,
})
} else {
success = false;
success.set(false);
err_pred
}
},
AutoTrait(did) => {
AutoTrait(self.translate_orig(did))
},
}
}));
}).collect();

if success {
if success.get() {
let target_preds = self.tcx.mk_existential_predicates(res.iter());
self.tcx.mk_dynamic(Binder(target_preds), region)
} else {
ty
Expand Down
1 change: 1 addition & 0 deletions tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,5 @@ test!(structs);
test!(swap);
test!(traits);
test!(trait_impls);
test!(trait_objects);
test!(ty_alias);

0 comments on commit 4835764

Please sign in to comment.