Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MIR building: Stop using unpack! for BlockAnd<()> #127472

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions compiler/rustc_mir_build/src/build/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
StmtKind::Expr { scope, expr } => {
this.block_context.push(BlockFrame::Statement { ignores_expr_result: true });
let si = (*scope, source_info);
unpack!(
block = this.in_scope(si, LintLevel::Inherited, |this| {
block = this
.in_scope(si, LintLevel::Inherited, |this| {
this.stmt_expr(block, *expr, Some(*scope))
})
);
.into_block();
}
StmtKind::Let {
remainder_scope,
Expand Down Expand Up @@ -166,14 +166,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let dummy_place = this.temp(this.tcx.types.never, else_block_span);
let failure_entry = this.cfg.start_new_block();
let failure_block;
unpack!(
failure_block = this.ast_block(
failure_block = this
.ast_block(
dummy_place,
failure_entry,
*else_block,
this.source_info(else_block_span),
)
);
.into_block();
this.cfg.terminate(
failure_block,
this.source_info(else_block_span),
Expand Down Expand Up @@ -267,8 +267,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let initializer_span = this.thir[init].span;
let scope = (*init_scope, source_info);

unpack!(
block = this.in_scope(scope, *lint_level, |this| {
block = this
.in_scope(scope, *lint_level, |this| {
this.declare_bindings(
visibility_scope,
remainder_span,
Expand All @@ -279,10 +279,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
this.expr_into_pattern(block, &pattern, init)
// irrefutable pattern
})
)
.into_block();
} else {
let scope = (*init_scope, source_info);
unpack!(this.in_scope(scope, *lint_level, |this| {
let _: BlockAnd<()> = this.in_scope(scope, *lint_level, |this| {
this.declare_bindings(
visibility_scope,
remainder_span,
Expand All @@ -291,7 +291,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
None,
);
block.unit()
}));
});

debug!("ast_block_stmts: pattern={:?}", pattern);
this.visit_primary_bindings(
Expand Down Expand Up @@ -333,7 +333,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
this.block_context
.push(BlockFrame::TailExpr { tail_result_is_ignored, span: expr.span });

unpack!(block = this.expr_into_dest(destination, block, expr_id));
block = this.expr_into_dest(destination, block, expr_id).into_block();
let popped = this.block_context.pop();

assert!(popped.is_some_and(|bf| bf.is_tail_expr()));
Expand All @@ -355,7 +355,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// Finally, we pop all the let scopes before exiting out from the scope of block
// itself.
for scope in let_scope_stack.into_iter().rev() {
unpack!(block = this.pop_scope((*scope, source_info), block));
block = this.pop_scope((*scope, source_info), block).into_block();
}
// Restore the original source scope.
this.source_scope = outer_source_scope;
Expand Down
12 changes: 4 additions & 8 deletions compiler/rustc_mir_build/src/build/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
this.cfg.push_assign(block, source_info, Place::from(result), box_);

// initialize the box contents:
unpack!(
block = this.expr_into_dest(
this.tcx.mk_place_deref(Place::from(result)),
block,
value,
)
);
block = this
.expr_into_dest(this.tcx.mk_place_deref(Place::from(result)), block, value)
.into_block();
block.and(Rvalue::Use(Operand::Move(Place::from(result))))
}
ExprKind::Cast { source } => {
Expand Down Expand Up @@ -486,7 +482,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block.and(Rvalue::Aggregate(result, operands))
}
ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
block = unpack!(this.stmt_expr(block, expr_id, None));
block = this.stmt_expr(block, expr_id, None).into_block();
block.and(Rvalue::Use(Operand::Constant(Box::new(ConstOperand {
span: expr_span,
user_ty: None,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/build/expr/as_temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
}

unpack!(block = this.expr_into_dest(temp_place, block, expr_id));
block = this.expr_into_dest(temp_place, block, expr_id).into_block();

if let Some(temp_lifetime) = temp_lifetime {
this.schedule_drop(expr_span, temp_lifetime, temp, DropKind::Value);
Expand Down
30 changes: 17 additions & 13 deletions compiler/rustc_mir_build/src/build/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// Lower the condition, and have it branch into `then` and `else` blocks.
let (then_block, else_block) =
this.in_if_then_scope(condition_scope, then_span, |this| {
let then_blk = unpack!(this.then_else_break(
block,
cond,
Some(condition_scope), // Temp scope
source_info,
DeclareLetBindings::Yes, // Declare `let` bindings normally
));
let then_blk = this
.then_else_break(
block,
cond,
Some(condition_scope), // Temp scope
source_info,
DeclareLetBindings::Yes, // Declare `let` bindings normally
)
.into_block();

// Lower the `then` arm into its block.
this.expr_into_dest(destination, then_blk, then)
Expand All @@ -105,7 +107,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

// If there is an `else` arm, lower it into `else_blk`.
if let Some(else_expr) = else_opt {
unpack!(else_blk = this.expr_into_dest(destination, else_blk, else_expr));
else_blk = this.expr_into_dest(destination, else_blk, else_expr).into_block();
} else {
// There is no `else` arm, so we know both arms have type `()`.
// Generate the implicit `else {}` by assigning unit.
Expand Down Expand Up @@ -187,7 +189,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
const_: Const::from_bool(this.tcx, constant),
},
);
let mut rhs_block = unpack!(this.expr_into_dest(destination, continuation, rhs));
let mut rhs_block =
this.expr_into_dest(destination, continuation, rhs).into_block();
// Instrument the lowered RHS's value for condition coverage.
// (Does nothing if condition coverage is not enabled.)
this.visit_coverage_standalone_condition(rhs, destination, &mut rhs_block);
Expand Down Expand Up @@ -230,7 +233,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// introduce a unit temporary as the destination for the loop body.
let tmp = this.get_unit_temp();
// Execute the body, branching back to the test.
let body_block_end = unpack!(this.expr_into_dest(tmp, body_block, body));
let body_block_end = this.expr_into_dest(tmp, body_block, body).into_block();
this.cfg.goto(body_block_end, source_info, loop_block);

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

let tmp = this.get_unit_temp();
let target = unpack!(this.ast_block(tmp, target, block, source_info));
let target =
this.ast_block(tmp, target, block, source_info).into_block();
this.cfg.terminate(
target,
source_info,
Expand Down Expand Up @@ -502,7 +506,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

// These cases don't actually need a destination
ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
unpack!(block = this.stmt_expr(block, expr_id, None));
block = this.stmt_expr(block, expr_id, None).into_block();
this.cfg.push_assign_unit(block, source_info, destination, this.tcx);
block.unit()
}
Expand All @@ -511,7 +515,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
| ExprKind::Break { .. }
| ExprKind::Return { .. }
| ExprKind::Become { .. } => {
unpack!(block = this.stmt_expr(block, expr_id, None));
block = this.stmt_expr(block, expr_id, None).into_block();
// No assign, as these have type `!`.
block.unit()
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_mir_build/src/build/expr/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
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));
unpack!(block = this.build_drop_and_replace(block, lhs_expr.span, lhs, rhs));
block =
this.build_drop_and_replace(block, lhs_expr.span, lhs, rhs).into_block();
} else {
let rhs = unpack!(block = this.as_local_rvalue(block, rhs));
let lhs = unpack!(block = this.as_place(block, lhs));
Expand Down
34 changes: 19 additions & 15 deletions compiler/rustc_mir_build/src/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
match expr.kind {
ExprKind::LogicalOp { op: op @ LogicalOp::And, lhs, rhs } => {
this.visit_coverage_branch_operation(op, expr_span);
let lhs_then_block = unpack!(this.then_else_break_inner(block, lhs, args));
let rhs_then_block = unpack!(this.then_else_break_inner(lhs_then_block, rhs, args));
let lhs_then_block = this.then_else_break_inner(block, lhs, args).into_block();
let rhs_then_block =
this.then_else_break_inner(lhs_then_block, rhs, args).into_block();
rhs_then_block.unit()
}
ExprKind::LogicalOp { op: op @ LogicalOp::Or, lhs, rhs } => {
Expand All @@ -139,14 +140,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
},
)
});
let rhs_success_block = unpack!(this.then_else_break_inner(
failure_block,
rhs,
ThenElseArgs {
declare_let_bindings: DeclareLetBindings::LetNotPermitted,
..args
},
));
let rhs_success_block = this
.then_else_break_inner(
failure_block,
rhs,
ThenElseArgs {
declare_let_bindings: DeclareLetBindings::LetNotPermitted,
..args
},
)
.into_block();

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

this.expr_into_dest(destination, arm_block, arm.body)
})
.into_block()
})
.collect();

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

self.cfg.goto(unpack!(arm_block), last_location.unwrap_or(end_brace), end_block);
self.cfg.goto(arm_block, last_location.unwrap_or(end_brace), end_block);
}

self.source_scope = outer_source_info.scope;
Expand Down Expand Up @@ -621,7 +625,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
OutsideGuard,
ScheduleDrops::Yes,
);
unpack!(block = self.expr_into_dest(place, block, initializer_id));
block = self.expr_into_dest(place, block, initializer_id).into_block();

// Inject a fake read, see comments on `FakeReadCause::ForLet`.
let source_info = self.source_info(irrefutable_pat.span);
Expand Down Expand Up @@ -660,7 +664,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
OutsideGuard,
ScheduleDrops::Yes,
);
unpack!(block = self.expr_into_dest(place, block, initializer_id));
block = self.expr_into_dest(place, block, initializer_id).into_block();

// Inject a fake read, see comments on `FakeReadCause::ForLet`.
let pattern_source_info = self.source_info(irrefutable_pat.span);
Expand Down
29 changes: 17 additions & 12 deletions compiler/rustc_mir_build/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,15 @@ enum NeedsTemporary {
#[must_use = "if you don't use one of these results, you're leaving a dangling edge"]
struct BlockAnd<T>(BasicBlock, T);

impl BlockAnd<()> {
/// Unpacks `BlockAnd<()>` into a [`BasicBlock`].
#[must_use]
fn into_block(self) -> BasicBlock {
let Self(block, ()) = self;
block
}
}

trait BlockAndExtension {
fn and<T>(self, v: T) -> BlockAnd<T>;
fn unit(self) -> BlockAnd<()>;
Expand All @@ -426,11 +435,6 @@ macro_rules! unpack {
$x = b;
v
}};

($c:expr) => {{
let BlockAnd(b, ()) = $c;
b
}};
}

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -516,21 +520,22 @@ fn construct_fn<'tcx>(
region::Scope { id: body.id().hir_id.local_id, data: region::ScopeData::Arguments };
let source_info = builder.source_info(span);
let call_site_s = (call_site_scope, source_info);
unpack!(builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
let _: BlockAnd<()> = builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
let arg_scope_s = (arg_scope, source_info);
// Attribute epilogue to function's closing brace
let fn_end = span_with_body.shrink_to_hi();
let return_block =
unpack!(builder.in_breakable_scope(None, Place::return_place(), fn_end, |builder| {
let return_block = builder
.in_breakable_scope(None, Place::return_place(), fn_end, |builder| {
Some(builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| {
builder.args_and_body(START_BLOCK, arguments, arg_scope, expr)
}))
}));
})
.into_block();
let source_info = builder.source_info(fn_end);
builder.cfg.terminate(return_block, source_info, TerminatorKind::Return);
builder.build_drop_trees();
return_block.unit()
}));
});

let mut body = builder.finish();

Expand Down Expand Up @@ -579,7 +584,7 @@ fn construct_const<'a, 'tcx>(
Builder::new(thir, infcx, def, hir_id, span, 0, const_ty, const_ty_span, None);

let mut block = START_BLOCK;
unpack!(block = builder.expr_into_dest(Place::return_place(), block, expr));
block = builder.expr_into_dest(Place::return_place(), block, expr).into_block();

let source_info = builder.source_info(span);
builder.cfg.terminate(block, source_info, TerminatorKind::Return);
Expand Down Expand Up @@ -961,7 +966,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Some((Some(&place), span)),
);
let place_builder = PlaceBuilder::from(local);
unpack!(block = self.place_into_pattern(block, pat, place_builder, false));
block = self.place_into_pattern(block, pat, place_builder, false).into_block();
}
}
self.source_scope = original_source_scope;
Expand Down
Loading
Loading