]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/expr/stmt.rs
Allow a dirty MirBuilt for make_extern and make_method_extern
[rust.git] / src / librustc_mir / build / expr / stmt.rs
1 use build::scope::BreakableScope;
2 use build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
3 use 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!(this.into(&Place::Local(RETURN_PLACE), block, value));
143                         this.block_context.pop();
144                         result
145                     }
146                     None => {
147                         this.cfg
148                             .push_assign_unit(block, source_info, &Place::Local(RETURN_PLACE));
149                         block
150                     }
151                 };
152                 let region_scope = this.region_scope_of_return_scope();
153                 let return_block = this.return_block();
154                 this.exit_scope(expr_span, (region_scope, source_info), block, return_block);
155                 this.cfg.start_new_block().unit()
156             }
157             ExprKind::InlineAsm {
158                 asm,
159                 outputs,
160                 inputs,
161             } => {
162                 debug!("stmt_expr InlineAsm block_context.push(SubExpr) : {:?}", expr2);
163                 this.block_context.push(BlockFrame::SubExpr);
164                 let outputs = outputs
165                     .into_iter()
166                     .map(|output| unpack!(block = this.as_place(block, output)))
167                     .collect::<Vec<_>>()
168                     .into_boxed_slice();
169                 let inputs = inputs
170                     .into_iter()
171                     .map(|input| {
172                         (
173                             input.span(),
174                             unpack!(block = this.as_local_operand(block, input)),
175                         )
176                     }).collect::<Vec<_>>()
177                     .into_boxed_slice();
178                 this.cfg.push(
179                     block,
180                     Statement {
181                         source_info,
182                         kind: StatementKind::InlineAsm {
183                             asm: box asm.clone(),
184                             outputs,
185                             inputs,
186                         },
187                     },
188                 );
189                 this.block_context.pop();
190                 block.unit()
191             }
192             _ => {
193                 let expr_ty = expr.ty;
194
195                 // Issue #54382: When creating temp for the value of
196                 // expression like:
197                 //
198                 // `{ side_effects(); { let l = stuff(); the_value } }`
199                 //
200                 // it is usually better to focus on `the_value` rather
201                 // than the entirety of block(s) surrounding it.
202                 let mut temp_span = expr_span;
203                 let mut temp_in_tail_of_block = false;
204                 if let ExprKind::Block { body } = expr.kind {
205                     if let Some(tail_expr) = &body.expr {
206                         let mut expr = tail_expr;
207                         while let rustc::hir::ExprKind::Block(subblock, _label) = &expr.node {
208                             if let Some(subtail_expr) = &subblock.expr {
209                                 expr = subtail_expr
210                             } else {
211                                 break;
212                             }
213                         }
214                         temp_span = expr.span;
215                         temp_in_tail_of_block = true;
216                     }
217                 }
218
219                 let temp = {
220                     let mut local_decl = LocalDecl::new_temp(expr.ty.clone(), temp_span);
221                     if temp_in_tail_of_block {
222                         if this.block_context.currently_ignores_tail_results() {
223                             local_decl = local_decl.block_tail(BlockTailInfo {
224                                 tail_result_is_ignored: true
225                             });
226                         }
227                     }
228                     let temp = this.local_decls.push(local_decl);
229                     let place = Place::Local(temp);
230                     debug!("created temp {:?} for expr {:?} in block_context: {:?}",
231                            temp, expr, this.block_context);
232                     place
233                 };
234                 unpack!(block = this.into(&temp, block, expr));
235
236                 // Attribute drops of the statement's temps to the
237                 // semicolon at the statement's end.
238                 let drop_point = this.hir.tcx().sess.source_map().end_point(match opt_stmt_span {
239                     None => expr_span,
240                     Some(StatementSpan(span)) => span,
241                 });
242
243                 unpack!(block = this.build_drop(block, drop_point, temp, expr_ty));
244                 block.unit()
245             }
246         }
247     }
248 }