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::mir::interpret::{PanicMessage::BoundsCheck};
9 use rustc::ty::{CanonicalUserTypeAnnotation, Variance};
11 use rustc_data_structures::indexed_vec::Idx;
13 impl<'a, 'tcx> Builder<'a, 'tcx> {
14 /// Compile `expr`, yielding a place that we can move from etc.
15 pub fn as_place<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Place<'tcx>>
17 M: Mirror<'tcx, Output = Expr<'tcx>>,
19 let expr = self.hir.mirror(expr);
20 self.expr_as_place(block, expr, Mutability::Mut)
23 /// Compile `expr`, yielding a place that we can move from etc.
24 /// Mutability note: The caller of this method promises only to read from the resulting
25 /// place. The place itself may or may not be mutable:
26 /// * If this expr is a place expr like a.b, then we will return that place.
27 /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
28 pub fn as_read_only_place<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Place<'tcx>>
30 M: Mirror<'tcx, Output = Expr<'tcx>>,
32 let expr = self.hir.mirror(expr);
33 self.expr_as_place(block, expr, Mutability::Not)
38 mut block: BasicBlock,
40 mutability: Mutability,
41 ) -> BlockAnd<Place<'tcx>> {
43 "expr_as_place(block={:?}, expr={:?}, mutability={:?})",
44 block, expr, mutability
48 let expr_span = expr.span;
49 let source_info = this.source_info(expr_span);
55 } => this.in_scope((region_scope, source_info), lint_level, |this| {
56 if mutability == Mutability::Not {
57 this.as_read_only_place(block, value)
59 this.as_place(block, value)
62 ExprKind::Field { lhs, name } => {
63 let place = unpack!(block = this.as_place(block, lhs));
64 let place = place.field(name, expr.ty);
67 ExprKind::Deref { arg } => {
68 let place = unpack!(block = this.as_place(block, arg));
69 let place = place.deref();
72 ExprKind::Index { lhs, index } => {
73 let (usize_ty, bool_ty) = (this.hir.usize_ty(), this.hir.bool_ty());
75 let slice = unpack!(block = this.as_place(block, lhs));
76 // Making this a *fresh* temporary also means we do not have to worry about
77 // the index changing later: Nothing will ever change this temporary.
78 // The "retagging" transformation (for Stacked Borrows) relies on this.
79 let idx = unpack!(block = this.as_temp(
88 this.temp(usize_ty.clone(), expr_span),
89 this.temp(bool_ty, expr_span),
93 source_info, // len = len(slice)
95 Rvalue::Len(slice.clone()),
99 source_info, // lt = idx < len
103 Operand::Copy(Place::from(idx)),
104 Operand::Copy(len.clone()),
108 let msg = BoundsCheck {
109 len: Operand::Move(len),
110 index: Operand::Copy(Place::from(idx)),
112 let success = this.assert(block, Operand::Move(lt), true, msg, expr_span);
113 success.and(slice.index(idx))
115 ExprKind::SelfRef => block.and(Place::from(Local::new(1))),
116 ExprKind::VarRef { id } => {
117 let place = if this.is_bound_var_in_guard(id) {
118 let index = this.var_local_id(id, RefWithinGuard);
119 Place::from(index).deref()
121 let index = this.var_local_id(id, OutsideGuard);
126 ExprKind::StaticRef { id } => block.and(Place {
127 base: PlaceBase::Static(Box::new(Static {
129 kind: StaticKind::Static(id),
134 ExprKind::PlaceTypeAscription { source, user_ty } => {
135 let place = unpack!(block = this.as_place(block, source));
136 if let Some(user_ty) = user_ty {
137 let annotation_index = this.canonical_user_type_annotations.push(
138 CanonicalUserTypeAnnotation {
139 span: source_info.span,
141 inferred_ty: expr.ty,
148 kind: StatementKind::AscribeUserType(
151 box UserTypeProjection { base: annotation_index, projs: vec![], },
158 ExprKind::ValueTypeAscription { source, user_ty } => {
159 let source = this.hir.mirror(source);
161 block = this.as_temp(block, source.temp_lifetime, source, mutability)
163 if let Some(user_ty) = user_ty {
164 let annotation_index = this.canonical_user_type_annotations.push(
165 CanonicalUserTypeAnnotation {
166 span: source_info.span,
168 inferred_ty: expr.ty,
175 kind: StatementKind::AscribeUserType(
176 Place::from(temp.clone()),
178 box UserTypeProjection { base: annotation_index, projs: vec![], },
183 block.and(Place::from(temp))
186 ExprKind::Array { .. }
187 | ExprKind::Tuple { .. }
188 | ExprKind::Adt { .. }
189 | ExprKind::Closure { .. }
190 | ExprKind::Unary { .. }
191 | ExprKind::Binary { .. }
192 | ExprKind::LogicalOp { .. }
193 | ExprKind::Box { .. }
194 | ExprKind::Cast { .. }
195 | ExprKind::Use { .. }
196 | ExprKind::NeverToAny { .. }
197 | ExprKind::Pointer { .. }
198 | ExprKind::Repeat { .. }
199 | ExprKind::Borrow { .. }
200 | ExprKind::Match { .. }
201 | ExprKind::Loop { .. }
202 | ExprKind::Block { .. }
203 | ExprKind::Assign { .. }
204 | ExprKind::AssignOp { .. }
205 | ExprKind::Break { .. }
206 | ExprKind::Continue { .. }
207 | ExprKind::Return { .. }
208 | ExprKind::Literal { .. }
209 | ExprKind::InlineAsm { .. }
210 | ExprKind::Yield { .. }
211 | ExprKind::Call { .. } => {
212 // these are not places, so we need to make a temporary.
213 debug_assert!(match Category::of(&expr.kind) {
214 Some(Category::Place) => false,
218 unpack!(block = this.as_temp(block, expr.temp_lifetime, expr, mutability));
219 block.and(Place::from(temp))