]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/build/expr/into.rs
235fe14cbf9fdd3ed395c65d7fa0a12242828242
[rust.git] / compiler / rustc_mir_build / src / build / expr / into.rs
1 //! See docs in build/expr/mod.rs
2
3 use crate::build::expr::category::{Category, RvalueFunc};
4 use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
5 use crate::thir::*;
6 use rustc_ast::InlineAsmOptions;
7 use rustc_data_structures::fx::FxHashMap;
8 use rustc_data_structures::stack::ensure_sufficient_stack;
9 use rustc_hir as hir;
10 use rustc_middle::mir::*;
11 use rustc_middle::ty::CanonicalUserTypeAnnotation;
12
13 impl<'a, 'tcx> Builder<'a, 'tcx> {
14     /// Compile `expr`, storing the result into `destination`, which
15     /// is assumed to be uninitialized.
16     crate fn into_expr(
17         &mut self,
18         destination: Place<'tcx>,
19         mut block: BasicBlock,
20         expr: Expr<'tcx>,
21     ) -> BlockAnd<()> {
22         debug!("into_expr(destination={:?}, block={:?}, expr={:?})", destination, block, expr);
23
24         // since we frequently have to reference `self` from within a
25         // closure, where `self` would be shadowed, it's easier to
26         // just use the name `this` uniformly
27         let this = self;
28         let expr_span = expr.span;
29         let source_info = this.source_info(expr_span);
30
31         let expr_is_block_or_scope =
32             matches!(expr.kind, ExprKind::Block { .. } | ExprKind::Scope { .. });
33
34         if !expr_is_block_or_scope {
35             this.block_context.push(BlockFrame::SubExpr);
36         }
37
38         let block_and = match expr.kind {
39             ExprKind::Scope { region_scope, lint_level, value } => {
40                 let region_scope = (region_scope, source_info);
41                 ensure_sufficient_stack(|| {
42                     this.in_scope(region_scope, lint_level, |this| {
43                         this.into(destination, block, value)
44                     })
45                 })
46             }
47             ExprKind::Block { body: ast_block } => {
48                 this.ast_block(destination, block, ast_block, source_info)
49             }
50             ExprKind::Match { scrutinee, arms } => {
51                 this.match_expr(destination, expr_span, block, scrutinee, arms)
52             }
53             ExprKind::If { cond, then, else_opt } => {
54                 let place = unpack!(
55                     block = this.as_temp(block, Some(this.local_scope()), cond, Mutability::Mut)
56                 );
57                 let operand = Operand::Move(Place::from(place));
58
59                 let mut then_block = this.cfg.start_new_block();
60                 let mut else_block = this.cfg.start_new_block();
61                 let term = TerminatorKind::if_(this.hir.tcx(), operand, then_block, else_block);
62                 this.cfg.terminate(block, source_info, term);
63
64                 unpack!(then_block = this.into(destination, then_block, then));
65                 else_block = if let Some(else_opt) = else_opt {
66                     unpack!(this.into(destination, else_block, else_opt))
67                 } else {
68                     // Body of the `if` expression without an `else` clause must return `()`, thus
69                     // we implicitly generate a `else {}` if it is not specified.
70                     let correct_si = this.source_info(expr_span.shrink_to_hi());
71                     this.cfg.push_assign_unit(else_block, correct_si, destination, this.hir.tcx());
72                     else_block
73                 };
74
75                 let join_block = this.cfg.start_new_block();
76                 this.cfg.terminate(
77                     then_block,
78                     source_info,
79                     TerminatorKind::Goto { target: join_block },
80                 );
81                 this.cfg.terminate(
82                     else_block,
83                     source_info,
84                     TerminatorKind::Goto { target: join_block },
85                 );
86
87                 join_block.unit()
88             }
89             ExprKind::NeverToAny { source } => {
90                 let source = this.hir.mirror(source);
91                 let is_call =
92                     matches!(source.kind, ExprKind::Call { .. } | ExprKind::InlineAsm { .. });
93
94                 // (#66975) Source could be a const of type `!`, so has to
95                 // exist in the generated MIR.
96                 unpack!(
97                     block = this.as_temp(block, Some(this.local_scope()), source, Mutability::Mut,)
98                 );
99
100                 // This is an optimization. If the expression was a call then we already have an
101                 // unreachable block. Don't bother to terminate it and create a new one.
102                 if is_call {
103                     block.unit()
104                 } else {
105                     this.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
106                     let end_block = this.cfg.start_new_block();
107                     end_block.unit()
108                 }
109             }
110             ExprKind::LogicalOp { op, lhs, rhs } => {
111                 // And:
112                 //
113                 // [block: If(lhs)] -true-> [else_block: If(rhs)] -true-> [true_block]
114                 //        |                          | (false)
115                 //        +----------false-----------+------------------> [false_block]
116                 //
117                 // Or:
118                 //
119                 // [block: If(lhs)] -false-> [else_block: If(rhs)] -true-> [true_block]
120                 //        | (true)                   | (false)
121                 //  [true_block]               [false_block]
122
123                 let (true_block, false_block, mut else_block, join_block) = (
124                     this.cfg.start_new_block(),
125                     this.cfg.start_new_block(),
126                     this.cfg.start_new_block(),
127                     this.cfg.start_new_block(),
128                 );
129
130                 let lhs = unpack!(block = this.as_local_operand(block, lhs));
131                 let blocks = match op {
132                     LogicalOp::And => (else_block, false_block),
133                     LogicalOp::Or => (true_block, else_block),
134                 };
135                 let term = TerminatorKind::if_(this.hir.tcx(), lhs, blocks.0, blocks.1);
136                 this.cfg.terminate(block, source_info, term);
137
138                 let rhs = unpack!(else_block = this.as_local_operand(else_block, rhs));
139                 let term = TerminatorKind::if_(this.hir.tcx(), rhs, true_block, false_block);
140                 this.cfg.terminate(else_block, source_info, term);
141
142                 this.cfg.push_assign_constant(
143                     true_block,
144                     source_info,
145                     destination,
146                     Constant { span: expr_span, user_ty: None, literal: this.hir.true_literal() },
147                 );
148
149                 this.cfg.push_assign_constant(
150                     false_block,
151                     source_info,
152                     destination,
153                     Constant { span: expr_span, user_ty: None, literal: this.hir.false_literal() },
154                 );
155
156                 // Link up both branches:
157                 this.cfg.goto(true_block, source_info, join_block);
158                 this.cfg.goto(false_block, source_info, join_block);
159                 join_block.unit()
160             }
161             ExprKind::Loop { body } => {
162                 // [block]
163                 //    |
164                 //   [loop_block] -> [body_block] -/eval. body/-> [body_block_end]
165                 //    |        ^                                         |
166                 // false link  |                                         |
167                 //    |        +-----------------------------------------+
168                 //    +-> [diverge_cleanup]
169                 // The false link is required to make sure borrowck considers unwinds through the
170                 // body, even when the exact code in the body cannot unwind
171
172                 let loop_block = this.cfg.start_new_block();
173
174                 // Start the loop.
175                 this.cfg.goto(block, source_info, loop_block);
176
177                 this.in_breakable_scope(Some(loop_block), destination, expr_span, move |this| {
178                     // conduct the test, if necessary
179                     let body_block = this.cfg.start_new_block();
180                     this.cfg.terminate(
181                         loop_block,
182                         source_info,
183                         TerminatorKind::FalseUnwind { real_target: body_block, unwind: None },
184                     );
185                     this.diverge_from(loop_block);
186
187                     // The “return” value of the loop body must always be an unit. We therefore
188                     // introduce a unit temporary as the destination for the loop body.
189                     let tmp = this.get_unit_temp();
190                     // Execute the body, branching back to the test.
191                     let body_block_end = unpack!(this.into(tmp, body_block, body));
192                     this.cfg.goto(body_block_end, source_info, loop_block);
193
194                     // Loops are only exited by `break` expressions.
195                     None
196                 })
197             }
198             ExprKind::Call { ty: _, fun, args, from_hir_call, fn_span } => {
199                 let fun = unpack!(block = this.as_local_operand(block, fun));
200                 let args: Vec<_> = args
201                     .into_iter()
202                     .map(|arg| unpack!(block = this.as_local_call_operand(block, arg)))
203                     .collect();
204
205                 let success = this.cfg.start_new_block();
206
207                 this.record_operands_moved(&args);
208
209                 debug!("into_expr: fn_span={:?}", fn_span);
210
211                 this.cfg.terminate(
212                     block,
213                     source_info,
214                     TerminatorKind::Call {
215                         func: fun,
216                         args,
217                         cleanup: None,
218                         // FIXME(varkor): replace this with an uninhabitedness-based check.
219                         // This requires getting access to the current module to call
220                         // `tcx.is_ty_uninhabited_from`, which is currently tricky to do.
221                         destination: if expr.ty.is_never() {
222                             None
223                         } else {
224                             Some((destination, success))
225                         },
226                         from_hir_call,
227                         fn_span,
228                     },
229                 );
230                 this.diverge_from(block);
231                 success.unit()
232             }
233             ExprKind::Use { source } => this.into(destination, block, source),
234             ExprKind::Borrow { arg, borrow_kind } => {
235                 // We don't do this in `as_rvalue` because we use `as_place`
236                 // for borrow expressions, so we cannot create an `RValue` that
237                 // remains valid across user code. `as_rvalue` is usually called
238                 // by this method anyway, so this shouldn't cause too many
239                 // unnecessary temporaries.
240                 let arg_place = match borrow_kind {
241                     BorrowKind::Shared => unpack!(block = this.as_read_only_place(block, arg)),
242                     _ => unpack!(block = this.as_place(block, arg)),
243                 };
244                 let borrow =
245                     Rvalue::Ref(this.hir.tcx().lifetimes.re_erased, borrow_kind, arg_place);
246                 this.cfg.push_assign(block, source_info, destination, borrow);
247                 block.unit()
248             }
249             ExprKind::AddressOf { mutability, arg } => {
250                 let place = match mutability {
251                     hir::Mutability::Not => this.as_read_only_place(block, arg),
252                     hir::Mutability::Mut => this.as_place(block, arg),
253                 };
254                 let address_of = Rvalue::AddressOf(mutability, unpack!(block = place));
255                 this.cfg.push_assign(block, source_info, destination, address_of);
256                 block.unit()
257             }
258             ExprKind::Adt { adt_def, variant_index, substs, user_ty, fields, base } => {
259                 // See the notes for `ExprKind::Array` in `as_rvalue` and for
260                 // `ExprKind::Borrow` above.
261                 let is_union = adt_def.is_union();
262                 let active_field_index = if is_union { Some(fields[0].name.index()) } else { None };
263
264                 let scope = this.local_scope();
265
266                 // first process the set of fields that were provided
267                 // (evaluating them in order given by user)
268                 let fields_map: FxHashMap<_, _> = fields
269                     .into_iter()
270                     .map(|f| (f.name, unpack!(block = this.as_operand(block, Some(scope), f.expr))))
271                     .collect();
272
273                 let field_names = this.hir.all_fields(adt_def, variant_index);
274
275                 let fields: Vec<_> = if let Some(FruInfo { base, field_types }) = base {
276                     let place_builder = unpack!(block = this.as_place_builder(block, base));
277
278                     // MIR does not natively support FRU, so for each
279                     // base-supplied field, generate an operand that
280                     // reads it from the base.
281                     field_names
282                         .into_iter()
283                         .zip(field_types.into_iter())
284                         .map(|(n, ty)| match fields_map.get(&n) {
285                             Some(v) => v.clone(),
286                             None => {
287                                 let place_builder = place_builder.clone();
288                                 this.consume_by_copy_or_move(
289                                     place_builder
290                                         .field(n, ty)
291                                         .into_place(this.hir.tcx(), this.hir.typeck_results()),
292                                 )
293                             }
294                         })
295                         .collect()
296                 } else {
297                     field_names.iter().filter_map(|n| fields_map.get(n).cloned()).collect()
298                 };
299
300                 let inferred_ty = expr.ty;
301                 let user_ty = user_ty.map(|ty| {
302                     this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
303                         span: source_info.span,
304                         user_ty: ty,
305                         inferred_ty,
306                     })
307                 });
308                 let adt = box AggregateKind::Adt(
309                     adt_def,
310                     variant_index,
311                     substs,
312                     user_ty,
313                     active_field_index,
314                 );
315                 this.cfg.push_assign(
316                     block,
317                     source_info,
318                     destination,
319                     Rvalue::Aggregate(adt, fields),
320                 );
321                 block.unit()
322             }
323             ExprKind::InlineAsm { template, operands, options, line_spans } => {
324                 use crate::thir;
325                 use rustc_middle::mir;
326                 let operands = operands
327                     .into_iter()
328                     .map(|op| match op {
329                         thir::InlineAsmOperand::In { reg, expr } => mir::InlineAsmOperand::In {
330                             reg,
331                             value: unpack!(block = this.as_local_operand(block, expr)),
332                         },
333                         thir::InlineAsmOperand::Out { reg, late, expr } => {
334                             mir::InlineAsmOperand::Out {
335                                 reg,
336                                 late,
337                                 place: expr.map(|expr| unpack!(block = this.as_place(block, expr))),
338                             }
339                         }
340                         thir::InlineAsmOperand::InOut { reg, late, expr } => {
341                             let place = unpack!(block = this.as_place(block, expr));
342                             mir::InlineAsmOperand::InOut {
343                                 reg,
344                                 late,
345                                 // This works because asm operands must be Copy
346                                 in_value: Operand::Copy(place),
347                                 out_place: Some(place),
348                             }
349                         }
350                         thir::InlineAsmOperand::SplitInOut { reg, late, in_expr, out_expr } => {
351                             mir::InlineAsmOperand::InOut {
352                                 reg,
353                                 late,
354                                 in_value: unpack!(block = this.as_local_operand(block, in_expr)),
355                                 out_place: out_expr.map(|out_expr| {
356                                     unpack!(block = this.as_place(block, out_expr))
357                                 }),
358                             }
359                         }
360                         thir::InlineAsmOperand::Const { expr } => mir::InlineAsmOperand::Const {
361                             value: unpack!(block = this.as_local_operand(block, expr)),
362                         },
363                         thir::InlineAsmOperand::SymFn { expr } => {
364                             mir::InlineAsmOperand::SymFn { value: box this.as_constant(expr) }
365                         }
366                         thir::InlineAsmOperand::SymStatic { def_id } => {
367                             mir::InlineAsmOperand::SymStatic { def_id }
368                         }
369                     })
370                     .collect();
371
372                 let destination = this.cfg.start_new_block();
373
374                 this.cfg.terminate(
375                     block,
376                     source_info,
377                     TerminatorKind::InlineAsm {
378                         template,
379                         operands,
380                         options,
381                         line_spans,
382                         destination: if options.contains(InlineAsmOptions::NORETURN) {
383                             None
384                         } else {
385                             Some(destination)
386                         },
387                     },
388                 );
389                 destination.unit()
390             }
391
392             // These cases don't actually need a destination
393             ExprKind::Assign { .. }
394             | ExprKind::AssignOp { .. }
395             | ExprKind::LlvmInlineAsm { .. } => {
396                 unpack!(block = this.stmt_expr(block, expr, None));
397                 this.cfg.push_assign_unit(block, source_info, destination, this.hir.tcx());
398                 block.unit()
399             }
400
401             ExprKind::Continue { .. } | ExprKind::Break { .. } | ExprKind::Return { .. } => {
402                 unpack!(block = this.stmt_expr(block, expr, None));
403                 // No assign, as these have type `!`.
404                 block.unit()
405             }
406
407             // Avoid creating a temporary
408             ExprKind::VarRef { .. }
409             | ExprKind::UpvarRef { .. }
410             | ExprKind::PlaceTypeAscription { .. }
411             | ExprKind::ValueTypeAscription { .. } => {
412                 debug_assert!(Category::of(&expr.kind) == Some(Category::Place));
413
414                 let place = unpack!(block = this.as_place(block, expr));
415                 let rvalue = Rvalue::Use(this.consume_by_copy_or_move(place));
416                 this.cfg.push_assign(block, source_info, destination, rvalue);
417                 block.unit()
418             }
419             ExprKind::Index { .. } | ExprKind::Deref { .. } | ExprKind::Field { .. } => {
420                 debug_assert!(Category::of(&expr.kind) == Some(Category::Place));
421
422                 // Create a "fake" temporary variable so that we check that the
423                 // value is Sized. Usually, this is caught in type checking, but
424                 // in the case of box expr there is no such check.
425                 if !destination.projection.is_empty() {
426                     this.local_decls.push(LocalDecl::new(expr.ty, expr.span));
427                 }
428
429                 debug_assert!(Category::of(&expr.kind) == Some(Category::Place));
430
431                 let place = unpack!(block = this.as_place(block, expr));
432                 let rvalue = Rvalue::Use(this.consume_by_copy_or_move(place));
433                 this.cfg.push_assign(block, source_info, destination, rvalue);
434                 block.unit()
435             }
436
437             ExprKind::Yield { value } => {
438                 let scope = this.local_scope();
439                 let value = unpack!(block = this.as_operand(block, Some(scope), value));
440                 let resume = this.cfg.start_new_block();
441                 this.cfg.terminate(
442                     block,
443                     source_info,
444                     TerminatorKind::Yield { value, resume, resume_arg: destination, drop: None },
445                 );
446                 this.generator_drop_cleanup(block);
447                 resume.unit()
448             }
449
450             // these are the cases that are more naturally handled by some other mode
451             ExprKind::Unary { .. }
452             | ExprKind::Binary { .. }
453             | ExprKind::Box { .. }
454             | ExprKind::Cast { .. }
455             | ExprKind::Pointer { .. }
456             | ExprKind::Repeat { .. }
457             | ExprKind::Array { .. }
458             | ExprKind::Tuple { .. }
459             | ExprKind::Closure { .. }
460             | ExprKind::ConstBlock { .. }
461             | ExprKind::Literal { .. }
462             | ExprKind::ThreadLocalRef(_)
463             | ExprKind::StaticRef { .. } => {
464                 debug_assert!(match Category::of(&expr.kind).unwrap() {
465                     // should be handled above
466                     Category::Rvalue(RvalueFunc::Into) => false,
467
468                     // must be handled above or else we get an
469                     // infinite loop in the builder; see
470                     // e.g., `ExprKind::VarRef` above
471                     Category::Place => false,
472
473                     _ => true,
474                 });
475
476                 let rvalue = unpack!(block = this.as_local_rvalue(block, expr));
477                 this.cfg.push_assign(block, source_info, destination, rvalue);
478                 block.unit()
479             }
480         };
481
482         if !expr_is_block_or_scope {
483             let popped = this.block_context.pop();
484             assert!(popped.is_some());
485         }
486
487         block_and
488     }
489 }