Skip to content

Commit ec4bcc6

Browse files
authored
Rollup merge of rust-lang#40104 - nagisa:mir-the-shiny, r=eddyb
[MIR] Rvalue::ty infallible + remove TypedConstVal Feel free to r+ whenever there aren't any big bit-rot sensitive PRs in the queue. r? @eddyb
2 parents afb9831 + 21c6133 commit ec4bcc6

File tree

11 files changed

+45
-96
lines changed

11 files changed

+45
-96
lines changed

src/librustc/mir/mod.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ pub enum Rvalue<'tcx> {
983983
Use(Operand<'tcx>),
984984

985985
/// [x; 32]
986-
Repeat(Operand<'tcx>, TypedConstVal<'tcx>),
986+
Repeat(Operand<'tcx>, ConstUsize),
987987

988988
/// &x or &mut x
989989
Ref(&'tcx Region, BorrowKind, Lvalue<'tcx>),
@@ -1038,7 +1038,8 @@ pub enum CastKind {
10381038

10391039
#[derive(Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
10401040
pub enum AggregateKind<'tcx> {
1041-
Array,
1041+
/// The type is of the element
1042+
Array(Ty<'tcx>),
10421043
Tuple,
10431044
/// The second field is variant number (discriminant), it's equal to 0
10441045
/// for struct and union expressions. The fourth field is active field
@@ -1135,7 +1136,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
11351136
}
11361137

11371138
match *kind {
1138-
AggregateKind::Array => write!(fmt, "{:?}", lvs),
1139+
AggregateKind::Array(_) => write!(fmt, "{:?}", lvs),
11391140

11401141
AggregateKind::Tuple => {
11411142
match lvs.len() {
@@ -1202,19 +1203,6 @@ pub struct Constant<'tcx> {
12021203
pub literal: Literal<'tcx>,
12031204
}
12041205

1205-
#[derive(Clone, RustcEncodable, RustcDecodable)]
1206-
pub struct TypedConstVal<'tcx> {
1207-
pub ty: Ty<'tcx>,
1208-
pub span: Span,
1209-
pub value: ConstUsize,
1210-
}
1211-
1212-
impl<'tcx> Debug for TypedConstVal<'tcx> {
1213-
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1214-
write!(fmt, "const {}", ConstInt::Usize(self.value))
1215-
}
1216-
}
1217-
12181206
newtype_index!(Promoted, "promoted");
12191207

12201208
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]

src/librustc/mir/tcx.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -134,76 +134,70 @@ impl<'tcx> Lvalue<'tcx> {
134134
}
135135

136136
impl<'tcx> Rvalue<'tcx> {
137-
pub fn ty<'a, 'gcx>(&self, mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Ty<'tcx>>
137+
pub fn ty<'a, 'gcx>(&self, mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>
138138
{
139139
match *self {
140-
Rvalue::Use(ref operand) => Some(operand.ty(mir, tcx)),
140+
Rvalue::Use(ref operand) => operand.ty(mir, tcx),
141141
Rvalue::Repeat(ref operand, ref count) => {
142142
let op_ty = operand.ty(mir, tcx);
143-
let count = count.value.as_u64(tcx.sess.target.uint_type);
143+
let count = count.as_u64(tcx.sess.target.uint_type);
144144
assert_eq!(count as usize as u64, count);
145-
Some(tcx.mk_array(op_ty, count as usize))
145+
tcx.mk_array(op_ty, count as usize)
146146
}
147147
Rvalue::Ref(reg, bk, ref lv) => {
148148
let lv_ty = lv.ty(mir, tcx).to_ty(tcx);
149-
Some(tcx.mk_ref(reg,
149+
tcx.mk_ref(reg,
150150
ty::TypeAndMut {
151151
ty: lv_ty,
152152
mutbl: bk.to_mutbl_lossy()
153153
}
154-
))
154+
)
155155
}
156-
Rvalue::Len(..) => Some(tcx.types.usize),
157-
Rvalue::Cast(.., ty) => Some(ty),
156+
Rvalue::Len(..) => tcx.types.usize,
157+
Rvalue::Cast(.., ty) => ty,
158158
Rvalue::BinaryOp(op, ref lhs, ref rhs) => {
159159
let lhs_ty = lhs.ty(mir, tcx);
160160
let rhs_ty = rhs.ty(mir, tcx);
161-
Some(op.ty(tcx, lhs_ty, rhs_ty))
161+
op.ty(tcx, lhs_ty, rhs_ty)
162162
}
163163
Rvalue::CheckedBinaryOp(op, ref lhs, ref rhs) => {
164164
let lhs_ty = lhs.ty(mir, tcx);
165165
let rhs_ty = rhs.ty(mir, tcx);
166166
let ty = op.ty(tcx, lhs_ty, rhs_ty);
167-
let ty = tcx.intern_tup(&[ty, tcx.types.bool], false);
168-
Some(ty)
167+
tcx.intern_tup(&[ty, tcx.types.bool], false)
169168
}
170169
Rvalue::UnaryOp(_, ref operand) => {
171-
Some(operand.ty(mir, tcx))
170+
operand.ty(mir, tcx)
172171
}
173172
Rvalue::Discriminant(ref lval) => {
174173
let ty = lval.ty(mir, tcx).to_ty(tcx);
175174
if let ty::TyAdt(adt_def, _) = ty.sty {
176-
Some(adt_def.repr.discr_type().to_ty(tcx))
175+
adt_def.repr.discr_type().to_ty(tcx)
177176
} else {
178177
// Undefined behaviour, bug for now; may want to return something for
179178
// the `discriminant` intrinsic later.
180179
bug!("Rvalue::Discriminant on Lvalue of type {:?}", ty);
181180
}
182181
}
183182
Rvalue::Box(t) => {
184-
Some(tcx.mk_box(t))
183+
tcx.mk_box(t)
185184
}
186185
Rvalue::Aggregate(ref ak, ref ops) => {
187186
match *ak {
188-
AggregateKind::Array => {
189-
if let Some(operand) = ops.get(0) {
190-
let ty = operand.ty(mir, tcx);
191-
Some(tcx.mk_array(ty, ops.len()))
192-
} else {
193-
None
194-
}
187+
AggregateKind::Array(ty) => {
188+
tcx.mk_array(ty, ops.len())
195189
}
196190
AggregateKind::Tuple => {
197-
Some(tcx.mk_tup(
191+
tcx.mk_tup(
198192
ops.iter().map(|op| op.ty(mir, tcx)),
199193
false
200-
))
194+
)
201195
}
202196
AggregateKind::Adt(def, _, substs, _) => {
203-
Some(tcx.item_type(def.did).subst(tcx, substs))
197+
tcx.item_type(def.did).subst(tcx, substs)
204198
}
205199
AggregateKind::Closure(did, substs) => {
206-
Some(tcx.mk_closure_from_closure_substs(did, substs))
200+
tcx.mk_closure_from_closure_substs(did, substs)
207201
}
208202
}
209203
}

src/librustc/mir/visit.rs

+4-23
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,6 @@ macro_rules! make_mir_visitor {
235235
self.super_const_usize(const_usize);
236236
}
237237

238-
fn visit_typed_const_val(&mut self,
239-
val: & $($mutability)* TypedConstVal<'tcx>,
240-
location: Location) {
241-
self.super_typed_const_val(val, location);
242-
}
243-
244238
fn visit_local_decl(&mut self,
245239
local_decl: & $($mutability)* LocalDecl<'tcx>) {
246240
self.super_local_decl(local_decl);
@@ -467,9 +461,9 @@ macro_rules! make_mir_visitor {
467461
}
468462

469463
Rvalue::Repeat(ref $($mutability)* value,
470-
ref $($mutability)* typed_const_val) => {
464+
ref $($mutability)* length) => {
471465
self.visit_operand(value, location);
472-
self.visit_typed_const_val(typed_const_val, location);
466+
self.visit_const_usize(length, location);
473467
}
474468

475469
Rvalue::Ref(r, bk, ref $($mutability)* path) => {
@@ -515,7 +509,8 @@ macro_rules! make_mir_visitor {
515509
Rvalue::Aggregate(ref $($mutability)* kind,
516510
ref $($mutability)* operands) => {
517511
match *kind {
518-
AggregateKind::Array => {
512+
AggregateKind::Array(ref $($mutability)* ty) => {
513+
self.visit_ty(ty);
519514
}
520515
AggregateKind::Tuple => {
521516
}
@@ -647,20 +642,6 @@ macro_rules! make_mir_visitor {
647642
self.visit_literal(literal, location);
648643
}
649644

650-
fn super_typed_const_val(&mut self,
651-
constant: & $($mutability)* TypedConstVal<'tcx>,
652-
location: Location) {
653-
let TypedConstVal {
654-
ref $($mutability)* span,
655-
ref $($mutability)* ty,
656-
ref $($mutability)* value,
657-
} = *constant;
658-
659-
self.visit_span(span);
660-
self.visit_ty(ty);
661-
self.visit_const_usize(value, location);
662-
}
663-
664645
fn super_literal(&mut self,
665646
literal: & $($mutability)* Literal<'tcx>,
666647
location: Location) {

src/librustc_mir/build/expr/as_rvalue.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,13 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
148148
// to the same MIR as `let x = ();`.
149149

150150
// first process the set of fields
151+
let el_ty = expr.ty.sequence_element_type(this.hir.tcx());
151152
let fields: Vec<_> =
152153
fields.into_iter()
153154
.map(|f| unpack!(block = this.as_operand(block, f)))
154155
.collect();
155156

156-
block.and(Rvalue::Aggregate(AggregateKind::Array, fields))
157+
block.and(Rvalue::Aggregate(AggregateKind::Array(el_ty), fields))
157158
}
158159
ExprKind::Tuple { fields } => { // see (*) above
159160
// first process the set of fields

src/librustc_mir/hair/cx/expr.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -602,11 +602,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
602602

603603
ExprKind::Repeat {
604604
value: v.to_ref(),
605-
count: TypedConstVal {
606-
ty: cx.tcx.types.usize,
607-
span: c.span,
608-
value: count
609-
}
605+
count: count,
610606
}
611607
}
612608
hir::ExprRet(ref v) => ExprKind::Return { value: v.to_ref() },

src/librustc_mir/hair/mod.rs

+3-2
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::{BinOp, BorrowKind, Field, Literal, UnOp, TypedConstVal};
17+
use rustc_const_math::ConstUsize;
18+
use rustc::mir::{BinOp, BorrowKind, Field, Literal, UnOp};
1819
use rustc::hir::def_id::DefId;
1920
use rustc::middle::region::CodeExtent;
2021
use rustc::ty::subst::Substs;
@@ -219,7 +220,7 @@ pub enum ExprKind<'tcx> {
219220
},
220221
Repeat {
221222
value: ExprRef<'tcx>,
222-
count: TypedConstVal<'tcx>,
223+
count: ConstUsize,
223224
},
224225
Array {
225226
fields: Vec<ExprRef<'tcx>>,

src/librustc_mir/transform/qualify_consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
752752
}
753753

754754
if Some(def.did) == self.tcx.lang_items.unsafe_cell_type() {
755-
let ty = rvalue.ty(self.mir, self.tcx).unwrap();
755+
let ty = rvalue.ty(self.mir, self.tcx);
756756
self.add_type(ty);
757757
assert!(self.qualif.intersects(Qualif::MUTABLE_INTERIOR));
758758
// Even if the value inside may not need dropping,

src/librustc_mir/transform/type_check.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,8 @@ impl<'a, 'b, 'gcx, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'gcx, 'tcx> {
8383

8484
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
8585
self.super_rvalue(rvalue, location);
86-
if let Some(ty) = rvalue.ty(self.mir, self.tcx()) {
87-
self.sanitize_type(rvalue, ty);
88-
}
86+
let rval_ty = rvalue.ty(self.mir, self.tcx());
87+
self.sanitize_type(rvalue, rval_ty);
8988
}
9089

9190
fn visit_mir(&mut self, mir: &Mir<'tcx>) {
@@ -356,14 +355,10 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
356355
StatementKind::Assign(ref lv, ref rv) => {
357356
let lv_ty = lv.ty(mir, tcx).to_ty(tcx);
358357
let rv_ty = rv.ty(mir, tcx);
359-
if let Some(rv_ty) = rv_ty {
360-
if let Err(terr) = self.sub_types(rv_ty, lv_ty) {
361-
span_mirbug!(self, stmt, "bad assignment ({:?} = {:?}): {:?}",
362-
lv_ty, rv_ty, terr);
363-
}
358+
if let Err(terr) = self.sub_types(rv_ty, lv_ty) {
359+
span_mirbug!(self, stmt, "bad assignment ({:?} = {:?}): {:?}",
360+
lv_ty, rv_ty, terr);
364361
}
365-
// FIXME: rvalue with undeterminable type - e.g. AggregateKind::Array branch that
366-
// returns `None`.
367362
}
368363
StatementKind::SetDiscriminant{ ref lvalue, variant_index } => {
369364
let lvalue_type = lvalue.ty(mir, tcx).to_ty(tcx);

src/librustc_passes/mir_stats.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc::mir::{Constant, Literal, Location, LocalDecl};
1919
use rustc::mir::{Lvalue, LvalueElem, LvalueProjection};
2020
use rustc::mir::{Mir, Operand, ProjectionElem};
2121
use rustc::mir::{Rvalue, SourceInfo, Statement, StatementKind};
22-
use rustc::mir::{Terminator, TerminatorKind, TypedConstVal, VisibilityScope, VisibilityScopeData};
22+
use rustc::mir::{Terminator, TerminatorKind, VisibilityScope, VisibilityScopeData};
2323
use rustc::mir::visit as mir_visit;
2424
use rustc::mir::visit::Visitor;
2525
use rustc::ty::{ClosureSubsts, TyCtxt};
@@ -191,7 +191,7 @@ impl<'a, 'tcx> mir_visit::Visitor<'tcx> for StatCollector<'a, 'tcx> {
191191
// AggregateKind is not distinguished by visit API, so
192192
// record it. (`super_rvalue` handles `_operands`.)
193193
self.record(match *kind {
194-
AggregateKind::Array => "AggregateKind::Array",
194+
AggregateKind::Array(_) => "AggregateKind::Array",
195195
AggregateKind::Tuple => "AggregateKind::Tuple",
196196
AggregateKind::Adt(..) => "AggregateKind::Adt",
197197
AggregateKind::Closure(..) => "AggregateKind::Closure",
@@ -297,13 +297,6 @@ impl<'a, 'tcx> mir_visit::Visitor<'tcx> for StatCollector<'a, 'tcx> {
297297
self.super_const_usize(const_usize);
298298
}
299299

300-
fn visit_typed_const_val(&mut self,
301-
val: &TypedConstVal<'tcx>,
302-
location: Location) {
303-
self.record("TypedConstVal", val);
304-
self.super_typed_const_val(val, location);
305-
}
306-
307300
fn visit_local_decl(&mut self,
308301
local_decl: &LocalDecl<'tcx>) {
309302
self.record("LocalDecl", local_decl);

src/librustc_trans/mir/constant.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
529529

530530
mir::Rvalue::Repeat(ref elem, ref count) => {
531531
let elem = self.const_operand(elem, span)?;
532-
let size = count.value.as_u64(tcx.sess.target.uint_type);
532+
let size = count.as_u64(tcx.sess.target.uint_type);
533533
let fields = vec![elem.llval; size as usize];
534534
self.const_array(dest_ty, &fields)
535535
}
@@ -548,7 +548,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
548548
failure?;
549549

550550
match *kind {
551-
mir::AggregateKind::Array => {
551+
mir::AggregateKind::Array(_) => {
552552
self.const_array(dest_ty, &fields)
553553
}
554554
mir::AggregateKind::Adt(..) |

src/librustc_trans/mir/rvalue.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
9595

9696
mir::Rvalue::Repeat(ref elem, ref count) => {
9797
let tr_elem = self.trans_operand(&bcx, elem);
98-
let size = count.value.as_u64(bcx.tcx().sess.target.uint_type);
98+
let size = count.as_u64(bcx.tcx().sess.target.uint_type);
9999
let size = C_uint(bcx.ccx, size);
100100
let base = base::get_dataptr(&bcx, dest.llval);
101101
tvec::slice_for_each(&bcx, base, tr_elem.ty, size, |bcx, llslot| {
@@ -435,7 +435,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
435435
mir::Rvalue::Discriminant(ref lvalue) => {
436436
let discr_lvalue = self.trans_lvalue(&bcx, lvalue);
437437
let enum_ty = discr_lvalue.ty.to_ty(bcx.tcx());
438-
let discr_ty = rvalue.ty(&*self.mir, bcx.tcx()).unwrap();
438+
let discr_ty = rvalue.ty(&*self.mir, bcx.tcx());
439439
let discr_type = type_of::immediate_type_of(bcx.ccx, discr_ty);
440440
let discr = adt::trans_get_discr(&bcx, enum_ty, discr_lvalue.llval,
441441
discr_lvalue.alignment, Some(discr_type), true);

0 commit comments

Comments
 (0)