From 8357e4ede76093df71bc2ed560620d751e5d7b30 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 6 Jul 2024 12:23:14 +1000 Subject: [PATCH] Stop using the `unpack!` macro --- compiler/rustc_mir_build/src/build/block.rs | 2 +- .../src/build/expr/as_operand.rs | 6 +- .../src/build/expr/as_place.rs | 32 +++-- .../src/build/expr/as_rvalue.rs | 136 ++++++------------ .../rustc_mir_build/src/build/expr/into.rs | 69 ++++----- .../rustc_mir_build/src/build/expr/stmt.rs | 24 ++-- .../rustc_mir_build/src/build/matches/mod.rs | 13 +- compiler/rustc_mir_build/src/build/mod.rs | 32 +++-- compiler/rustc_mir_build/src/build/scope.rs | 4 +- 9 files changed, 140 insertions(+), 178 deletions(-) diff --git a/compiler/rustc_mir_build/src/build/block.rs b/compiler/rustc_mir_build/src/build/block.rs index 6831d5bd94718..5c85ff6b46157 100644 --- a/compiler/rustc_mir_build/src/build/block.rs +++ b/compiler/rustc_mir_build/src/build/block.rs @@ -226,7 +226,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { }); matching.and(failure) }); - let failure = unpack!(block = failure_and_block); + let failure = failure_and_block.unpack(&mut block); this.cfg.goto(failure, source_info, failure_entry); if let Some(source_scope) = visibility_scope { diff --git a/compiler/rustc_mir_build/src/build/expr/as_operand.rs b/compiler/rustc_mir_build/src/build/expr/as_operand.rs index 09ce134a2bf6c..abfc934549ef0 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_operand.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_operand.rs @@ -128,7 +128,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { block.and(Operand::Constant(Box::new(constant))) } Category::Constant | Category::Place | Category::Rvalue(..) => { - let operand = unpack!(block = this.as_temp(block, scope, expr_id, Mutability::Mut)); + let operand = + this.as_temp(block, scope, expr_id, Mutability::Mut).unpack(&mut block); // Overwrite temp local info if we have something more interesting to record. if !matches!(local_info, LocalInfo::Boring) { let decl_info = @@ -174,7 +175,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // type, and that value is coming from the deref of a box. if let ExprKind::Deref { arg } = expr.kind { // Generate let tmp0 = arg0 - let operand = unpack!(block = this.as_temp(block, scope, arg, Mutability::Mut)); + let operand = + this.as_temp(block, scope, arg, Mutability::Mut).unpack(&mut block); // Return the operand *tmp0 to be used as the call argument let place = Place { diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index 91a3b53cc79c7..5b8291a1807e9 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -369,7 +369,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { mut block: BasicBlock, expr_id: ExprId, ) -> BlockAnd> { - let place_builder = unpack!(block = self.as_place_builder(block, expr_id)); + let place_builder = self.as_place_builder(block, expr_id).unpack(&mut block); block.and(place_builder.to_place(self)) } @@ -393,7 +393,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { mut block: BasicBlock, expr_id: ExprId, ) -> BlockAnd> { - let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr_id)); + let place_builder = self.as_read_only_place_builder(block, expr_id).unpack(&mut block); block.and(place_builder.to_place(self)) } @@ -432,8 +432,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } ExprKind::Field { lhs, variant_index, name } => { let lhs_expr = &this.thir[lhs]; - let mut place_builder = - unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,)); + let mut place_builder = this + .expr_as_place(block, lhs, mutability, fake_borrow_temps) + .unpack(&mut block); if let ty::Adt(adt_def, _) = lhs_expr.ty.kind() { if adt_def.is_enum() { place_builder = place_builder.downcast(*adt_def, variant_index); @@ -442,8 +443,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { block.and(place_builder.field(name, expr.ty)) } ExprKind::Deref { arg } => { - let place_builder = - unpack!(block = this.expr_as_place(block, arg, mutability, fake_borrow_temps,)); + let place_builder = this + .expr_as_place(block, arg, mutability, fake_borrow_temps) + .unpack(&mut block); block.and(place_builder.deref()) } ExprKind::Index { lhs, index } => this.lower_index_expression( @@ -472,9 +474,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } ExprKind::PlaceTypeAscription { source, ref user_ty } => { - let place_builder = unpack!( - block = this.expr_as_place(block, source, mutability, fake_borrow_temps,) - ); + let place_builder = this + .expr_as_place(block, source, mutability, fake_borrow_temps) + .unpack(&mut block); if let Some(user_ty) = user_ty { let annotation_index = this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { @@ -502,9 +504,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } ExprKind::ValueTypeAscription { source, ref user_ty } => { let source_expr = &this.thir[source]; - let temp = unpack!( - block = this.as_temp(block, source_expr.temp_lifetime, source, mutability) - ); + let temp = this + .as_temp(block, source_expr.temp_lifetime, source, mutability) + .unpack(&mut block); if let Some(user_ty) = user_ty { let annotation_index = this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { @@ -570,7 +572,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // these are not places, so we need to make a temporary. debug_assert!(!matches!(Category::of(&expr.kind), Some(Category::Place))); let temp = - unpack!(block = this.as_temp(block, expr.temp_lifetime, expr_id, mutability)); + this.as_temp(block, expr.temp_lifetime, expr_id, mutability).unpack(&mut block); block.and(PlaceBuilder::from(temp)) } } @@ -612,12 +614,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let fake_borrow_temps = fake_borrow_temps.unwrap_or(base_fake_borrow_temps); let base_place = - unpack!(block = self.expr_as_place(block, base, mutability, Some(fake_borrow_temps),)); + self.expr_as_place(block, base, mutability, Some(fake_borrow_temps)).unpack(&mut block); // Making this a *fresh* temporary means we do not have to worry about // the index changing later: Nothing will ever change this temporary. // The "retagging" transformation (for Stacked Borrows) relies on this. - let idx = unpack!(block = self.as_temp(block, temp_lifetime, index, Mutability::Not)); + let idx = self.as_temp(block, temp_lifetime, index, Mutability::Not).unpack(&mut block); block = self.bounds_check(block, &base_place, idx, expr_span, source_info); diff --git a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs index 8238d4a44008b..6f916e16cf267 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs @@ -60,39 +60,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { if Some(0) == count.try_eval_target_usize(this.tcx, this.param_env) { this.build_zero_repeat(block, value, scope, source_info) } else { - let value_operand = unpack!( - block = this.as_operand( - block, - scope, - value, - LocalInfo::Boring, - NeedsTemporary::No - ) - ); + let value_operand = this + .as_operand(block, scope, value, LocalInfo::Boring, NeedsTemporary::No) + .unpack(&mut block); block.and(Rvalue::Repeat(value_operand, count)) } } ExprKind::Binary { op, lhs, rhs } => { - let lhs = unpack!( - block = this.as_operand( - block, - scope, - lhs, - LocalInfo::Boring, - NeedsTemporary::Maybe - ) - ); - let rhs = unpack!( - block = - this.as_operand(block, scope, rhs, LocalInfo::Boring, NeedsTemporary::No) - ); + let lhs = this + .as_operand(block, scope, lhs, LocalInfo::Boring, NeedsTemporary::Maybe) + .unpack(&mut block); + let rhs = this + .as_operand(block, scope, rhs, LocalInfo::Boring, NeedsTemporary::No) + .unpack(&mut block); this.build_binary_op(block, op, expr_span, expr.ty, lhs, rhs) } ExprKind::Unary { op, arg } => { - let arg = unpack!( - block = - this.as_operand(block, scope, arg, LocalInfo::Boring, NeedsTemporary::No) - ); + let arg = this + .as_operand(block, scope, arg, LocalInfo::Boring, NeedsTemporary::No) + .unpack(&mut block); // Check for -MIN on signed integers if this.check_overflow && op == UnOp::Neg && expr.ty.is_signed() { let bool_ty = this.tcx.types.bool; @@ -200,7 +186,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { && adt_def.is_enum() { let discr_ty = adt_def.repr().discr_type().to_ty(this.tcx); - let temp = unpack!(block = this.as_temp(block, scope, source, Mutability::Not)); + let temp = + this.as_temp(block, scope, source, Mutability::Not).unpack(&mut block); let layout = this.tcx.layout_of(this.param_env.and(source_expr.ty)); let discr = this.temp(discr_ty, source_expr.span); this.cfg.push_assign( @@ -282,15 +269,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { (op, ty) } else { let ty = source_expr.ty; - let source = unpack!( - block = this.as_operand( - block, - scope, - source, - LocalInfo::Boring, - NeedsTemporary::No - ) - ); + let source = this + .as_operand(block, scope, source, LocalInfo::Boring, NeedsTemporary::No) + .unpack(&mut block); (source, ty) }; let from_ty = CastTy::from_ty(ty); @@ -300,15 +281,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { block.and(Rvalue::Cast(cast_kind, source, expr.ty)) } ExprKind::PointerCoercion { cast, source } => { - let source = unpack!( - block = this.as_operand( - block, - scope, - source, - LocalInfo::Boring, - NeedsTemporary::No - ) - ); + let source = this + .as_operand(block, scope, source, LocalInfo::Boring, NeedsTemporary::No) + .unpack(&mut block); block.and(Rvalue::Cast(CastKind::PointerCoercion(cast), source, expr.ty)) } ExprKind::Array { ref fields } => { @@ -344,15 +319,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { .into_iter() .copied() .map(|f| { - unpack!( - block = this.as_operand( - block, - scope, - f, - LocalInfo::Boring, - NeedsTemporary::Maybe - ) - ) + this.as_operand(block, scope, f, LocalInfo::Boring, NeedsTemporary::Maybe) + .unpack(&mut block) }) .collect(); @@ -365,15 +333,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { .into_iter() .copied() .map(|f| { - unpack!( - block = this.as_operand( - block, - scope, - f, - LocalInfo::Boring, - NeedsTemporary::Maybe - ) - ) + this.as_operand(block, scope, f, LocalInfo::Boring, NeedsTemporary::Maybe) + .unpack(&mut block) }) .collect(); @@ -401,7 +362,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // ``` // for (thir_place, cause, hir_id) in fake_reads.into_iter() { - let place_builder = unpack!(block = this.as_place_builder(block, *thir_place)); + let place_builder = + this.as_place_builder(block, *thir_place).unpack(&mut block); if let Some(mir_place) = place_builder.try_to_place(this) { this.cfg.push_fake_read( @@ -429,7 +391,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // This occurs when capturing by copy/move, while // by reference captures use as_operand Some(Category::Place) => { - let place = unpack!(block = this.as_place(block, upvar)); + let place = this.as_place(block, upvar).unpack(&mut block); this.consume_by_copy_or_move(place) } _ => { @@ -442,26 +404,24 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { borrow_kind: BorrowKind::Mut { kind: MutBorrowKind::Default }, arg, - } => unpack!( - block = this.limit_capture_mutability( + } => this + .limit_capture_mutability( upvar_expr.span, upvar_expr.ty, scope, block, arg, ) - ), - _ => { - unpack!( - block = this.as_operand( - block, - scope, - upvar, - LocalInfo::Boring, - NeedsTemporary::Maybe - ) + .unpack(&mut block), + _ => this + .as_operand( + block, + scope, + upvar, + LocalInfo::Boring, + NeedsTemporary::Maybe, ) - } + .unpack(&mut block), } } } @@ -536,15 +496,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { Category::of(&expr.kind), Some(Category::Rvalue(RvalueFunc::AsRvalue) | Category::Constant) )); - let operand = unpack!( - block = this.as_operand( - block, - scope, - expr_id, - LocalInfo::Boring, - NeedsTemporary::No, - ) - ); + let operand = this + .as_operand(block, scope, expr_id, LocalInfo::Boring, NeedsTemporary::No) + .unpack(&mut block); block.and(Rvalue::Use(operand)) } } @@ -716,9 +670,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // Repeating a const does nothing } else { // For a non-const, we may need to generate an appropriate `Drop` - let value_operand = unpack!( - block = this.as_operand(block, scope, value, LocalInfo::Boring, NeedsTemporary::No) - ); + let value_operand = this + .as_operand(block, scope, value, LocalInfo::Boring, NeedsTemporary::No) + .unpack(&mut block); if let Operand::Move(to_drop) = value_operand { let success = this.cfg.start_new_block(); this.cfg.terminate( @@ -754,7 +708,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { this.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) }); - let arg_place_builder = unpack!(block = this.as_place_builder(block, arg)); + let arg_place_builder = this.as_place_builder(block, arg).unpack(&mut block); let mutability = match arg_place_builder.base() { // We are capturing a path that starts off a local variable in the parent. diff --git a/compiler/rustc_mir_build/src/build/expr/into.rs b/compiler/rustc_mir_build/src/build/expr/into.rs index cd249e34ff87e..95fd7f2477e47 100644 --- a/compiler/rustc_mir_build/src/build/expr/into.rs +++ b/compiler/rustc_mir_build/src/build/expr/into.rs @@ -102,8 +102,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { ); // Unpack `BlockAnd` into `(then_blk, else_blk)`. - let (then_blk, mut else_blk); - else_blk = unpack!(then_blk = then_and_else_blocks); + let (then_blk, mut else_blk) = then_and_else_blocks.unpack_to_pair(); // If there is an `else` arm, lower it into `else_blk`. if let Some(else_expr) = else_opt { @@ -138,9 +137,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // (#66975) Source could be a const of type `!`, so has to // exist in the generated MIR. - unpack!( - block = this.as_temp(block, Some(this.local_scope()), source, Mutability::Mut) - ); + let _: Local = this + .as_temp(block, Some(this.local_scope()), source, Mutability::Mut) + .unpack(&mut block); // This is an optimization. If the expression was a call then we already have an // unreachable block. Don't bother to terminate it and create a new one. @@ -244,12 +243,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { }) } ExprKind::Call { ty: _, fun, ref args, from_hir_call, fn_span } => { - let fun = unpack!(block = this.as_local_operand(block, fun)); + let fun = this.as_local_operand(block, fun).unpack(&mut block); let args: Box<[_]> = args .into_iter() .copied() .map(|arg| Spanned { - node: unpack!(block = this.as_local_call_operand(block, arg)), + node: this.as_local_call_operand(block, arg).unpack(&mut block), span: this.thir.exprs[arg].span, }) .collect(); @@ -295,10 +294,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // by this method anyway, so this shouldn't cause too many // unnecessary temporaries. let arg_place = match borrow_kind { - BorrowKind::Shared => { - unpack!(block = this.as_read_only_place(block, arg)) - } - _ => unpack!(block = this.as_place(block, arg)), + BorrowKind::Shared => this.as_read_only_place(block, arg).unpack(&mut block), + _ => this.as_place(block, arg).unpack(&mut block), }; let borrow = Rvalue::Ref(this.tcx.lifetimes.re_erased, borrow_kind, arg_place); this.cfg.push_assign(block, source_info, destination, borrow); @@ -309,7 +306,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { hir::Mutability::Not => this.as_read_only_place(block, arg), hir::Mutability::Mut => this.as_place(block, arg), }; - let address_of = Rvalue::AddressOf(mutability, unpack!(block = place)); + let address_of = Rvalue::AddressOf(mutability, place.unpack(&mut block)); this.cfg.push_assign(block, source_info, destination, address_of); block.unit() } @@ -335,15 +332,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { .map(|f| { ( f.name, - unpack!( - block = this.as_operand( - block, - Some(scope), - f.expr, - LocalInfo::AggregateTemp, - NeedsTemporary::Maybe, - ) - ), + this.as_operand( + block, + Some(scope), + f.expr, + LocalInfo::AggregateTemp, + NeedsTemporary::Maybe, + ) + .unpack(&mut block), ) }) .collect(); @@ -351,7 +347,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let field_names = adt_def.variant(variant_index).fields.indices(); let fields = if let Some(FruInfo { base, field_types }) = base { - let place_builder = unpack!(block = this.as_place_builder(block, *base)); + let place_builder = this.as_place_builder(block, *base).unpack(&mut block); // MIR does not natively support FRU, so for each // base-supplied field, generate an operand that @@ -412,17 +408,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { .map(|op| match *op { thir::InlineAsmOperand::In { reg, expr } => mir::InlineAsmOperand::In { reg, - value: unpack!(block = this.as_local_operand(block, expr)), + value: this.as_local_operand(block, expr).unpack(&mut block), }, thir::InlineAsmOperand::Out { reg, late, expr } => { mir::InlineAsmOperand::Out { reg, late, - place: expr.map(|expr| unpack!(block = this.as_place(block, expr))), + place: expr + .map(|expr| this.as_place(block, expr).unpack(&mut block)), } } thir::InlineAsmOperand::InOut { reg, late, expr } => { - let place = unpack!(block = this.as_place(block, expr)); + let place = this.as_place(block, expr).unpack(&mut block); mir::InlineAsmOperand::InOut { reg, late, @@ -435,9 +432,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { mir::InlineAsmOperand::InOut { reg, late, - in_value: unpack!(block = this.as_local_operand(block, in_expr)), + in_value: this.as_local_operand(block, in_expr).unpack(&mut block), out_place: out_expr.map(|out_expr| { - unpack!(block = this.as_place(block, out_expr)) + this.as_place(block, out_expr).unpack(&mut block) }), } } @@ -531,7 +528,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | ExprKind::ValueTypeAscription { .. } => { debug_assert!(Category::of(&expr.kind) == Some(Category::Place)); - let place = unpack!(block = this.as_place(block, expr_id)); + let place = this.as_place(block, expr_id).unpack(&mut block); let rvalue = Rvalue::Use(this.consume_by_copy_or_move(place)); this.cfg.push_assign(block, source_info, destination, rvalue); block.unit() @@ -546,7 +543,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { this.local_decls.push(LocalDecl::new(expr.ty, expr.span)); } - let place = unpack!(block = this.as_place(block, expr_id)); + let place = this.as_place(block, expr_id).unpack(&mut block); let rvalue = Rvalue::Use(this.consume_by_copy_or_move(place)); this.cfg.push_assign(block, source_info, destination, rvalue); block.unit() @@ -554,15 +551,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { ExprKind::Yield { value } => { let scope = this.local_scope(); - let value = unpack!( - block = this.as_operand( - block, - Some(scope), - value, - LocalInfo::Boring, - NeedsTemporary::No - ) - ); + let value = this + .as_operand(block, Some(scope), value, LocalInfo::Boring, NeedsTemporary::No) + .unpack(&mut block); let resume = this.cfg.start_new_block(); this.cfg.terminate( block, @@ -604,7 +595,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { _ => true, }); - let rvalue = unpack!(block = this.as_local_rvalue(block, expr_id)); + let rvalue = this.as_local_rvalue(block, expr_id).unpack(&mut block); this.cfg.push_assign(block, source_info, destination, rvalue); block.unit() } diff --git a/compiler/rustc_mir_build/src/build/expr/stmt.rs b/compiler/rustc_mir_build/src/build/expr/stmt.rs index a1fb4b788c866..c49b59a098a3c 100644 --- a/compiler/rustc_mir_build/src/build/expr/stmt.rs +++ b/compiler/rustc_mir_build/src/build/expr/stmt.rs @@ -40,14 +40,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // Generate better code for things that don't need to be // dropped. if lhs_expr.ty.needs_drop(this.tcx, this.param_env) { - let rhs = unpack!(block = this.as_local_rvalue(block, rhs)); - let lhs = unpack!(block = this.as_place(block, lhs)); + let rhs = this.as_local_rvalue(block, rhs).unpack(&mut block); + let lhs = this.as_place(block, lhs).unpack(&mut block); block = this .build_drop_and_replace(block, lhs_expr.span, lhs, rhs) .unpack_block_and_unit(); } else { - let rhs = unpack!(block = this.as_local_rvalue(block, rhs)); - let lhs = unpack!(block = this.as_place(block, lhs)); + let rhs = this.as_local_rvalue(block, rhs).unpack(&mut block); + let lhs = this.as_place(block, lhs).unpack(&mut block); this.cfg.push_assign(block, source_info, lhs, rhs); } @@ -69,16 +69,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { this.block_context.push(BlockFrame::SubExpr); // As above, RTL. - let rhs = unpack!(block = this.as_local_operand(block, rhs)); - let lhs = unpack!(block = this.as_place(block, lhs)); + let rhs = this.as_local_operand(block, rhs).unpack(&mut block); + let lhs = this.as_place(block, lhs).unpack(&mut block); // we don't have to drop prior contents or anything // because AssignOp is only legal for Copy types // (overloaded ops should be desugared into a call). - let result = unpack!( - block = - this.build_binary_op(block, op, expr_span, lhs_ty, Operand::Copy(lhs), rhs) - ); + let result = this + .build_binary_op(block, op, expr_span, lhs_ty, Operand::Copy(lhs), rhs) + .unpack(&mut block); this.cfg.push_assign(block, source_info, lhs, result); this.block_context.pop(); @@ -137,8 +136,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { None }; - let temp = - unpack!(block = this.as_temp(block, statement_scope, expr_id, Mutability::Not)); + let temp = this + .as_temp(block, statement_scope, expr_id, Mutability::Not) + .unpack(&mut block); if let Some(span) = adjusted_span { this.local_decls[temp].source_info.span = span; diff --git a/compiler/rustc_mir_build/src/build/matches/mod.rs b/compiler/rustc_mir_build/src/build/matches/mod.rs index a2fbd6a03eaf2..8ed26ed00fa3f 100644 --- a/compiler/rustc_mir_build/src/build/matches/mod.rs +++ b/compiler/rustc_mir_build/src/build/matches/mod.rs @@ -213,7 +213,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // further down. this.mcdc_increment_depth_if_enabled(); let place = - unpack!(block = this.as_temp(block, Some(temp_scope), expr_id, mutability)); + this.as_temp(block, Some(temp_scope), expr_id, mutability).unpack(&mut block); this.mcdc_decrement_depth_if_enabled(); let operand = Operand::Move(Place::from(place)); @@ -361,7 +361,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { scrutinee_span: Span, ) -> BlockAnd<()> { let scrutinee_place = - unpack!(block = self.lower_scrutinee(block, scrutinee_id, scrutinee_span)); + self.lower_scrutinee(block, scrutinee_id, scrutinee_span).unpack(&mut block); let mut arm_candidates = self.create_match_candidates(&scrutinee_place, arms); @@ -405,7 +405,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { scrutinee_id: ExprId, scrutinee_span: Span, ) -> BlockAnd> { - let scrutinee_place_builder = unpack!(block = self.as_place_builder(block, scrutinee_id)); + let scrutinee_place_builder = self.as_place_builder(block, scrutinee_id).unpack(&mut block); if let Some(scrutinee_place) = scrutinee_place_builder.try_to_place(self) { let source_info = self.source_info(scrutinee_span); self.cfg.push_place_mention(block, source_info, scrutinee_place); @@ -706,8 +706,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { _ => { let initializer = &self.thir[initializer_id]; - let place_builder = - unpack!(block = self.lower_scrutinee(block, initializer_id, initializer.span)); + let place_builder = self + .lower_scrutinee(block, initializer_id, initializer.span) + .unpack(&mut block); self.place_into_pattern(block, irrefutable_pat, place_builder, true) } } @@ -2078,7 +2079,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { emit_storage_live: EmitStorageLive, ) -> BlockAnd<()> { let expr_span = self.thir[expr_id].span; - let scrutinee = unpack!(block = self.lower_scrutinee(block, expr_id, expr_span)); + let scrutinee = self.lower_scrutinee(block, expr_id, expr_span).unpack(&mut block); let mut candidate = Candidate::new(scrutinee.clone(), pat, false, self); let otherwise_block = self.lower_match_tree( block, diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs index 18014b4118559..4df1fd7bf8d0a 100644 --- a/compiler/rustc_mir_build/src/build/mod.rs +++ b/compiler/rustc_mir_build/src/build/mod.rs @@ -403,6 +403,28 @@ enum NeedsTemporary { #[must_use = "if you don't use one of these results, you're leaving a dangling edge"] struct BlockAnd(BasicBlock, T); +impl BlockAnd { + /// Unpacks `BlockAnd` by writing the block to the given reference, + /// and then returning the value. + #[must_use] + fn unpack(self, block_mut: &mut BasicBlock) -> T { + let Self(block, value) = self; + *block_mut = block; + value + } + + /// Unpacks `BlockAnd` into `(BasicBlock, T)`. + /// + /// This is an alternative to [`Self::unpack`] for cases where the block + /// variable hasn't been initialized yet, so a `&mut` reference can't be + /// taken. + #[must_use] + fn unpack_to_pair(self) -> (BasicBlock, T) { + let Self(block, value) = self; + (block, value) + } +} + impl BlockAnd<()> { /// Unpacks `BlockAnd<()>` into a [`BasicBlock`]. #[must_use] @@ -427,16 +449,6 @@ impl BlockAndExtension for BasicBlock { } } -/// Update a block pointer and return the value. -/// Use it like `let x = unpack!(block = self.foo(block, foo))`. -macro_rules! unpack { - ($x:ident = $c:expr) => {{ - let BlockAnd(b, v) = $c; - $x = b; - v - }}; -} - /////////////////////////////////////////////////////////////////////////// /// the main entry point for building MIR for a function diff --git a/compiler/rustc_mir_build/src/build/scope.rs b/compiler/rustc_mir_build/src/build/scope.rs index 471ade2c449fb..5e0ead9ffeda1 100644 --- a/compiler/rustc_mir_build/src/build/scope.rs +++ b/compiler/rustc_mir_build/src/build/scope.rs @@ -585,8 +585,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { self.maybe_new_source_scope(region_scope.1.span, current_hir_id, parent_id); } self.push_scope(region_scope); - let mut block; - let rv = unpack!(block = f(self)); + + let (mut block, rv) = f(self).unpack_to_pair(); block = self.pop_scope(region_scope, block).unpack_block_and_unit(); self.source_scope = source_scope; debug!(?block);