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};
|
2022-08-23 22:22:15 +02:00
|
|
|
use crate::build::{BlockAnd, BlockAndExtension, Builder, Capture, CaptureMap};
|
2022-07-12 09:10:22 -05:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2022-05-23 00:00:00 +00:00
|
|
|
use rustc_middle::hir::place::Projection as HirProjection;
|
2020-11-26 00:05:18 -05:00
|
|
|
use rustc_middle::hir::place::ProjectionKind as HirProjectionKind;
|
2021-01-09 12:00:45 -05:00
|
|
|
use rustc_middle::middle::region;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::AssertKind::BoundsCheck;
|
|
|
|
use rustc_middle::mir::*;
|
2021-04-04 18:42:17 +02:00
|
|
|
use rustc_middle::thir::*;
|
2021-02-18 19:02:07 -05:00
|
|
|
use rustc_middle::ty::AdtDef;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt, Variance};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::Span;
|
2020-11-26 00:05:18 -05:00
|
|
|
use rustc_target::abi::VariantIdx;
|
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
|
|
|
|
2022-10-09 16:15:16 +00:00
|
|
|
use std::assert_matches::assert_matches;
|
2021-03-08 15:32:41 -08:00
|
|
|
use std::iter;
|
|
|
|
|
2022-12-19 15:30:43 +00:00
|
|
|
/// The "outermost" place that holds this value.
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
pub(crate) enum PlaceBase {
|
2020-11-18 22:54:31 -05:00
|
|
|
/// Denotes the start of a `Place`.
|
2022-12-19 15:30:43 +00:00
|
|
|
Local(Local),
|
2020-11-18 22:54:31 -05:00
|
|
|
|
|
|
|
/// When building place for an expression within a closure, the place might start off a
|
|
|
|
/// captured path. When `capture_disjoint_fields` is enabled, we might not know the capture
|
|
|
|
/// index (within the desugared closure) of the captured path until most of the projections
|
2022-12-19 15:30:43 +00:00
|
|
|
/// are applied. We use `PlaceBase::Upvar` to keep track of the root variable off of which the
|
2020-11-18 22:54:31 -05:00
|
|
|
/// captured path starts, the closure the capture belongs to and the trait the closure
|
|
|
|
/// implements.
|
|
|
|
///
|
2022-12-19 15:30:43 +00:00
|
|
|
/// Once we have figured out the capture index, we can convert the place builder to start from
|
|
|
|
/// `PlaceBase::Local`.
|
2020-11-18 22:54:31 -05:00
|
|
|
///
|
|
|
|
/// Consider the following example
|
|
|
|
/// ```rust
|
2022-04-15 15:04:34 -07:00
|
|
|
/// let t = (((10, 10), 10), 10);
|
2020-11-18 22:54:31 -05:00
|
|
|
///
|
|
|
|
/// let c = || {
|
|
|
|
/// println!("{}", t.0.0.0);
|
|
|
|
/// };
|
|
|
|
/// ```
|
|
|
|
/// Here the THIR expression for `t.0.0.0` will be something like
|
|
|
|
///
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```ignore (illustrative)
|
2020-11-18 22:54:31 -05:00
|
|
|
/// * Field(0)
|
|
|
|
/// * Field(0)
|
|
|
|
/// * Field(0)
|
|
|
|
/// * UpvarRef(t)
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// When `capture_disjoint_fields` is enabled, `t.0.0.0` is captured and we won't be able to
|
|
|
|
/// figure out that it is captured until all the `Field` projections are applied.
|
2022-12-19 15:30:43 +00:00
|
|
|
Upvar {
|
|
|
|
/// HirId of the upvar
|
|
|
|
var_hir_id: LocalVarId,
|
|
|
|
/// DefId of the closure
|
|
|
|
closure_def_id: LocalDefId,
|
|
|
|
},
|
2020-11-18 22:54:31 -05:00
|
|
|
}
|
|
|
|
|
2022-12-19 15:30:43 +00: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 `to_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, Debug, PartialEq)]
|
|
|
|
pub(in crate::build) struct PlaceBuilder<'tcx> {
|
|
|
|
base: PlaceBase,
|
|
|
|
projection: Vec<PlaceElem<'tcx>>,
|
2019-09-30 13:09:10 -03:00
|
|
|
}
|
|
|
|
|
2020-11-26 00:05:18 -05:00
|
|
|
/// Given a list of MIR projections, convert them to list of HIR ProjectionKind.
|
|
|
|
/// The projections are truncated to represent a path that might be captured by a
|
|
|
|
/// closure/generator. This implies the vector returned from this function doesn't contain
|
|
|
|
/// ProjectionElems `Downcast`, `ConstantIndex`, `Index`, or `Subslice` because those will never be
|
2021-04-19 15:57:08 +03:00
|
|
|
/// part of a path that is captured by a closure. We stop applying projections once we see the first
|
2020-11-26 00:05:18 -05:00
|
|
|
/// projection that isn't captured by a closure.
|
2022-12-20 22:10:40 +01:00
|
|
|
fn convert_to_hir_projections_and_truncate_for_capture(
|
|
|
|
mir_projections: &[PlaceElem<'_>],
|
2020-11-26 00:05:18 -05:00
|
|
|
) -> Vec<HirProjectionKind> {
|
2021-01-09 12:00:45 -05:00
|
|
|
let mut hir_projections = Vec::new();
|
2021-02-23 17:55:36 -05:00
|
|
|
let mut variant = None;
|
2020-11-26 00:05:18 -05:00
|
|
|
|
|
|
|
for mir_projection in mir_projections {
|
|
|
|
let hir_projection = match mir_projection {
|
|
|
|
ProjectionElem::Deref => HirProjectionKind::Deref,
|
|
|
|
ProjectionElem::Field(field, _) => {
|
2021-02-23 17:55:36 -05:00
|
|
|
let variant = variant.unwrap_or(VariantIdx::new(0));
|
|
|
|
HirProjectionKind::Field(field.index() as u32, variant)
|
2021-01-09 12:00:45 -05:00
|
|
|
}
|
2021-02-23 17:55:36 -05:00
|
|
|
ProjectionElem::Downcast(.., idx) => {
|
2021-02-25 18:03:41 -05:00
|
|
|
// We don't expect to see multi-variant enums here, as earlier
|
|
|
|
// phases will have truncated them already. However, there can
|
|
|
|
// still be downcasts, thanks to single-variant enums.
|
2021-02-23 17:55:36 -05:00
|
|
|
// We keep track of VariantIdx so we can use this information
|
2021-02-23 23:39:33 -05:00
|
|
|
// if the next ProjectionElem is a Field.
|
2021-02-23 17:55:36 -05:00
|
|
|
variant = Some(*idx);
|
|
|
|
continue;
|
2021-01-09 12:00:45 -05:00
|
|
|
}
|
2022-07-27 11:58:34 +00:00
|
|
|
// These do not affect anything, they just make sure we know the right type.
|
|
|
|
ProjectionElem::OpaqueCast(_) => continue,
|
2020-11-26 00:05:18 -05:00
|
|
|
ProjectionElem::Index(..)
|
|
|
|
| ProjectionElem::ConstantIndex { .. }
|
|
|
|
| ProjectionElem::Subslice { .. } => {
|
|
|
|
// We don't capture array-access projections.
|
|
|
|
// We can stop here as arrays are captured completely.
|
2021-01-09 12:00:45 -05:00
|
|
|
break;
|
|
|
|
}
|
2020-11-26 00:05:18 -05:00
|
|
|
};
|
2021-02-23 17:55:36 -05:00
|
|
|
variant = None;
|
2020-11-26 00:05:18 -05:00
|
|
|
hir_projections.push(hir_projection);
|
|
|
|
}
|
|
|
|
|
|
|
|
hir_projections
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if the `proj_possible_ancestor` represents an ancestor path
|
|
|
|
/// to `proj_capture` or `proj_possible_ancestor` is same as `proj_capture`,
|
|
|
|
/// assuming they both start off of the same root variable.
|
|
|
|
///
|
|
|
|
/// **Note:** It's the caller's responsibility to ensure that both lists of projections
|
|
|
|
/// start off of the same root variable.
|
|
|
|
///
|
|
|
|
/// Eg: 1. `foo.x` which is represented using `projections=[Field(x)]` is an ancestor of
|
|
|
|
/// `foo.x.y` which is represented using `projections=[Field(x), Field(y)]`.
|
|
|
|
/// Note both `foo.x` and `foo.x.y` start off of the same root variable `foo`.
|
|
|
|
/// 2. Since we only look at the projections here function will return `bar.x` as an a valid
|
|
|
|
/// ancestor of `foo.x.y`. It's the caller's responsibility to ensure that both projections
|
|
|
|
/// list are being applied to the same root variable.
|
|
|
|
fn is_ancestor_or_same_capture(
|
2022-06-03 20:42:42 +04:00
|
|
|
proj_possible_ancestor: &[HirProjectionKind],
|
2020-12-30 12:59:07 +01:00
|
|
|
proj_capture: &[HirProjectionKind],
|
2020-11-26 00:05:18 -05:00
|
|
|
) -> bool {
|
|
|
|
// We want to make sure `is_ancestor_or_same_capture("x.0.0", "x.0")` to return false.
|
|
|
|
// Therefore we can't just check if all projections are same in the zipped iterator below.
|
|
|
|
if proj_possible_ancestor.len() > proj_capture.len() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-08 15:32:41 -08:00
|
|
|
iter::zip(proj_possible_ancestor, proj_capture).all(|(a, b)| a == b)
|
2020-11-26 00:05:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a closure, returns the index of a capture within the desugared closure struct and the
|
|
|
|
/// `ty::CapturedPlace` which is the ancestor of the Place represented using the `var_hir_id`
|
|
|
|
/// and `projection`.
|
|
|
|
///
|
|
|
|
/// Note there will be at most one ancestor for any given Place.
|
|
|
|
///
|
|
|
|
/// Returns None, when the ancestor is not found.
|
|
|
|
fn find_capture_matching_projections<'a, 'tcx>(
|
2022-08-23 22:22:15 +02:00
|
|
|
upvars: &'a CaptureMap<'tcx>,
|
2022-06-08 20:23:07 +08:00
|
|
|
var_hir_id: LocalVarId,
|
2022-12-19 15:30:43 +00:00
|
|
|
projections: &[PlaceElem<'tcx>],
|
2022-08-23 22:22:15 +02:00
|
|
|
) -> Option<(usize, &'a Capture<'tcx>)> {
|
2020-11-26 00:05:18 -05:00
|
|
|
let hir_projections = convert_to_hir_projections_and_truncate_for_capture(projections);
|
|
|
|
|
2022-08-23 22:22:15 +02:00
|
|
|
upvars.get_by_key_enumerated(var_hir_id.0).find(|(_, capture)| {
|
2022-06-03 20:42:42 +04:00
|
|
|
let possible_ancestor_proj_kinds: Vec<_> =
|
2022-08-23 22:22:15 +02:00
|
|
|
capture.captured_place.place.projections.iter().map(|proj| proj.kind).collect();
|
2021-01-09 12:00:45 -05:00
|
|
|
is_ancestor_or_same_capture(&possible_ancestor_proj_kinds, &hir_projections)
|
2022-08-23 22:22:15 +02:00
|
|
|
})
|
2020-11-18 22:54:31 -05:00
|
|
|
}
|
|
|
|
|
2022-11-02 15:16:08 -05:00
|
|
|
/// Takes an upvar place and tries to resolve it into a `PlaceBuilder`
|
|
|
|
/// with `PlaceBase::Local`
|
2022-07-27 11:58:34 +00:00
|
|
|
#[instrument(level = "trace", skip(cx), ret)]
|
2022-09-13 08:51:45 +00:00
|
|
|
fn to_upvars_resolved_place_builder<'tcx>(
|
|
|
|
cx: &Builder<'_, 'tcx>,
|
2022-11-02 15:16:08 -05:00
|
|
|
var_hir_id: LocalVarId,
|
|
|
|
closure_def_id: LocalDefId,
|
2022-12-19 15:30:43 +00:00
|
|
|
projection: &[PlaceElem<'tcx>],
|
2022-11-02 15:16:08 -05:00
|
|
|
) -> Option<PlaceBuilder<'tcx>> {
|
|
|
|
let Some((capture_index, capture)) =
|
|
|
|
find_capture_matching_projections(
|
|
|
|
&cx.upvars,
|
|
|
|
var_hir_id,
|
|
|
|
&projection,
|
|
|
|
) else {
|
|
|
|
let closure_span = cx.tcx.def_span(closure_def_id);
|
|
|
|
if !enable_precise_capture(cx.tcx, closure_span) {
|
|
|
|
bug!(
|
|
|
|
"No associated capture found for {:?}[{:#?}] even though \
|
|
|
|
capture_disjoint_fields isn't enabled",
|
|
|
|
var_hir_id,
|
|
|
|
projection
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
debug!(
|
|
|
|
"No associated capture found for {:?}[{:#?}]",
|
|
|
|
var_hir_id, projection,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return None;
|
|
|
|
};
|
2020-11-18 22:54:31 -05:00
|
|
|
|
2022-11-02 15:16:08 -05:00
|
|
|
// Access the capture by accessing the field within the Closure struct.
|
|
|
|
let capture_info = &cx.upvars[capture_index];
|
2022-08-23 22:22:15 +02:00
|
|
|
|
2022-12-19 15:30:43 +00:00
|
|
|
let mut upvar_resolved_place_builder = PlaceBuilder::from(capture_info.use_place);
|
2020-11-18 22:54:31 -05:00
|
|
|
|
2022-11-02 15:16:08 -05:00
|
|
|
// We used some of the projections to build the capture itself,
|
|
|
|
// now we apply the remaining to the upvar resolved place.
|
2022-12-19 15:30:43 +00:00
|
|
|
trace!(?capture.captured_place, ?projection);
|
|
|
|
let remaining_projections = strip_prefix(
|
2022-11-02 15:16:08 -05:00
|
|
|
capture.captured_place.place.base_ty,
|
|
|
|
projection,
|
|
|
|
&capture.captured_place.place.projections,
|
2022-11-23 19:55:24 +01:00
|
|
|
);
|
2022-12-19 15:30:43 +00:00
|
|
|
upvar_resolved_place_builder.projection.extend(remaining_projections);
|
2022-11-23 19:55:24 +01:00
|
|
|
|
2022-11-02 15:16:08 -05:00
|
|
|
Some(upvar_resolved_place_builder)
|
2020-11-18 22:54:31 -05:00
|
|
|
}
|
|
|
|
|
2022-05-23 00:00:00 +00:00
|
|
|
/// Returns projections remaining after stripping an initial prefix of HIR
|
|
|
|
/// projections.
|
|
|
|
///
|
|
|
|
/// Supports only HIR projection kinds that represent a path that might be
|
|
|
|
/// captured by a closure or a generator, i.e., an `Index` or a `Subslice`
|
|
|
|
/// projection kinds are unsupported.
|
2022-11-02 15:16:08 -05:00
|
|
|
fn strip_prefix<'a, 'tcx>(
|
2022-05-23 00:00:00 +00:00
|
|
|
mut base_ty: Ty<'tcx>,
|
2022-12-19 15:30:43 +00:00
|
|
|
projections: &'a [PlaceElem<'tcx>],
|
2022-05-23 00:00:00 +00:00
|
|
|
prefix_projections: &[HirProjection<'tcx>],
|
2022-12-19 15:30:43 +00:00
|
|
|
) -> impl Iterator<Item = PlaceElem<'tcx>> + 'a {
|
2022-10-09 16:15:16 +00:00
|
|
|
let mut iter = projections
|
2022-11-02 15:16:08 -05:00
|
|
|
.iter()
|
2022-12-09 18:08:56 +01:00
|
|
|
.copied()
|
2022-07-27 11:58:34 +00:00
|
|
|
// Filter out opaque casts, they are unnecessary in the prefix.
|
2022-12-09 18:08:56 +01:00
|
|
|
.filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..)));
|
2022-05-23 00:00:00 +00:00
|
|
|
for projection in prefix_projections {
|
|
|
|
match projection.kind {
|
|
|
|
HirProjectionKind::Deref => {
|
2022-10-09 16:15:16 +00:00
|
|
|
assert_matches!(iter.next(), Some(ProjectionElem::Deref));
|
2022-05-23 00:00:00 +00:00
|
|
|
}
|
|
|
|
HirProjectionKind::Field(..) => {
|
|
|
|
if base_ty.is_enum() {
|
2022-10-09 16:15:16 +00:00
|
|
|
assert_matches!(iter.next(), Some(ProjectionElem::Downcast(..)));
|
2022-05-23 00:00:00 +00:00
|
|
|
}
|
2022-10-09 16:15:16 +00:00
|
|
|
assert_matches!(iter.next(), Some(ProjectionElem::Field(..)));
|
2022-05-23 00:00:00 +00:00
|
|
|
}
|
|
|
|
HirProjectionKind::Index | HirProjectionKind::Subslice => {
|
|
|
|
bug!("unexpected projection kind: {:?}", projection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
base_ty = projection.ty;
|
|
|
|
}
|
2022-11-29 13:42:52 +01:00
|
|
|
iter
|
2022-05-23 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:46:44 +00:00
|
|
|
impl<'tcx> PlaceBuilder<'tcx> {
|
2022-11-17 18:55:06 -06:00
|
|
|
pub(in crate::build) fn to_place(&self, cx: &Builder<'_, 'tcx>) -> Place<'tcx> {
|
|
|
|
self.try_to_place(cx).unwrap()
|
2022-11-02 15:16:08 -05:00
|
|
|
}
|
|
|
|
|
2022-11-17 18:52:28 -06:00
|
|
|
/// Creates a `Place` or returns `None` if an upvar cannot be resolved
|
|
|
|
pub(in crate::build) fn try_to_place(&self, cx: &Builder<'_, 'tcx>) -> Option<Place<'tcx>> {
|
|
|
|
let resolved = self.resolve_upvar(cx);
|
|
|
|
let builder = resolved.as_ref().unwrap_or(self);
|
2022-12-19 15:30:43 +00:00
|
|
|
let PlaceBase::Local(local) = builder.base else { return None };
|
2023-02-17 14:33:08 +11:00
|
|
|
let projection = cx.tcx.mk_place_elems(&builder.projection);
|
2022-12-19 15:30:43 +00:00
|
|
|
Some(Place { local, projection })
|
2019-09-30 13:09:10 -03:00
|
|
|
}
|
|
|
|
|
2021-02-25 18:03:41 -05:00
|
|
|
/// Attempts to resolve the `PlaceBuilder`.
|
2022-11-02 15:16:08 -05:00
|
|
|
/// Returns `None` if this is not an upvar.
|
2021-02-25 18:03:41 -05:00
|
|
|
///
|
|
|
|
/// Upvars resolve may fail for a `PlaceBuilder` when attempting to
|
|
|
|
/// resolve a disjoint field whose root variable is not captured
|
|
|
|
/// (destructured assignments) or when attempting to resolve a root
|
|
|
|
/// variable (discriminant matching with only wildcard arm) that is
|
|
|
|
/// not captured. This can happen because the final mir that will be
|
|
|
|
/// generated doesn't require a read for this place. Failures will only
|
|
|
|
/// happen inside closures.
|
2022-11-02 15:16:08 -05:00
|
|
|
pub(in crate::build) fn resolve_upvar(
|
|
|
|
&self,
|
2022-09-13 08:51:45 +00:00
|
|
|
cx: &Builder<'_, 'tcx>,
|
2022-11-02 15:16:08 -05:00
|
|
|
) -> Option<PlaceBuilder<'tcx>> {
|
2022-12-19 15:30:43 +00:00
|
|
|
let PlaceBase::Upvar { var_hir_id, closure_def_id } = self.base else {
|
2022-11-02 15:16:08 -05:00
|
|
|
return None;
|
|
|
|
};
|
2022-12-19 15:30:43 +00:00
|
|
|
to_upvars_resolved_place_builder(cx, var_hir_id, closure_def_id, &self.projection)
|
2020-11-26 00:07:41 -05:00
|
|
|
}
|
|
|
|
|
2022-12-19 15:30:43 +00:00
|
|
|
pub(crate) fn base(&self) -> PlaceBase {
|
|
|
|
self.base
|
|
|
|
}
|
2022-11-29 13:42:52 +01:00
|
|
|
|
2022-12-19 15:30:43 +00:00
|
|
|
pub(crate) fn projection(&self) -> &[PlaceElem<'tcx>] {
|
|
|
|
&self.projection
|
|
|
|
}
|
2022-12-09 18:08:56 +01:00
|
|
|
|
2022-12-19 15:30:43 +00:00
|
|
|
pub(crate) fn field(self, f: Field, ty: Ty<'tcx>) -> Self {
|
|
|
|
self.project(PlaceElem::Field(f, ty))
|
2019-09-30 13:09:10 -03:00
|
|
|
}
|
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn deref(self) -> Self {
|
2019-09-30 13:09:10 -03:00
|
|
|
self.project(PlaceElem::Deref)
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn downcast(self, adt_def: AdtDef<'tcx>, variant_index: VariantIdx) -> Self {
|
2022-03-05 07:28:41 +11:00
|
|
|
self.project(PlaceElem::Downcast(Some(adt_def.variant(variant_index).name), variant_index))
|
2021-02-18 19:02:07 -05:00
|
|
|
}
|
|
|
|
|
2019-09-30 13:09:10 -03:00
|
|
|
fn index(self, index: Local) -> Self {
|
|
|
|
self.project(PlaceElem::Index(index))
|
|
|
|
}
|
|
|
|
|
2022-12-19 15:30:43 +00:00
|
|
|
pub(crate) fn project(mut self, elem: PlaceElem<'tcx>) -> Self {
|
|
|
|
self.projection.push(elem);
|
|
|
|
self
|
2019-09-30 13:09:10 -03:00
|
|
|
}
|
2022-11-03 12:07:09 -05:00
|
|
|
|
|
|
|
/// Same as `.clone().project(..)` but more efficient
|
|
|
|
pub(crate) fn clone_project(&self, elem: PlaceElem<'tcx>) -> Self {
|
2022-12-19 15:30:43 +00:00
|
|
|
Self {
|
|
|
|
base: self.base,
|
|
|
|
projection: Vec::from_iter(self.projection.iter().copied().chain([elem])),
|
2022-11-03 12:07:09 -05:00
|
|
|
}
|
2022-11-02 13:37:37 +01:00
|
|
|
}
|
2019-09-30 13:09:10 -03:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-12-19 15:30:43 +00:00
|
|
|
Self { base: PlaceBase::Local(local), projection: Vec::new() }
|
2019-09-30 13:09:10 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-19 15:30:43 +00:00
|
|
|
impl<'tcx> From<PlaceBase> for PlaceBuilder<'tcx> {
|
|
|
|
fn from(base: PlaceBase) -> Self {
|
|
|
|
Self { base, projection: Vec::new() }
|
2022-11-29 13:42:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-19 15:30:43 +00:00
|
|
|
impl<'tcx> From<Place<'tcx>> for PlaceBuilder<'tcx> {
|
|
|
|
fn from(p: Place<'tcx>) -> Self {
|
|
|
|
Self { base: PlaceBase::Local(p.local), projection: p.projection.to_vec() }
|
2022-11-29 13:42:52 +01: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.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn as_place(
|
2021-02-24 21:29:09 +01:00
|
|
|
&mut self,
|
|
|
|
mut block: BasicBlock,
|
2021-04-03 19:58:46 +02:00
|
|
|
expr: &Expr<'tcx>,
|
2021-02-24 21:29:09 +01:00
|
|
|
) -> BlockAnd<Place<'tcx>> {
|
2019-09-30 13:09:10 -03:00
|
|
|
let place_builder = unpack!(block = self.as_place_builder(block, expr));
|
2022-11-17 18:55:06 -06:00
|
|
|
block.and(place_builder.to_place(self))
|
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.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn as_place_builder(
|
2021-01-09 12:00:45 -05:00
|
|
|
&mut self,
|
|
|
|
block: BasicBlock,
|
2021-04-03 19:58:46 +02:00
|
|
|
expr: &Expr<'tcx>,
|
2021-02-24 21:29:09 +01:00
|
|
|
) -> BlockAnd<PlaceBuilder<'tcx>> {
|
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.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn as_read_only_place(
|
2020-01-05 15:46:44 +00:00
|
|
|
&mut self,
|
|
|
|
mut block: BasicBlock,
|
2021-04-03 19:58:46 +02:00
|
|
|
expr: &Expr<'tcx>,
|
2021-02-24 21:29:09 +01:00
|
|
|
) -> BlockAnd<Place<'tcx>> {
|
2019-09-30 13:09:10 -03:00
|
|
|
let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr));
|
2022-11-17 18:55:06 -06:00
|
|
|
block.and(place_builder.to_place(self))
|
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.
|
2021-02-24 21:29:09 +01:00
|
|
|
fn as_read_only_place_builder(
|
2019-09-30 13:09:10 -03:00
|
|
|
&mut self,
|
|
|
|
block: BasicBlock,
|
2021-04-03 19:58:46 +02:00
|
|
|
expr: &Expr<'tcx>,
|
2021-02-24 21:29:09 +01:00
|
|
|
) -> BlockAnd<PlaceBuilder<'tcx>> {
|
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,
|
2021-04-03 19:58:46 +02:00
|
|
|
expr: &Expr<'tcx>,
|
2018-09-06 22:34:26 +01:00
|
|
|
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>> {
|
2022-12-19 15:30:43 +00: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);
|
2021-03-06 22:24:04 +01:00
|
|
|
match expr.kind {
|
2018-09-06 22:34:26 +01:00
|
|
|
ExprKind::Scope { region_scope, lint_level, value } => {
|
2021-03-06 22:24:04 +01:00
|
|
|
this.in_scope((region_scope, source_info), lint_level, |this| {
|
2021-04-03 19:58:46 +02:00
|
|
|
this.expr_as_place(block, &this.thir[value], mutability, fake_borrow_temps)
|
2018-09-06 22:34:26 +01:00
|
|
|
})
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2022-05-23 00:00:00 +00:00
|
|
|
ExprKind::Field { lhs, variant_index, name } => {
|
|
|
|
let lhs = &this.thir[lhs];
|
|
|
|
let mut place_builder =
|
|
|
|
unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,));
|
|
|
|
if let ty::Adt(adt_def, _) = lhs.ty.kind() {
|
|
|
|
if adt_def.is_enum() {
|
|
|
|
place_builder = place_builder.downcast(*adt_def, variant_index);
|
|
|
|
}
|
|
|
|
}
|
2022-12-19 15:30:43 +00:00
|
|
|
block.and(place_builder.field(name, expr.ty))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
ExprKind::Deref { arg } => {
|
2021-04-03 19:58:46 +02:00
|
|
|
let place_builder = unpack!(
|
|
|
|
block =
|
|
|
|
this.expr_as_place(block, &this.thir[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
|
|
|
}
|
|
|
|
ExprKind::Index { lhs, index } => this.lower_index_expression(
|
2019-07-07 15:03:07 +01:00
|
|
|
block,
|
2021-04-03 19:58:46 +02:00
|
|
|
&this.thir[lhs],
|
|
|
|
&this.thir[index],
|
2019-10-19 21:00:21 +01:00
|
|
|
mutability,
|
|
|
|
fake_borrow_temps,
|
|
|
|
expr.temp_lifetime,
|
|
|
|
expr_span,
|
|
|
|
source_info,
|
|
|
|
),
|
2020-11-17 01:52:14 -05:00
|
|
|
ExprKind::UpvarRef { closure_def_id, var_hir_id } => {
|
2022-06-08 20:23:07 +08:00
|
|
|
this.lower_captured_upvar(block, closure_def_id.expect_local(), var_hir_id)
|
2020-11-17 01:52:14 -05:00
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
ExprKind::VarRef { id } => {
|
2021-03-06 22:24:04 +01:00
|
|
|
let place_builder = if this.is_bound_var_in_guard(id) {
|
|
|
|
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 {
|
2021-03-06 22:24:04 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-24 10:37:41 +10:00
|
|
|
ExprKind::PlaceTypeAscription { source, ref user_ty } => {
|
2019-10-19 21:00:21 +01:00
|
|
|
let place_builder = unpack!(
|
2021-04-03 19:58:46 +02:00
|
|
|
block = this.expr_as_place(
|
|
|
|
block,
|
|
|
|
&this.thir[source],
|
|
|
|
mutability,
|
|
|
|
fake_borrow_temps,
|
|
|
|
)
|
2019-10-19 21:00:21 +01:00
|
|
|
);
|
2022-08-26 11:53:54 +10:00
|
|
|
if let Some(user_ty) = user_ty {
|
2018-11-16 22:56:18 +01:00
|
|
|
let annotation_index =
|
|
|
|
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
|
2019-01-12 14:55:23 +00:00
|
|
|
span: source_info.span,
|
2022-08-26 11:53:54 +10:00
|
|
|
user_ty: user_ty.clone(),
|
2019-01-12 14:55:23 +00:00
|
|
|
inferred_ty: expr.ty,
|
2018-11-16 22:56:18 +01:00
|
|
|
});
|
2019-09-30 13:09:10 -03:00
|
|
|
|
2022-11-17 18:55:06 -06:00
|
|
|
let place = place_builder.to_place(this);
|
2018-10-17 18:23:43 -04:00
|
|
|
this.cfg.push(
|
|
|
|
block,
|
|
|
|
Statement {
|
|
|
|
source_info,
|
|
|
|
kind: StatementKind::AscribeUserType(
|
2021-08-05 03:16:19 +02:00
|
|
|
Box::new((
|
2019-09-30 13:09:10 -03:00
|
|
|
place,
|
2019-09-11 16:05:45 -03:00
|
|
|
UserTypeProjection { base: annotation_index, projs: vec![] },
|
2021-08-05 03:16:19 +02: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
|
|
|
}
|
2022-08-24 10:37:41 +10:00
|
|
|
ExprKind::ValueTypeAscription { source, ref user_ty } => {
|
2021-04-03 19:58:46 +02:00
|
|
|
let source = &this.thir[source];
|
2018-09-20 18:43:35 -07:00
|
|
|
let temp =
|
2021-03-06 22:24:04 +01:00
|
|
|
unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability));
|
2022-08-26 11:53:54 +10:00
|
|
|
if let Some(user_ty) = user_ty {
|
2018-11-16 22:56:18 +01:00
|
|
|
let annotation_index =
|
|
|
|
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
|
2019-01-12 14:55:23 +00:00
|
|
|
span: source_info.span,
|
2022-08-26 11:53:54 +10:00
|
|
|
user_ty: user_ty.clone(),
|
2019-01-12 14:55:23 +00:00
|
|
|
inferred_ty: expr.ty,
|
2018-11-16 22:56:18 +01:00
|
|
|
});
|
2018-10-17 18:23:43 -04:00
|
|
|
this.cfg.push(
|
|
|
|
block,
|
|
|
|
Statement {
|
|
|
|
source_info,
|
|
|
|
kind: StatementKind::AscribeUserType(
|
2021-08-05 03:16:19 +02:00
|
|
|
Box::new((
|
2020-01-22 16:30:15 +01:00
|
|
|
Place::from(temp),
|
2019-09-11 16:05:45 -03:00
|
|
|
UserTypeProjection { base: annotation_index, projs: vec![] },
|
2021-08-05 03:16:19 +02: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 { .. }
|
2021-01-01 15:38:11 -03:00
|
|
|
| ExprKind::If { .. }
|
2018-09-06 22:34:26 +01:00
|
|
|
| ExprKind::Loop { .. }
|
|
|
|
| ExprKind::Block { .. }
|
2021-08-08 11:49:13 -03:00
|
|
|
| ExprKind::Let { .. }
|
2018-09-06 22:34:26 +01:00
|
|
|
| ExprKind::Assign { .. }
|
|
|
|
| ExprKind::AssignOp { .. }
|
|
|
|
| ExprKind::Break { .. }
|
|
|
|
| ExprKind::Continue { .. }
|
|
|
|
| ExprKind::Return { .. }
|
|
|
|
| ExprKind::Literal { .. }
|
2022-03-11 12:07:53 +01:00
|
|
|
| ExprKind::NamedConst { .. }
|
2022-03-23 08:47:11 +01:00
|
|
|
| ExprKind::NonHirLiteral { .. }
|
2022-07-03 11:17:23 -04:00
|
|
|
| ExprKind::ZstLiteral { .. }
|
2022-03-11 12:07:53 +01:00
|
|
|
| ExprKind::ConstParam { .. }
|
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 { .. }
|
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.
|
2020-10-26 21:02:48 -04:00
|
|
|
debug_assert!(!matches!(Category::of(&expr.kind), Some(Category::Place)));
|
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
|
|
|
|
2020-11-18 22:54:31 -05:00
|
|
|
/// Lower a captured upvar. Note we might not know the actual capture index,
|
2022-12-19 15:30:43 +00:00
|
|
|
/// so we create a place starting from `PlaceBase::Upvar`, which will be resolved
|
2021-04-19 15:57:08 +03:00
|
|
|
/// once all projections that allow us to identify a capture have been applied.
|
2020-11-18 22:54:31 -05:00
|
|
|
fn lower_captured_upvar(
|
2020-11-17 01:52:14 -05:00
|
|
|
&mut self,
|
|
|
|
block: BasicBlock,
|
2022-07-12 09:10:22 -05:00
|
|
|
closure_def_id: LocalDefId,
|
2022-06-08 20:23:07 +08:00
|
|
|
var_hir_id: LocalVarId,
|
2020-11-18 22:54:31 -05:00
|
|
|
) -> BlockAnd<PlaceBuilder<'tcx>> {
|
2022-12-19 15:30:43 +00:00
|
|
|
block.and(PlaceBuilder::from(PlaceBase::Upvar { var_hir_id, closure_def_id }))
|
2020-11-17 01:52:14 -05: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,
|
2021-04-03 19:58:46 +02:00
|
|
|
base: &Expr<'tcx>,
|
|
|
|
index: &Expr<'tcx>,
|
2019-10-19 21:00:21 +01:00
|
|
|
mutability: Mutability,
|
|
|
|
fake_borrow_temps: Option<&mut Vec<Local>>,
|
|
|
|
temp_lifetime: Option<region::Scope>,
|
|
|
|
expr_span: Span,
|
|
|
|
source_info: SourceInfo,
|
|
|
|
) -> BlockAnd<PlaceBuilder<'tcx>> {
|
|
|
|
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);
|
|
|
|
|
2022-11-17 18:55:06 -06:00
|
|
|
let base_place =
|
2021-02-24 21:29:09 +01:00
|
|
|
unpack!(block = self.expr_as_place(block, base, 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.
|
|
|
|
let idx = unpack!(block = self.as_temp(block, temp_lifetime, index, Mutability::Not,));
|
|
|
|
|
2022-11-17 18:55:06 -06:00
|
|
|
block = self.bounds_check(block, &base_place, idx, expr_span, source_info);
|
2019-10-19 21:00:21 +01:00
|
|
|
|
|
|
|
if is_outermost_index {
|
|
|
|
self.read_fake_borrows(block, fake_borrow_temps, source_info)
|
|
|
|
} else {
|
|
|
|
self.add_fake_borrows_of_base(
|
2022-11-17 18:55:06 -06:00
|
|
|
base_place.to_place(self),
|
2019-10-19 21:00:21 +01:00
|
|
|
block,
|
|
|
|
fake_borrow_temps,
|
|
|
|
expr_span,
|
|
|
|
source_info,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
block.and(base_place.index(idx))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bounds_check(
|
|
|
|
&mut self,
|
|
|
|
block: BasicBlock,
|
2022-11-17 18:55:06 -06:00
|
|
|
slice: &PlaceBuilder<'tcx>,
|
2019-10-19 21:00:21 +01:00
|
|
|
index: Local,
|
|
|
|
expr_span: Span,
|
|
|
|
source_info: SourceInfo,
|
|
|
|
) -> BasicBlock {
|
2021-03-03 16:35:54 +01:00
|
|
|
let usize_ty = self.tcx.types.usize;
|
|
|
|
let bool_ty = self.tcx.types.bool;
|
2019-10-19 21:00:21 +01:00
|
|
|
// bounds check:
|
|
|
|
let len = self.temp(usize_ty, expr_span);
|
|
|
|
let lt = self.temp(bool_ty, expr_span);
|
|
|
|
|
|
|
|
// len = len(slice)
|
2022-11-17 18:55:06 -06:00
|
|
|
self.cfg.push_assign(block, source_info, len, Rvalue::Len(slice.to_place(self)));
|
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,
|
2021-03-05 09:32:47 +00:00
|
|
|
Rvalue::BinaryOp(
|
|
|
|
BinOp::Lt,
|
2021-08-05 03:16:19 +02:00
|
|
|
Box::new((Operand::Copy(Place::from(index)), Operand::Copy(len))),
|
2021-03-05 09:32:47 +00:00
|
|
|
),
|
2019-10-19 21:00:21 +01:00
|
|
|
);
|
|
|
|
let msg = BoundsCheck { len: Operand::Move(len), index: Operand::Copy(Place::from(index)) };
|
|
|
|
// assert!(lt, "...")
|
|
|
|
self.assert(block, Operand::Move(lt), true, msg, expr_span)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_fake_borrows_of_base(
|
|
|
|
&mut self,
|
2022-11-17 18:55:06 -06:00
|
|
|
base_place: Place<'tcx>,
|
2019-10-19 21:00:21 +01:00
|
|
|
block: BasicBlock,
|
|
|
|
fake_borrow_temps: &mut Vec<Local>,
|
|
|
|
expr_span: Span,
|
|
|
|
source_info: SourceInfo,
|
|
|
|
) {
|
2021-03-03 16:35:54 +01:00
|
|
|
let tcx = self.tcx;
|
2022-11-21 20:56:27 +01:00
|
|
|
|
2022-12-19 15:30:43 +00:00
|
|
|
let place_ty = base_place.ty(&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(
|
2022-11-17 18:55:06 -06:00
|
|
|
base_place.local,
|
2019-10-19 21:00:21 +01:00
|
|
|
&base_place.projection[..idx],
|
|
|
|
&self.local_decls,
|
|
|
|
tcx,
|
|
|
|
)
|
|
|
|
.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));
|
2023-02-17 14:33:08 +11:00
|
|
|
let projection = tcx.mk_place_elems(&base_place.projection[..idx]);
|
2019-10-19 21:00:21 +01:00
|
|
|
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,
|
2022-11-17 18:55:06 -06: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(
|
2022-11-17 18:55:06 -06: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(..)
|
2022-07-27 11:58:34 +00:00
|
|
|
| ProjectionElem::OpaqueCast(..)
|
2019-10-19 21:00:21 +01:00
|
|
|
| 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
|
|
|
}
|
2021-06-27 02:22:46 -04:00
|
|
|
|
|
|
|
/// Precise capture is enabled if the feature gate `capture_disjoint_fields` is enabled or if
|
|
|
|
/// user is using Rust Edition 2021 or higher.
|
|
|
|
fn enable_precise_capture(tcx: TyCtxt<'_>, closure_span: Span) -> bool {
|
|
|
|
tcx.features().capture_disjoint_fields || closure_span.rust_2021()
|
|
|
|
}
|