]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/expr/as_place.rs
Rollup merge of #56416 - GuillaumeGomez:css-body, r=QuietMisdreavus
[rust.git] / src / librustc_mir / build / expr / as_place.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! See docs in build/expr/mod.rs
12
13 use build::expr::category::Category;
14 use build::ForGuard::{OutsideGuard, RefWithinGuard};
15 use build::{BlockAnd, BlockAndExtension, Builder};
16 use hair::*;
17 use rustc::mir::interpret::EvalErrorKind::BoundsCheck;
18 use rustc::mir::*;
19 use rustc::ty::Variance;
20
21 use rustc_data_structures::indexed_vec::Idx;
22
23 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
24     /// Compile `expr`, yielding a place that we can move from etc.
25     pub fn as_place<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Place<'tcx>>
26     where
27         M: Mirror<'tcx, Output = Expr<'tcx>>,
28     {
29         let expr = self.hir.mirror(expr);
30         self.expr_as_place(block, expr, Mutability::Mut)
31     }
32
33     /// Compile `expr`, yielding a place that we can move from etc.
34     /// Mutability note: The caller of this method promises only to read from the resulting
35     /// place. The place itself may or may not be mutable:
36     /// * If this expr is a place expr like a.b, then we will return that place.
37     /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
38     pub fn as_read_only_place<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Place<'tcx>>
39     where
40         M: Mirror<'tcx, Output = Expr<'tcx>>,
41     {
42         let expr = self.hir.mirror(expr);
43         self.expr_as_place(block, expr, Mutability::Not)
44     }
45
46     fn expr_as_place(
47         &mut self,
48         mut block: BasicBlock,
49         expr: Expr<'tcx>,
50         mutability: Mutability,
51     ) -> BlockAnd<Place<'tcx>> {
52         debug!(
53             "expr_as_place(block={:?}, expr={:?}, mutability={:?})",
54             block, expr, mutability
55         );
56
57         let this = self;
58         let expr_span = expr.span;
59         let source_info = this.source_info(expr_span);
60         match expr.kind {
61             ExprKind::Scope {
62                 region_scope,
63                 lint_level,
64                 value,
65             } => this.in_scope((region_scope, source_info), lint_level, block, |this| {
66                 if mutability == Mutability::Not {
67                     this.as_read_only_place(block, value)
68                 } else {
69                     this.as_place(block, value)
70                 }
71             }),
72             ExprKind::Field { lhs, name } => {
73                 let place = unpack!(block = this.as_place(block, lhs));
74                 let place = place.field(name, expr.ty);
75                 block.and(place)
76             }
77             ExprKind::Deref { arg } => {
78                 let place = unpack!(block = this.as_place(block, arg));
79                 let place = place.deref();
80                 block.and(place)
81             }
82             ExprKind::Index { lhs, index } => {
83                 let (usize_ty, bool_ty) = (this.hir.usize_ty(), this.hir.bool_ty());
84
85                 let slice = unpack!(block = this.as_place(block, lhs));
86                 // region_scope=None so place indexes live forever. They are scalars so they
87                 // do not need storage annotations, and they are often copied between
88                 // places.
89                 // Making this a *fresh* temporary also means we do not have to worry about
90                 // the index changing later: Nothing will ever change this temporary.
91                 // The "retagging" transformation (for Stacked Borrows) relies on this.
92                 let idx = unpack!(block = this.as_temp(block, None, index, Mutability::Mut));
93
94                 // bounds check:
95                 let (len, lt) = (
96                     this.temp(usize_ty.clone(), expr_span),
97                     this.temp(bool_ty, expr_span),
98                 );
99                 this.cfg.push_assign(
100                     block,
101                     source_info, // len = len(slice)
102                     &len,
103                     Rvalue::Len(slice.clone()),
104                 );
105                 this.cfg.push_assign(
106                     block,
107                     source_info, // lt = idx < len
108                     &lt,
109                     Rvalue::BinaryOp(
110                         BinOp::Lt,
111                         Operand::Copy(Place::Local(idx)),
112                         Operand::Copy(len.clone()),
113                     ),
114                 );
115
116                 let msg = BoundsCheck {
117                     len: Operand::Move(len),
118                     index: Operand::Copy(Place::Local(idx)),
119                 };
120                 let success = this.assert(block, Operand::Move(lt), true, msg, expr_span);
121                 success.and(slice.index(idx))
122             }
123             ExprKind::SelfRef => block.and(Place::Local(Local::new(1))),
124             ExprKind::VarRef { id } => {
125                 let place = if this.is_bound_var_in_guard(id) && this
126                     .hir
127                     .tcx()
128                     .all_pat_vars_are_implicit_refs_within_guards()
129                 {
130                     let index = this.var_local_id(id, RefWithinGuard);
131                     Place::Local(index).deref()
132                 } else {
133                     let index = this.var_local_id(id, OutsideGuard);
134                     Place::Local(index)
135                 };
136                 block.and(place)
137             }
138             ExprKind::StaticRef { id } => block.and(Place::Static(Box::new(Static {
139                 def_id: id,
140                 ty: expr.ty,
141             }))),
142
143             ExprKind::PlaceTypeAscription { source, user_ty } => {
144                 let place = unpack!(block = this.as_place(block, source));
145                 if let Some(user_ty) = user_ty {
146                     this.cfg.push(
147                         block,
148                         Statement {
149                             source_info,
150                             kind: StatementKind::AscribeUserType(
151                                 place.clone(),
152                                 Variance::Invariant,
153                                 box UserTypeProjection { base: user_ty, projs: vec![], },
154                             ),
155                         },
156                     );
157                 }
158                 block.and(place)
159             }
160             ExprKind::ValueTypeAscription { source, user_ty } => {
161                 let source = this.hir.mirror(source);
162                 let temp = unpack!(
163                     block = this.as_temp(block, source.temp_lifetime, source, mutability)
164                 );
165                 if let Some(user_ty) = user_ty {
166                     this.cfg.push(
167                         block,
168                         Statement {
169                             source_info,
170                             kind: StatementKind::AscribeUserType(
171                                 Place::Local(temp.clone()),
172                                 Variance::Invariant,
173                                 box UserTypeProjection { base: user_ty, projs: vec![], },
174                             ),
175                         },
176                     );
177                 }
178                 block.and(Place::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::ReifyFnPointer { .. }
193             | ExprKind::ClosureFnPointer { .. }
194             | ExprKind::UnsafeFnPointer { .. }
195             | ExprKind::Unsize { .. }
196             | ExprKind::Repeat { .. }
197             | ExprKind::Borrow { .. }
198             | ExprKind::If { .. }
199             | ExprKind::Match { .. }
200             | ExprKind::Loop { .. }
201             | ExprKind::Block { .. }
202             | ExprKind::Assign { .. }
203             | ExprKind::AssignOp { .. }
204             | ExprKind::Break { .. }
205             | ExprKind::Continue { .. }
206             | ExprKind::Return { .. }
207             | ExprKind::Literal { .. }
208             | ExprKind::InlineAsm { .. }
209             | ExprKind::Yield { .. }
210             | ExprKind::Call { .. } => {
211                 // these are not places, so we need to make a temporary.
212                 debug_assert!(match Category::of(&expr.kind) {
213                     Some(Category::Place) => false,
214                     _ => true,
215                 });
216                 let temp =
217                     unpack!(block = this.as_temp(block, expr.temp_lifetime, expr, mutability));
218                 block.and(Place::Local(temp))
219             }
220         }
221     }
222 }