Skip to content

Commit 3a14e9e

Browse files
committed
Make Rvalue::ty infallible
1 parent 5872a8d commit 3a14e9e

File tree

9 files changed

+34
-42
lines changed

9 files changed

+34
-42
lines changed

src/librustc/mir/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -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() {

src/librustc/mir/tcx.rs

+18-24
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);
143143
let count = count.value.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

+2-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,8 @@ macro_rules! make_mir_visitor {
515515
Rvalue::Aggregate(ref $($mutability)* kind,
516516
ref $($mutability)* operands) => {
517517
match *kind {
518-
AggregateKind::Array => {
518+
AggregateKind::Array(ref $($mutability)* ty) => {
519+
self.visit_ty(ty);
519520
}
520521
AggregateKind::Tuple => {
521522
}

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/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

+1-1
Original file line numberDiff line numberDiff line change
@@ -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",

src/librustc_trans/mir/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -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

+1-1
Original file line numberDiff line numberDiff line change
@@ -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)