rustc_mir_build/builder/expr/
stmt.rs1use rustc_middle::middle::region;
2use rustc_middle::mir::*;
3use rustc_middle::span_bug;
4use rustc_middle::thir::*;
5use rustc_span::source_map::Spanned;
6use tracing::debug;
7
8use crate::builder::scope::BreakableTarget;
9use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
10
11impl<'a, 'tcx> Builder<'a, 'tcx> {
12 pub(crate) fn stmt_expr(
16 &mut self,
17 mut block: BasicBlock,
18 expr_id: ExprId,
19 statement_scope: Option<region::Scope>,
20 ) -> BlockAnd<()> {
21 let this = self;
22 let expr = &this.thir[expr_id];
23 let expr_span = expr.span;
24 let source_info = this.source_info(expr.span);
25 match expr.kind {
28 ExprKind::Scope { region_scope, lint_level, value } => {
29 this.in_scope((region_scope, source_info), lint_level, |this| {
30 this.stmt_expr(block, value, statement_scope)
31 })
32 }
33 ExprKind::Assign { lhs, rhs } => {
34 let lhs_expr = &this.thir[lhs];
35
36 debug!("stmt_expr Assign block_context.push(SubExpr) : {:?}", expr);
41 this.block_context.push(BlockFrame::SubExpr);
42
43 if lhs_expr.ty.needs_drop(this.tcx, this.typing_env()) {
46 let rhs = unpack!(block = this.as_local_rvalue(block, rhs));
47 let lhs = unpack!(block = this.as_place(block, lhs));
48 block =
49 this.build_drop_and_replace(block, lhs_expr.span, lhs, rhs).into_block();
50 } else {
51 let rhs = unpack!(block = this.as_local_rvalue(block, rhs));
52 let lhs = unpack!(block = this.as_place(block, lhs));
53 this.cfg.push_assign(block, source_info, lhs, rhs);
54 }
55
56 this.block_context.pop();
57 block.unit()
58 }
59 ExprKind::AssignOp { op, lhs, rhs } => {
60 let lhs_ty = this.thir[lhs].ty;
69
70 debug!("stmt_expr AssignOp block_context.push(SubExpr) : {:?}", expr);
71 this.block_context.push(BlockFrame::SubExpr);
72
73 let rhs = unpack!(block = this.as_local_operand(block, rhs));
75 let lhs = unpack!(block = this.as_place(block, lhs));
76
77 let result = unpack!(
81 block = this.build_binary_op(
82 block,
83 op.into(),
84 expr_span,
85 lhs_ty,
86 Operand::Copy(lhs),
87 rhs
88 )
89 );
90 this.cfg.push_assign(block, source_info, lhs, result);
91
92 this.block_context.pop();
93 block.unit()
94 }
95 ExprKind::Continue { label } => {
96 this.break_scope(block, None, BreakableTarget::Continue(label), source_info)
97 }
98 ExprKind::Break { label, value } => {
99 this.break_scope(block, value, BreakableTarget::Break(label), source_info)
100 }
101 ExprKind::Return { value } => {
102 this.break_scope(block, value, BreakableTarget::Return, source_info)
103 }
104 ExprKind::Become { value } => {
105 let v = &this.thir[value];
106 let ExprKind::Scope { value, lint_level, region_scope } = v.kind else {
107 span_bug!(v.span, "`thir_check_tail_calls` should have disallowed this {v:?}")
108 };
109
110 let v = &this.thir[value];
111 let ExprKind::Call { ref args, fun, fn_span, .. } = v.kind else {
112 span_bug!(v.span, "`thir_check_tail_calls` should have disallowed this {v:?}")
113 };
114
115 this.in_scope((region_scope, source_info), lint_level, |this| {
116 let fun = unpack!(block = this.as_local_operand(block, fun));
117 let args: Box<[_]> = args
118 .into_iter()
119 .copied()
120 .map(|arg| Spanned {
121 node: unpack!(block = this.as_local_call_operand(block, arg)),
122 span: this.thir.exprs[arg].span,
123 })
124 .collect();
125
126 this.record_operands_moved(&args);
127
128 debug!("expr_into_dest: fn_span={:?}", fn_span);
129
130 unpack!(block = this.break_for_tail_call(block, &args, source_info));
131
132 this.cfg.terminate(
133 block,
134 source_info,
135 TerminatorKind::TailCall { func: fun, args, fn_span },
136 );
137
138 this.cfg.start_new_block().unit()
139 })
140 }
141 _ => {
142 assert!(
143 statement_scope.is_some(),
144 "Should not be calling `stmt_expr` on a general expression \
145 without a statement scope",
146 );
147
148 let adjusted_span = if let ExprKind::Block { block } = expr.kind
156 && let Some(tail_ex) = this.thir[block].expr
157 {
158 let mut expr = &this.thir[tail_ex];
159 loop {
160 match expr.kind {
161 ExprKind::Block { block }
162 if let Some(nested_expr) = this.thir[block].expr =>
163 {
164 expr = &this.thir[nested_expr];
165 }
166 ExprKind::Scope { value: nested_expr, .. } => {
167 expr = &this.thir[nested_expr];
168 }
169 _ => break,
170 }
171 }
172 this.block_context.push(BlockFrame::TailExpr {
173 info: BlockTailInfo { tail_result_is_ignored: true, span: expr.span },
174 });
175 Some(expr.span)
176 } else {
177 None
178 };
179
180 let temp = unpack!(
181 block = this.as_temp(
182 block,
183 TempLifetime {
184 temp_lifetime: statement_scope,
185 backwards_incompatible: None
186 },
187 expr_id,
188 Mutability::Not
189 )
190 );
191
192 if let Some(span) = adjusted_span {
193 this.local_decls[temp].source_info.span = span;
194 this.block_context.pop();
195 }
196
197 block.unit()
198 }
199 }
200 }
201}