Skip to content

Commit

Permalink
Const-eval more constants during MIR building
Browse files Browse the repository at this point in the history
  • Loading branch information
nagisa committed Dec 11, 2015
1 parent 81dd382 commit 5fb2edc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/librustc_mir/hair/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,17 +514,29 @@ fn convert_arm<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, arm: &'tcx hir::Arm) -> Arm<

fn convert_path_expr<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, expr: &'tcx hir::Expr) -> ExprKind<'tcx> {
let substs = cx.tcx.mk_substs(cx.tcx.node_id_item_substs(expr.id).substs);
match cx.tcx.def_map.borrow()[&expr.id].full_def() {
// Otherwise there may be def_map borrow conflicts
let def = { cx.tcx.def_map.borrow()[&expr.id].full_def() };

This comment has been minimized.

Copy link
@nikomatsakis

nikomatsakis Dec 15, 2015

Nit: braces are not needed, just the let will do.

This comment has been minimized.

Copy link
@nagisa

nagisa Dec 15, 2015

Author Owner

This was previously fixed in a separatee commit 1a1f474, but I went ahead and squashed it with this commit.

match def {
def::DefVariant(_, def_id, false) |
def::DefStruct(def_id) |
def::DefFn(def_id, _) |
def::DefConst(def_id) |
def::DefMethod(def_id) |
def::DefAssociatedConst(def_id) =>
def::DefMethod(def_id) =>
ExprKind::Literal {
literal: Literal::Item { def_id: def_id, substs: substs }
},

def::DefConst(def_id) |
def::DefAssociatedConst(def_id) => {
if let Some(v) = cx.try_const_eval_literal(expr) {
ExprKind::Literal { literal: v }
} else {
ExprKind::Literal {
literal: Literal::Item { def_id: def_id, substs: substs }
}
}
}


def::DefStatic(node_id, _) =>
ExprKind::StaticRef {
id: node_id,
Expand Down
7 changes: 7 additions & 0 deletions src/librustc_mir/hair/cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ impl<'a,'tcx:'a> Cx<'a, 'tcx> {
Literal::Value { value: const_eval::eval_const_expr(self.tcx, e) }
}

pub fn try_const_eval_literal(&mut self, e: &hir::Expr) -> Option<Literal<'tcx>> {
let hint = const_eval::EvalHint::ExprTypeChecked;
const_eval::eval_const_expr_partial(self.tcx, e, hint, None)
.ok()
.map(|v| Literal::Value { value: v })
}

pub fn partial_eq(&mut self, ty: Ty<'tcx>) -> ItemRef<'tcx> {
let eq_def_id = self.tcx.lang_items.eq_trait().unwrap();
self.cmp_method_ref(eq_def_id, "eq", ty)
Expand Down

0 comments on commit 5fb2edc

Please sign in to comment.