Skip to content

Commit 2f42cd8

Browse files
committed
rustc_mir: use Local in ProjectionElem::Index.
1 parent c911925 commit 2f42cd8

File tree

14 files changed

+65
-74
lines changed

14 files changed

+65
-74
lines changed

src/librustc/mir/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1077,12 +1077,12 @@ pub enum ProjectionElem<'tcx, V, T> {
10771077
}
10781078

10791079
/// Alias for projections as they appear in lvalues, where the base is an lvalue
1080-
/// and the index is an operand.
1081-
pub type LvalueProjection<'tcx> = Projection<'tcx, Lvalue<'tcx>, Operand<'tcx>, Ty<'tcx>>;
1080+
/// and the index is a local.
1081+
pub type LvalueProjection<'tcx> = Projection<'tcx, Lvalue<'tcx>, Local, Ty<'tcx>>;
10821082

10831083
/// Alias for projections as they appear in lvalues, where the base is an lvalue
1084-
/// and the index is an operand.
1085-
pub type LvalueElem<'tcx> = ProjectionElem<'tcx, Operand<'tcx>, Ty<'tcx>>;
1084+
/// and the index is a local.
1085+
pub type LvalueElem<'tcx> = ProjectionElem<'tcx, Local, Ty<'tcx>>;
10861086

10871087
newtype_index!(Field, "field");
10881088

@@ -1099,7 +1099,7 @@ impl<'tcx> Lvalue<'tcx> {
10991099
self.elem(ProjectionElem::Downcast(adt_def, variant_index))
11001100
}
11011101

1102-
pub fn index(self, index: Operand<'tcx>) -> Lvalue<'tcx> {
1102+
pub fn index(self, index: Local) -> Lvalue<'tcx> {
11031103
self.elem(ProjectionElem::Index(index))
11041104
}
11051105

src/librustc/mir/visit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -664,8 +664,8 @@ macro_rules! make_mir_visitor {
664664
ProjectionElem::Field(_field, ref $($mutability)* ty) => {
665665
self.visit_ty(ty, Lookup::Loc(location));
666666
}
667-
ProjectionElem::Index(ref $($mutability)* operand) => {
668-
self.visit_operand(operand, location);
667+
ProjectionElem::Index(ref $($mutability)* local) => {
668+
self.visit_local(local, LvalueContext::Consume, location);
669669
}
670670
ProjectionElem::ConstantIndex { offset: _,
671671
min_length: _,

src/librustc/ty/structural_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ macro_rules! CopyImpls {
421421
}
422422
}
423423

424-
CopyImpls! { (), hir::Unsafety, abi::Abi, hir::def_id::DefId }
424+
CopyImpls! { (), hir::Unsafety, abi::Abi, hir::def_id::DefId, ::mir::Local }
425425

426426
impl<'tcx, T:TypeFoldable<'tcx>, U:TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
427427
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> (T, U) {

src/librustc_mir/borrow_check.rs

+4-21
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ mod restrictions {
710710

711711
use rustc::hir;
712712
use rustc::ty::{self, TyCtxt};
713-
use rustc::mir::{Lvalue, Mir, Operand, ProjectionElem};
713+
use rustc::mir::{Lvalue, Mir, ProjectionElem};
714714

715715
pub(super) struct Restrictions<'c, 'tcx: 'c> {
716716
mir: &'c Mir<'tcx>,
@@ -809,12 +809,7 @@ mod restrictions {
809809
ProjectionElem::Downcast(..) |
810810
ProjectionElem::Subslice { .. } |
811811
ProjectionElem::ConstantIndex { .. } |
812-
ProjectionElem::Index(Operand::Constant(..)) => {
813-
cursor = &proj.base;
814-
continue 'cursor;
815-
}
816-
ProjectionElem::Index(Operand::Consume(ref index)) => {
817-
self.lvalue_stack.push(index); // FIXME: did old borrowck do this?
812+
ProjectionElem::Index(_) => {
818813
cursor = &proj.base;
819814
continue 'cursor;
820815
}
@@ -1004,7 +999,7 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
1004999
("", format!(""), None), // (dont emit downcast info)
10051000
ProjectionElem::Field(field, _ty) =>
10061001
("", format!(".{}", field.index()), None),
1007-
ProjectionElem::Index(ref index) =>
1002+
ProjectionElem::Index(index) =>
10081003
("", format!(""), Some(index)),
10091004
ProjectionElem::ConstantIndex { offset, min_length, from_end: true } =>
10101005
("", format!("[{} of {}]", offset, min_length), None),
@@ -1021,23 +1016,11 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
10211016
self.append_lvalue_to_string(&proj.base, buf);
10221017
if let Some(index) = index_operand {
10231018
buf.push_str("[");
1024-
self.append_operand_to_string(index, buf);
1019+
self.append_lvalue_to_string(&Lvalue::Local(index), buf);
10251020
buf.push_str("]");
10261021
} else {
10271022
buf.push_str(&suffix);
10281023
}
1029-
1030-
}
1031-
}
1032-
}
1033-
1034-
fn append_operand_to_string(&self, operand: &Operand, buf: &mut String) {
1035-
match *operand {
1036-
Operand::Consume(ref lvalue) => {
1037-
self.append_lvalue_to_string(lvalue, buf);
1038-
}
1039-
Operand::Constant(ref constant) => {
1040-
buf.push_str(&format!("{:?}", constant));
10411024
}
10421025
}
10431026
}

src/librustc_mir/build/expr/as_lvalue.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
6161
// region_scope=None so lvalue indexes live forever. They are scalars so they
6262
// do not need storage annotations, and they are often copied between
6363
// places.
64-
let idx = unpack!(block = this.as_operand(block, None, index));
64+
let idx = unpack!(block = this.as_temp(block, None, index));
6565

6666
// bounds check:
6767
let (len, lt) = (this.temp(usize_ty.clone(), expr_span),
@@ -70,12 +70,12 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
7070
&len, Rvalue::Len(slice.clone()));
7171
this.cfg.push_assign(block, source_info, // lt = idx < len
7272
&lt, Rvalue::BinaryOp(BinOp::Lt,
73-
idx.clone(),
73+
Operand::Consume(Lvalue::Local(idx)),
7474
Operand::Consume(len.clone())));
7575

7676
let msg = AssertMessage::BoundsCheck {
7777
len: Operand::Consume(len),
78-
index: idx.clone()
78+
index: Operand::Consume(Lvalue::Local(idx))
7979
};
8080
let success = this.assert(block, Operand::Consume(lt), true,
8181
msg, expr_span);
@@ -127,7 +127,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
127127
Some(Category::Lvalue) => false,
128128
_ => true,
129129
});
130-
this.as_temp(block, expr.temp_lifetime, expr)
130+
let temp = unpack!(block = this.as_temp(block, expr.temp_lifetime, expr));
131+
block.and(Lvalue::Local(temp))
131132
}
132133
}
133134
}

src/librustc_mir/build/expr/as_operand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
7474
Category::Rvalue(..) => {
7575
let operand =
7676
unpack!(block = this.as_temp(block, scope, expr));
77-
block.and(Operand::Consume(operand))
77+
block.and(Operand::Consume(Lvalue::Local(operand)))
7878
}
7979
}
8080
}

src/librustc_mir/build/expr/as_temp.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
2323
block: BasicBlock,
2424
temp_lifetime: Option<region::Scope>,
2525
expr: M)
26-
-> BlockAnd<Lvalue<'tcx>>
26+
-> BlockAnd<Local>
2727
where M: Mirror<'tcx, Output = Expr<'tcx>>
2828
{
2929
let expr = self.hir.mirror(expr);
@@ -34,7 +34,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
3434
mut block: BasicBlock,
3535
temp_lifetime: Option<region::Scope>,
3636
expr: Expr<'tcx>)
37-
-> BlockAnd<Lvalue<'tcx>> {
37+
-> BlockAnd<Local> {
3838
debug!("expr_as_temp(block={:?}, temp_lifetime={:?}, expr={:?})",
3939
block, temp_lifetime, expr);
4040
let this = self;
@@ -47,13 +47,13 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
4747
});
4848
}
4949

50-
let expr_ty = expr.ty.clone();
51-
let temp = this.temp(expr_ty.clone(), expr_span);
50+
let expr_ty = expr.ty;
51+
let temp = this.local_decls.push(LocalDecl::new_temp(expr_ty, expr_span));
5252

5353
if !expr_ty.is_never() {
5454
this.cfg.push(block, Statement {
5555
source_info,
56-
kind: StatementKind::StorageLive(temp.clone())
56+
kind: StatementKind::StorageLive(Lvalue::Local(temp))
5757
});
5858
}
5959

@@ -68,18 +68,18 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
6868
Category::Lvalue => {
6969
let lvalue = unpack!(block = this.as_lvalue(block, expr));
7070
let rvalue = Rvalue::Use(Operand::Consume(lvalue));
71-
this.cfg.push_assign(block, source_info, &temp, rvalue);
71+
this.cfg.push_assign(block, source_info, &Lvalue::Local(temp), rvalue);
7272
}
7373
_ => {
74-
unpack!(block = this.into(&temp, block, expr));
74+
unpack!(block = this.into(&Lvalue::Local(temp), block, expr));
7575
}
7676
}
7777

7878
// In constants, temp_lifetime is None. We should not need to drop
7979
// anything because no values with a destructor can be created in
8080
// a constant at this time, even if the type may need dropping.
8181
if let Some(temp_lifetime) = temp_lifetime {
82-
this.schedule_drop(expr_span, temp_lifetime, &temp, expr_ty);
82+
this.schedule_drop(expr_span, temp_lifetime, &Lvalue::Local(temp), expr_ty);
8383
}
8484

8585
block.and(temp)

src/librustc_mir/build/expr/into.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
229229

230230
let topmost_scope = this.topmost_scope();
231231
let ptr = unpack!(block = this.as_temp(block, Some(topmost_scope), ptr));
232-
this.into(&ptr.deref(), block, val)
232+
this.into(&Lvalue::Local(ptr).deref(), block, val)
233233
} else {
234234
let args: Vec<_> =
235235
args.into_iter()

src/librustc_mir/dataflow/move_paths/abs_domain.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
//! `a[x]` would still overlap them both. But that is not this
2222
//! representation does today.)
2323
24-
use rustc::mir::LvalueElem;
25-
use rustc::mir::{Operand, ProjectionElem};
24+
use rustc::mir::{Local, LvalueElem, Operand, ProjectionElem};
2625
use rustc::ty::Ty;
2726

2827
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
@@ -40,6 +39,10 @@ impl<'tcx> Lift for Operand<'tcx> {
4039
type Abstract = AbstractOperand;
4140
fn lift(&self) -> Self::Abstract { AbstractOperand }
4241
}
42+
impl Lift for Local {
43+
type Abstract = AbstractOperand;
44+
fn lift(&self) -> Self::Abstract { AbstractOperand }
45+
}
4346
impl<'tcx> Lift for Ty<'tcx> {
4447
type Abstract = AbstractType;
4548
fn lift(&self) -> Self::Abstract { AbstractType }

src/librustc_mir/shim.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,10 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
479479

480480
fn array_shim(&mut self, ty: ty::Ty<'tcx>, len: usize) {
481481
let tcx = self.tcx;
482+
let span = self.span;
482483
let rcvr = Lvalue::Local(Local::new(1+0)).deref();
483484

484-
let beg = self.make_lvalue(Mutability::Mut, tcx.types.usize);
485+
let beg = self.local_decls.push(temp_decl(Mutability::Mut, tcx.types.usize, span));
485486
let end = self.make_lvalue(Mutability::Not, tcx.types.usize);
486487
let ret = self.make_lvalue(Mutability::Mut, tcx.mk_array(ty, len));
487488

@@ -492,7 +493,7 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
492493
let inits = vec![
493494
self.make_statement(
494495
StatementKind::Assign(
495-
beg.clone(),
496+
Lvalue::Local(beg),
496497
Rvalue::Use(Operand::Constant(self.make_usize(0)))
497498
)
498499
),
@@ -510,19 +511,19 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
510511
// BB #3;
511512
// }
512513
// BB #4;
513-
self.loop_header(beg.clone(), end, BasicBlock::new(2), BasicBlock::new(4), false);
514+
self.loop_header(Lvalue::Local(beg), end, BasicBlock::new(2), BasicBlock::new(4), false);
514515

515516
// BB #2
516517
// `let cloned = Clone::clone(rcvr[beg])`;
517518
// Goto #3 if ok, #5 if unwinding happens.
518-
let rcvr_field = rcvr.clone().index(Operand::Consume(beg.clone()));
519+
let rcvr_field = rcvr.clone().index(beg);
519520
let cloned = self.make_clone_call(ty, rcvr_field, BasicBlock::new(3), BasicBlock::new(5));
520521

521522
// BB #3
522523
// `ret[beg] = cloned;`
523524
// `beg = beg + 1;`
524525
// `goto #1`;
525-
let ret_field = ret.clone().index(Operand::Consume(beg.clone()));
526+
let ret_field = ret.clone().index(beg);
526527
let statements = vec![
527528
self.make_statement(
528529
StatementKind::Assign(
@@ -532,10 +533,10 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
532533
),
533534
self.make_statement(
534535
StatementKind::Assign(
535-
beg.clone(),
536+
Lvalue::Local(beg),
536537
Rvalue::BinaryOp(
537538
BinOp::Add,
538-
Operand::Consume(beg.clone()),
539+
Operand::Consume(Lvalue::Local(beg)),
539540
Operand::Constant(self.make_usize(1))
540541
)
541542
)
@@ -558,10 +559,10 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
558559
// `let mut beg = 0;`
559560
// goto #6;
560561
let end = beg;
561-
let beg = self.make_lvalue(Mutability::Mut, tcx.types.usize);
562+
let beg = self.local_decls.push(temp_decl(Mutability::Mut, tcx.types.usize, span));
562563
let init = self.make_statement(
563564
StatementKind::Assign(
564-
beg.clone(),
565+
Lvalue::Local(beg),
565566
Rvalue::Use(Operand::Constant(self.make_usize(0)))
566567
)
567568
);
@@ -572,12 +573,13 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
572573
// BB #8;
573574
// }
574575
// BB #9;
575-
self.loop_header(beg.clone(), end, BasicBlock::new(7), BasicBlock::new(9), true);
576+
self.loop_header(Lvalue::Local(beg), Lvalue::Local(end),
577+
BasicBlock::new(7), BasicBlock::new(9), true);
576578

577579
// BB #7 (cleanup)
578580
// `drop(ret[beg])`;
579581
self.block(vec![], TerminatorKind::Drop {
580-
location: ret.index(Operand::Consume(beg.clone())),
582+
location: ret.index(beg),
581583
target: BasicBlock::new(8),
582584
unwind: None,
583585
}, true);
@@ -587,10 +589,10 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
587589
// `goto #6;`
588590
let statement = self.make_statement(
589591
StatementKind::Assign(
590-
beg.clone(),
592+
Lvalue::Local(beg),
591593
Rvalue::BinaryOp(
592594
BinOp::Add,
593-
Operand::Consume(beg.clone()),
595+
Operand::Consume(Lvalue::Local(beg)),
594596
Operand::Constant(self.make_usize(1))
595597
)
596598
)

src/librustc_mir/transform/type_check.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
165165
base: LvalueTy<'tcx>,
166166
pi: &LvalueElem<'tcx>,
167167
lvalue: &Lvalue<'tcx>,
168-
location: Location)
168+
_: Location)
169169
-> LvalueTy<'tcx> {
170170
debug!("sanitize_projection: {:?} {:?} {:?}", base, pi, lvalue);
171171
let tcx = self.tcx();
@@ -181,9 +181,8 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
181181
})
182182
}
183183
}
184-
ProjectionElem::Index(ref i) => {
185-
self.visit_operand(i, location);
186-
let index_ty = i.ty(self.mir, tcx);
184+
ProjectionElem::Index(i) => {
185+
let index_ty = Lvalue::Local(i).ty(self.mir, tcx).to_ty(tcx);
187186
if index_ty != tcx.types.usize {
188187
LvalueTy::Ty {
189188
ty: span_mirbug_and_err!(self, i, "index by non-usize {:?}", i)

0 commit comments

Comments
 (0)