]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/expr/as_place.rs
61c57f792c3bafaeea09d208e6d4fc5fb84c58d7
[rust.git] / src / librustc_mir / build / expr / as_place.rs
1 //! See docs in build/expr/mod.rs
2
3 use crate::build::expr::category::Category;
4 use crate::build::ForGuard::{OutsideGuard, RefWithinGuard};
5 use crate::build::{BlockAnd, BlockAndExtension, Builder};
6 use crate::hair::*;
7 use rustc::mir::interpret::{InterpError::Panic, PanicMessage::BoundsCheck};
8 use rustc::mir::*;
9 use rustc::ty::{CanonicalUserTypeAnnotation, Variance};
10
11 use rustc_data_structures::indexed_vec::Idx;
12
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>>
16     where
17         M: Mirror<'tcx, Output = Expr<'tcx>>,
18     {
19         let expr = self.hir.mirror(expr);
20         self.expr_as_place(block, expr, Mutability::Mut)
21     }
22
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>>
29     where
30         M: Mirror<'tcx, Output = Expr<'tcx>>,
31     {
32         let expr = self.hir.mirror(expr);
33         self.expr_as_place(block, expr, Mutability::Not)
34     }
35
36     fn expr_as_place(
37         &mut self,
38         mut block: BasicBlock,
39         expr: Expr<'tcx>,
40         mutability: Mutability,
41     ) -> BlockAnd<Place<'tcx>> {
42         debug!(
43             "expr_as_place(block={:?}, expr={:?}, mutability={:?})",
44             block, expr, mutability
45         );
46
47         let this = self;
48         let expr_span = expr.span;
49         let source_info = this.source_info(expr_span);
50         match expr.kind {
51             ExprKind::Scope {
52                 region_scope,
53                 lint_level,
54                 value,
55             } => this.in_scope((region_scope, source_info), lint_level, |this| {
56                 if mutability == Mutability::Not {
57                     this.as_read_only_place(block, value)
58                 } else {
59                     this.as_place(block, value)
60                 }
61             }),
62             ExprKind::Field { lhs, name } => {
63                 let place = unpack!(block = this.as_place(block, lhs));
64                 let place = place.field(name, expr.ty);
65                 block.and(place)
66             }
67             ExprKind::Deref { arg } => {
68                 let place = unpack!(block = this.as_place(block, arg));
69                 let place = place.deref();
70                 block.and(place)
71             }
72             ExprKind::Index { lhs, index } => {
73                 let (usize_ty, bool_ty) = (this.hir.usize_ty(), this.hir.bool_ty());
74
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(
80                     block,
81                     expr.temp_lifetime,
82                     index,
83                     Mutability::Not,
84                 ));
85
86                 // bounds check:
87                 let (len, lt) = (
88                     this.temp(usize_ty.clone(), expr_span),
89                     this.temp(bool_ty, expr_span),
90                 );
91                 this.cfg.push_assign(
92                     block,
93                     source_info, // len = len(slice)
94                     &len,
95                     Rvalue::Len(slice.clone()),
96                 );
97                 this.cfg.push_assign(
98                     block,
99                     source_info, // lt = idx < len
100                     &lt,
101                     Rvalue::BinaryOp(
102                         BinOp::Lt,
103                         Operand::Copy(Place::from(idx)),
104                         Operand::Copy(len.clone()),
105                     ),
106                 );
107
108                 let msg = Panic(BoundsCheck {
109                     len: Operand::Move(len),
110                     index: Operand::Copy(Place::from(idx)),
111                 });
112                 let success = this.assert(block, Operand::Move(lt), true, msg, expr_span);
113                 success.and(slice.index(idx))
114             }
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()
120                 } else {
121                     let index = this.var_local_id(id, OutsideGuard);
122                     Place::from(index)
123                 };
124                 block.and(place)
125             }
126             ExprKind::StaticRef { id } => block.and(Place::Base(PlaceBase::Static(Box::new(Static {
127                 ty: expr.ty,
128                 kind: StaticKind::Static(id),
129             })))),
130
131             ExprKind::PlaceTypeAscription { source, user_ty } => {
132                 let place = unpack!(block = this.as_place(block, source));
133                 if let Some(user_ty) = user_ty {
134                     let annotation_index = this.canonical_user_type_annotations.push(
135                         CanonicalUserTypeAnnotation {
136                             span: source_info.span,
137                             user_ty,
138                             inferred_ty: expr.ty,
139                         }
140                     );
141                     this.cfg.push(
142                         block,
143                         Statement {
144                             source_info,
145                             kind: StatementKind::AscribeUserType(
146                                 place.clone(),
147                                 Variance::Invariant,
148                                 box UserTypeProjection { base: annotation_index, projs: vec![], },
149                             ),
150                         },
151                     );
152                 }
153                 block.and(place)
154             }
155             ExprKind::ValueTypeAscription { source, user_ty } => {
156                 let source = this.hir.mirror(source);
157                 let temp = unpack!(
158                     block = this.as_temp(block, source.temp_lifetime, source, mutability)
159                 );
160                 if let Some(user_ty) = user_ty {
161                     let annotation_index = this.canonical_user_type_annotations.push(
162                         CanonicalUserTypeAnnotation {
163                             span: source_info.span,
164                             user_ty,
165                             inferred_ty: expr.ty,
166                         }
167                     );
168                     this.cfg.push(
169                         block,
170                         Statement {
171                             source_info,
172                             kind: StatementKind::AscribeUserType(
173                                 Place::from(temp.clone()),
174                                 Variance::Invariant,
175                                 box UserTypeProjection { base: annotation_index, projs: vec![], },
176                             ),
177                         },
178                     );
179                 }
180                 block.and(Place::from(temp))
181             }
182
183             ExprKind::Array { .. }
184             | ExprKind::Tuple { .. }
185             | ExprKind::Adt { .. }
186             | ExprKind::Closure { .. }
187             | ExprKind::Unary { .. }
188             | ExprKind::Binary { .. }
189             | ExprKind::LogicalOp { .. }
190             | ExprKind::Box { .. }
191             | ExprKind::Cast { .. }
192             | ExprKind::Use { .. }
193             | ExprKind::NeverToAny { .. }
194             | ExprKind::Pointer { .. }
195             | ExprKind::Repeat { .. }
196             | ExprKind::Borrow { .. }
197             | ExprKind::Match { .. }
198             | ExprKind::Loop { .. }
199             | ExprKind::Block { .. }
200             | ExprKind::Assign { .. }
201             | ExprKind::AssignOp { .. }
202             | ExprKind::Break { .. }
203             | ExprKind::Continue { .. }
204             | ExprKind::Return { .. }
205             | ExprKind::Literal { .. }
206             | ExprKind::InlineAsm { .. }
207             | ExprKind::Yield { .. }
208             | ExprKind::Call { .. } => {
209                 // these are not places, so we need to make a temporary.
210                 debug_assert!(match Category::of(&expr.kind) {
211                     Some(Category::Place) => false,
212                     _ => true,
213                 });
214                 let temp =
215                     unpack!(block = this.as_temp(block, expr.temp_lifetime, expr, mutability));
216                 block.and(Place::from(temp))
217             }
218         }
219     }
220 }