1 //! See docs in build/expr/mod.rs
3 use crate::build::expr::category::Category;
4 use crate::build::ForGuard::{OutsideGuard, RefWithinGuard};
5 use crate::build::{BlockAnd, BlockAndExtension, Builder};
7 use rustc_hir::def_id::DefId;
9 use rustc_middle::middle::region;
10 use rustc_middle::mir::AssertKind::BoundsCheck;
11 use rustc_middle::mir::*;
12 use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt, Variance};
15 use rustc_index::vec::Idx;
17 /// The "outermost" place that holds this value.
18 #[derive(Copy, Clone)]
20 /// Denotes the start of a `Place`.
23 /// When building place for an expression within a closure, the place might start off a
24 /// captured path. When `capture_disjoint_fields` is enabled, we might not know the capture
25 /// index (within the desugared closure) of the captured path until most of the projections
26 /// are applied. We use `PlaceBase::Upvar` to keep track of the root variable off of which the
27 /// captured path starts, the closure the capture belongs to and the trait the closure
30 /// Once we have figured out the capture index, we can convert the place builder to start from
31 /// `PlaceBase::Local`.
33 /// Consider the following example
35 /// let t = (10, (10, (10, 10)));
38 /// println!("{}", t.0.0.0);
41 /// Here the THIR expression for `t.0.0.0` will be something like
50 /// When `capture_disjoint_fields` is enabled, `t.0.0.0` is captured and we won't be able to
51 /// figure out that it is captured until all the `Field` projections are applied.
53 /// HirId of the upvar
55 /// DefId of the closure
56 closure_def_id: DefId,
57 /// The trait closure implements, `Fn`, `FnMut`, `FnOnce`
58 closure_kind: ty::ClosureKind },
61 /// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a
62 /// place by pushing more and more projections onto the end, and then convert the final set into a
63 /// place using the `into_place` method.
65 /// This is used internally when building a place for an expression like `a.b.c`. The fields `b`
66 /// and `c` can be progressively pushed onto the place builder that is created when converting `a`.
68 struct PlaceBuilder<'tcx> {
70 projection: Vec<PlaceElem<'tcx>>,
73 fn capture_matching_projections<'a, 'tcx>(
74 typeck_results: &'a ty::TypeckResults<'tcx>,
76 closure_def_id: DefId,
77 _projections: &Vec<PlaceElem<'tcx>>,
78 ) -> Option<(usize, ty::UpvarCapture<'tcx>)> {
82 .and_then(|captures| captures.get_full(&var_hir_id))
83 .and_then(|(capture_index, _, _)|{
84 let upvar_id = ty::UpvarId::new(var_hir_id, closure_def_id.expect_local());
85 let capture_kind = typeck_results.upvar_capture(upvar_id);
86 Some((capture_index, capture_kind))
90 /// Takes a PlaceBuilder and resolves the upvar (if any) within it,
91 /// so that the PlaceBuilder now starts from PlaceBase::Local.
93 /// Returns a Result with the error being the HirId of the
94 /// Upvar that was not found.
95 fn to_upvars_resolved_place_builder<'a, 'tcx>(
96 from_builder: PlaceBuilder<'tcx>,
98 typeck_results: &'a ty::TypeckResults<'tcx>,
99 ) -> Result<PlaceBuilder<'tcx>, HirId> {
100 match from_builder.base {
101 PlaceBase::Local(_) => Ok(from_builder),
102 PlaceBase::Upvar { var_hir_id, closure_def_id, closure_kind } => {
103 // Captures are represented using fields inside a structure.
104 // This represents accessing self in the closure structure
105 let mut upvar_resolved_place_builder = PlaceBuilder::from(Local::new(1));
107 ty::ClosureKind::Fn | ty::ClosureKind::FnMut => {
108 upvar_resolved_place_builder = upvar_resolved_place_builder.deref();
110 ty::ClosureKind::FnOnce => {}
113 let (capture_index, capture_kind) =
114 if let Some(capture_details) = capture_matching_projections(
118 &from_builder.projection,
122 if !tcx.features().capture_disjoint_fields {
124 "No associated capture found for {:?}[{:#?}] even though \
125 capture_disjoint_fields isn't enabled",
127 from_builder.projection
130 // FIXME(project-rfc-2229#24): Handle this case properly
132 "No associated capture found for {:?}[{:#?}]",
134 from_builder.projection,
137 return Err(var_hir_id);
141 typeck_results.node_type(tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local()));
143 let substs = match closure_ty.kind() {
144 ty::Closure(_, substs) => ty::UpvarSubsts::Closure(substs),
145 ty::Generator(_, substs, _) => ty::UpvarSubsts::Generator(substs),
146 _ => bug!("Lowering capture for non-closure type {:?}", closure_ty),
149 // Access the capture by accessing the field within the Closure struct.
151 // We must have inferred the capture types since we are building MIR, therefore
152 // it's safe to call `upvar_tys` and we can unwrap here because
153 // we know that the capture exists and is the `capture_index`-th capture.
154 let var_ty = substs.upvar_tys().nth(capture_index).unwrap();
156 upvar_resolved_place_builder = upvar_resolved_place_builder.field(Field::new(capture_index), var_ty);
158 // If the variable is captured via ByRef(Immutable/Mutable) Borrow,
159 // we need to deref it
160 upvar_resolved_place_builder = match capture_kind {
161 ty::UpvarCapture::ByRef(_) => upvar_resolved_place_builder.deref(),
162 ty::UpvarCapture::ByValue(_) => upvar_resolved_place_builder,
165 let next_projection = 0;
166 let mut curr_projections = from_builder.projection;
167 upvar_resolved_place_builder.projection.extend(
168 curr_projections.drain(next_projection..));
170 Ok(upvar_resolved_place_builder)
175 impl<'tcx> PlaceBuilder<'tcx> {
179 typeck_results: &'a ty::TypeckResults<'tcx>,
181 if let PlaceBase::Local(local) = self.base {
182 Place { local, projection: tcx.intern_place_elems(&self.projection) }
184 self.expect_upvars_resolved(tcx, typeck_results).into_place(tcx, typeck_results)
188 fn expect_upvars_resolved<'a>(
191 typeck_results: &'a ty::TypeckResults<'tcx>,
192 ) -> PlaceBuilder<'tcx> {
193 to_upvars_resolved_place_builder(self, tcx, typeck_results).unwrap()
196 fn field(self, f: Field, ty: Ty<'tcx>) -> Self {
197 self.project(PlaceElem::Field(f, ty))
200 fn deref(self) -> Self {
201 self.project(PlaceElem::Deref)
204 fn index(self, index: Local) -> Self {
205 self.project(PlaceElem::Index(index))
208 fn project(mut self, elem: PlaceElem<'tcx>) -> Self {
209 self.projection.push(elem);
214 impl<'tcx> From<Local> for PlaceBuilder<'tcx> {
215 fn from(local: Local) -> Self {
216 Self { base: PlaceBase::Local(local), projection: Vec::new() }
220 impl<'tcx> From<PlaceBase> for PlaceBuilder<'tcx> {
221 fn from(base: PlaceBase) -> Self {
222 Self { base, projection: Vec::new() }
226 impl<'a, 'tcx> Builder<'a, 'tcx> {
227 /// Compile `expr`, yielding a place that we can move from etc.
229 /// WARNING: Any user code might:
230 /// * Invalidate any slice bounds checks performed.
231 /// * Change the address that this `Place` refers to.
232 /// * Modify the memory that this place refers to.
233 /// * Invalidate the memory that this place refers to, this will be caught
234 /// by borrow checking.
236 /// Extra care is needed if any user code is allowed to run between calling
237 /// this method and using it, as is the case for `match` and index
239 crate fn as_place<M>(&mut self, mut block: BasicBlock, expr: M) -> BlockAnd<Place<'tcx>>
241 M: Mirror<'tcx, Output = Expr<'tcx>>,
243 let place_builder = unpack!(block = self.as_place_builder(block, expr));
244 block.and(place_builder.into_place(self.hir.tcx(), self.hir.typeck_results()))
247 /// This is used when constructing a compound `Place`, so that we can avoid creating
248 /// intermediate `Place` values until we know the full set of projections.
249 fn as_place_builder<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<PlaceBuilder<'tcx>>
251 M: Mirror<'tcx, Output = Expr<'tcx>>,
253 let expr = self.hir.mirror(expr);
254 self.expr_as_place(block, expr, Mutability::Mut, None)
257 /// Compile `expr`, yielding a place that we can move from etc.
258 /// Mutability note: The caller of this method promises only to read from the resulting
259 /// place. The place itself may or may not be mutable:
260 /// * If this expr is a place expr like a.b, then we will return that place.
261 /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
262 crate fn as_read_only_place<M>(
264 mut block: BasicBlock,
266 ) -> BlockAnd<Place<'tcx>>
268 M: Mirror<'tcx, Output = Expr<'tcx>>,
270 let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr));
271 block.and(place_builder.into_place(self.hir.tcx(), self.hir.typeck_results()))
274 /// This is used when constructing a compound `Place`, so that we can avoid creating
275 /// intermediate `Place` values until we know the full set of projections.
276 /// Mutability note: The caller of this method promises only to read from the resulting
277 /// place. The place itself may or may not be mutable:
278 /// * If this expr is a place expr like a.b, then we will return that place.
279 /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
280 fn as_read_only_place_builder<M>(
284 ) -> BlockAnd<PlaceBuilder<'tcx>>
286 M: Mirror<'tcx, Output = Expr<'tcx>>,
288 let expr = self.hir.mirror(expr);
289 self.expr_as_place(block, expr, Mutability::Not, None)
294 mut block: BasicBlock,
296 mutability: Mutability,
297 fake_borrow_temps: Option<&mut Vec<Local>>,
298 ) -> BlockAnd<PlaceBuilder<'tcx>> {
299 debug!("expr_as_place(block={:?}, expr={:?}, mutability={:?})", block, expr, mutability);
302 let expr_span = expr.span;
303 let source_info = this.source_info(expr_span);
305 ExprKind::Scope { region_scope, lint_level, value } => {
306 this.in_scope((region_scope, source_info), lint_level, |this| {
307 let value = this.hir.mirror(value);
308 this.expr_as_place(block, value, mutability, fake_borrow_temps)
311 ExprKind::Field { lhs, name } => {
312 let lhs = this.hir.mirror(lhs);
314 unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,));
315 block.and(place_builder.field(name, expr.ty))
317 ExprKind::Deref { arg } => {
318 let arg = this.hir.mirror(arg);
320 unpack!(block = this.expr_as_place(block, arg, mutability, fake_borrow_temps,));
321 block.and(place_builder.deref())
323 ExprKind::Index { lhs, index } => this.lower_index_expression(
333 ExprKind::UpvarRef { closure_def_id, var_hir_id } => {
334 let upvar_id = ty::UpvarId::new(var_hir_id, closure_def_id.expect_local());
335 this.lower_captured_upvar(block, upvar_id)
338 ExprKind::VarRef { id } => {
339 let place_builder = if this.is_bound_var_in_guard(id) {
340 let index = this.var_local_id(id, RefWithinGuard);
341 PlaceBuilder::from(index).deref()
343 let index = this.var_local_id(id, OutsideGuard);
344 PlaceBuilder::from(index)
346 block.and(place_builder)
349 ExprKind::PlaceTypeAscription { source, user_ty } => {
350 let source = this.hir.mirror(source);
351 let place_builder = unpack!(
352 block = this.expr_as_place(block, source, mutability, fake_borrow_temps,)
354 if let Some(user_ty) = user_ty {
355 let annotation_index =
356 this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
357 span: source_info.span,
359 inferred_ty: expr.ty,
363 place_builder.clone().into_place(this.hir.tcx(), this.hir.typeck_results());
368 kind: StatementKind::AscribeUserType(
371 UserTypeProjection { base: annotation_index, projs: vec![] },
378 block.and(place_builder)
380 ExprKind::ValueTypeAscription { source, user_ty } => {
381 let source = this.hir.mirror(source);
383 unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability));
384 if let Some(user_ty) = user_ty {
385 let annotation_index =
386 this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
387 span: source_info.span,
389 inferred_ty: expr.ty,
395 kind: StatementKind::AscribeUserType(
398 UserTypeProjection { base: annotation_index, projs: vec![] },
405 block.and(PlaceBuilder::from(temp))
408 ExprKind::Array { .. }
409 | ExprKind::Tuple { .. }
410 | ExprKind::Adt { .. }
411 | ExprKind::Closure { .. }
412 | ExprKind::Unary { .. }
413 | ExprKind::Binary { .. }
414 | ExprKind::LogicalOp { .. }
415 | ExprKind::Box { .. }
416 | ExprKind::Cast { .. }
417 | ExprKind::Use { .. }
418 | ExprKind::NeverToAny { .. }
419 | ExprKind::Pointer { .. }
420 | ExprKind::Repeat { .. }
421 | ExprKind::Borrow { .. }
422 | ExprKind::AddressOf { .. }
423 | ExprKind::Match { .. }
424 | ExprKind::Loop { .. }
425 | ExprKind::Block { .. }
426 | ExprKind::Assign { .. }
427 | ExprKind::AssignOp { .. }
428 | ExprKind::Break { .. }
429 | ExprKind::Continue { .. }
430 | ExprKind::Return { .. }
431 | ExprKind::Literal { .. }
432 | ExprKind::ConstBlock { .. }
433 | ExprKind::StaticRef { .. }
434 | ExprKind::InlineAsm { .. }
435 | ExprKind::LlvmInlineAsm { .. }
436 | ExprKind::Yield { .. }
437 | ExprKind::ThreadLocalRef(_)
438 | ExprKind::Call { .. } => {
439 // these are not places, so we need to make a temporary.
440 debug_assert!(!matches!(Category::of(&expr.kind), Some(Category::Place)));
442 unpack!(block = this.as_temp(block, expr.temp_lifetime, expr, mutability));
443 block.and(PlaceBuilder::from(temp))
448 /// Lower a captured upvar. Note we might not know the actual capture index,
449 /// so we create a place starting from `PlaceBase::Upvar`, which will be resolved
450 /// once all projections that allow us to indentify a capture have been applied.
451 fn lower_captured_upvar(
454 upvar_id: ty::UpvarId,
455 ) -> BlockAnd<PlaceBuilder<'tcx>> {
456 let closure_ty = self
459 .node_type(self.hir.tcx().hir().local_def_id_to_hir_id(upvar_id.closure_expr_id));
461 let closure_kind = if let ty::Closure(_, closure_substs) = closure_ty.kind() {
462 self.hir.infcx().closure_kind(closure_substs).unwrap()
464 // Generators are considered FnOnce.
465 ty::ClosureKind::FnOnce
468 block.and(PlaceBuilder::from(PlaceBase::Upvar {
469 var_hir_id: upvar_id.var_path.hir_id,
470 closure_def_id: upvar_id.closure_expr_id.to_def_id(),
475 /// Lower an index expression
477 /// This has two complications;
479 /// * We need to do a bounds check.
480 /// * We need to ensure that the bounds check can't be invalidated using an
481 /// expression like `x[1][{x = y; 2}]`. We use fake borrows here to ensure
482 /// that this is the case.
483 fn lower_index_expression(
485 mut block: BasicBlock,
487 index: ExprRef<'tcx>,
488 mutability: Mutability,
489 fake_borrow_temps: Option<&mut Vec<Local>>,
490 temp_lifetime: Option<region::Scope>,
492 source_info: SourceInfo,
493 ) -> BlockAnd<PlaceBuilder<'tcx>> {
494 let lhs = self.hir.mirror(base);
496 let base_fake_borrow_temps = &mut Vec::new();
497 let is_outermost_index = fake_borrow_temps.is_none();
498 let fake_borrow_temps = fake_borrow_temps.unwrap_or(base_fake_borrow_temps);
501 unpack!(block = self.expr_as_place(block, lhs, mutability, Some(fake_borrow_temps),));
503 // Making this a *fresh* temporary means we do not have to worry about
504 // the index changing later: Nothing will ever change this temporary.
505 // The "retagging" transformation (for Stacked Borrows) relies on this.
506 let idx = unpack!(block = self.as_temp(block, temp_lifetime, index, Mutability::Not,));
508 block = self.bounds_check(
510 base_place.clone().into_place(self.hir.tcx(), self.hir.typeck_results()),
516 if is_outermost_index {
517 self.read_fake_borrows(block, fake_borrow_temps, source_info)
519 base_place = base_place.expect_upvars_resolved(self.hir.tcx(), self.hir.typeck_results());
520 self.add_fake_borrows_of_base(
529 block.and(base_place.index(idx))
538 source_info: SourceInfo,
540 let usize_ty = self.hir.usize_ty();
541 let bool_ty = self.hir.bool_ty();
543 let len = self.temp(usize_ty, expr_span);
544 let lt = self.temp(bool_ty, expr_span);
547 self.cfg.push_assign(block, source_info, len, Rvalue::Len(slice));
549 self.cfg.push_assign(
553 Rvalue::BinaryOp(BinOp::Lt, Operand::Copy(Place::from(index)), Operand::Copy(len)),
555 let msg = BoundsCheck { len: Operand::Move(len), index: Operand::Copy(Place::from(index)) };
556 // assert!(lt, "...")
557 self.assert(block, Operand::Move(lt), true, msg, expr_span)
560 fn add_fake_borrows_of_base(
562 base_place: &PlaceBuilder<'tcx>,
564 fake_borrow_temps: &mut Vec<Local>,
566 source_info: SourceInfo,
568 let tcx = self.hir.tcx();
569 let local = match base_place.base {
570 PlaceBase::Local(local) => local,
571 PlaceBase::Upvar { .. } => bug!("Expected PlacseBase::Local found Upvar")
574 let place_ty = Place::ty_from(local, &base_place.projection, &self.local_decls, tcx);
575 if let ty::Slice(_) = place_ty.ty.kind() {
576 // We need to create fake borrows to ensure that the bounds
577 // check that we just did stays valid. Since we can't assign to
578 // unsized values, we only need to ensure that none of the
579 // pointers in the base place are modified.
580 for (idx, elem) in base_place.projection.iter().enumerate().rev() {
582 ProjectionElem::Deref => {
583 let fake_borrow_deref_ty = Place::ty_from(
585 &base_place.projection[..idx],
591 tcx.mk_imm_ref(tcx.lifetimes.re_erased, fake_borrow_deref_ty);
592 let fake_borrow_temp =
593 self.local_decls.push(LocalDecl::new(fake_borrow_ty, expr_span));
594 let projection = tcx.intern_place_elems(&base_place.projection[..idx]);
595 self.cfg.push_assign(
598 fake_borrow_temp.into(),
600 tcx.lifetimes.re_erased,
602 Place { local, projection },
605 fake_borrow_temps.push(fake_borrow_temp);
607 ProjectionElem::Index(_) => {
608 let index_ty = Place::ty_from(
610 &base_place.projection[..idx],
614 match index_ty.ty.kind() {
615 // The previous index expression has already
616 // done any index expressions needed here.
617 ty::Slice(_) => break,
619 _ => bug!("unexpected index base"),
622 ProjectionElem::Field(..)
623 | ProjectionElem::Downcast(..)
624 | ProjectionElem::ConstantIndex { .. }
625 | ProjectionElem::Subslice { .. } => (),
631 fn read_fake_borrows(
634 fake_borrow_temps: &mut Vec<Local>,
635 source_info: SourceInfo,
637 // All indexes have been evaluated now, read all of the
638 // fake borrows so that they are live across those index
640 for temp in fake_borrow_temps {
641 self.cfg.push_fake_read(bb, source_info, FakeReadCause::ForIndex, Place::from(*temp));