Skip to content

Commit

Permalink
Resolve associated constants.
Browse files Browse the repository at this point in the history
Fixes #130.
  • Loading branch information
solson committed Feb 10, 2017
1 parent 9e24893 commit d8d813c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
// function items are zero sized
Value::ByRef(self.memory.allocate(0, 0)?)
} else {
let (def_id, substs) = self.resolve_associated_const(def_id, substs);
let cid = GlobalId { def_id, substs, promoted: None };
self.read_lvalue(Lvalue::Global(cid))
}
Expand Down
1 change: 1 addition & 0 deletions src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ struct ConstantExtractor<'a, 'b: 'a, 'tcx: 'b> {

impl<'a, 'b, 'tcx> ConstantExtractor<'a, 'b, 'tcx> {
fn global_item(&mut self, def_id: DefId, substs: &'tcx subst::Substs<'tcx>, span: Span, immutable: bool) {
let (def_id, substs) = self.ecx.resolve_associated_const(def_id, substs);
let cid = GlobalId { def_id, substs, promoted: None };
if self.ecx.globals.contains_key(&cid) {
return;
Expand Down
20 changes: 20 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,24 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
fulfill_cx.select_all_or_error(&infcx).is_ok()
})
}

pub(crate) fn resolve_associated_const(
&self,
def_id: DefId,
substs: &'tcx Substs<'tcx>,
) -> (DefId, &'tcx Substs<'tcx>) {
if let Some(trait_id) = self.tcx.trait_of_item(def_id) {
let trait_ref = ty::Binder(ty::TraitRef::new(trait_id, substs));
let vtable = self.fulfill_obligation(trait_ref);
if let traits::VtableImpl(vtable_impl) = vtable {
let name = self.tcx.item_name(def_id);
let assoc_const_opt = self.tcx.associated_items(vtable_impl.impl_def_id)
.find(|item| item.kind == ty::AssociatedKind::Const && item.name == name);
if let Some(assoc_const) = assoc_const_opt {
return (assoc_const.def_id, vtable_impl.substs);
}
}
}
(def_id, substs)
}
}

0 comments on commit d8d813c

Please sign in to comment.