]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/expr/stmt.rs
Shrink `mir::Statement`.
[rust.git] / src / librustc_mir / build / expr / stmt.rs
1 use crate::build::scope::BreakableScope;
2 use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
3 use crate::hair::*;
4 use rustc::mir::*;
5
6 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
7     /// Builds a block of MIR statements to evaluate the HAIR `expr`.
8     /// If the original expression was an AST statement,
9     /// (e.g., `some().code(&here());`) then `opt_stmt_span` is the
10     /// span of that statement (including its semicolon, if any).
11     /// Diagnostics use this span (which may be larger than that of
12     /// `expr`) to identify when statement temporaries are dropped.
13     pub fn stmt_expr(&mut self,
14                      mut block: BasicBlock,
15                      expr: Expr<'tcx>,
16                      opt_stmt_span: Option<StatementSpan>)
17                      -> BlockAnd<()>
18     {
19         let this = self;
20         let expr_span = expr.span;
21         let source_info = this.source_info(expr.span);
22         // Handle a number of expressions that don't need a destination at all. This
23         // avoids needing a mountain of temporary `()` variables.
24         let expr2 = expr.clone();
25         match expr.kind {
26             ExprKind::Scope {
27                 region_scope,
28                 lint_level,
29                 value,
30             } => {
31                 let value = this.hir.mirror(value);
32                 this.in_scope((region_scope, source_info), lint_level, block, |this| {
33                     this.stmt_expr(block, value, opt_stmt_span)
34                 })
35             }
36             ExprKind::Assign { lhs, rhs } => {
37                 let lhs = this.hir.mirror(lhs);
38                 let rhs = this.hir.mirror(rhs);
39                 let lhs_span = lhs.span;
40
41                 // Note: we evaluate assignments right-to-left. This
42                 // is better for borrowck interaction with overloaded
43                 // operators like x[j] = x[i].
44
45                 debug!("stmt_expr Assign block_context.push(SubExpr) : {:?}", expr2);
46                 this.block_context.push(BlockFrame::SubExpr);
47
48                 // Generate better code for things that don't need to be
49                 // dropped.
50                 if this.hir.needs_drop(lhs.ty) {
51                     let rhs = unpack!(block = this.as_local_operand(block, rhs));
52                     let lhs = unpack!(block = this.as_place(block, lhs));
53                     unpack!(block = this.build_drop_and_replace(block, lhs_span, lhs, rhs));
54                 } else {
55                     let rhs = unpack!(block = this.as_local_rvalue(block, rhs));
56                     let lhs = unpack!(block = this.as_place(block, lhs));
57                     this.cfg.push_assign(block, source_info, &lhs, rhs);
58                 }
59
60                 this.block_context.pop();
61                 block.unit()
62             }
63             ExprKind::AssignOp { op, lhs, rhs } => {
64                 // FIXME(#28160) there is an interesting semantics
65                 // question raised here -- should we "freeze" the
66                 // value of the lhs here?  I'm inclined to think not,
67                 // since it seems closer to the semantics of the
68                 // overloaded version, which takes `&mut self`.  This
69                 // only affects weird things like `x += {x += 1; x}`
70                 // -- is that equal to `x + (x + 1)` or `2*(x+1)`?
71
72                 let lhs = this.hir.mirror(lhs);
73                 let lhs_ty = lhs.ty;
74
75                 debug!("stmt_expr AssignOp block_context.push(SubExpr) : {:?}", expr2);
76                 this.block_context.push(BlockFrame::SubExpr);
77
78                 // As above, RTL.
79                 let rhs = unpack!(block = this.as_local_operand(block, rhs));
80                 let lhs = unpack!(block = this.as_place(block, lhs));
81
82                 // we don't have to drop prior contents or anything
83                 // because AssignOp is only legal for Copy types
84                 // (overloaded ops should be desugared into a call).
85                 let result = unpack!(
86                     block = this.build_binary_op(
87                         block,
88                         op,
89                         expr_span,
90                         lhs_ty,
91                         Operand::Copy(lhs.clone()),
92                         rhs
93                     )
94                 );
95                 this.cfg.push_assign(block, source_info, &lhs, result);
96
97                 this.block_context.pop();
98                 block.unit()
99             }
100             ExprKind::Continue { label } => {
101                 let BreakableScope {
102                     continue_block,
103                     region_scope,
104                     ..
105                 } = *this.find_breakable_scope(expr_span, label);
106                 let continue_block = continue_block
107                     .expect("Attempted to continue in non-continuable breakable block");
108                 this.exit_scope(
109                     expr_span,
110                     (region_scope, source_info),
111                     block,
112                     continue_block,
113                 );
114                 this.cfg.start_new_block().unit()
115             }
116             ExprKind::Break { label, value } => {
117                 let (break_block, region_scope, destination) = {
118                     let BreakableScope {
119                         break_block,
120                         region_scope,
121                         ref break_destination,
122                         ..
123                     } = *this.find_breakable_scope(expr_span, label);
124                     (break_block, region_scope, break_destination.clone())
125                 };
126                 if let Some(value) = value {
127                     debug!("stmt_expr Break val block_context.push(SubExpr) : {:?}", expr2);
128                     this.block_context.push(BlockFrame::SubExpr);
129                     unpack!(block = this.into(&destination, block, value));
130                     this.block_context.pop();
131                 } else {
132                     this.cfg.push_assign_unit(block, source_info, &destination)
133                 }
134                 this.exit_scope(expr_span, (region_scope, source_info), block, break_block);
135                 this.cfg.start_new_block().unit()
136             }
137             ExprKind::Return { value } => {
138                 block = match value {
139                     Some(value) => {
140                         debug!("stmt_expr Return val block_context.push(SubExpr) : {:?}", expr2);
141                         this.block_context.push(BlockFrame::SubExpr);
142                         let result = unpack!(
143                             this.into(
144                                 &Place::RETURN_PLACE,
145                                 block,
146                                 value
147                             )
148                         );
149                         this.block_context.pop();
150                         result
151                     }
152                     None => {
153                         this.cfg.push_assign_unit(
154                             block,
155                             source_info,
156                             &Place::RETURN_PLACE,
157                         );
158                         block
159                     }
160                 };
161                 let region_scope = this.region_scope_of_return_scope();
162                 let return_block = this.return_block();
163                 this.exit_scope(expr_span, (region_scope, source_info), block, return_block);
164                 this.cfg.start_new_block().unit()
165             }
166             ExprKind::InlineAsm {
167                 asm,
168                 outputs,
169                 inputs,
170             } => {
171                 debug!("stmt_expr InlineAsm block_context.push(SubExpr) : {:?}", expr2);
172                 this.block_context.push(BlockFrame::SubExpr);
173                 let outputs = outputs
174                     .into_iter()
175                     .map(|output| unpack!(block = this.as_place(block, output)))
176                     .collect::<Vec<_>>()
177                     .into_boxed_slice();
178                 let inputs = inputs
179                     .into_iter()
180                     .map(|input| {
181                         (
182                             input.span(),
183                             unpack!(block = this.as_local_operand(block, input)),
184                         )
185                     }).collect::<Vec<_>>()
186                     .into_boxed_slice();
187                 this.cfg.push(
188                     block,
189                     Statement {
190                         source_info,
191                         kind: StatementKind::InlineAsm(box InlineAsm {
192                             asm: asm.clone(),
193                             outputs,
194                             inputs,
195                         }),
196                     },
197                 );
198                 this.block_context.pop();
199                 block.unit()
200             }
201             _ => {
202                 let expr_ty = expr.ty;
203
204                 // Issue #54382: When creating temp for the value of
205                 // expression like:
206                 //
207                 // `{ side_effects(); { let l = stuff(); the_value } }`
208                 //
209                 // it is usually better to focus on `the_value` rather
210                 // than the entirety of block(s) surrounding it.
211                 let mut temp_span = expr_span;
212                 let mut temp_in_tail_of_block = false;
213                 if let ExprKind::Block { body } = expr.kind {
214                     if let Some(tail_expr) = &body.expr {
215                         let mut expr = tail_expr;
216                         while let rustc::hir::ExprKind::Block(subblock, _label) = &expr.node {
217                             if let Some(subtail_expr) = &subblock.expr {
218                                 expr = subtail_expr
219                             } else {
220                                 break;
221                             }
222                         }
223                         temp_span = expr.span;
224                         temp_in_tail_of_block = true;
225                     }
226                 }
227
228                 let temp = {
229                     let mut local_decl = LocalDecl::new_temp(expr.ty.clone(), temp_span);
230                     if temp_in_tail_of_block {
231                         if this.block_context.currently_ignores_tail_results() {
232                             local_decl = local_decl.block_tail(BlockTailInfo {
233                                 tail_result_is_ignored: true
234                             });
235                         }
236                     }
237                     let temp = this.local_decls.push(local_decl);
238                     let place = Place::Base(PlaceBase::Local(temp));
239                     debug!("created temp {:?} for expr {:?} in block_context: {:?}",
240                            temp, expr, this.block_context);
241                     place
242                 };
243                 unpack!(block = this.into(&temp, block, expr));
244
245                 // Attribute drops of the statement's temps to the
246                 // semicolon at the statement's end.
247                 let drop_point = this.hir.tcx().sess.source_map().end_point(match opt_stmt_span {
248                     None => expr_span,
249                     Some(StatementSpan(span)) => span,
250                 });
251
252                 unpack!(block = this.build_drop(block, drop_point, temp, expr_ty));
253                 block.unit()
254             }
255         }
256     }
257 }