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