Skip to content

Commit 1dd9801

Browse files
authored
Rollup merge of rust-lang#41232 - arielb1:mir-rvalues, r=eddyb
move rvalue checking to MIR
2 parents 092f19a + 540a069 commit 1dd9801

File tree

28 files changed

+197
-224
lines changed

28 files changed

+197
-224
lines changed

src/librustc/ich/impls_mir.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ impl_stable_hash_for!(struct mir::SourceInfo { span, scope });
2222
impl_stable_hash_for!(enum mir::Mutability { Mut, Not });
2323
impl_stable_hash_for!(enum mir::BorrowKind { Shared, Unique, Mut });
2424
impl_stable_hash_for!(enum mir::LocalKind { Var, Temp, Arg, ReturnPointer });
25-
impl_stable_hash_for!(struct mir::LocalDecl<'tcx> { mutability, ty, name, source_info });
25+
impl_stable_hash_for!(struct mir::LocalDecl<'tcx> { mutability, ty, name, source_info,
26+
is_user_variable});
2627
impl_stable_hash_for!(struct mir::UpvarDecl { debug_name, by_ref });
2728
impl_stable_hash_for!(struct mir::BasicBlockData<'tcx> { statements, terminator, is_cleanup });
2829
impl_stable_hash_for!(struct mir::Terminator<'tcx> { source_info, kind });

src/librustc/mir/mod.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ impl<'tcx> Mir<'tcx> {
197197
pub fn temps_iter<'a>(&'a self) -> impl Iterator<Item=Local> + 'a {
198198
(self.arg_count+1..self.local_decls.len()).filter_map(move |index| {
199199
let local = Local::new(index);
200-
if self.local_decls[local].source_info.is_none() {
201-
Some(local)
202-
} else {
200+
if self.local_decls[local].is_user_variable {
203201
None
202+
} else {
203+
Some(local)
204204
}
205205
})
206206
}
@@ -210,10 +210,10 @@ impl<'tcx> Mir<'tcx> {
210210
pub fn vars_iter<'a>(&'a self) -> impl Iterator<Item=Local> + 'a {
211211
(self.arg_count+1..self.local_decls.len()).filter_map(move |index| {
212212
let local = Local::new(index);
213-
if self.local_decls[local].source_info.is_none() {
214-
None
215-
} else {
213+
if self.local_decls[local].is_user_variable {
216214
Some(local)
215+
} else {
216+
None
217217
}
218218
})
219219
}
@@ -370,6 +370,9 @@ pub struct LocalDecl<'tcx> {
370370
/// Temporaries and the return pointer are always mutable.
371371
pub mutability: Mutability,
372372

373+
/// True if this corresponds to a user-declared local variable.
374+
pub is_user_variable: bool,
375+
373376
/// Type of this local.
374377
pub ty: Ty<'tcx>,
375378

@@ -379,37 +382,40 @@ pub struct LocalDecl<'tcx> {
379382
/// to generate better debuginfo.
380383
pub name: Option<Name>,
381384

382-
/// For user-declared variables, stores their source information.
383-
///
384-
/// For temporaries, this is `None`.
385-
///
386-
/// This is the primary way to differentiate between user-declared
387-
/// variables and compiler-generated temporaries.
388-
pub source_info: Option<SourceInfo>,
385+
/// Source info of the local.
386+
pub source_info: SourceInfo,
389387
}
390388

391389
impl<'tcx> LocalDecl<'tcx> {
392390
/// Create a new `LocalDecl` for a temporary.
393391
#[inline]
394-
pub fn new_temp(ty: Ty<'tcx>) -> Self {
392+
pub fn new_temp(ty: Ty<'tcx>, span: Span) -> Self {
395393
LocalDecl {
396394
mutability: Mutability::Mut,
397395
ty: ty,
398396
name: None,
399-
source_info: None,
397+
source_info: SourceInfo {
398+
span: span,
399+
scope: ARGUMENT_VISIBILITY_SCOPE
400+
},
401+
is_user_variable: false
400402
}
401403
}
402404

403405
/// Builds a `LocalDecl` for the return pointer.
404406
///
405407
/// This must be inserted into the `local_decls` list as the first local.
406408
#[inline]
407-
pub fn new_return_pointer(return_ty: Ty) -> LocalDecl {
409+
pub fn new_return_pointer(return_ty: Ty, span: Span) -> LocalDecl {
408410
LocalDecl {
409411
mutability: Mutability::Mut,
410412
ty: return_ty,
411-
source_info: None,
413+
source_info: SourceInfo {
414+
span: span,
415+
scope: ARGUMENT_VISIBILITY_SCOPE
416+
},
412417
name: None, // FIXME maybe we do want some name here?
418+
is_user_variable: false
413419
}
414420
}
415421
}

src/librustc/mir/visit.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -630,12 +630,11 @@ macro_rules! make_mir_visitor {
630630
ref $($mutability)* ty,
631631
name: _,
632632
ref $($mutability)* source_info,
633+
is_user_variable: _,
633634
} = *local_decl;
634635

635636
self.visit_ty(ty);
636-
if let Some(ref $($mutability)* info) = *source_info {
637-
self.visit_source_info(info);
638-
}
637+
self.visit_source_info(source_info);
639638
}
640639

641640
fn super_visibility_scope(&mut self,

src/librustc_borrowck/borrowck/mir/elaborate_drops.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,12 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
307307
data
308308
}
309309

310-
fn create_drop_flag(&mut self, index: MovePathIndex) {
310+
fn create_drop_flag(&mut self, index: MovePathIndex, span: Span) {
311311
let tcx = self.tcx;
312312
let patch = &mut self.patch;
313313
debug!("create_drop_flag({:?})", self.mir.span);
314314
self.drop_flags.entry(index).or_insert_with(|| {
315-
patch.new_temp(tcx.types.bool)
315+
patch.new_temp(tcx.types.bool, span)
316316
});
317317
}
318318

@@ -374,7 +374,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
374374
debug!("collect_drop_flags: collecting {:?} from {:?}@{:?} - {:?}",
375375
child, location, path, (maybe_live, maybe_dead));
376376
if maybe_live && maybe_dead {
377-
self.create_drop_flag(child)
377+
self.create_drop_flag(child, terminator.source_info.span)
378378
}
379379
});
380380
}

src/librustc_driver/driver.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use rustc_typeck as typeck;
3535
use rustc_privacy;
3636
use rustc_plugin::registry::Registry;
3737
use rustc_plugin as plugin;
38-
use rustc_passes::{ast_validation, no_asm, loops, consts, rvalues,
38+
use rustc_passes::{ast_validation, no_asm, loops, consts,
3939
static_recursion, hir_stats, mir_stats};
4040
use rustc_const_eval::check_match;
4141
use super::Compilation;
@@ -958,10 +958,6 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
958958
"liveness checking",
959959
|| middle::liveness::check_crate(tcx));
960960

961-
time(time_passes,
962-
"rvalue checking",
963-
|| rvalues::check_crate(tcx));
964-
965961
time(time_passes,
966962
"MIR dump",
967963
|| mir::mir_map::build_mir_for_crate(tcx));
@@ -977,8 +973,8 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
977973
// in stage 4 below.
978974
passes.push_hook(box mir::transform::dump_mir::DumpMir);
979975
passes.push_pass(box mir::transform::simplify::SimplifyCfg::new("initial"));
980-
passes.push_pass(box mir::transform::qualify_consts::QualifyAndPromoteConstants);
981976
passes.push_pass(box mir::transform::type_check::TypeckMir);
977+
passes.push_pass(box mir::transform::qualify_consts::QualifyAndPromoteConstants);
982978
passes.push_pass(
983979
box mir::transform::simplify_branches::SimplifyBranches::new("initial"));
984980
passes.push_pass(box mir::transform::simplify::SimplifyCfg::new("qualify-consts"));

src/librustc_mir/build/expr/as_lvalue.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
6262
let idx = unpack!(block = this.as_operand(block, None, index));
6363

6464
// bounds check:
65-
let (len, lt) = (this.temp(usize_ty.clone()), this.temp(bool_ty));
65+
let (len, lt) = (this.temp(usize_ty.clone(), expr_span),
66+
this.temp(bool_ty, expr_span));
6667
this.cfg.push_assign(block, source_info, // len = len(slice)
6768
&len, Rvalue::Len(slice.clone()));
6869
this.cfg.push_assign(block, source_info, // lt = idx < len

src/librustc_mir/build/expr/as_rvalue.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
8282
let bool_ty = this.hir.bool_ty();
8383

8484
let minval = this.minval_literal(expr_span, expr.ty);
85-
let is_min = this.temp(bool_ty);
85+
let is_min = this.temp(bool_ty, expr_span);
8686

8787
this.cfg.push_assign(block, source_info, &is_min,
8888
Rvalue::BinaryOp(BinOp::Eq, arg.clone(), minval));
@@ -95,7 +95,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
9595
}
9696
ExprKind::Box { value, value_extents } => {
9797
let value = this.hir.mirror(value);
98-
let result = this.temp(expr.ty);
98+
let result = this.temp(expr.ty, expr_span);
9999
// to start, malloc some memory of suitable type (thus far, uninitialized):
100100
this.cfg.push_assign(block, source_info, &result, Rvalue::Box(value.ty));
101101
this.in_scope(value_extents, block, |this| {
@@ -260,7 +260,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
260260
let bool_ty = self.hir.bool_ty();
261261
if self.hir.check_overflow() && op.is_checkable() && ty.is_integral() {
262262
let result_tup = self.hir.tcx().intern_tup(&[ty, bool_ty], false);
263-
let result_value = self.temp(result_tup);
263+
let result_value = self.temp(result_tup, span);
264264

265265
self.cfg.push_assign(block, source_info,
266266
&result_value, Rvalue::CheckedBinaryOp(op,
@@ -301,7 +301,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
301301
};
302302

303303
// Check for / 0
304-
let is_zero = self.temp(bool_ty);
304+
let is_zero = self.temp(bool_ty, span);
305305
let zero = self.zero_literal(span, ty);
306306
self.cfg.push_assign(block, source_info, &is_zero,
307307
Rvalue::BinaryOp(BinOp::Eq, rhs.clone(), zero));
@@ -315,9 +315,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
315315
let neg_1 = self.neg_1_literal(span, ty);
316316
let min = self.minval_literal(span, ty);
317317

318-
let is_neg_1 = self.temp(bool_ty);
319-
let is_min = self.temp(bool_ty);
320-
let of = self.temp(bool_ty);
318+
let is_neg_1 = self.temp(bool_ty, span);
319+
let is_min = self.temp(bool_ty, span);
320+
let of = self.temp(bool_ty, span);
321321

322322
// this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
323323

src/librustc_mir/build/expr/as_temp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
4444
}
4545

4646
let expr_ty = expr.ty.clone();
47-
let temp = this.temp(expr_ty.clone());
4847
let expr_span = expr.span;
48+
let temp = this.temp(expr_ty.clone(), expr_span);
4949
let source_info = this.source_info(expr_span);
5050

5151
if expr.temp_lifetime_was_shrunk && this.hir.needs_drop(expr_ty) {

src/librustc_mir/build/expr/stmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
138138
}
139139
_ => {
140140
let expr_ty = expr.ty;
141-
let temp = this.temp(expr.ty.clone());
141+
let temp = this.temp(expr.ty.clone(), expr_span);
142142
unpack!(block = this.into(&temp, block, expr));
143143
unpack!(block = this.build_drop(block, expr_span, temp, expr_ty));
144144
block.unit()

src/librustc_mir/build/matches/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
710710
mutability: mutability,
711711
ty: var_ty.clone(),
712712
name: Some(name),
713-
source_info: Some(source_info),
713+
source_info: source_info,
714+
is_user_variable: true,
714715
});
715716
self.var_indices.insert(var_id, var);
716717

src/librustc_mir/build/matches/test.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
210210
debug!("num_enum_variants: {}, tested variants: {:?}, variants: {:?}",
211211
num_enum_variants, values, variants);
212212
let discr_ty = adt_def.repr.discr_type().to_ty(tcx);
213-
let discr = self.temp(discr_ty);
213+
let discr = self.temp(discr_ty, test.span);
214214
self.cfg.push_assign(block, source_info, &discr,
215215
Rvalue::Discriminant(lvalue.clone()));
216216
assert_eq!(values.len() + 1, targets.len());
@@ -270,7 +270,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
270270
if let ty::TyRef(region, mt) = ty.sty {
271271
if let ty::TyArray(_, _) = mt.ty.sty {
272272
ty = tcx.mk_imm_ref(region, tcx.mk_slice(tcx.types.u8));
273-
let val_slice = self.temp(ty);
273+
let val_slice = self.temp(ty, test.span);
274274
self.cfg.push_assign(block, source_info, &val_slice,
275275
Rvalue::Cast(CastKind::Unsize, val, ty));
276276
val = Operand::Consume(val_slice);
@@ -285,7 +285,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
285285
value: value.clone()
286286
});
287287

288-
let slice = self.temp(ty);
288+
let slice = self.temp(ty, test.span);
289289
self.cfg.push_assign(block, source_info, &slice,
290290
Rvalue::Cast(CastKind::Unsize, array, ty));
291291
Operand::Consume(slice)
@@ -304,7 +304,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
304304
let (mty, method) = self.hir.trait_method(eq_def_id, "eq", ty, &[ty]);
305305

306306
let bool_ty = self.hir.bool_ty();
307-
let eq_result = self.temp(bool_ty);
307+
let eq_result = self.temp(bool_ty, test.span);
308308
let eq_block = self.cfg.start_new_block();
309309
let cleanup = self.diverge_cleanup();
310310
self.cfg.terminate(block, source_info, TerminatorKind::Call {
@@ -349,7 +349,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
349349

350350
TestKind::Len { len, op } => {
351351
let (usize_ty, bool_ty) = (self.hir.usize_ty(), self.hir.bool_ty());
352-
let (actual, result) = (self.temp(usize_ty), self.temp(bool_ty));
352+
let (actual, result) = (self.temp(usize_ty, test.span),
353+
self.temp(bool_ty, test.span));
353354

354355
// actual = len(lvalue)
355356
self.cfg.push_assign(block, source_info,
@@ -383,7 +384,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
383384
left: Operand<'tcx>,
384385
right: Operand<'tcx>) -> BasicBlock {
385386
let bool_ty = self.hir.bool_ty();
386-
let result = self.temp(bool_ty);
387+
let result = self.temp(bool_ty, span);
387388

388389
// result = op(left, right)
389390
let source_info = self.source_info(span);

src/librustc_mir/build/misc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
2727
///
2828
/// NB: **No cleanup is scheduled for this temporary.** You should
2929
/// call `schedule_drop` once the temporary is initialized.
30-
pub fn temp(&mut self, ty: Ty<'tcx>) -> Lvalue<'tcx> {
31-
let temp = self.local_decls.push(LocalDecl::new_temp(ty));
30+
pub fn temp(&mut self, ty: Ty<'tcx>, span: Span) -> Lvalue<'tcx> {
31+
let temp = self.local_decls.push(LocalDecl::new_temp(ty, span));
3232
let lvalue = Lvalue::Local(temp);
3333
debug!("temp: created temp {:?} with type {:?}",
3434
lvalue, self.local_decls[temp].ty);
@@ -106,7 +106,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
106106
value: u64)
107107
-> Lvalue<'tcx> {
108108
let usize_ty = self.hir.usize_ty();
109-
let temp = self.temp(usize_ty);
109+
let temp = self.temp(usize_ty, source_info.span);
110110
self.cfg.push_assign_constant(
111111
block, source_info, &temp,
112112
Constant {

src/librustc_mir/build/mod.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
249249
visibility_scopes: IndexVec::new(),
250250
visibility_scope: ARGUMENT_VISIBILITY_SCOPE,
251251
breakable_scopes: vec![],
252-
local_decls: IndexVec::from_elem_n(LocalDecl::new_return_pointer(return_ty), 1),
252+
local_decls: IndexVec::from_elem_n(LocalDecl::new_return_pointer(return_ty,
253+
span), 1),
253254
var_indices: NodeMap(),
254255
unit_temp: None,
255256
cached_resume_block: None,
@@ -304,8 +305,12 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
304305
self.local_decls.push(LocalDecl {
305306
mutability: Mutability::Not,
306307
ty: ty,
307-
source_info: None,
308+
source_info: SourceInfo {
309+
scope: ARGUMENT_VISIBILITY_SCOPE,
310+
span: pattern.map_or(self.fn_span, |pat| pat.span)
311+
},
308312
name: name,
313+
is_user_variable: false,
309314
});
310315
}
311316

@@ -341,7 +346,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
341346
Some(ref tmp) => tmp.clone(),
342347
None => {
343348
let ty = self.hir.unit_ty();
344-
let tmp = self.temp(ty);
349+
let fn_span = self.fn_span;
350+
let tmp = self.temp(ty, fn_span);
345351
self.unit_temp = Some(tmp.clone());
346352
tmp
347353
}

0 commit comments

Comments
 (0)