提交 d8d813c4 编写于 作者: S Scott Olson

Resolve associated constants.

Fixes #130.
上级 9e248938
......@@ -814,6 +814,7 @@ pub(super) fn eval_operand(&mut self, op: &mir::Operand<'tcx>) -> EvalResult<'tc
// 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))
}
......
......@@ -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;
......
......@@ -209,4 +209,24 @@ fn normalize_and_test_predicates(&mut self, predicates: Vec<ty::Predicate<'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)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册