Skip to content

Commit

Permalink
Stricter check for a use of locals without storage
Browse files Browse the repository at this point in the history
  • Loading branch information
tmiasko committed Dec 18, 2023
1 parent 3ce121c commit 4921e5a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
28 changes: 18 additions & 10 deletions compiler/rustc_mir_transform/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ use rustc_index::bit_set::BitSet;
use rustc_middle::mir::visit::{PlaceContext, Visitor};
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use rustc_mir_dataflow::impls::MaybeStorageLive;
use rustc_mir_dataflow::impls::{MaybeStorageDead, MaybeStorageLive};
use rustc_mir_dataflow::storage::always_storage_live_locals;
use rustc_mir_dataflow::{Analysis, ResultsCursor};
use std::borrow::Cow;

pub fn lint_body<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, when: String) {
let reachable_blocks = traversal::reachable_as_bitset(body);
let always_live_locals = &always_storage_live_locals(body);
let storage_liveness = MaybeStorageLive::new(Cow::Borrowed(always_live_locals))

let maybe_storage_live = MaybeStorageLive::new(Cow::Borrowed(always_live_locals))
.into_engine(tcx, body)
.iterate_to_fixpoint()
.into_results_cursor(body);

let maybe_storage_dead = MaybeStorageDead::new(Cow::Borrowed(always_live_locals))
.into_engine(tcx, body)
.iterate_to_fixpoint()
.into_results_cursor(body);
Expand All @@ -25,7 +31,8 @@ pub fn lint_body<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, when: String) {
is_fn_like: tcx.def_kind(body.source.def_id()).is_fn_like(),
always_live_locals,
reachable_blocks,
storage_liveness,
maybe_storage_live,
maybe_storage_dead,
}
.visit_body(body);
}
Expand All @@ -37,7 +44,8 @@ struct Lint<'a, 'tcx> {
is_fn_like: bool,
always_live_locals: &'a BitSet<Local>,
reachable_blocks: BitSet<BasicBlock>,
storage_liveness: ResultsCursor<'a, 'tcx, MaybeStorageLive<'a>>,
maybe_storage_live: ResultsCursor<'a, 'tcx, MaybeStorageLive<'a>>,
maybe_storage_dead: ResultsCursor<'a, 'tcx, MaybeStorageDead<'a>>,
}

impl<'a, 'tcx> Lint<'a, 'tcx> {
Expand All @@ -60,8 +68,8 @@ impl<'a, 'tcx> Lint<'a, 'tcx> {
impl<'a, 'tcx> Visitor<'tcx> for Lint<'a, 'tcx> {
fn visit_local(&mut self, local: Local, context: PlaceContext, location: Location) {
if self.reachable_blocks.contains(location.block) && context.is_use() {
self.storage_liveness.seek_after_primary_effect(location);
if !self.storage_liveness.get().contains(local) {
self.maybe_storage_dead.seek_after_primary_effect(location);
if self.maybe_storage_dead.get().contains(local) {
self.fail(location, format!("use of local {local:?}, which has no storage here"));
}
}
Expand All @@ -71,8 +79,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Lint<'a, 'tcx> {
match statement.kind {
StatementKind::StorageLive(local) => {
if self.reachable_blocks.contains(location.block) {
self.storage_liveness.seek_before_primary_effect(location);
if self.storage_liveness.get().contains(local) {
self.maybe_storage_live.seek_before_primary_effect(location);
if self.maybe_storage_live.get().contains(local) {
self.fail(
location,
format!("StorageLive({local:?}) which already has storage here"),
Expand All @@ -90,8 +98,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Lint<'a, 'tcx> {
match terminator.kind {
TerminatorKind::Return => {
if self.is_fn_like && self.reachable_blocks.contains(location.block) {
self.storage_liveness.seek_after_primary_effect(location);
for local in self.storage_liveness.get().iter() {
self.maybe_storage_live.seek_after_primary_effect(location);
for local in self.maybe_storage_live.get().iter() {
if !self.always_live_locals.contains(local) {
self.fail(
location,
Expand Down
30 changes: 30 additions & 0 deletions tests/ui/mir/lint/no-storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// compile-flags: -Zlint-mir --crate-type=lib -Ztreat-err-as-bug
// failure-status: 101
// dont-check-compiler-stderr
// regex-error-pattern: use of local .*, which has no storage here
#![feature(custom_mir, core_intrinsics)]
extern crate core;
use core::intrinsics::mir::*;

#[custom_mir(dialect = "built")]
pub fn f(a: bool) {
mir!(
let b: ();
{
match a { true => bb1, _ => bb2 }
}
bb1 = {
StorageLive(b);
Goto(bb3)
}
bb2 = {
Goto(bb3)
}
bb3 = {
b = ();
RET = b;
StorageDead(b);
Return()
}
)
}

0 comments on commit 4921e5a

Please sign in to comment.