]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/build/expr/stmt.rs
Merge commit '97a5daa65908e59744e2bc625b14849352231c75' into clippyup
[rust.git] / compiler / rustc_mir_build / src / build / expr / stmt.rs
1 use crate::build::scope::BreakableTarget;
2 use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
3 use rustc_middle::middle::region;
4 use rustc_middle::mir::*;
5 use rustc_middle::thir::*;
6
7 impl<'a, 'tcx> Builder<'a, 'tcx> {
8     /// Builds a block of MIR statements to evaluate the THIR `expr`.
9     /// If the original expression was an AST statement,
10     /// (e.g., `some().code(&here());`) then `opt_stmt_span` is the
11     /// span of that statement (including its semicolon, if any).
12     /// The scope is used if a statement temporary must be dropped.
13     crate fn stmt_expr(
14         &mut self,
15         mut block: BasicBlock,
16         expr: &Expr<'tcx>,
17         statement_scope: Option<region::Scope>,
18     ) -> BlockAnd<()> {
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         match expr.kind {
25             ExprKind::Scope { region_scope, lint_level, value } => {
26                 this.in_scope((region_scope, source_info), lint_level, |this| {
27                     this.stmt_expr(block, &this.thir[value], statement_scope)
28                 })
29             }
30             ExprKind::Assign { lhs, rhs } => {
31                 let lhs = &this.thir[lhs];
32                 let rhs = &this.thir[rhs];
33                 let lhs_span = lhs.span;
34
35                 // Note: we evaluate assignments right-to-left. This
36                 // is better for borrowck interaction with overloaded
37                 // operators like x[j] = x[i].
38
39                 debug!("stmt_expr Assign block_context.push(SubExpr) : {:?}", expr);
40                 this.block_context.push(BlockFrame::SubExpr);
41
42                 // Generate better code for things that don't need to be
43                 // dropped.
44                 if lhs.ty.needs_drop(this.tcx, this.param_env) {
45                     let rhs = unpack!(block = this.as_local_operand(block, rhs));
46                     let lhs = unpack!(block = this.as_place(block, lhs));
47                     unpack!(block = this.build_drop_and_replace(block, lhs_span, lhs, rhs));
48                 } else {
49                     let rhs = unpack!(block = this.as_local_rvalue(block, rhs));
50                     let lhs = unpack!(block = this.as_place(block, lhs));
51                     this.cfg.push_assign(block, source_info, lhs, rhs);
52                 }
53
54                 this.block_context.pop();
55                 block.unit()
56             }
57             ExprKind::AssignOp { op, lhs, rhs } => {
58                 // FIXME(#28160) there is an interesting semantics
59                 // question raised here -- should we "freeze" the
60                 // value of the lhs here?  I'm inclined to think not,
61                 // since it seems closer to the semantics of the
62                 // overloaded version, which takes `&mut self`.  This
63                 // only affects weird things like `x += {x += 1; x}`
64                 // -- is that equal to `x + (x + 1)` or `2*(x+1)`?
65
66                 let lhs = &this.thir[lhs];
67                 let rhs = &this.thir[rhs];
68                 let lhs_ty = lhs.ty;
69
70                 debug!("stmt_expr AssignOp block_context.push(SubExpr) : {:?}", expr);
71                 this.block_context.push(BlockFrame::SubExpr);
72
73                 // As above, RTL.
74                 let rhs = unpack!(block = this.as_local_operand(block, rhs));
75                 let lhs = unpack!(block = this.as_place(block, lhs));
76
77                 // we don't have to drop prior contents or anything
78                 // because AssignOp is only legal for Copy types
79                 // (overloaded ops should be desugared into a call).
80                 let result = unpack!(
81                     block =
82                         this.build_binary_op(block, op, expr_span, lhs_ty, Operand::Copy(lhs), rhs)
83                 );
84                 this.cfg.push_assign(block, source_info, lhs, result);
85
86                 this.block_context.pop();
87                 block.unit()
88             }
89             ExprKind::Continue { label } => {
90                 this.break_scope(block, None, BreakableTarget::Continue(label), source_info)
91             }
92             ExprKind::Break { label, value } => this.break_scope(
93                 block,
94                 value.map(|value| &this.thir[value]),
95                 BreakableTarget::Break(label),
96                 source_info,
97             ),
98             ExprKind::Return { value } => this.break_scope(
99                 block,
100                 value.map(|value| &this.thir[value]),
101                 BreakableTarget::Return,
102                 source_info,
103             ),
104             ExprKind::LlvmInlineAsm { asm, ref outputs, ref inputs } => {
105                 debug!("stmt_expr LlvmInlineAsm block_context.push(SubExpr) : {:?}", expr);
106                 this.block_context.push(BlockFrame::SubExpr);
107                 let outputs = outputs
108                     .into_iter()
109                     .copied()
110                     .map(|output| unpack!(block = this.as_place(block, &this.thir[output])))
111                     .collect::<Vec<_>>()
112                     .into_boxed_slice();
113                 let inputs = inputs
114                     .into_iter()
115                     .copied()
116                     .map(|input| {
117                         let input = &this.thir[input];
118                         (input.span, unpack!(block = this.as_local_operand(block, &input)))
119                     })
120                     .collect::<Vec<_>>()
121                     .into_boxed_slice();
122                 this.cfg.push(
123                     block,
124                     Statement {
125                         source_info,
126                         kind: StatementKind::LlvmInlineAsm(Box::new(LlvmInlineAsm {
127                             asm: asm.clone(),
128                             outputs,
129                             inputs,
130                         })),
131                     },
132                 );
133                 this.block_context.pop();
134                 block.unit()
135             }
136             _ => {
137                 assert!(
138                     statement_scope.is_some(),
139                     "Should not be calling `stmt_expr` on a general expression \
140                      without a statement scope",
141                 );
142
143                 // Issue #54382: When creating temp for the value of
144                 // expression like:
145                 //
146                 // `{ side_effects(); { let l = stuff(); the_value } }`
147                 //
148                 // it is usually better to focus on `the_value` rather
149                 // than the entirety of block(s) surrounding it.
150                 let adjusted_span = (|| {
151                     if let ExprKind::Block { body } = &expr.kind {
152                         if let Some(tail_expr) = body.expr {
153                             let mut expr = &this.thir[tail_expr];
154                             while let ExprKind::Block {
155                                 body: Block { expr: Some(nested_expr), .. },
156                             }
157                             | ExprKind::Scope { value: nested_expr, .. } = expr.kind
158                             {
159                                 expr = &this.thir[nested_expr];
160                             }
161                             this.block_context.push(BlockFrame::TailExpr {
162                                 tail_result_is_ignored: true,
163                                 span: expr.span,
164                             });
165                             return Some(expr.span);
166                         }
167                     }
168                     None
169                 })();
170
171                 let temp =
172                     unpack!(block = this.as_temp(block, statement_scope, expr, Mutability::Not));
173
174                 if let Some(span) = adjusted_span {
175                     this.local_decls[temp].source_info.span = span;
176                     this.block_context.pop();
177                 }
178
179                 block.unit()
180             }
181         }
182     }
183 }