Skip to content

Commit 59e6b20

Browse files
committed
Const-eval more constants during MIR building
1 parent 81dd382 commit 59e6b20

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/librustc_mir/hair/cx/expr.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,17 +514,29 @@ fn convert_arm<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, arm: &'tcx hir::Arm) -> Arm<
514514

515515
fn convert_path_expr<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, expr: &'tcx hir::Expr) -> ExprKind<'tcx> {
516516
let substs = cx.tcx.mk_substs(cx.tcx.node_id_item_substs(expr.id).substs);
517-
match cx.tcx.def_map.borrow()[&expr.id].full_def() {
517+
// Otherwise there may be def_map borrow conflicts
518+
let def = cx.tcx.def_map.borrow()[&expr.id].full_def();
519+
match def {
518520
def::DefVariant(_, def_id, false) |
519521
def::DefStruct(def_id) |
520522
def::DefFn(def_id, _) |
521-
def::DefConst(def_id) |
522-
def::DefMethod(def_id) |
523-
def::DefAssociatedConst(def_id) =>
523+
def::DefMethod(def_id) =>
524524
ExprKind::Literal {
525525
literal: Literal::Item { def_id: def_id, substs: substs }
526526
},
527527

528+
def::DefConst(def_id) |
529+
def::DefAssociatedConst(def_id) => {
530+
if let Some(v) = cx.try_const_eval_literal(expr) {
531+
ExprKind::Literal { literal: v }
532+
} else {
533+
ExprKind::Literal {
534+
literal: Literal::Item { def_id: def_id, substs: substs }
535+
}
536+
}
537+
}
538+
539+
528540
def::DefStatic(node_id, _) =>
529541
ExprKind::StaticRef {
530542
id: node_id,

src/librustc_mir/hair/cx/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ impl<'a,'tcx:'a> Cx<'a, 'tcx> {
7676
Literal::Value { value: const_eval::eval_const_expr(self.tcx, e) }
7777
}
7878

79+
pub fn try_const_eval_literal(&mut self, e: &hir::Expr) -> Option<Literal<'tcx>> {
80+
let hint = const_eval::EvalHint::ExprTypeChecked;
81+
const_eval::eval_const_expr_partial(self.tcx, e, hint, None)
82+
.ok()
83+
.map(|v| Literal::Value { value: v })
84+
}
85+
7986
pub fn partial_eq(&mut self, ty: Ty<'tcx>) -> ItemRef<'tcx> {
8087
let eq_def_id = self.tcx.lang_items.eq_trait().unwrap();
8188
self.cmp_method_ref(eq_def_id, "eq", ty)

0 commit comments

Comments
 (0)