Skip to content

Commit e4e880d

Browse files
committed
Const-eval array element repetition count
1 parent db89a75 commit e4e880d

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/librustc_mir/hair/cx/expr.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use hair::cx::block;
1717
use hair::cx::pattern::PatNode;
1818
use hair::cx::to_ref::ToRef;
1919
use rustc::front::map;
20-
use rustc::middle::const_eval;
2120
use rustc::middle::def;
2221
use rustc::middle::region::CodeExtent;
2322
use rustc::middle::pat_util;
@@ -81,10 +80,9 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
8180
}
8281
}
8382

84-
hir::ExprLit(..) => {
85-
let value = const_eval::eval_const_expr(cx.tcx, self);
86-
ExprKind::Literal { literal: Literal::Value { value: value } }
87-
}
83+
hir::ExprLit(..) => ExprKind::Literal {
84+
literal: cx.const_eval_literal(self)
85+
},
8886

8987
hir::ExprBinary(op, ref lhs, ref rhs) => {
9088
if cx.tcx.is_method_call(self.id) {
@@ -273,8 +271,17 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
273271

274272
// Now comes the rote stuff:
275273

276-
hir::ExprRepeat(ref v, ref c) =>
277-
ExprKind::Repeat { value: v.to_ref(), count: c.to_ref() },
274+
hir::ExprRepeat(ref v, ref c) => ExprKind::Repeat {
275+
value: v.to_ref(),
276+
count: Expr {
277+
ty: cx.tcx.expr_ty(c),
278+
temp_lifetime: None,
279+
span: c.span,
280+
kind: ExprKind::Literal {
281+
literal: cx.const_eval_literal(c)
282+
}
283+
}.to_ref()
284+
},
278285
hir::ExprRet(ref v) =>
279286
ExprKind::Return { value: v.to_ref() },
280287
hir::ExprBreak(label) =>

src/librustc_mir/hair/cx/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
use hair::*;
1919
use repr::*;
2020

21-
use rustc::middle::const_eval::ConstVal;
21+
use rustc::middle::const_eval::{self, ConstVal};
2222
use rustc::middle::def_id::DefId;
2323
use rustc::middle::infer::InferCtxt;
2424
use rustc::middle::subst::{Subst, Substs};
2525
use rustc::middle::ty::{self, Ty};
2626
use syntax::codemap::Span;
2727
use syntax::parse::token;
28+
use rustc_front::hir;
2829

2930
#[derive(Copy, Clone)]
3031
pub struct Cx<'a, 'tcx: 'a> {
@@ -73,6 +74,10 @@ impl<'a,'tcx:'a> Cx<'a, 'tcx> {
7374
Literal::Value { value: ConstVal::Bool(false) }
7475
}
7576

77+
pub fn const_eval_literal(&mut self, e: &hir::Expr) -> Literal<'tcx> {
78+
Literal::Value { value: const_eval::eval_const_expr(self.tcx, e) }
79+
}
80+
7681
pub fn partial_eq(&mut self, ty: Ty<'tcx>) -> ItemRef<'tcx> {
7782
let eq_def_id = self.tcx.lang_items.eq_trait().unwrap();
7883
self.cmp_method_ref(eq_def_id, "eq", ty)

src/librustc_mir/hair/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ pub enum ExprKind<'tcx> {
210210
},
211211
Repeat {
212212
value: ExprRef<'tcx>,
213+
// FIXME(#29789): Add a separate hair::Constant<'tcx> so this could be more explicit about
214+
// its contained data. Currently this should only contain expression of ExprKind::Literal
215+
// kind.
213216
count: ExprRef<'tcx>,
214217
},
215218
Vec {

0 commit comments

Comments
 (0)