2015-08-18 17:59:21 -04:00
|
|
|
//! See docs in build/expr/mod.rs
|
|
|
|
|
2019-02-08 06:28:15 +09:00
|
|
|
use crate::build::expr::category::Category;
|
|
|
|
use crate::build::ForGuard::{OutsideGuard, RefWithinGuard};
|
|
|
|
use crate::build::{BlockAnd, BlockAndExtension, Builder};
|
2020-07-21 09:09:27 +00:00
|
|
|
use crate::thir::*;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::middle::region;
|
|
|
|
use rustc_middle::mir::AssertKind::BoundsCheck;
|
|
|
|
use rustc_middle::mir::*;
|
|
|
|
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt, Variance};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::Span;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
use rustc_index::vec::Idx;
|
2016-06-07 17:28:36 +03:00
|
|
|
|
2019-09-30 13:09:10 -03:00
|
|
|
/// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a
|
|
|
|
/// place by pushing more and more projections onto the end, and then convert the final set into a
|
|
|
|
/// place using the `into_place` method.
|
|
|
|
///
|
|
|
|
/// This is used internally when building a place for an expression like `a.b.c`. The fields `b`
|
|
|
|
/// and `c` can be progressively pushed onto the place builder that is created when converting `a`.
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct PlaceBuilder<'tcx> {
|
2019-12-11 16:50:03 -03:00
|
|
|
local: Local,
|
2019-09-30 13:09:10 -03:00
|
|
|
projection: Vec<PlaceElem<'tcx>>,
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:46:44 +00:00
|
|
|
impl<'tcx> PlaceBuilder<'tcx> {
|
2019-10-20 16:11:04 -04:00
|
|
|
fn into_place(self, tcx: TyCtxt<'tcx>) -> Place<'tcx> {
|
2019-12-11 16:50:03 -03:00
|
|
|
Place { local: self.local, projection: tcx.intern_place_elems(&self.projection) }
|
2019-09-30 13:09:10 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn field(self, f: Field, ty: Ty<'tcx>) -> Self {
|
|
|
|
self.project(PlaceElem::Field(f, ty))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deref(self) -> Self {
|
|
|
|
self.project(PlaceElem::Deref)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn index(self, index: Local) -> Self {
|
|
|
|
self.project(PlaceElem::Index(index))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn project(mut self, elem: PlaceElem<'tcx>) -> Self {
|
|
|
|
self.projection.push(elem);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:46:44 +00:00
|
|
|
impl<'tcx> From<Local> for PlaceBuilder<'tcx> {
|
2019-09-30 13:09:10 -03:00
|
|
|
fn from(local: Local) -> Self {
|
2019-12-11 16:50:03 -03:00
|
|
|
Self { local, projection: Vec::new() }
|
2019-09-30 13:09:10 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 13:38:36 +02:00
|
|
|
impl<'a, 'tcx> Builder<'a, 'tcx> {
|
2017-12-01 14:39:51 +02:00
|
|
|
/// Compile `expr`, yielding a place that we can move from etc.
|
2019-10-19 21:00:21 +01:00
|
|
|
///
|
|
|
|
/// WARNING: Any user code might:
|
|
|
|
/// * Invalidate any slice bounds checks performed.
|
|
|
|
/// * Change the address that this `Place` refers to.
|
|
|
|
/// * Modify the memory that this place refers to.
|
|
|
|
/// * Invalidate the memory that this place refers to, this will be caught
|
|
|
|
/// by borrow checking.
|
|
|
|
///
|
|
|
|
/// Extra care is needed if any user code is allowed to run between calling
|
|
|
|
/// this method and using it, as is the case for `match` and index
|
|
|
|
/// expressions.
|
2020-01-05 15:46:44 +00:00
|
|
|
crate fn as_place<M>(&mut self, mut block: BasicBlock, expr: M) -> BlockAnd<Place<'tcx>>
|
2019-09-30 13:09:10 -03:00
|
|
|
where
|
|
|
|
M: Mirror<'tcx, Output = Expr<'tcx>>,
|
|
|
|
{
|
|
|
|
let place_builder = unpack!(block = self.as_place_builder(block, expr));
|
2019-10-20 16:11:04 -04:00
|
|
|
block.and(place_builder.into_place(self.hir.tcx()))
|
2019-09-30 13:09:10 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This is used when constructing a compound `Place`, so that we can avoid creating
|
|
|
|
/// intermediate `Place` values until we know the full set of projections.
|
|
|
|
fn as_place_builder<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<PlaceBuilder<'tcx>>
|
2018-09-06 22:34:26 +01:00
|
|
|
where
|
|
|
|
M: Mirror<'tcx, Output = Expr<'tcx>>,
|
2015-08-18 17:59:21 -04:00
|
|
|
{
|
|
|
|
let expr = self.hir.mirror(expr);
|
2019-10-19 21:00:21 +01:00
|
|
|
self.expr_as_place(block, expr, Mutability::Mut, None)
|
2018-09-05 23:49:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Compile `expr`, yielding a place that we can move from etc.
|
|
|
|
/// Mutability note: The caller of this method promises only to read from the resulting
|
|
|
|
/// place. The place itself may or may not be mutable:
|
|
|
|
/// * If this expr is a place expr like a.b, then we will return that place.
|
|
|
|
/// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
|
2020-01-05 15:46:44 +00:00
|
|
|
crate fn as_read_only_place<M>(
|
|
|
|
&mut self,
|
|
|
|
mut block: BasicBlock,
|
|
|
|
expr: M,
|
|
|
|
) -> BlockAnd<Place<'tcx>>
|
2019-09-30 13:09:10 -03:00
|
|
|
where
|
|
|
|
M: Mirror<'tcx, Output = Expr<'tcx>>,
|
|
|
|
{
|
|
|
|
let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr));
|
2019-10-20 16:11:04 -04:00
|
|
|
block.and(place_builder.into_place(self.hir.tcx()))
|
2019-09-30 13:09:10 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This is used when constructing a compound `Place`, so that we can avoid creating
|
|
|
|
/// intermediate `Place` values until we know the full set of projections.
|
|
|
|
/// Mutability note: The caller of this method promises only to read from the resulting
|
|
|
|
/// place. The place itself may or may not be mutable:
|
|
|
|
/// * If this expr is a place expr like a.b, then we will return that place.
|
|
|
|
/// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
|
|
|
|
fn as_read_only_place_builder<M>(
|
|
|
|
&mut self,
|
|
|
|
block: BasicBlock,
|
|
|
|
expr: M,
|
|
|
|
) -> BlockAnd<PlaceBuilder<'tcx>>
|
2018-09-06 22:34:26 +01:00
|
|
|
where
|
|
|
|
M: Mirror<'tcx, Output = Expr<'tcx>>,
|
2018-09-05 23:49:58 +01:00
|
|
|
{
|
|
|
|
let expr = self.hir.mirror(expr);
|
2019-10-19 21:00:21 +01:00
|
|
|
self.expr_as_place(block, expr, Mutability::Not, None)
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2018-09-06 22:34:26 +01:00
|
|
|
fn expr_as_place(
|
|
|
|
&mut self,
|
|
|
|
mut block: BasicBlock,
|
|
|
|
expr: Expr<'tcx>,
|
|
|
|
mutability: Mutability,
|
2019-10-19 21:00:21 +01:00
|
|
|
fake_borrow_temps: Option<&mut Vec<Local>>,
|
2019-09-30 13:09:10 -03:00
|
|
|
) -> BlockAnd<PlaceBuilder<'tcx>> {
|
2019-12-22 17:42:04 -05:00
|
|
|
debug!("expr_as_place(block={:?}, expr={:?}, mutability={:?})", block, expr, mutability);
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
let this = self;
|
|
|
|
let expr_span = expr.span;
|
2016-06-07 19:21:56 +03:00
|
|
|
let source_info = this.source_info(expr_span);
|
2015-08-18 17:59:21 -04:00
|
|
|
match expr.kind {
|
2019-12-22 17:42:04 -05:00
|
|
|
ExprKind::Scope { region_scope, lint_level, value } => {
|
|
|
|
this.in_scope((region_scope, source_info), lint_level, |this| {
|
|
|
|
let value = this.hir.mirror(value);
|
|
|
|
this.expr_as_place(block, value, mutability, fake_borrow_temps)
|
|
|
|
})
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
ExprKind::Field { lhs, name } => {
|
2019-10-19 21:00:21 +01:00
|
|
|
let lhs = this.hir.mirror(lhs);
|
2019-12-22 17:42:04 -05:00
|
|
|
let place_builder =
|
|
|
|
unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,));
|
2019-09-30 13:09:10 -03:00
|
|
|
block.and(place_builder.field(name, expr.ty))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
ExprKind::Deref { arg } => {
|
2019-10-19 21:00:21 +01:00
|
|
|
let arg = this.hir.mirror(arg);
|
2019-12-22 17:42:04 -05:00
|
|
|
let place_builder =
|
|
|
|
unpack!(block = this.expr_as_place(block, arg, mutability, fake_borrow_temps,));
|
2019-09-30 13:09:10 -03:00
|
|
|
block.and(place_builder.deref())
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
ExprKind::Index { lhs, index } => this.lower_index_expression(
|
|
|
|
block,
|
|
|
|
lhs,
|
|
|
|
index,
|
|
|
|
mutability,
|
|
|
|
fake_borrow_temps,
|
|
|
|
expr.temp_lifetime,
|
|
|
|
expr_span,
|
|
|
|
source_info,
|
|
|
|
),
|
2019-09-30 13:09:10 -03:00
|
|
|
ExprKind::SelfRef => block.and(PlaceBuilder::from(Local::new(1))),
|
2015-08-18 17:59:21 -04:00
|
|
|
ExprKind::VarRef { id } => {
|
2019-09-30 13:09:10 -03:00
|
|
|
let place_builder = if this.is_bound_var_in_guard(id) {
|
2018-07-26 13:14:12 +02:00
|
|
|
let index = this.var_local_id(id, RefWithinGuard);
|
2019-09-30 13:09:10 -03:00
|
|
|
PlaceBuilder::from(index).deref()
|
2018-02-26 15:45:13 +01:00
|
|
|
} else {
|
|
|
|
let index = this.var_local_id(id, OutsideGuard);
|
2019-09-30 13:09:10 -03:00
|
|
|
PlaceBuilder::from(index)
|
2018-02-26 15:45:13 +01:00
|
|
|
};
|
2019-09-30 13:09:10 -03:00
|
|
|
block.and(place_builder)
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2018-09-20 18:43:35 -07:00
|
|
|
ExprKind::PlaceTypeAscription { source, user_ty } => {
|
2019-10-19 21:00:21 +01:00
|
|
|
let source = this.hir.mirror(source);
|
2019-12-22 17:42:04 -05:00
|
|
|
let place_builder = unpack!(
|
|
|
|
block = this.expr_as_place(block, source, mutability, fake_borrow_temps,)
|
|
|
|
);
|
2018-10-17 18:23:43 -04:00
|
|
|
if let Some(user_ty) = user_ty {
|
2019-12-22 17:42:04 -05:00
|
|
|
let annotation_index =
|
|
|
|
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
|
2019-01-12 14:55:23 +00:00
|
|
|
span: source_info.span,
|
|
|
|
user_ty,
|
|
|
|
inferred_ty: expr.ty,
|
2019-12-22 17:42:04 -05:00
|
|
|
});
|
2019-09-30 13:09:10 -03:00
|
|
|
|
2019-10-20 16:11:04 -04:00
|
|
|
let place = place_builder.clone().into_place(this.hir.tcx());
|
2018-10-17 18:23:43 -04:00
|
|
|
this.cfg.push(
|
|
|
|
block,
|
|
|
|
Statement {
|
|
|
|
source_info,
|
|
|
|
kind: StatementKind::AscribeUserType(
|
2019-12-22 17:42:04 -05:00
|
|
|
box (
|
2019-09-30 13:09:10 -03:00
|
|
|
place,
|
2019-12-22 17:42:04 -05:00
|
|
|
UserTypeProjection { base: annotation_index, projs: vec![] },
|
2019-09-11 16:05:45 -03:00
|
|
|
),
|
2018-10-17 18:23:43 -04:00
|
|
|
Variance::Invariant,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2019-09-30 13:09:10 -03:00
|
|
|
block.and(place_builder)
|
2018-09-20 18:43:35 -07:00
|
|
|
}
|
|
|
|
ExprKind::ValueTypeAscription { source, user_ty } => {
|
|
|
|
let source = this.hir.mirror(source);
|
2019-12-22 17:42:04 -05:00
|
|
|
let temp =
|
|
|
|
unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability));
|
2018-10-17 18:23:43 -04:00
|
|
|
if let Some(user_ty) = user_ty {
|
2019-12-22 17:42:04 -05:00
|
|
|
let annotation_index =
|
|
|
|
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
|
2019-01-12 14:55:23 +00:00
|
|
|
span: source_info.span,
|
|
|
|
user_ty,
|
|
|
|
inferred_ty: expr.ty,
|
2019-12-22 17:42:04 -05:00
|
|
|
});
|
2018-10-17 18:23:43 -04:00
|
|
|
this.cfg.push(
|
|
|
|
block,
|
|
|
|
Statement {
|
|
|
|
source_info,
|
|
|
|
kind: StatementKind::AscribeUserType(
|
2019-12-22 17:42:04 -05:00
|
|
|
box (
|
2020-01-22 16:30:15 +01:00
|
|
|
Place::from(temp),
|
2019-12-22 17:42:04 -05:00
|
|
|
UserTypeProjection { base: annotation_index, projs: vec![] },
|
2019-09-11 16:05:45 -03:00
|
|
|
),
|
2018-10-17 18:23:43 -04:00
|
|
|
Variance::Invariant,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2019-09-30 13:09:10 -03:00
|
|
|
block.and(PlaceBuilder::from(temp))
|
2018-09-20 18:43:35 -07:00
|
|
|
}
|
|
|
|
|
2018-09-06 22:34:26 +01:00
|
|
|
ExprKind::Array { .. }
|
|
|
|
| ExprKind::Tuple { .. }
|
|
|
|
| ExprKind::Adt { .. }
|
|
|
|
| ExprKind::Closure { .. }
|
|
|
|
| ExprKind::Unary { .. }
|
|
|
|
| ExprKind::Binary { .. }
|
|
|
|
| ExprKind::LogicalOp { .. }
|
|
|
|
| ExprKind::Box { .. }
|
|
|
|
| ExprKind::Cast { .. }
|
|
|
|
| ExprKind::Use { .. }
|
|
|
|
| ExprKind::NeverToAny { .. }
|
2019-04-16 17:36:41 +05:30
|
|
|
| ExprKind::Pointer { .. }
|
2018-09-06 22:34:26 +01:00
|
|
|
| ExprKind::Repeat { .. }
|
|
|
|
| ExprKind::Borrow { .. }
|
2019-04-20 18:06:03 +01:00
|
|
|
| ExprKind::AddressOf { .. }
|
2018-09-06 22:34:26 +01:00
|
|
|
| ExprKind::Match { .. }
|
|
|
|
| ExprKind::Loop { .. }
|
|
|
|
| ExprKind::Block { .. }
|
|
|
|
| ExprKind::Assign { .. }
|
|
|
|
| ExprKind::AssignOp { .. }
|
|
|
|
| ExprKind::Break { .. }
|
|
|
|
| ExprKind::Continue { .. }
|
|
|
|
| ExprKind::Return { .. }
|
|
|
|
| ExprKind::Literal { .. }
|
2020-10-06 17:51:15 -03:00
|
|
|
| ExprKind::ConstBlock { .. }
|
2019-11-18 23:04:06 +00:00
|
|
|
| ExprKind::StaticRef { .. }
|
2020-02-14 18:17:50 +00:00
|
|
|
| ExprKind::InlineAsm { .. }
|
2020-01-14 13:40:42 +00:00
|
|
|
| ExprKind::LlvmInlineAsm { .. }
|
2018-09-06 22:34:26 +01:00
|
|
|
| ExprKind::Yield { .. }
|
2020-05-02 21:44:25 +02:00
|
|
|
| ExprKind::ThreadLocalRef(_)
|
2018-09-06 22:34:26 +01:00
|
|
|
| ExprKind::Call { .. } => {
|
2017-12-01 14:39:51 +02:00
|
|
|
// these are not places, so we need to make a temporary.
|
2015-08-18 17:59:21 -04:00
|
|
|
debug_assert!(match Category::of(&expr.kind) {
|
2017-12-01 14:31:47 +02:00
|
|
|
Some(Category::Place) => false,
|
2015-08-18 17:59:21 -04:00
|
|
|
_ => true,
|
|
|
|
});
|
2018-09-06 22:34:26 +01:00
|
|
|
let temp =
|
|
|
|
unpack!(block = this.as_temp(block, expr.temp_lifetime, expr, mutability));
|
2019-09-30 13:09:10 -03:00
|
|
|
block.and(PlaceBuilder::from(temp))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-19 21:00:21 +01:00
|
|
|
|
|
|
|
/// Lower an index expression
|
|
|
|
///
|
|
|
|
/// This has two complications;
|
|
|
|
///
|
|
|
|
/// * We need to do a bounds check.
|
|
|
|
/// * We need to ensure that the bounds check can't be invalidated using an
|
|
|
|
/// expression like `x[1][{x = y; 2}]`. We use fake borrows here to ensure
|
|
|
|
/// that this is the case.
|
|
|
|
fn lower_index_expression(
|
|
|
|
&mut self,
|
|
|
|
mut block: BasicBlock,
|
|
|
|
base: ExprRef<'tcx>,
|
|
|
|
index: ExprRef<'tcx>,
|
|
|
|
mutability: Mutability,
|
|
|
|
fake_borrow_temps: Option<&mut Vec<Local>>,
|
|
|
|
temp_lifetime: Option<region::Scope>,
|
|
|
|
expr_span: Span,
|
2019-12-22 17:42:04 -05:00
|
|
|
source_info: SourceInfo,
|
2019-10-19 21:00:21 +01:00
|
|
|
) -> BlockAnd<PlaceBuilder<'tcx>> {
|
|
|
|
let lhs = self.hir.mirror(base);
|
|
|
|
|
|
|
|
let base_fake_borrow_temps = &mut Vec::new();
|
|
|
|
let is_outermost_index = fake_borrow_temps.is_none();
|
|
|
|
let fake_borrow_temps = fake_borrow_temps.unwrap_or(base_fake_borrow_temps);
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let base_place =
|
|
|
|
unpack!(block = self.expr_as_place(block, lhs, mutability, Some(fake_borrow_temps),));
|
2019-10-19 21:00:21 +01:00
|
|
|
|
|
|
|
// Making this a *fresh* temporary means we do not have to worry about
|
|
|
|
// the index changing later: Nothing will ever change this temporary.
|
|
|
|
// The "retagging" transformation (for Stacked Borrows) relies on this.
|
2019-12-22 17:42:04 -05:00
|
|
|
let idx = unpack!(block = self.as_temp(block, temp_lifetime, index, Mutability::Not,));
|
2019-10-19 21:00:21 +01:00
|
|
|
|
|
|
|
block = self.bounds_check(
|
|
|
|
block,
|
|
|
|
base_place.clone().into_place(self.hir.tcx()),
|
|
|
|
idx,
|
|
|
|
expr_span,
|
|
|
|
source_info,
|
|
|
|
);
|
|
|
|
|
|
|
|
if is_outermost_index {
|
|
|
|
self.read_fake_borrows(block, fake_borrow_temps, source_info)
|
|
|
|
} else {
|
|
|
|
self.add_fake_borrows_of_base(
|
|
|
|
&base_place,
|
|
|
|
block,
|
|
|
|
fake_borrow_temps,
|
|
|
|
expr_span,
|
|
|
|
source_info,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
block.and(base_place.index(idx))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bounds_check(
|
|
|
|
&mut self,
|
|
|
|
block: BasicBlock,
|
|
|
|
slice: Place<'tcx>,
|
|
|
|
index: Local,
|
|
|
|
expr_span: Span,
|
|
|
|
source_info: SourceInfo,
|
|
|
|
) -> BasicBlock {
|
|
|
|
let usize_ty = self.hir.usize_ty();
|
|
|
|
let bool_ty = self.hir.bool_ty();
|
|
|
|
// bounds check:
|
|
|
|
let len = self.temp(usize_ty, expr_span);
|
|
|
|
let lt = self.temp(bool_ty, expr_span);
|
|
|
|
|
|
|
|
// len = len(slice)
|
2020-03-31 14:08:48 -03:00
|
|
|
self.cfg.push_assign(block, source_info, len, Rvalue::Len(slice));
|
2019-10-19 21:00:21 +01:00
|
|
|
// lt = idx < len
|
|
|
|
self.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
2020-03-31 14:08:48 -03:00
|
|
|
lt,
|
2020-01-22 16:30:15 +01:00
|
|
|
Rvalue::BinaryOp(BinOp::Lt, Operand::Copy(Place::from(index)), Operand::Copy(len)),
|
2019-10-19 21:00:21 +01:00
|
|
|
);
|
2019-12-22 17:42:04 -05:00
|
|
|
let msg = BoundsCheck { len: Operand::Move(len), index: Operand::Copy(Place::from(index)) };
|
2019-10-19 21:00:21 +01:00
|
|
|
// assert!(lt, "...")
|
|
|
|
self.assert(block, Operand::Move(lt), true, msg, expr_span)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_fake_borrows_of_base(
|
|
|
|
&mut self,
|
|
|
|
base_place: &PlaceBuilder<'tcx>,
|
|
|
|
block: BasicBlock,
|
|
|
|
fake_borrow_temps: &mut Vec<Local>,
|
|
|
|
expr_span: Span,
|
|
|
|
source_info: SourceInfo,
|
|
|
|
) {
|
|
|
|
let tcx = self.hir.tcx();
|
2019-12-22 17:42:04 -05:00
|
|
|
let place_ty =
|
2020-01-14 02:21:42 -03:00
|
|
|
Place::ty_from(base_place.local, &base_place.projection, &self.local_decls, tcx);
|
2020-08-03 00:49:11 +02:00
|
|
|
if let ty::Slice(_) = place_ty.ty.kind() {
|
2019-10-19 21:00:21 +01:00
|
|
|
// We need to create fake borrows to ensure that the bounds
|
|
|
|
// check that we just did stays valid. Since we can't assign to
|
|
|
|
// unsized values, we only need to ensure that none of the
|
|
|
|
// pointers in the base place are modified.
|
|
|
|
for (idx, elem) in base_place.projection.iter().enumerate().rev() {
|
|
|
|
match elem {
|
|
|
|
ProjectionElem::Deref => {
|
|
|
|
let fake_borrow_deref_ty = Place::ty_from(
|
2020-01-14 02:21:42 -03:00
|
|
|
base_place.local,
|
2019-10-19 21:00:21 +01:00
|
|
|
&base_place.projection[..idx],
|
|
|
|
&self.local_decls,
|
|
|
|
tcx,
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
|
|
|
.ty;
|
|
|
|
let fake_borrow_ty =
|
|
|
|
tcx.mk_imm_ref(tcx.lifetimes.re_erased, fake_borrow_deref_ty);
|
|
|
|
let fake_borrow_temp =
|
2020-05-06 10:17:38 +10:00
|
|
|
self.local_decls.push(LocalDecl::new(fake_borrow_ty, expr_span));
|
2019-10-19 21:00:21 +01:00
|
|
|
let projection = tcx.intern_place_elems(&base_place.projection[..idx]);
|
|
|
|
self.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
2020-03-31 14:08:48 -03:00
|
|
|
fake_borrow_temp.into(),
|
2019-10-19 21:00:21 +01:00
|
|
|
Rvalue::Ref(
|
|
|
|
tcx.lifetimes.re_erased,
|
|
|
|
BorrowKind::Shallow,
|
2020-01-22 16:30:15 +01:00
|
|
|
Place { local: base_place.local, projection },
|
2019-10-19 21:00:21 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
fake_borrow_temps.push(fake_borrow_temp);
|
|
|
|
}
|
|
|
|
ProjectionElem::Index(_) => {
|
|
|
|
let index_ty = Place::ty_from(
|
2020-01-14 02:21:42 -03:00
|
|
|
base_place.local,
|
2019-10-19 21:00:21 +01:00
|
|
|
&base_place.projection[..idx],
|
|
|
|
&self.local_decls,
|
|
|
|
tcx,
|
|
|
|
);
|
2020-08-03 00:49:11 +02:00
|
|
|
match index_ty.ty.kind() {
|
2019-10-19 21:00:21 +01:00
|
|
|
// The previous index expression has already
|
|
|
|
// done any index expressions needed here.
|
|
|
|
ty::Slice(_) => break,
|
|
|
|
ty::Array(..) => (),
|
|
|
|
_ => bug!("unexpected index base"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ProjectionElem::Field(..)
|
|
|
|
| ProjectionElem::Downcast(..)
|
|
|
|
| ProjectionElem::ConstantIndex { .. }
|
|
|
|
| ProjectionElem::Subslice { .. } => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_fake_borrows(
|
|
|
|
&mut self,
|
2019-12-15 16:11:01 +01:00
|
|
|
bb: BasicBlock,
|
2019-10-19 21:00:21 +01:00
|
|
|
fake_borrow_temps: &mut Vec<Local>,
|
|
|
|
source_info: SourceInfo,
|
|
|
|
) {
|
|
|
|
// All indexes have been evaluated now, read all of the
|
|
|
|
// fake borrows so that they are live across those index
|
|
|
|
// expressions.
|
|
|
|
for temp in fake_borrow_temps {
|
2019-12-15 16:11:01 +01:00
|
|
|
self.cfg.push_fake_read(bb, source_info, FakeReadCause::ForIndex, Place::from(*temp));
|
2019-10-19 21:00:21 +01:00
|
|
|
}
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|