mirror of
https://github.com/rust-lang/rust.git
synced 2024-10-31 14:31:55 +00:00
parent
de7cb0fdd6
commit
a091cfd4f3
@ -613,6 +613,24 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
|
||||
value.trans_normalize(&infcx)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn normalize_associated_type_in_env<T>(
|
||||
self, value: &T, env: &'a ty::ParameterEnvironment<'tcx>
|
||||
) -> T
|
||||
where T: TransNormalize<'tcx>
|
||||
{
|
||||
debug!("normalize_associated_type_in_env(t={:?})", value);
|
||||
|
||||
let value = self.erase_regions(value);
|
||||
|
||||
if !value.has_projection_types() {
|
||||
return value;
|
||||
}
|
||||
|
||||
self.infer_ctxt(None, Some(env.clone()), ProjectionMode::Any).enter(|infcx| {
|
||||
value.trans_normalize(&infcx)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
|
||||
|
@ -200,6 +200,12 @@ impl<'a, 'tcx: 'a, O> DataflowAnalysis<'a, 'tcx, O>
|
||||
|
||||
pub struct DataflowResults<O>(DataflowState<O>) where O: BitDenotation;
|
||||
|
||||
impl<O: BitDenotation> DataflowResults<O> {
|
||||
pub fn sets(&self) -> &AllSets {
|
||||
&self.0.sets
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: This type shouldn't be public, but the graphviz::MirWithFlowState trait
|
||||
// references it in a method signature. Look into using `pub(crate)` to address this.
|
||||
pub struct DataflowState<O: BitDenotation>
|
||||
|
1038
src/librustc_borrowck/borrowck/mir/elaborate_drops.rs
Normal file
1038
src/librustc_borrowck/borrowck/mir/elaborate_drops.rs
Normal file
File diff suppressed because it is too large
Load Diff
@ -24,8 +24,10 @@ use rustc::session::Session;
|
||||
use rustc::ty::{self, TyCtxt};
|
||||
|
||||
mod abs_domain;
|
||||
pub mod elaborate_drops;
|
||||
mod dataflow;
|
||||
mod gather_moves;
|
||||
mod patch;
|
||||
// mod graphviz;
|
||||
|
||||
use self::dataflow::{BitDenotation};
|
||||
@ -34,7 +36,7 @@ use self::dataflow::{Dataflow, DataflowAnalysis, DataflowResults};
|
||||
use self::dataflow::{MaybeInitializedLvals, MaybeUninitializedLvals};
|
||||
use self::dataflow::{DefinitelyInitializedLvals};
|
||||
use self::gather_moves::{MoveData, MovePathIndex, Location};
|
||||
use self::gather_moves::{MovePathContent};
|
||||
use self::gather_moves::{MovePathContent, MovePathData};
|
||||
|
||||
fn has_rustc_mir_with(attrs: &[ast::Attribute], name: &str) -> Option<P<MetaItem>> {
|
||||
for attr in attrs {
|
||||
@ -202,6 +204,37 @@ enum DropFlagState {
|
||||
Absent, // i.e. deinitialized or "moved"
|
||||
}
|
||||
|
||||
impl DropFlagState {
|
||||
fn value(self) -> bool {
|
||||
match self {
|
||||
DropFlagState::Live => true,
|
||||
DropFlagState::Dead => false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn move_path_children_matching<'tcx, F>(move_paths: &MovePathData<'tcx>,
|
||||
path: MovePathIndex,
|
||||
mut cond: F)
|
||||
-> Option<MovePathIndex>
|
||||
where F: FnMut(&repr::LvalueProjection<'tcx>) -> bool
|
||||
{
|
||||
let mut next_child = move_paths[path].first_child;
|
||||
while let Some(child_index) = next_child {
|
||||
match move_paths[child_index].content {
|
||||
MovePathContent::Lvalue(repr::Lvalue::Projection(ref proj)) => {
|
||||
if cond(proj) {
|
||||
return Some(child_index)
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
next_child = move_paths[child_index].next_sibling;
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn on_all_children_bits<'a, 'tcx, F>(
|
||||
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||
mir: &Mir<'tcx>,
|
||||
|
184
src/librustc_borrowck/borrowck/mir/patch.rs
Normal file
184
src/librustc_borrowck/borrowck/mir/patch.rs
Normal file
@ -0,0 +1,184 @@
|
||||
// Copyright 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.
|
||||
|
||||
use super::gather_moves::Location;
|
||||
use rustc::ty::Ty;
|
||||
use rustc::mir::repr::*;
|
||||
use syntax::codemap::Span;
|
||||
|
||||
use std::iter;
|
||||
use std::u32;
|
||||
|
||||
/// This struct represents a patch to MIR, which can add
|
||||
/// new statements and basic blocks and patch over block
|
||||
/// terminators.
|
||||
pub struct MirPatch<'tcx> {
|
||||
patch_map: Vec<Option<TerminatorKind<'tcx>>>,
|
||||
new_blocks: Vec<BasicBlockData<'tcx>>,
|
||||
new_statements: Vec<(Location, StatementKind<'tcx>)>,
|
||||
new_temps: Vec<TempDecl<'tcx>>,
|
||||
resume_block: BasicBlock,
|
||||
next_temp: u32,
|
||||
}
|
||||
|
||||
impl<'tcx> MirPatch<'tcx> {
|
||||
pub fn new(mir: &Mir<'tcx>) -> Self {
|
||||
let mut result = MirPatch {
|
||||
patch_map: iter::repeat(None)
|
||||
.take(mir.basic_blocks.len()).collect(),
|
||||
new_blocks: vec![],
|
||||
new_temps: vec![],
|
||||
new_statements: vec![],
|
||||
next_temp: mir.temp_decls.len() as u32,
|
||||
resume_block: START_BLOCK
|
||||
};
|
||||
|
||||
// make sure the MIR we create has a resume block. It is
|
||||
// completely legal to convert jumps to the resume block
|
||||
// to jumps to None, but we occasionally have to add
|
||||
// instructions just before that.
|
||||
|
||||
let mut resume_block = None;
|
||||
let mut resume_stmt_block = None;
|
||||
for block in mir.all_basic_blocks() {
|
||||
let data = mir.basic_block_data(block);
|
||||
if let TerminatorKind::Resume = data.terminator().kind {
|
||||
if data.statements.len() > 0 {
|
||||
resume_stmt_block = Some(block);
|
||||
} else {
|
||||
resume_block = Some(block);
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
let resume_block = resume_block.unwrap_or_else(|| {
|
||||
result.new_block(BasicBlockData {
|
||||
statements: vec![],
|
||||
terminator: Some(Terminator {
|
||||
span: mir.span,
|
||||
scope: ScopeId::new(0),
|
||||
kind: TerminatorKind::Resume
|
||||
}),
|
||||
is_cleanup: true
|
||||
})});
|
||||
result.resume_block = resume_block;
|
||||
if let Some(resume_stmt_block) = resume_stmt_block {
|
||||
result.patch_terminator(resume_stmt_block, TerminatorKind::Goto {
|
||||
target: resume_block
|
||||
});
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
pub fn resume_block(&self) -> BasicBlock {
|
||||
self.resume_block
|
||||
}
|
||||
|
||||
pub fn is_patched(&self, bb: BasicBlock) -> bool {
|
||||
self.patch_map[bb.index()].is_some()
|
||||
}
|
||||
|
||||
pub fn terminator_loc(&self, mir: &Mir<'tcx>, bb: BasicBlock) -> Location {
|
||||
let offset = match bb.index().checked_sub(mir.basic_blocks.len()) {
|
||||
Some(index) => self.new_blocks[index].statements.len(),
|
||||
None => mir.basic_block_data(bb).statements.len()
|
||||
};
|
||||
Location {
|
||||
block: bb,
|
||||
index: offset
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_temp(&mut self, ty: Ty<'tcx>) -> u32 {
|
||||
let index = self.next_temp;
|
||||
assert!(self.next_temp < u32::MAX);
|
||||
self.next_temp += 1;
|
||||
self.new_temps.push(TempDecl { ty: ty });
|
||||
index
|
||||
}
|
||||
|
||||
pub fn new_block(&mut self, data: BasicBlockData<'tcx>) -> BasicBlock {
|
||||
let block = BasicBlock::new(self.patch_map.len());
|
||||
debug!("MirPatch: new_block: {:?}: {:?}", block, data);
|
||||
self.new_blocks.push(data);
|
||||
self.patch_map.push(None);
|
||||
block
|
||||
}
|
||||
|
||||
pub fn patch_terminator(&mut self, block: BasicBlock, new: TerminatorKind<'tcx>) {
|
||||
assert!(self.patch_map[block.index()].is_none());
|
||||
debug!("MirPatch: patch_terminator({:?}, {:?})", block, new);
|
||||
self.patch_map[block.index()] = Some(new);
|
||||
}
|
||||
|
||||
pub fn add_statement(&mut self, loc: Location, stmt: StatementKind<'tcx>) {
|
||||
debug!("MirPatch: add_statement({:?}, {:?})", loc, stmt);
|
||||
self.new_statements.push((loc, stmt));
|
||||
}
|
||||
|
||||
pub fn add_assign(&mut self, loc: Location, lv: Lvalue<'tcx>, rv: Rvalue<'tcx>) {
|
||||
self.add_statement(loc, StatementKind::Assign(lv, rv));
|
||||
}
|
||||
|
||||
pub fn apply(self, mir: &mut Mir<'tcx>) {
|
||||
debug!("MirPatch: {:?} new temps, starting from index {}: {:?}",
|
||||
self.new_temps.len(), mir.temp_decls.len(), self.new_temps);
|
||||
debug!("MirPatch: {} new blocks, starting from index {}",
|
||||
self.new_blocks.len(), mir.basic_blocks.len());
|
||||
mir.basic_blocks.extend(self.new_blocks);
|
||||
mir.temp_decls.extend(self.new_temps);
|
||||
for (src, patch) in self.patch_map.into_iter().enumerate() {
|
||||
if let Some(patch) = patch {
|
||||
debug!("MirPatch: patching block {:?}", src);
|
||||
mir.basic_blocks[src].terminator_mut().kind = patch;
|
||||
}
|
||||
}
|
||||
|
||||
let mut new_statements = self.new_statements;
|
||||
new_statements.sort_by(|u,v| u.0.cmp(&v.0));
|
||||
|
||||
let mut delta = 0;
|
||||
let mut last_bb = START_BLOCK;
|
||||
for (mut loc, stmt) in new_statements {
|
||||
if loc.block != last_bb {
|
||||
delta = 0;
|
||||
last_bb = loc.block;
|
||||
}
|
||||
debug!("MirPatch: adding statement {:?} at loc {:?}+{}",
|
||||
stmt, loc, delta);
|
||||
loc.index += delta;
|
||||
let (span, scope) = Self::context_for_index(
|
||||
mir.basic_block_data(loc.block), loc
|
||||
);
|
||||
mir.basic_block_data_mut(loc.block).statements.insert(
|
||||
loc.index, Statement {
|
||||
span: span,
|
||||
scope: scope,
|
||||
kind: stmt
|
||||
});
|
||||
delta += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn context_for_index(data: &BasicBlockData, loc: Location) -> (Span, ScopeId) {
|
||||
match data.statements.get(loc.index) {
|
||||
Some(stmt) => (stmt.span, stmt.scope),
|
||||
None => (data.terminator().span, data.terminator().scope)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn context_for_location(&self, mir: &Mir, loc: Location) -> (Span, ScopeId) {
|
||||
let data = match loc.block.index().checked_sub(mir.basic_blocks.len()) {
|
||||
Some(new) => &self.new_blocks[new],
|
||||
None => mir.basic_block_data(loc.block)
|
||||
};
|
||||
Self::context_for_index(data, loc)
|
||||
}
|
||||
}
|
@ -18,6 +18,8 @@ pub use self::bckerr_code::*;
|
||||
pub use self::AliasableViolationKind::*;
|
||||
pub use self::MovedValueUseKind::*;
|
||||
|
||||
pub use self::mir::elaborate_drops::ElaborateDrops;
|
||||
|
||||
use self::InteriorKind::*;
|
||||
|
||||
use rustc::dep_graph::DepNode;
|
||||
|
@ -39,7 +39,7 @@ extern crate core; // for NonZero
|
||||
|
||||
pub use borrowck::check_crate;
|
||||
pub use borrowck::build_borrowck_dataflow_data_for_fn;
|
||||
pub use borrowck::{AnalysisData, BorrowckCtxt};
|
||||
pub use borrowck::{AnalysisData, BorrowckCtxt, ElaborateDrops};
|
||||
|
||||
// NB: This module needs to be declared first so diagnostics are
|
||||
// registered before they are used.
|
||||
|
@ -1033,6 +1033,10 @@ pub fn phase_4_translate_to_llvm<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||
passes.push_pass(box mir::transform::remove_dead_blocks::RemoveDeadBlocks);
|
||||
passes.push_pass(box mir::transform::erase_regions::EraseRegions);
|
||||
passes.push_pass(box mir::transform::break_cleanup_edges::BreakCleanupEdges);
|
||||
passes.push_pass(box borrowck::ElaborateDrops);
|
||||
passes.push_pass(box mir::transform::no_landing_pads::NoLandingPads);
|
||||
passes.push_pass(box mir::transform::simplify_cfg::SimplifyCfg);
|
||||
passes.push_pass(box mir::transform::break_cleanup_edges::BreakCleanupEdges);
|
||||
passes.run_passes(tcx, &mut mir_map);
|
||||
});
|
||||
|
||||
|
44
src/test/run-fail/issue-30380.rs
Normal file
44
src/test/run-fail/issue-30380.rs
Normal file
@ -0,0 +1,44 @@
|
||||
// Copyright 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.
|
||||
|
||||
// check that panics in destructors during assignment do not leave
|
||||
// destroyed values lying around for other destructors to observe.
|
||||
|
||||
// error-pattern:panicking destructors ftw!
|
||||
|
||||
struct Observer<'a>(&'a mut FilledOnDrop);
|
||||
|
||||
struct FilledOnDrop(u32);
|
||||
impl Drop for FilledOnDrop {
|
||||
fn drop(&mut self) {
|
||||
if self.0 == 0 {
|
||||
// this is only set during the destructor - safe
|
||||
// code should not be able to observe this.
|
||||
self.0 = 0x1c1c1c1c;
|
||||
panic!("panicking destructors ftw!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for Observer<'a> {
|
||||
fn drop(&mut self) {
|
||||
assert_eq!(self.0 .0, 1);
|
||||
}
|
||||
}
|
||||
|
||||
fn foo(b: &mut Observer) {
|
||||
*b.0 = FilledOnDrop(1);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut bomb = FilledOnDrop(0);
|
||||
let mut observer = Observer(&mut bomb);
|
||||
foo(&mut observer);
|
||||
}
|
100
src/test/run-pass/dynamic-drop.rs
Normal file
100
src/test/run-pass/dynamic-drop.rs
Normal file
@ -0,0 +1,100 @@
|
||||
// Copyright 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.
|
||||
|
||||
use std::cell::RefCell;
|
||||
|
||||
struct Allocator {
|
||||
data: RefCell<Vec<bool>>,
|
||||
}
|
||||
|
||||
impl Drop for Allocator {
|
||||
fn drop(&mut self) {
|
||||
let data = self.data.borrow();
|
||||
if data.iter().any(|d| *d) {
|
||||
panic!("missing free: {:?}", data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Allocator {
|
||||
fn new() -> Self { Allocator { data: RefCell::new(vec![]) } }
|
||||
fn alloc(&self) -> Ptr {
|
||||
let mut data = self.data.borrow_mut();
|
||||
let addr = data.len();
|
||||
data.push(true);
|
||||
Ptr(addr, self)
|
||||
}
|
||||
}
|
||||
|
||||
struct Ptr<'a>(usize, &'a Allocator);
|
||||
impl<'a> Drop for Ptr<'a> {
|
||||
fn drop(&mut self) {
|
||||
match self.1.data.borrow_mut()[self.0] {
|
||||
false => {
|
||||
panic!("double free at index {:?}", self.0)
|
||||
}
|
||||
ref mut d => *d = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn dynamic_init(a: &Allocator, c: bool) {
|
||||
let _x;
|
||||
if c {
|
||||
_x = Some(a.alloc());
|
||||
}
|
||||
}
|
||||
|
||||
fn dynamic_drop(a: &Allocator, c: bool) -> Option<Ptr> {
|
||||
let x = a.alloc();
|
||||
if c {
|
||||
Some(x)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn assignment2(a: &Allocator, c0: bool, c1: bool) {
|
||||
let mut _v = a.alloc();
|
||||
let mut _w = a.alloc();
|
||||
if c0 {
|
||||
drop(_v);
|
||||
}
|
||||
_v = _w;
|
||||
if c1 {
|
||||
_w = a.alloc();
|
||||
}
|
||||
}
|
||||
|
||||
fn assignment1(a: &Allocator, c0: bool) {
|
||||
let mut _v = a.alloc();
|
||||
let mut _w = a.alloc();
|
||||
if c0 {
|
||||
drop(_v);
|
||||
}
|
||||
_v = _w;
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let a = Allocator::new();
|
||||
dynamic_init(&a, false);
|
||||
dynamic_init(&a, true);
|
||||
dynamic_drop(&a, false);
|
||||
dynamic_drop(&a, true);
|
||||
|
||||
assignment2(&a, false, false);
|
||||
assignment2(&a, false, true);
|
||||
assignment2(&a, true, false);
|
||||
assignment2(&a, true, true);
|
||||
|
||||
assignment1(&a, false);
|
||||
assignment1(&a, true);
|
||||
}
|
Loading…
Reference in New Issue
Block a user