Skip to content

Commit

Permalink
Move cached_block out of DropKind
Browse files Browse the repository at this point in the history
  • Loading branch information
tmandry committed May 30, 2019
1 parent 7d4d205 commit a09f605
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 50 deletions.
6 changes: 2 additions & 4 deletions src/librustc_mir/build/expr/as_temp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! See docs in build/expr/mod.rs

use crate::build::{BlockAnd, BlockAndExtension, Builder};
use crate::build::scope::{CachedBlock, DropKind};
use crate::build::scope::DropKind;
use crate::hair::*;
use rustc::middle::region;
use rustc::mir::*;
Expand Down Expand Up @@ -103,9 +103,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
temp_lifetime,
temp_place,
expr_ty,
DropKind::Value {
cached_block: CachedBlock::default(),
},
DropKind::Value,
);
}

Expand Down
8 changes: 3 additions & 5 deletions src/librustc_mir/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! This also includes code for pattern bindings in `let` statements and
//! function parameters.

use crate::build::scope::{CachedBlock, DropKind};
use crate::build::scope::DropKind;
use crate::build::ForGuard::{self, OutsideGuard, RefWithinGuard};
use crate::build::{BlockAnd, BlockAndExtension, Builder};
use crate::build::{GuardFrame, GuardFrameLocal, LocalsForNode};
Expand Down Expand Up @@ -544,7 +544,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
let place = Place::Base(PlaceBase::Local(local_id));
let var_ty = self.local_decls[local_id].ty;
let region_scope = self.hir.region_scope_tree.var_scope(var.local_id);
self.schedule_drop(span, region_scope, &place, var_ty, DropKind::Storage { cached_block: CachedBlock::default() });
self.schedule_drop(span, region_scope, &place, var_ty, DropKind::Storage);
place
}

Expand All @@ -557,9 +557,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
region_scope,
&Place::Base(PlaceBase::Local(local_id)),
var_ty,
DropKind::Value {
cached_block: CachedBlock::default(),
},
DropKind::Value,
);
}

Expand Down
5 changes: 2 additions & 3 deletions src/librustc_mir/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::build;
use crate::build::scope::{CachedBlock, DropKind};
use crate::build::scope::DropKind;
use crate::hair::cx::Cx;
use crate::hair::{LintLevel, BindingMode, PatternKind};
use crate::shim;
Expand Down Expand Up @@ -923,8 +923,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
// Make sure we drop (parts of) the argument even when not matched on.
self.schedule_drop(
pattern.as_ref().map_or(ast_body.span, |pat| pat.span),
argument_scope, &place, ty,
DropKind::Value { cached_block: CachedBlock::default() },
argument_scope, &place, ty, DropKind::Value,
);

if let Some(pattern) = pattern {
Expand Down
66 changes: 28 additions & 38 deletions src/librustc_mir/build/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,13 @@ struct DropData<'tcx> {

/// Whether this is a value Drop or a StorageDead.
kind: DropKind,

/// The cached blocks for unwinds.
cached_block: CachedBlock,
}

#[derive(Debug, Default, Clone, Copy)]
pub(crate) struct CachedBlock {
struct CachedBlock {
/// The cached block for the cleanups-on-diverge path. This block
/// contains code to run the current drop and all the preceding
/// drops (i.e., those having lower index in Drop’s Scope drop
Expand All @@ -164,8 +167,8 @@ pub(crate) struct CachedBlock {

#[derive(Debug)]
pub(crate) enum DropKind {
Value { cached_block: CachedBlock },
Storage { cached_block: CachedBlock },
Value,
Storage,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -208,8 +211,8 @@ impl CachedBlock {
impl DropKind {
fn may_panic(&self) -> bool {
match *self {
DropKind::Value { .. } => true,
DropKind::Storage { .. } => false
DropKind::Value => true,
DropKind::Storage => false
}
}
}
Expand Down Expand Up @@ -240,11 +243,7 @@ impl<'tcx> Scope<'tcx> {

if !ignore_unwinds && !this_scope_only {
for drop_data in &mut self.drops {
let cached_block = match drop_data.kind {
DropKind::Storage { ref mut cached_block } => cached_block,
DropKind::Value { ref mut cached_block } => cached_block,
};
cached_block.invalidate();
drop_data.cached_block.invalidate();
}
}
}
Expand Down Expand Up @@ -642,18 +641,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
place: &Place<'tcx>,
place_ty: Ty<'tcx>,
) {
self.schedule_drop(
span, region_scope, place, place_ty,
DropKind::Storage {
cached_block: CachedBlock::default(),
},
);
self.schedule_drop(
span, region_scope, place, place_ty,
DropKind::Value {
cached_block: CachedBlock::default(),
},
);
self.schedule_drop(span, region_scope, place, place_ty, DropKind::Storage);
self.schedule_drop(span, region_scope, place, place_ty, DropKind::Value);
}

// Scheduling drops
Expand All @@ -673,8 +662,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
) {
let needs_drop = self.hir.needs_drop(place_ty);
match drop_kind {
DropKind::Value { .. } => if !needs_drop { return },
DropKind::Storage { .. } => {
DropKind::Value => if !needs_drop { return },
DropKind::Storage => {
match *place {
Place::Base(PlaceBase::Local(index)) => if index.index() <= self.arg_count {
span_bug!(
Expand Down Expand Up @@ -740,7 +729,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
// cache of outer scpoe stays intact.
scope.invalidate_cache(!needs_drop, self.is_generator, this_scope);
if this_scope {
if let DropKind::Value { .. } = drop_kind {
if let DropKind::Value = drop_kind {
scope.needs_cleanup = true;
}

Expand All @@ -752,7 +741,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
scope.drops.push(DropData {
span: scope_end,
location: place.clone(),
kind: drop_kind
kind: drop_kind,
cached_block: CachedBlock::default(),
});
return;
}
Expand Down Expand Up @@ -984,7 +974,7 @@ fn build_scope_drops<'tcx>(
let drop_data = &scope.drops[drop_idx];
let source_info = scope.source_info(drop_data.span);
match drop_data.kind {
DropKind::Value { .. } => {
DropKind::Value => {
let unwind_to = get_unwind_to(scope, is_generator, drop_idx, generator_drop)
.unwrap_or(last_unwind_to);

Expand All @@ -996,7 +986,7 @@ fn build_scope_drops<'tcx>(
});
block = next;
}
DropKind::Storage { .. } => {
DropKind::Storage => {
// Drop the storage for both value and storage drops.
// Only temps and vars need their storage dead.
match drop_data.location {
Expand All @@ -1022,14 +1012,14 @@ fn get_unwind_to<'tcx>(
) -> Option<BasicBlock> {
for drop_idx in (0..unwind_from).rev() {
let drop_data = &scope.drops[drop_idx];
match drop_data.kind {
DropKind::Storage { cached_block } if is_generator => {
return Some(cached_block.get(generator_drop).unwrap_or_else(|| {
match (is_generator, &drop_data.kind) {
(true, DropKind::Storage) => {
return Some(drop_data.cached_block.get(generator_drop).unwrap_or_else(|| {
span_bug!(drop_data.span, "cached block not present for {:?}", drop_data)
}));
}
DropKind::Value { cached_block } if !is_generator => {
return Some(cached_block.get(generator_drop).unwrap_or_else(|| {
(false, DropKind::Value) => {
return Some(drop_data.cached_block.get(generator_drop).unwrap_or_else(|| {
span_bug!(drop_data.span, "cached block not present for {:?}", drop_data)
}));
}
Expand Down Expand Up @@ -1084,7 +1074,7 @@ fn build_diverge_scope<'tcx>(cfg: &mut CFG<'tcx>,
// match the behavior of clang, but on inspection eddyb says
// this is not what clang does.
match drop_data.kind {
DropKind::Storage { ref mut cached_block } if is_generator => {
DropKind::Storage if is_generator => {
// Only temps and vars need their storage dead.
match drop_data.location {
Place::Base(PlaceBase::Local(index)) => {
Expand All @@ -1105,11 +1095,11 @@ fn build_diverge_scope<'tcx>(cfg: &mut CFG<'tcx>,
}
_ => unreachable!(),
};
*cached_block.ref_mut(generator_drop) = Some(target);
*drop_data.cached_block.ref_mut(generator_drop) = Some(target);
}
DropKind::Storage { .. } => {}
DropKind::Value { ref mut cached_block } => {
let cached_block = cached_block.ref_mut(generator_drop);
DropKind::Storage => {}
DropKind::Value => {
let cached_block = drop_data.cached_block.ref_mut(generator_drop);
target = if let Some(cached_block) = *cached_block {
storage_deads.clear();
target_built_by_us = false;
Expand Down

0 comments on commit a09f605

Please sign in to comment.