Skip to content

Commit 18b851b

Browse files
committed
Auto merge of #30944 - fhahn:issue-29789-use-constant2, r=nagisa
This PR for #29789 uses `rustc::repr::mir::Constant` in `ExprKind::Repeat`, which seems to fit quite nicely. Is there a reason for not re-using that type?
2 parents 54475e9 + 9884ff1 commit 18b851b

File tree

7 files changed

+26
-18
lines changed

7 files changed

+26
-18
lines changed

src/librustc/mir/repr.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ pub enum Rvalue<'tcx> {
683683
Use(Operand<'tcx>),
684684

685685
// [x; 32]
686-
Repeat(Operand<'tcx>, Constant<'tcx>),
686+
Repeat(Operand<'tcx>, TypedConstVal<'tcx>),
687687

688688
// &x or &mut x
689689
Ref(Region, BorrowKind, Lvalue<'tcx>),
@@ -891,6 +891,20 @@ pub struct Constant<'tcx> {
891891
pub literal: Literal<'tcx>,
892892
}
893893

894+
#[derive(Clone, RustcEncodable, RustcDecodable)]
895+
pub struct TypedConstVal<'tcx> {
896+
pub ty: Ty<'tcx>,
897+
pub span: Span,
898+
pub value: ConstVal
899+
}
900+
901+
impl<'tcx> Debug for TypedConstVal<'tcx> {
902+
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
903+
try!(write!(fmt, "const "));
904+
fmt_const_val(fmt, &self.value)
905+
}
906+
}
907+
894908
#[derive(Clone, Copy, Debug, PartialEq, RustcEncodable, RustcDecodable)]
895909
pub enum ItemKind {
896910
Constant,

src/librustc/mir/visit.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,8 @@ macro_rules! make_mir_visitor {
213213
}
214214

215215
Rvalue::Repeat(ref $($mutability)* value,
216-
ref $($mutability)* len) => {
216+
_) => {
217217
self.visit_operand(value);
218-
self.visit_constant(len);
219218
}
220219

221220
Rvalue::Ref(r, bk, ref $($mutability)* path) => {

src/librustc_mir/build/expr/as_rvalue.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ impl<'a,'tcx> Builder<'a,'tcx> {
4444
}
4545
ExprKind::Repeat { value, count } => {
4646
let value_operand = unpack!(block = this.as_operand(block, value));
47-
let count = this.as_constant(count);
4847
block.and(Rvalue::Repeat(value_operand, count))
4948
}
5049
ExprKind::Borrow { region, borrow_kind, arg } => {

src/librustc_mir/hair/cx/expr.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use hair::cx::block;
1515
use hair::cx::to_ref::ToRef;
1616
use rustc::front::map;
1717
use rustc::middle::def::Def;
18+
use rustc::middle::const_eval;
1819
use rustc::middle::region::CodeExtent;
1920
use rustc::middle::pat_util;
2021
use rustc::middle::ty::{self, VariantDef, Ty};
@@ -325,14 +326,11 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
325326

326327
hir::ExprRepeat(ref v, ref c) => ExprKind::Repeat {
327328
value: v.to_ref(),
328-
count: Expr {
329+
count: TypedConstVal {
329330
ty: cx.tcx.expr_ty(c),
330-
temp_lifetime: None,
331331
span: c.span,
332-
kind: ExprKind::Literal {
333-
literal: cx.const_eval_literal(c)
334-
}
335-
}.to_ref()
332+
value: const_eval::eval_const_expr(cx.tcx, c)
333+
}
336334
},
337335
hir::ExprRet(ref v) =>
338336
ExprKind::Return { value: v.to_ref() },

src/librustc_mir/hair/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
//! unit-tested and separated from the Rust source and compiler data
1515
//! structures.
1616
17-
use rustc::mir::repr::{BinOp, BorrowKind, Field, Literal, Mutability, UnOp, ItemKind};
17+
use rustc::mir::repr::{BinOp, BorrowKind, Field, Literal, Mutability, UnOp, ItemKind,
18+
TypedConstVal};
1819
use rustc::middle::const_eval::ConstVal;
1920
use rustc::middle::def_id::DefId;
2021
use rustc::middle::region::CodeExtent;
@@ -213,10 +214,7 @@ pub enum ExprKind<'tcx> {
213214
},
214215
Repeat {
215216
value: ExprRef<'tcx>,
216-
// FIXME(#29789): Add a separate hair::Constant<'tcx> so this could be more explicit about
217-
// its contained data. Currently this should only contain expression of ExprKind::Literal
218-
// kind.
219-
count: ExprRef<'tcx>,
217+
count: TypedConstVal<'tcx>,
220218
},
221219
Vec {
222220
fields: Vec<ExprRef<'tcx>>,

src/librustc_mir/transform/erase_regions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ impl<'a, 'tcx> EraseRegions<'a, 'tcx> {
143143
Rvalue::Use(ref mut operand) => {
144144
self.erase_regions_operand(operand)
145145
}
146-
Rvalue::Repeat(ref mut operand, ref mut constant) => {
146+
Rvalue::Repeat(ref mut operand, ref mut value) => {
147147
self.erase_regions_operand(operand);
148-
self.erase_regions_constant(constant);
148+
value.ty = self.tcx.erase_regions(&value.ty);
149149
}
150150
Rvalue::Ref(ref mut region, _, ref mut lvalue) => {
151151
*region = ty::ReStatic;

src/librustc_trans/trans/mir/rvalue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
8989

9090
mir::Rvalue::Repeat(ref elem, ref count) => {
9191
let elem = self.trans_operand(bcx, elem);
92-
let size = self.trans_constant(bcx, count).immediate();
92+
let size = self.trans_constval(bcx, &count.value, count.ty).immediate();
9393
let base = expr::get_dataptr(bcx, dest.llval);
9494
tvec::iter_vec_raw(bcx, base, elem.ty, size, |bcx, llslot, _| {
9595
self.store_operand(bcx, llslot, elem);

0 commit comments

Comments
 (0)