Skip to content

Commit 4835764

Browse files
committed
Fixed issue rust-lang#28.
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.
1 parent 54cb2cf commit 4835764

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/semcheck/translate.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -174,21 +174,31 @@ impl<'a, 'gcx, 'tcx> TranslationContext<'a, 'gcx, 'tcx> {
174174
TyDynamic(preds, region) => {
175175
// hacky error catching mechanism
176176
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
177-
let mut success = true;
177+
use std::cell::Cell;
178+
179+
let success = Cell::new(true);
178180
let err_pred = AutoTrait(DefId::local(CRATE_DEF_INDEX));
179181

180-
let target_preds = self.tcx.mk_existential_predicates(preds.iter().map(|p| {
182+
let res: Vec<_> = preds.iter().map(|p| {
183+
debug!("pred: {:?}", p);
181184
match *p.skip_binder() {
182-
Trait(ExistentialTraitRef { def_id: did, substs }) => {
185+
Trait(existential_trait_ref) => {
186+
let trait_ref = Binder(existential_trait_ref)
187+
.with_self_ty(self.tcx, self.tcx.types.err);
188+
let did = trait_ref.skip_binder().def_id;
189+
let substs = trait_ref.skip_binder().substs;
190+
183191
if let Some((target_def_id, target_substs)) =
184192
self.translate_orig_substs(index_map, did, substs)
185193
{
186-
Trait(ExistentialTraitRef {
194+
let target_trait_ref = TraitRef {
187195
def_id: target_def_id,
188-
substs: target_substs
189-
})
190-
} else {
191-
success = false;
196+
substs: target_substs,
197+
};
198+
Trait(ExistentialTraitRef::erase_self_ty(self.tcx,
199+
target_trait_ref))
200+
} else {
201+
success.set(false);
192202
err_pred
193203
}
194204
},
@@ -202,17 +212,18 @@ impl<'a, 'gcx, 'tcx> TranslationContext<'a, 'gcx, 'tcx> {
202212
ty: ty,
203213
})
204214
} else {
205-
success = false;
215+
success.set(false);
206216
err_pred
207217
}
208218
},
209219
AutoTrait(did) => {
210220
AutoTrait(self.translate_orig(did))
211221
},
212222
}
213-
}));
223+
}).collect();
214224

215-
if success {
225+
if success.get() {
226+
let target_preds = self.tcx.mk_existential_predicates(res.iter());
216227
self.tcx.mk_dynamic(Binder(target_preds), region)
217228
} else {
218229
ty

tests/examples.rs

+1
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,5 @@ test!(structs);
121121
test!(swap);
122122
test!(traits);
123123
test!(trait_impls);
124+
test!(trait_objects);
124125
test!(ty_alias);

0 commit comments

Comments
 (0)