Skip to content

Commit

Permalink
validate: storage must be allocated on local use
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-schievink committed Oct 2, 2020
1 parent 4dedf5e commit c1a431e
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions compiler/rustc_mir/src/transform/validate.rs
@@ -1,7 +1,12 @@
//! Validates the MIR to ensure that invariants are upheld.

use crate::{
dataflow::impls::MaybeStorageLive, dataflow::Analysis, dataflow::ResultsCursor,
util::storage::AlwaysLiveLocals,
};

use super::{MirPass, MirSource};
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::{visit::PlaceContext, visit::Visitor, Local};
use rustc_middle::{
mir::{
AggregateKind, BasicBlock, Body, BorrowKind, Location, MirPhase, Operand, Rvalue,
Expand Down Expand Up @@ -33,9 +38,18 @@ pub struct Validator {

impl<'tcx> MirPass<'tcx> for Validator {
fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut Body<'tcx>) {
let param_env = tcx.param_env(source.def_id());
let def_id = source.def_id();
let param_env = tcx.param_env(def_id);
let mir_phase = self.mir_phase;
TypeChecker { when: &self.when, source, body, tcx, param_env, mir_phase }.visit_body(body);

let always_live_locals = AlwaysLiveLocals::new(body);
let storage_liveness = MaybeStorageLive::new(always_live_locals)
.into_engine(tcx, body, def_id)
.iterate_to_fixpoint()
.into_results_cursor(body);

TypeChecker { when: &self.when, source, body, tcx, param_env, mir_phase, storage_liveness }
.visit_body(body);
}
}

Expand Down Expand Up @@ -138,6 +152,7 @@ struct TypeChecker<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
mir_phase: MirPhase,
storage_liveness: ResultsCursor<'a, 'tcx, MaybeStorageLive>,
}

impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
Expand Down Expand Up @@ -210,6 +225,17 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
}

impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
fn visit_local(&mut self, local: &Local, context: PlaceContext, location: Location) {
if context.is_use() {
// Uses of locals must occur while the local's storage is allocated.
self.storage_liveness.seek_after_primary_effect(location);
let locals_with_storage = self.storage_liveness.get();
if !locals_with_storage.contains(*local) {
self.fail(location, format!("use of local {:?}, which has no storage here", local));
}
}
}

fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
// `Operand::Copy` is only supposed to be used with `Copy` types.
if let Operand::Copy(place) = operand {
Expand Down

0 comments on commit c1a431e

Please sign in to comment.