]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/expr/as_place.rs
Rollup merge of #61447 - scottmcm:vec-vecdeque, r=sfackler
[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::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                 // region_scope=None so place indexes live forever. They are scalars so they
77                 // do not need storage annotations, and they are often copied between
78                 // places.
79                 // Making this a *fresh* temporary also means we do not have to worry about
80                 // the index changing later: Nothing will ever change this temporary.
81                 // The "retagging" transformation (for Stacked Borrows) relies on this.
82                 let idx = unpack!(block = this.as_temp(block, None, index, Mutability::Mut));
83
84                 // bounds check:
85                 let (len, lt) = (
86                     this.temp(usize_ty.clone(), expr_span),
87                     this.temp(bool_ty, expr_span),
88                 );
89                 this.cfg.push_assign(
90                     block,
91                     source_info, // len = len(slice)
92                     &len,
93                     Rvalue::Len(slice.clone()),
94                 );
95                 this.cfg.push_assign(
96                     block,
97                     source_info, // lt = idx < len
98                     &lt,
99                     Rvalue::BinaryOp(
100                         BinOp::Lt,
101                         Operand::Copy(Place::Base(PlaceBase::Local(idx))),
102                         Operand::Copy(len.clone()),
103                     ),
104                 );
105
106                 let msg = BoundsCheck {
107                     len: Operand::Move(len),
108                     index: Operand::Copy(Place::Base(PlaceBase::Local(idx))),
109                 };
110                 let success = this.assert(block, Operand::Move(lt), true, msg, expr_span);
111                 success.and(slice.index(idx))
112             }
113             ExprKind::SelfRef => block.and(Place::Base(PlaceBase::Local(Local::new(1)))),
114             ExprKind::VarRef { id } => {
115                 let place = if this.is_bound_var_in_guard(id) {
116                     let index = this.var_local_id(id, RefWithinGuard);
117                     Place::Base(PlaceBase::Local(index)).deref()
118                 } else {
119                     let index = this.var_local_id(id, OutsideGuard);
120                     Place::Base(PlaceBase::Local(index))
121                 };
122                 block.and(place)
123             }
124             ExprKind::StaticRef { id } => block.and(Place::Base(PlaceBase::Static(Box::new(Static {
125                 ty: expr.ty,
126                 kind: StaticKind::Static(id),
127             })))),
128
129             ExprKind::PlaceTypeAscription { source, user_ty } => {
130                 let place = unpack!(block = this.as_place(block, source));
131                 if let Some(user_ty) = user_ty {
132                     let annotation_index = this.canonical_user_type_annotations.push(
133                         CanonicalUserTypeAnnotation {
134                             span: source_info.span,
135                             user_ty,
136                             inferred_ty: expr.ty,
137                         }
138                     );
139                     this.cfg.push(
140                         block,
141                         Statement {
142                             source_info,
143                             kind: StatementKind::AscribeUserType(
144                                 place.clone(),
145                                 Variance::Invariant,
146                                 box UserTypeProjection { base: annotation_index, projs: vec![], },
147                             ),
148                         },
149                     );
150                 }
151                 block.and(place)
152             }
153             ExprKind::ValueTypeAscription { source, user_ty } => {
154                 let source = this.hir.mirror(source);
155                 let temp = unpack!(
156                     block = this.as_temp(block, source.temp_lifetime, source, mutability)
157                 );
158                 if let Some(user_ty) = user_ty {
159                     let annotation_index = this.canonical_user_type_annotations.push(
160                         CanonicalUserTypeAnnotation {
161                             span: source_info.span,
162                             user_ty,
163                             inferred_ty: expr.ty,
164                         }
165                     );
166                     this.cfg.push(
167                         block,
168                         Statement {
169                             source_info,
170                             kind: StatementKind::AscribeUserType(
171                                 Place::Base(PlaceBase::Local(temp.clone())),
172                                 Variance::Invariant,
173                                 box UserTypeProjection { base: annotation_index, projs: vec![], },
174                             ),
175                         },
176                     );
177                 }
178                 block.and(Place::Base(PlaceBase::Local(temp)))
179             }
180
181             ExprKind::Array { .. }
182             | ExprKind::Tuple { .. }
183             | ExprKind::Adt { .. }
184             | ExprKind::Closure { .. }
185             | ExprKind::Unary { .. }
186             | ExprKind::Binary { .. }
187             | ExprKind::LogicalOp { .. }
188             | ExprKind::Box { .. }
189             | ExprKind::Cast { .. }
190             | ExprKind::Use { .. }
191             | ExprKind::NeverToAny { .. }
192             | ExprKind::Pointer { .. }
193             | ExprKind::Repeat { .. }
194             | ExprKind::Borrow { .. }
195             | ExprKind::Match { .. }
196             | ExprKind::Loop { .. }
197             | ExprKind::Block { .. }
198             | ExprKind::Assign { .. }
199             | ExprKind::AssignOp { .. }
200             | ExprKind::Break { .. }
201             | ExprKind::Continue { .. }
202             | ExprKind::Return { .. }
203             | ExprKind::Literal { .. }
204             | ExprKind::InlineAsm { .. }
205             | ExprKind::Yield { .. }
206             | ExprKind::Call { .. } => {
207                 // these are not places, so we need to make a temporary.
208                 debug_assert!(match Category::of(&expr.kind) {
209                     Some(Category::Place) => false,
210                     _ => true,
211                 });
212                 let temp =
213                     unpack!(block = this.as_temp(block, expr.temp_lifetime, expr, mutability));
214                 block.and(Place::Base(PlaceBase::Local(temp)))
215             }
216         }
217     }
218 }