Skip to content

Commit 4fe8dd0

Browse files
committed
Remove the non-assigning form of unpack!
This kind of unpacking can be expressed as an ordinary method on `BlockAnd<()>`.
1 parent 20ae37c commit 4fe8dd0

File tree

6 files changed

+60
-44
lines changed

6 files changed

+60
-44
lines changed

compiler/rustc_mir_build/src/build/block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
282282
)
283283
} else {
284284
let scope = (*init_scope, source_info);
285-
unpack!(this.in_scope(scope, *lint_level, |this| {
285+
let _: BlockAnd<()> = this.in_scope(scope, *lint_level, |this| {
286286
this.declare_bindings(
287287
visibility_scope,
288288
remainder_span,
@@ -291,7 +291,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
291291
None,
292292
);
293293
block.unit()
294-
}));
294+
});
295295

296296
debug!("ast_block_stmts: pattern={:?}", pattern);
297297
this.visit_primary_bindings(

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
486486
block.and(Rvalue::Aggregate(result, operands))
487487
}
488488
ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
489-
block = unpack!(this.stmt_expr(block, expr_id, None));
489+
block = this.stmt_expr(block, expr_id, None).into_block();
490490
block.and(Rvalue::Use(Operand::Constant(Box::new(ConstOperand {
491491
span: expr_span,
492492
user_ty: None,

compiler/rustc_mir_build/src/build/expr/into.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8282
// Lower the condition, and have it branch into `then` and `else` blocks.
8383
let (then_block, else_block) =
8484
this.in_if_then_scope(condition_scope, then_span, |this| {
85-
let then_blk = unpack!(this.then_else_break(
86-
block,
87-
cond,
88-
Some(condition_scope), // Temp scope
89-
source_info,
90-
DeclareLetBindings::Yes, // Declare `let` bindings normally
91-
));
85+
let then_blk = this
86+
.then_else_break(
87+
block,
88+
cond,
89+
Some(condition_scope), // Temp scope
90+
source_info,
91+
DeclareLetBindings::Yes, // Declare `let` bindings normally
92+
)
93+
.into_block();
9294

9395
// Lower the `then` arm into its block.
9496
this.expr_into_dest(destination, then_blk, then)
@@ -187,7 +189,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
187189
const_: Const::from_bool(this.tcx, constant),
188190
},
189191
);
190-
let mut rhs_block = unpack!(this.expr_into_dest(destination, continuation, rhs));
192+
let mut rhs_block =
193+
this.expr_into_dest(destination, continuation, rhs).into_block();
191194
// Instrument the lowered RHS's value for condition coverage.
192195
// (Does nothing if condition coverage is not enabled.)
193196
this.visit_coverage_standalone_condition(rhs, destination, &mut rhs_block);
@@ -230,7 +233,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
230233
// introduce a unit temporary as the destination for the loop body.
231234
let tmp = this.get_unit_temp();
232235
// Execute the body, branching back to the test.
233-
let body_block_end = unpack!(this.expr_into_dest(tmp, body_block, body));
236+
let body_block_end = this.expr_into_dest(tmp, body_block, body).into_block();
234237
this.cfg.goto(body_block_end, source_info, loop_block);
235238

236239
// Loops are only exited by `break` expressions.
@@ -462,7 +465,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
462465
targets.push(target);
463466

464467
let tmp = this.get_unit_temp();
465-
let target = unpack!(this.ast_block(tmp, target, block, source_info));
468+
let target =
469+
this.ast_block(tmp, target, block, source_info).into_block();
466470
this.cfg.terminate(
467471
target,
468472
source_info,

compiler/rustc_mir_build/src/build/matches/mod.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
121121
match expr.kind {
122122
ExprKind::LogicalOp { op: op @ LogicalOp::And, lhs, rhs } => {
123123
this.visit_coverage_branch_operation(op, expr_span);
124-
let lhs_then_block = unpack!(this.then_else_break_inner(block, lhs, args));
125-
let rhs_then_block = unpack!(this.then_else_break_inner(lhs_then_block, rhs, args));
124+
let lhs_then_block = this.then_else_break_inner(block, lhs, args).into_block();
125+
let rhs_then_block =
126+
this.then_else_break_inner(lhs_then_block, rhs, args).into_block();
126127
rhs_then_block.unit()
127128
}
128129
ExprKind::LogicalOp { op: op @ LogicalOp::Or, lhs, rhs } => {
@@ -139,14 +140,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
139140
},
140141
)
141142
});
142-
let rhs_success_block = unpack!(this.then_else_break_inner(
143-
failure_block,
144-
rhs,
145-
ThenElseArgs {
146-
declare_let_bindings: DeclareLetBindings::LetNotPermitted,
147-
..args
148-
},
149-
));
143+
let rhs_success_block = this
144+
.then_else_break_inner(
145+
failure_block,
146+
rhs,
147+
ThenElseArgs {
148+
declare_let_bindings: DeclareLetBindings::LetNotPermitted,
149+
..args
150+
},
151+
)
152+
.into_block();
150153

151154
// Make the LHS and RHS success arms converge to a common block.
152155
// (We can't just make LHS goto RHS, because `rhs_success_block`
@@ -451,7 +454,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
451454
outer_source_info: SourceInfo,
452455
fake_borrow_temps: Vec<(Place<'tcx>, Local, FakeBorrowKind)>,
453456
) -> BlockAnd<()> {
454-
let arm_end_blocks: Vec<_> = arm_candidates
457+
let arm_end_blocks: Vec<BasicBlock> = arm_candidates
455458
.into_iter()
456459
.map(|(arm, candidate)| {
457460
debug!("lowering arm {:?}\ncandidate = {:?}", arm, candidate);
@@ -502,6 +505,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
502505

503506
this.expr_into_dest(destination, arm_block, arm.body)
504507
})
508+
.into_block()
505509
})
506510
.collect();
507511

@@ -512,10 +516,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
512516
outer_source_info.span.with_lo(outer_source_info.span.hi() - BytePos::from_usize(1)),
513517
);
514518
for arm_block in arm_end_blocks {
515-
let block = &self.cfg.basic_blocks[arm_block.0];
519+
let block = &self.cfg.basic_blocks[arm_block];
516520
let last_location = block.statements.last().map(|s| s.source_info);
517521

518-
self.cfg.goto(unpack!(arm_block), last_location.unwrap_or(end_brace), end_block);
522+
self.cfg.goto(arm_block, last_location.unwrap_or(end_brace), end_block);
519523
}
520524

521525
self.source_scope = outer_source_info.scope;

compiler/rustc_mir_build/src/build/mod.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,15 @@ enum NeedsTemporary {
403403
#[must_use = "if you don't use one of these results, you're leaving a dangling edge"]
404404
struct BlockAnd<T>(BasicBlock, T);
405405

406+
impl BlockAnd<()> {
407+
/// Unpacks `BlockAnd<()>` into a [`BasicBlock`].
408+
#[must_use]
409+
fn into_block(self) -> BasicBlock {
410+
let Self(block, ()) = self;
411+
block
412+
}
413+
}
414+
406415
trait BlockAndExtension {
407416
fn and<T>(self, v: T) -> BlockAnd<T>;
408417
fn unit(self) -> BlockAnd<()>;
@@ -426,11 +435,6 @@ macro_rules! unpack {
426435
$x = b;
427436
v
428437
}};
429-
430-
($c:expr) => {{
431-
let BlockAnd(b, ()) = $c;
432-
b
433-
}};
434438
}
435439

436440
///////////////////////////////////////////////////////////////////////////
@@ -516,21 +520,22 @@ fn construct_fn<'tcx>(
516520
region::Scope { id: body.id().hir_id.local_id, data: region::ScopeData::Arguments };
517521
let source_info = builder.source_info(span);
518522
let call_site_s = (call_site_scope, source_info);
519-
unpack!(builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
523+
let _: BlockAnd<()> = builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
520524
let arg_scope_s = (arg_scope, source_info);
521525
// Attribute epilogue to function's closing brace
522526
let fn_end = span_with_body.shrink_to_hi();
523-
let return_block =
524-
unpack!(builder.in_breakable_scope(None, Place::return_place(), fn_end, |builder| {
527+
let return_block = builder
528+
.in_breakable_scope(None, Place::return_place(), fn_end, |builder| {
525529
Some(builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| {
526530
builder.args_and_body(START_BLOCK, arguments, arg_scope, expr)
527531
}))
528-
}));
532+
})
533+
.into_block();
529534
let source_info = builder.source_info(fn_end);
530535
builder.cfg.terminate(return_block, source_info, TerminatorKind::Return);
531536
builder.build_drop_trees();
532537
return_block.unit()
533-
}));
538+
});
534539

535540
let mut body = builder.finish();
536541

compiler/rustc_mir_build/src/build/scope.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
510510
let target = self.cfg.start_new_block();
511511
let source_info = self.source_info(span);
512512
self.cfg.terminate(
513-
unpack!(normal_block),
513+
normal_block.into_block(),
514514
source_info,
515515
TerminatorKind::Goto { target },
516516
);
517517
self.cfg.terminate(
518-
unpack!(exit_block),
518+
exit_block.into_block(),
519519
source_info,
520520
TerminatorKind::Goto { target },
521521
);
@@ -552,14 +552,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
552552
let scope = IfThenScope { region_scope, else_drops: DropTree::new() };
553553
let previous_scope = mem::replace(&mut self.scopes.if_then_scope, Some(scope));
554554

555-
let then_block = unpack!(f(self));
555+
let then_block = f(self).into_block();
556556

557557
let if_then_scope = mem::replace(&mut self.scopes.if_then_scope, previous_scope).unwrap();
558558
assert!(if_then_scope.region_scope == region_scope);
559559

560-
let else_block = self
561-
.build_exit_tree(if_then_scope.else_drops, region_scope, span, None)
562-
.map_or_else(|| self.cfg.start_new_block(), |else_block_and| unpack!(else_block_and));
560+
let else_block =
561+
self.build_exit_tree(if_then_scope.else_drops, region_scope, span, None).map_or_else(
562+
|| self.cfg.start_new_block(),
563+
|else_block_and| else_block_and.into_block(),
564+
);
563565

564566
(then_block, else_block)
565567
}
@@ -753,15 +755,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
753755
let unwind_to = if needs_cleanup { self.diverge_cleanup() } else { DropIdx::MAX };
754756

755757
let scope = self.scopes.scopes.last().expect("leave_top_scope called with no scopes");
756-
unpack!(build_scope_drops(
758+
build_scope_drops(
757759
&mut self.cfg,
758760
&mut self.scopes.unwind_drops,
759761
scope,
760762
block,
761763
unwind_to,
762764
is_coroutine && needs_cleanup,
763765
self.arg_count,
764-
))
766+
)
767+
.into_block()
765768
}
766769

767770
/// Possibly creates a new source scope if `current_root` and `parent_root`

0 commit comments

Comments
 (0)