2016-05-24 13:26:54 +02:00
|
|
|
// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-06-26 14:57:26 +02:00
|
|
|
//! Dataflow analyses are built upon some interpretation of the
|
|
|
|
//! bitvectors attached to each basic block, represented via a
|
|
|
|
//! zero-sized structure.
|
|
|
|
|
2016-05-24 13:26:54 +02:00
|
|
|
use rustc::ty::TyCtxt;
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir::{self, Mir, Location};
|
2018-09-13 14:19:01 +10:00
|
|
|
use rustc_data_structures::bitvec::{BitwiseOperator, Word};
|
2016-10-05 22:43:27 -04:00
|
|
|
use rustc_data_structures::indexed_set::{IdxSet};
|
2017-11-08 18:32:08 +03:00
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
2016-05-24 13:26:54 +02:00
|
|
|
|
2017-06-26 14:57:26 +02:00
|
|
|
use super::MoveDataParamEnv;
|
2018-04-06 15:18:01 -04:00
|
|
|
|
2017-06-26 14:57:26 +02:00
|
|
|
use util::elaborate_drops::DropFlagState;
|
2016-05-24 13:26:54 +02:00
|
|
|
|
2018-08-30 18:54:32 -03:00
|
|
|
use super::move_paths::{HasMoveData, MoveData, MovePathIndex, InitIndex};
|
2017-11-27 08:06:36 +00:00
|
|
|
use super::move_paths::{LookupResult, InitKind};
|
2017-11-27 15:08:11 +01:00
|
|
|
use super::{BitDenotation, BlockSets, InitialFlow};
|
2016-05-24 13:26:54 +02:00
|
|
|
|
2017-06-26 14:57:26 +02:00
|
|
|
use super::drop_flag_effects_for_function_entry;
|
|
|
|
use super::drop_flag_effects_for_location;
|
2018-08-30 18:54:32 -03:00
|
|
|
use super::on_lookup_result_bits;
|
2016-05-24 13:26:54 +02:00
|
|
|
|
2017-09-10 22:34:56 +02:00
|
|
|
mod storage_liveness;
|
|
|
|
|
|
|
|
pub use self::storage_liveness::*;
|
|
|
|
|
2018-01-29 08:48:56 +01:00
|
|
|
mod borrowed_locals;
|
|
|
|
|
|
|
|
pub use self::borrowed_locals::*;
|
|
|
|
|
2017-07-03 18:40:20 +02:00
|
|
|
pub(super) mod borrows;
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
/// `MaybeInitializedPlaces` tracks all places that might be
|
2016-05-24 13:26:54 +02:00
|
|
|
/// initialized upon reaching a particular point in the control flow
|
|
|
|
/// for a function.
|
|
|
|
///
|
|
|
|
/// For example, in code like the following, we have corresponding
|
|
|
|
/// dataflow information shown in the right-hand comments.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// struct S;
|
|
|
|
/// fn foo(pred: bool) { // maybe-init:
|
|
|
|
/// // {}
|
|
|
|
/// let a = S; let b = S; let c; let d; // {a, b}
|
|
|
|
///
|
|
|
|
/// if pred {
|
|
|
|
/// drop(a); // { b}
|
|
|
|
/// b = S; // { b}
|
|
|
|
///
|
|
|
|
/// } else {
|
|
|
|
/// drop(b); // {a}
|
|
|
|
/// d = S; // {a, d}
|
|
|
|
///
|
|
|
|
/// } // {a, b, d}
|
|
|
|
///
|
|
|
|
/// c = S; // {a, b, c, d}
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2018-01-29 01:49:29 +02:00
|
|
|
/// To determine whether a place *must* be initialized at a
|
2016-05-24 13:26:54 +02:00
|
|
|
/// particular control-flow point, one can take the set-difference
|
2018-01-29 01:49:29 +02:00
|
|
|
/// between this data and the data from `MaybeUninitializedPlaces` at the
|
2016-05-24 13:26:54 +02:00
|
|
|
/// corresponding control-flow point.
|
|
|
|
///
|
|
|
|
/// Similarly, at a given `drop` statement, the set-intersection
|
2018-01-29 01:49:29 +02:00
|
|
|
/// between this data and `MaybeUninitializedPlaces` yields the set of
|
|
|
|
/// places that would require a dynamic drop-flag at that statement.
|
|
|
|
pub struct MaybeInitializedPlaces<'a, 'gcx: 'tcx, 'tcx: 'a> {
|
2017-10-30 05:50:39 -04:00
|
|
|
tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
2016-05-24 15:01:48 +02:00
|
|
|
mir: &'a Mir<'tcx>,
|
2017-10-30 05:50:39 -04:00
|
|
|
mdpe: &'a MoveDataParamEnv<'gcx, 'tcx>,
|
2016-05-24 15:01:48 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx: 'tcx, 'tcx> MaybeInitializedPlaces<'a, 'gcx, 'tcx> {
|
2017-10-30 05:50:39 -04:00
|
|
|
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
2016-12-26 09:40:15 -05:00
|
|
|
mir: &'a Mir<'tcx>,
|
2017-10-30 05:50:39 -04:00
|
|
|
mdpe: &'a MoveDataParamEnv<'gcx, 'tcx>)
|
2016-12-26 09:40:15 -05:00
|
|
|
-> Self
|
|
|
|
{
|
2018-01-29 01:49:29 +02:00
|
|
|
MaybeInitializedPlaces { tcx: tcx, mir: mir, mdpe: mdpe }
|
2016-05-24 15:01:48 +02:00
|
|
|
}
|
2016-05-24 13:26:54 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> HasMoveData<'tcx> for MaybeInitializedPlaces<'a, 'gcx, 'tcx> {
|
2016-12-26 09:40:15 -05:00
|
|
|
fn move_data(&self) -> &MoveData<'tcx> { &self.mdpe.move_data }
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
/// `MaybeUninitializedPlaces` tracks all places that might be
|
2016-05-24 13:26:54 +02:00
|
|
|
/// uninitialized upon reaching a particular point in the control flow
|
|
|
|
/// for a function.
|
|
|
|
///
|
|
|
|
/// For example, in code like the following, we have corresponding
|
|
|
|
/// dataflow information shown in the right-hand comments.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// struct S;
|
|
|
|
/// fn foo(pred: bool) { // maybe-uninit:
|
|
|
|
/// // {a, b, c, d}
|
|
|
|
/// let a = S; let b = S; let c; let d; // { c, d}
|
|
|
|
///
|
|
|
|
/// if pred {
|
|
|
|
/// drop(a); // {a, c, d}
|
|
|
|
/// b = S; // {a, c, d}
|
|
|
|
///
|
|
|
|
/// } else {
|
|
|
|
/// drop(b); // { b, c, d}
|
|
|
|
/// d = S; // { b, c }
|
|
|
|
///
|
|
|
|
/// } // {a, b, c, d}
|
|
|
|
///
|
|
|
|
/// c = S; // {a, b, d}
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2018-01-29 01:49:29 +02:00
|
|
|
/// To determine whether a place *must* be uninitialized at a
|
2016-05-24 13:26:54 +02:00
|
|
|
/// particular control-flow point, one can take the set-difference
|
2018-01-29 01:49:29 +02:00
|
|
|
/// between this data and the data from `MaybeInitializedPlaces` at the
|
2016-05-24 13:26:54 +02:00
|
|
|
/// corresponding control-flow point.
|
|
|
|
///
|
|
|
|
/// Similarly, at a given `drop` statement, the set-intersection
|
2018-01-29 01:49:29 +02:00
|
|
|
/// between this data and `MaybeInitializedPlaces` yields the set of
|
|
|
|
/// places that would require a dynamic drop-flag at that statement.
|
|
|
|
pub struct MaybeUninitializedPlaces<'a, 'gcx: 'tcx, 'tcx: 'a> {
|
2017-10-30 05:50:39 -04:00
|
|
|
tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
2016-05-24 15:01:48 +02:00
|
|
|
mir: &'a Mir<'tcx>,
|
2017-10-30 05:50:39 -04:00
|
|
|
mdpe: &'a MoveDataParamEnv<'gcx, 'tcx>,
|
2016-05-24 15:01:48 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> MaybeUninitializedPlaces<'a, 'gcx, 'tcx> {
|
2017-10-30 05:50:39 -04:00
|
|
|
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
2016-12-26 09:40:15 -05:00
|
|
|
mir: &'a Mir<'tcx>,
|
2017-10-30 05:50:39 -04:00
|
|
|
mdpe: &'a MoveDataParamEnv<'gcx, 'tcx>)
|
2016-12-26 09:40:15 -05:00
|
|
|
-> Self
|
|
|
|
{
|
2018-01-29 01:49:29 +02:00
|
|
|
MaybeUninitializedPlaces { tcx: tcx, mir: mir, mdpe: mdpe }
|
2016-05-24 15:01:48 +02:00
|
|
|
}
|
2016-05-24 13:26:54 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> HasMoveData<'tcx> for MaybeUninitializedPlaces<'a, 'gcx, 'tcx> {
|
2016-12-26 09:40:15 -05:00
|
|
|
fn move_data(&self) -> &MoveData<'tcx> { &self.mdpe.move_data }
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
/// `DefinitelyInitializedPlaces` tracks all places that are definitely
|
2016-05-24 13:26:54 +02:00
|
|
|
/// initialized upon reaching a particular point in the control flow
|
|
|
|
/// for a function.
|
|
|
|
///
|
|
|
|
/// FIXME: Note that once flow-analysis is complete, this should be
|
2018-01-29 01:49:29 +02:00
|
|
|
/// the set-complement of MaybeUninitializedPlaces; thus we can get rid
|
2016-05-24 13:26:54 +02:00
|
|
|
/// of one or the other of these two. I'm inclined to get rid of
|
2018-01-29 01:49:29 +02:00
|
|
|
/// MaybeUninitializedPlaces, simply because the sets will tend to be
|
2016-05-24 13:26:54 +02:00
|
|
|
/// smaller in this analysis and thus easier for humans to process
|
|
|
|
/// when debugging.
|
|
|
|
///
|
|
|
|
/// For example, in code like the following, we have corresponding
|
|
|
|
/// dataflow information shown in the right-hand comments.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// struct S;
|
|
|
|
/// fn foo(pred: bool) { // definite-init:
|
|
|
|
/// // { }
|
|
|
|
/// let a = S; let b = S; let c; let d; // {a, b }
|
|
|
|
///
|
|
|
|
/// if pred {
|
|
|
|
/// drop(a); // { b, }
|
|
|
|
/// b = S; // { b, }
|
|
|
|
///
|
|
|
|
/// } else {
|
|
|
|
/// drop(b); // {a, }
|
|
|
|
/// d = S; // {a, d}
|
|
|
|
///
|
|
|
|
/// } // { }
|
|
|
|
///
|
|
|
|
/// c = S; // { c }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2018-01-29 01:49:29 +02:00
|
|
|
/// To determine whether a place *may* be uninitialized at a
|
2016-05-24 13:26:54 +02:00
|
|
|
/// particular control-flow point, one can take the set-complement
|
|
|
|
/// of this data.
|
|
|
|
///
|
|
|
|
/// Similarly, at a given `drop` statement, the set-difference between
|
2018-01-29 01:49:29 +02:00
|
|
|
/// this data and `MaybeInitializedPlaces` yields the set of places
|
2016-05-24 13:26:54 +02:00
|
|
|
/// that would require a dynamic drop-flag at that statement.
|
2018-01-29 01:49:29 +02:00
|
|
|
pub struct DefinitelyInitializedPlaces<'a, 'gcx: 'tcx, 'tcx: 'a> {
|
2017-10-30 05:50:39 -04:00
|
|
|
tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
2016-05-24 15:01:48 +02:00
|
|
|
mir: &'a Mir<'tcx>,
|
2017-10-30 05:50:39 -04:00
|
|
|
mdpe: &'a MoveDataParamEnv<'gcx, 'tcx>,
|
2016-05-24 15:01:48 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx: 'a> DefinitelyInitializedPlaces<'a, 'gcx, 'tcx> {
|
2017-10-30 05:50:39 -04:00
|
|
|
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
2016-12-26 09:40:15 -05:00
|
|
|
mir: &'a Mir<'tcx>,
|
2017-10-30 05:50:39 -04:00
|
|
|
mdpe: &'a MoveDataParamEnv<'gcx, 'tcx>)
|
2016-12-26 09:40:15 -05:00
|
|
|
-> Self
|
|
|
|
{
|
2018-01-29 01:49:29 +02:00
|
|
|
DefinitelyInitializedPlaces { tcx: tcx, mir: mir, mdpe: mdpe }
|
2016-05-24 15:01:48 +02:00
|
|
|
}
|
2016-05-24 13:26:54 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx: 'a> HasMoveData<'tcx> for DefinitelyInitializedPlaces<'a, 'gcx, 'tcx> {
|
2016-12-26 09:40:15 -05:00
|
|
|
fn move_data(&self) -> &MoveData<'tcx> { &self.mdpe.move_data }
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
/// `EverInitializedPlaces` tracks all places that might have ever been
|
2017-11-27 08:06:36 +00:00
|
|
|
/// initialized upon reaching a particular point in the control flow
|
|
|
|
/// for a function, without an intervening `Storage Dead`.
|
|
|
|
///
|
|
|
|
/// This dataflow is used to determine if an immutable local variable may
|
|
|
|
/// be assigned to.
|
|
|
|
///
|
|
|
|
/// For example, in code like the following, we have corresponding
|
|
|
|
/// dataflow information shown in the right-hand comments.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// struct S;
|
|
|
|
/// fn foo(pred: bool) { // ever-init:
|
|
|
|
/// // { }
|
|
|
|
/// let a = S; let b = S; let c; let d; // {a, b }
|
|
|
|
///
|
|
|
|
/// if pred {
|
|
|
|
/// drop(a); // {a, b, }
|
|
|
|
/// b = S; // {a, b, }
|
|
|
|
///
|
|
|
|
/// } else {
|
|
|
|
/// drop(b); // {a, b, }
|
|
|
|
/// d = S; // {a, b, d }
|
|
|
|
///
|
|
|
|
/// } // {a, b, d }
|
|
|
|
///
|
|
|
|
/// c = S; // {a, b, c, d }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-01-29 01:49:29 +02:00
|
|
|
pub struct EverInitializedPlaces<'a, 'gcx: 'tcx, 'tcx: 'a> {
|
2017-11-27 08:06:36 +00:00
|
|
|
tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
mir: &'a Mir<'tcx>,
|
|
|
|
mdpe: &'a MoveDataParamEnv<'gcx, 'tcx>,
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx: 'tcx, 'tcx: 'a> EverInitializedPlaces<'a, 'gcx, 'tcx> {
|
2017-11-27 08:06:36 +00:00
|
|
|
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
mir: &'a Mir<'tcx>,
|
|
|
|
mdpe: &'a MoveDataParamEnv<'gcx, 'tcx>)
|
|
|
|
-> Self
|
|
|
|
{
|
2018-01-29 01:49:29 +02:00
|
|
|
EverInitializedPlaces { tcx: tcx, mir: mir, mdpe: mdpe }
|
2017-11-27 08:06:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> HasMoveData<'tcx> for EverInitializedPlaces<'a, 'gcx, 'tcx> {
|
2017-11-27 08:06:36 +00:00
|
|
|
fn move_data(&self) -> &MoveData<'tcx> { &self.mdpe.move_data }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> MaybeInitializedPlaces<'a, 'gcx, 'tcx> {
|
2016-05-24 13:26:54 +02:00
|
|
|
fn update_bits(sets: &mut BlockSets<MovePathIndex>, path: MovePathIndex,
|
|
|
|
state: DropFlagState)
|
|
|
|
{
|
|
|
|
match state {
|
|
|
|
DropFlagState::Absent => sets.kill(&path),
|
|
|
|
DropFlagState::Present => sets.gen(&path),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> MaybeUninitializedPlaces<'a, 'gcx, 'tcx> {
|
2016-05-24 13:26:54 +02:00
|
|
|
fn update_bits(sets: &mut BlockSets<MovePathIndex>, path: MovePathIndex,
|
|
|
|
state: DropFlagState)
|
|
|
|
{
|
|
|
|
match state {
|
|
|
|
DropFlagState::Absent => sets.gen(&path),
|
|
|
|
DropFlagState::Present => sets.kill(&path),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> DefinitelyInitializedPlaces<'a, 'gcx, 'tcx> {
|
2016-05-24 13:26:54 +02:00
|
|
|
fn update_bits(sets: &mut BlockSets<MovePathIndex>, path: MovePathIndex,
|
|
|
|
state: DropFlagState)
|
|
|
|
{
|
|
|
|
match state {
|
|
|
|
DropFlagState::Absent => sets.kill(&path),
|
|
|
|
DropFlagState::Present => sets.gen(&path),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> BitDenotation for MaybeInitializedPlaces<'a, 'gcx, 'tcx> {
|
2016-05-24 13:26:54 +02:00
|
|
|
type Idx = MovePathIndex;
|
|
|
|
fn name() -> &'static str { "maybe_init" }
|
2016-12-26 09:40:15 -05:00
|
|
|
fn bits_per_block(&self) -> usize {
|
|
|
|
self.move_data().move_paths.len()
|
2016-05-24 13:26:54 +02:00
|
|
|
}
|
2016-05-24 16:46:19 +02:00
|
|
|
|
2017-12-07 20:06:55 +02:00
|
|
|
fn start_block_effect(&self, entry_set: &mut IdxSet<MovePathIndex>) {
|
2016-05-24 13:26:54 +02:00
|
|
|
drop_flag_effects_for_function_entry(
|
2016-12-26 09:40:15 -05:00
|
|
|
self.tcx, self.mir, self.mdpe,
|
2016-05-24 13:26:54 +02:00
|
|
|
|path, s| {
|
|
|
|
assert!(s == DropFlagState::Present);
|
2017-12-07 20:06:55 +02:00
|
|
|
entry_set.add(&path);
|
2016-05-24 13:26:54 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn statement_effect(&self,
|
|
|
|
sets: &mut BlockSets<MovePathIndex>,
|
2017-07-03 17:58:19 +02:00
|
|
|
location: Location)
|
2016-05-24 13:26:54 +02:00
|
|
|
{
|
|
|
|
drop_flag_effects_for_location(
|
2016-12-26 09:40:15 -05:00
|
|
|
self.tcx, self.mir, self.mdpe,
|
2017-07-03 17:58:19 +02:00
|
|
|
location,
|
2016-05-24 13:26:54 +02:00
|
|
|
|path, s| Self::update_bits(sets, path, s)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn terminator_effect(&self,
|
|
|
|
sets: &mut BlockSets<MovePathIndex>,
|
2017-07-03 17:58:19 +02:00
|
|
|
location: Location)
|
2016-05-24 13:26:54 +02:00
|
|
|
{
|
|
|
|
drop_flag_effects_for_location(
|
2016-12-26 09:40:15 -05:00
|
|
|
self.tcx, self.mir, self.mdpe,
|
2017-07-03 17:58:19 +02:00
|
|
|
location,
|
2016-05-24 13:26:54 +02:00
|
|
|
|path, s| Self::update_bits(sets, path, s)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn propagate_call_return(&self,
|
|
|
|
in_out: &mut IdxSet<MovePathIndex>,
|
2016-09-19 23:50:00 +03:00
|
|
|
_call_bb: mir::BasicBlock,
|
|
|
|
_dest_bb: mir::BasicBlock,
|
2017-12-01 14:39:51 +02:00
|
|
|
dest_place: &mir::Place) {
|
2016-05-24 13:26:54 +02:00
|
|
|
// when a call returns successfully, that means we need to set
|
2017-12-01 14:39:51 +02:00
|
|
|
// the bits for that dest_place to 1 (initialized).
|
2016-12-26 09:40:15 -05:00
|
|
|
on_lookup_result_bits(self.tcx, self.mir, self.move_data(),
|
2017-12-01 14:39:51 +02:00
|
|
|
self.move_data().rev_lookup.find(dest_place),
|
2016-06-11 23:47:28 +03:00
|
|
|
|mpi| { in_out.add(&mpi); });
|
2016-05-24 13:26:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> BitDenotation for MaybeUninitializedPlaces<'a, 'gcx, 'tcx> {
|
2016-05-24 13:26:54 +02:00
|
|
|
type Idx = MovePathIndex;
|
|
|
|
fn name() -> &'static str { "maybe_uninit" }
|
2016-12-26 09:40:15 -05:00
|
|
|
fn bits_per_block(&self) -> usize {
|
|
|
|
self.move_data().move_paths.len()
|
2016-05-24 13:26:54 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
// sets on_entry bits for Arg places
|
2017-12-07 20:06:55 +02:00
|
|
|
fn start_block_effect(&self, entry_set: &mut IdxSet<MovePathIndex>) {
|
2016-05-24 13:26:54 +02:00
|
|
|
// set all bits to 1 (uninit) before gathering counterevidence
|
2018-04-02 00:14:44 +03:00
|
|
|
entry_set.set_up_to(self.bits_per_block());
|
2016-05-24 13:26:54 +02:00
|
|
|
|
|
|
|
drop_flag_effects_for_function_entry(
|
2016-12-26 09:40:15 -05:00
|
|
|
self.tcx, self.mir, self.mdpe,
|
2016-05-24 13:26:54 +02:00
|
|
|
|path, s| {
|
|
|
|
assert!(s == DropFlagState::Present);
|
2017-12-07 20:06:55 +02:00
|
|
|
entry_set.remove(&path);
|
2016-05-24 13:26:54 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn statement_effect(&self,
|
|
|
|
sets: &mut BlockSets<MovePathIndex>,
|
2017-07-03 17:58:19 +02:00
|
|
|
location: Location)
|
2016-05-24 13:26:54 +02:00
|
|
|
{
|
|
|
|
drop_flag_effects_for_location(
|
2016-12-26 09:40:15 -05:00
|
|
|
self.tcx, self.mir, self.mdpe,
|
2017-07-03 17:58:19 +02:00
|
|
|
location,
|
2016-05-24 13:26:54 +02:00
|
|
|
|path, s| Self::update_bits(sets, path, s)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn terminator_effect(&self,
|
|
|
|
sets: &mut BlockSets<MovePathIndex>,
|
2017-07-03 17:58:19 +02:00
|
|
|
location: Location)
|
2016-05-24 13:26:54 +02:00
|
|
|
{
|
|
|
|
drop_flag_effects_for_location(
|
2016-12-26 09:40:15 -05:00
|
|
|
self.tcx, self.mir, self.mdpe,
|
2017-07-03 17:58:19 +02:00
|
|
|
location,
|
2016-05-24 13:26:54 +02:00
|
|
|
|path, s| Self::update_bits(sets, path, s)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn propagate_call_return(&self,
|
|
|
|
in_out: &mut IdxSet<MovePathIndex>,
|
2016-09-19 23:50:00 +03:00
|
|
|
_call_bb: mir::BasicBlock,
|
|
|
|
_dest_bb: mir::BasicBlock,
|
2017-12-01 14:39:51 +02:00
|
|
|
dest_place: &mir::Place) {
|
2016-05-24 13:26:54 +02:00
|
|
|
// when a call returns successfully, that means we need to set
|
2017-12-01 14:39:51 +02:00
|
|
|
// the bits for that dest_place to 0 (initialized).
|
2016-12-26 09:40:15 -05:00
|
|
|
on_lookup_result_bits(self.tcx, self.mir, self.move_data(),
|
2017-12-01 14:39:51 +02:00
|
|
|
self.move_data().rev_lookup.find(dest_place),
|
2016-06-11 23:47:28 +03:00
|
|
|
|mpi| { in_out.remove(&mpi); });
|
2016-05-24 13:26:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> BitDenotation for DefinitelyInitializedPlaces<'a, 'gcx, 'tcx> {
|
2016-05-24 13:26:54 +02:00
|
|
|
type Idx = MovePathIndex;
|
|
|
|
fn name() -> &'static str { "definite_init" }
|
2016-12-26 09:40:15 -05:00
|
|
|
fn bits_per_block(&self) -> usize {
|
|
|
|
self.move_data().move_paths.len()
|
2016-05-24 13:26:54 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
// sets on_entry bits for Arg places
|
2017-12-07 20:06:55 +02:00
|
|
|
fn start_block_effect(&self, entry_set: &mut IdxSet<MovePathIndex>) {
|
2018-04-02 00:14:44 +03:00
|
|
|
entry_set.clear();
|
2016-05-24 13:26:54 +02:00
|
|
|
|
|
|
|
drop_flag_effects_for_function_entry(
|
2016-12-26 09:40:15 -05:00
|
|
|
self.tcx, self.mir, self.mdpe,
|
2016-05-24 13:26:54 +02:00
|
|
|
|path, s| {
|
|
|
|
assert!(s == DropFlagState::Present);
|
2017-12-07 20:06:55 +02:00
|
|
|
entry_set.add(&path);
|
2016-05-24 13:26:54 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn statement_effect(&self,
|
|
|
|
sets: &mut BlockSets<MovePathIndex>,
|
2017-07-03 17:58:19 +02:00
|
|
|
location: Location)
|
2016-05-24 13:26:54 +02:00
|
|
|
{
|
|
|
|
drop_flag_effects_for_location(
|
2016-12-26 09:40:15 -05:00
|
|
|
self.tcx, self.mir, self.mdpe,
|
2017-07-03 17:58:19 +02:00
|
|
|
location,
|
2016-05-24 13:26:54 +02:00
|
|
|
|path, s| Self::update_bits(sets, path, s)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn terminator_effect(&self,
|
|
|
|
sets: &mut BlockSets<MovePathIndex>,
|
2017-07-03 17:58:19 +02:00
|
|
|
location: Location)
|
2016-05-24 13:26:54 +02:00
|
|
|
{
|
|
|
|
drop_flag_effects_for_location(
|
2016-12-26 09:40:15 -05:00
|
|
|
self.tcx, self.mir, self.mdpe,
|
2017-07-03 17:58:19 +02:00
|
|
|
location,
|
2016-05-24 13:26:54 +02:00
|
|
|
|path, s| Self::update_bits(sets, path, s)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn propagate_call_return(&self,
|
|
|
|
in_out: &mut IdxSet<MovePathIndex>,
|
2016-09-19 23:50:00 +03:00
|
|
|
_call_bb: mir::BasicBlock,
|
|
|
|
_dest_bb: mir::BasicBlock,
|
2017-12-01 14:39:51 +02:00
|
|
|
dest_place: &mir::Place) {
|
2016-05-24 13:26:54 +02:00
|
|
|
// when a call returns successfully, that means we need to set
|
2017-12-01 14:39:51 +02:00
|
|
|
// the bits for that dest_place to 1 (initialized).
|
2016-12-26 09:40:15 -05:00
|
|
|
on_lookup_result_bits(self.tcx, self.mir, self.move_data(),
|
2017-12-01 14:39:51 +02:00
|
|
|
self.move_data().rev_lookup.find(dest_place),
|
2016-06-11 23:47:28 +03:00
|
|
|
|mpi| { in_out.add(&mpi); });
|
2016-05-24 13:26:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> BitDenotation for EverInitializedPlaces<'a, 'gcx, 'tcx> {
|
2017-11-27 08:06:36 +00:00
|
|
|
type Idx = InitIndex;
|
|
|
|
fn name() -> &'static str { "ever_init" }
|
|
|
|
fn bits_per_block(&self) -> usize {
|
|
|
|
self.move_data().inits.len()
|
|
|
|
}
|
|
|
|
|
2017-12-07 20:06:55 +02:00
|
|
|
fn start_block_effect(&self, entry_set: &mut IdxSet<InitIndex>) {
|
|
|
|
for arg_init in 0..self.mir.arg_count {
|
|
|
|
entry_set.add(&InitIndex::new(arg_init));
|
|
|
|
}
|
2017-11-27 08:06:36 +00:00
|
|
|
}
|
2017-12-07 20:06:55 +02:00
|
|
|
|
2017-11-27 08:06:36 +00:00
|
|
|
fn statement_effect(&self,
|
|
|
|
sets: &mut BlockSets<InitIndex>,
|
|
|
|
location: Location) {
|
|
|
|
let (_, mir, move_data) = (self.tcx, self.mir, self.move_data());
|
|
|
|
let stmt = &mir[location.block].statements[location.statement_index];
|
|
|
|
let init_path_map = &move_data.init_path_map;
|
|
|
|
let init_loc_map = &move_data.init_loc_map;
|
|
|
|
let rev_lookup = &move_data.rev_lookup;
|
|
|
|
|
|
|
|
debug!("statement {:?} at loc {:?} initializes move_indexes {:?}",
|
|
|
|
stmt, location, &init_loc_map[location]);
|
2017-12-04 01:00:46 +02:00
|
|
|
sets.gen_all(&init_loc_map[location]);
|
2017-11-27 08:06:36 +00:00
|
|
|
|
|
|
|
match stmt.kind {
|
2017-12-06 02:10:24 +02:00
|
|
|
mir::StatementKind::StorageDead(local) |
|
|
|
|
mir::StatementKind::StorageLive(local) => {
|
|
|
|
// End inits for StorageDead and StorageLive, so that an immutable
|
|
|
|
// variable can be reinitialized on the next iteration of the loop.
|
|
|
|
//
|
|
|
|
// FIXME(#46525): We *need* to do this for StorageLive as well as
|
|
|
|
// StorageDead, because lifetimes of match bindings with guards are
|
|
|
|
// weird - i.e. this code
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// match 0 {
|
|
|
|
// a | a
|
|
|
|
// if { println!("a={}", a); false } => {}
|
|
|
|
// _ => {}
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// runs the guard twice, using the same binding for `a`, and only
|
|
|
|
// storagedeads after everything ends, so if we don't regard the
|
|
|
|
// storagelive as killing storage, we would have a multiple assignment
|
|
|
|
// to immutable data error.
|
2017-12-01 14:31:47 +02:00
|
|
|
if let LookupResult::Exact(mpi) = rev_lookup.find(&mir::Place::Local(local)) {
|
2017-11-27 08:06:36 +00:00
|
|
|
debug!("stmt {:?} at loc {:?} clears the ever initialized status of {:?}",
|
2017-12-04 01:00:46 +02:00
|
|
|
stmt, location, &init_path_map[mpi]);
|
|
|
|
sets.kill_all(&init_path_map[mpi]);
|
2017-11-27 08:06:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn terminator_effect(&self,
|
|
|
|
sets: &mut BlockSets<InitIndex>,
|
|
|
|
location: Location)
|
|
|
|
{
|
|
|
|
let (mir, move_data) = (self.mir, self.move_data());
|
|
|
|
let term = mir[location.block].terminator();
|
|
|
|
let init_loc_map = &move_data.init_loc_map;
|
|
|
|
debug!("terminator {:?} at loc {:?} initializes move_indexes {:?}",
|
|
|
|
term, location, &init_loc_map[location]);
|
2017-12-04 01:00:46 +02:00
|
|
|
sets.gen_all(
|
|
|
|
init_loc_map[location].iter().filter(|init_index| {
|
|
|
|
move_data.inits[**init_index].kind != InitKind::NonPanicPathOnly
|
|
|
|
})
|
|
|
|
);
|
2017-11-27 08:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn propagate_call_return(&self,
|
|
|
|
in_out: &mut IdxSet<InitIndex>,
|
|
|
|
call_bb: mir::BasicBlock,
|
|
|
|
_dest_bb: mir::BasicBlock,
|
2017-12-01 14:39:51 +02:00
|
|
|
_dest_place: &mir::Place) {
|
2017-11-27 08:06:36 +00:00
|
|
|
let move_data = self.move_data();
|
|
|
|
let bits_per_block = self.bits_per_block();
|
|
|
|
let init_loc_map = &move_data.init_loc_map;
|
|
|
|
|
|
|
|
let call_loc = Location {
|
|
|
|
block: call_bb,
|
|
|
|
statement_index: self.mir[call_bb].statements.len(),
|
|
|
|
};
|
|
|
|
for init_index in &init_loc_map[call_loc] {
|
|
|
|
assert!(init_index.index() < bits_per_block);
|
|
|
|
in_out.add(init_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> BitwiseOperator for MaybeInitializedPlaces<'a, 'gcx, 'tcx> {
|
2016-05-24 13:26:54 +02:00
|
|
|
#[inline]
|
2018-07-13 10:53:57 +10:00
|
|
|
fn join(&self, pred1: Word, pred2: Word) -> Word {
|
2016-05-24 13:26:54 +02:00
|
|
|
pred1 | pred2 // "maybe" means we union effects of both preds
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> BitwiseOperator for MaybeUninitializedPlaces<'a, 'gcx, 'tcx> {
|
2016-05-24 13:26:54 +02:00
|
|
|
#[inline]
|
2018-07-13 10:53:57 +10:00
|
|
|
fn join(&self, pred1: Word, pred2: Word) -> Word {
|
2016-05-24 13:26:54 +02:00
|
|
|
pred1 | pred2 // "maybe" means we union effects of both preds
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> BitwiseOperator for DefinitelyInitializedPlaces<'a, 'gcx, 'tcx> {
|
2016-05-24 13:26:54 +02:00
|
|
|
#[inline]
|
2018-07-13 10:53:57 +10:00
|
|
|
fn join(&self, pred1: Word, pred2: Word) -> Word {
|
2016-05-24 13:26:54 +02:00
|
|
|
pred1 & pred2 // "definitely" means we intersect effects of both preds
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> BitwiseOperator for EverInitializedPlaces<'a, 'gcx, 'tcx> {
|
2017-11-27 08:06:36 +00:00
|
|
|
#[inline]
|
2018-07-13 10:53:57 +10:00
|
|
|
fn join(&self, pred1: Word, pred2: Word) -> Word {
|
2017-11-27 08:06:36 +00:00
|
|
|
pred1 | pred2 // inits from both preds are in scope
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 13:26:54 +02:00
|
|
|
// The way that dataflow fixed point iteration works, you want to
|
|
|
|
// start at bottom and work your way to a fixed point. Control-flow
|
|
|
|
// merges will apply the `join` operator to each block entry's current
|
|
|
|
// state (which starts at that bottom value).
|
|
|
|
//
|
|
|
|
// This means, for propagation across the graph, that you either want
|
|
|
|
// to start at all-zeroes and then use Union as your merge when
|
|
|
|
// propagating, or you start at all-ones and then use Intersect as
|
|
|
|
// your merge when propagating.
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> InitialFlow for MaybeInitializedPlaces<'a, 'gcx, 'tcx> {
|
2016-05-24 13:26:54 +02:00
|
|
|
#[inline]
|
|
|
|
fn bottom_value() -> bool {
|
|
|
|
false // bottom = uninitialized
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> InitialFlow for MaybeUninitializedPlaces<'a, 'gcx, 'tcx> {
|
2016-05-24 13:26:54 +02:00
|
|
|
#[inline]
|
|
|
|
fn bottom_value() -> bool {
|
|
|
|
false // bottom = initialized (start_block_effect counters this at outset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> InitialFlow for DefinitelyInitializedPlaces<'a, 'gcx, 'tcx> {
|
2016-05-24 13:26:54 +02:00
|
|
|
#[inline]
|
|
|
|
fn bottom_value() -> bool {
|
|
|
|
true // bottom = initialized (start_block_effect counters this at outset)
|
|
|
|
}
|
|
|
|
}
|
2017-11-08 18:32:08 +03:00
|
|
|
|
2018-01-29 01:49:29 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> InitialFlow for EverInitializedPlaces<'a, 'gcx, 'tcx> {
|
2017-11-27 08:06:36 +00:00
|
|
|
#[inline]
|
|
|
|
fn bottom_value() -> bool {
|
|
|
|
false // bottom = no initialized variables by default
|
|
|
|
}
|
|
|
|
}
|