Skip to content

Commit 66500a3

Browse files
authored
Rollup merge of rust-lang#139674 - yotamofek:pr/mir_transform/index-iterators, r=compiler-errors
In `rustc_mir_transform`, iterate over index newtypes instead of ints Just makes code more idiomatic/easier to read, IMHO. Also, some drive-by simplifications and cleanups.
2 parents 5a6087e + c36e8fc commit 66500a3

File tree

6 files changed

+46
-53
lines changed

6 files changed

+46
-53
lines changed

Diff for: compiler/rustc_index_macros/src/newtype.rs

+7
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ impl Parse for Newtype {
257257
}
258258
}
259259

260+
impl std::ops::AddAssign<usize> for #name {
261+
#[inline]
262+
fn add_assign(&mut self, other: usize) {
263+
*self = *self + other;
264+
}
265+
}
266+
260267
impl rustc_index::Idx for #name {
261268
#[inline]
262269
fn new(value: usize) -> Self {

Diff for: compiler/rustc_mir_transform/src/coroutine.rs

+22-31
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ fn transform_async_context<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
547547

548548
let get_context_def_id = tcx.require_lang_item(LangItem::GetContext, None);
549549

550-
for bb in START_BLOCK..body.basic_blocks.next_index() {
550+
for bb in body.basic_blocks.indices() {
551551
let bb_data = &body[bb];
552552
if bb_data.is_cleanup {
553553
continue;
@@ -556,11 +556,11 @@ fn transform_async_context<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
556556
match &bb_data.terminator().kind {
557557
TerminatorKind::Call { func, .. } => {
558558
let func_ty = func.ty(body, tcx);
559-
if let ty::FnDef(def_id, _) = *func_ty.kind() {
560-
if def_id == get_context_def_id {
561-
let local = eliminate_get_context_call(&mut body[bb]);
562-
replace_resume_ty_local(tcx, body, local, context_mut_ref);
563-
}
559+
if let ty::FnDef(def_id, _) = *func_ty.kind()
560+
&& def_id == get_context_def_id
561+
{
562+
let local = eliminate_get_context_call(&mut body[bb]);
563+
replace_resume_ty_local(tcx, body, local, context_mut_ref);
564564
}
565565
}
566566
TerminatorKind::Yield { resume_arg, .. } => {
@@ -1057,7 +1057,7 @@ fn insert_switch<'tcx>(
10571057
let blocks = body.basic_blocks_mut().iter_mut();
10581058

10591059
for target in blocks.flat_map(|b| b.terminator_mut().successors_mut()) {
1060-
*target = BasicBlock::new(target.index() + 1);
1060+
*target += 1;
10611061
}
10621062
}
10631063

@@ -1209,14 +1209,8 @@ fn can_return<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, typing_env: ty::Typing
12091209
}
12101210

12111211
// If there's a return terminator the function may return.
1212-
for block in body.basic_blocks.iter() {
1213-
if let TerminatorKind::Return = block.terminator().kind {
1214-
return true;
1215-
}
1216-
}
1217-
1212+
body.basic_blocks.iter().any(|block| matches!(block.terminator().kind, TerminatorKind::Return))
12181213
// Otherwise the function can't return.
1219-
false
12201214
}
12211215

12221216
fn can_unwind<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) -> bool {
@@ -1293,12 +1287,12 @@ fn create_coroutine_resume_function<'tcx>(
12931287
kind: TerminatorKind::Goto { target: poison_block },
12941288
};
12951289
}
1296-
} else if !block.is_cleanup {
1290+
} else if !block.is_cleanup
12971291
// Any terminators that *can* unwind but don't have an unwind target set are also
12981292
// pointed at our poisoning block (unless they're part of the cleanup path).
1299-
if let Some(unwind @ UnwindAction::Continue) = block.terminator_mut().unwind_mut() {
1300-
*unwind = UnwindAction::Cleanup(poison_block);
1301-
}
1293+
&& let Some(unwind @ UnwindAction::Continue) = block.terminator_mut().unwind_mut()
1294+
{
1295+
*unwind = UnwindAction::Cleanup(poison_block);
13021296
}
13031297
}
13041298
}
@@ -1340,12 +1334,14 @@ fn create_coroutine_resume_function<'tcx>(
13401334
make_coroutine_state_argument_indirect(tcx, body);
13411335

13421336
match transform.coroutine_kind {
1337+
CoroutineKind::Coroutine(_)
1338+
| CoroutineKind::Desugared(CoroutineDesugaring::Async | CoroutineDesugaring::AsyncGen, _) =>
1339+
{
1340+
make_coroutine_state_argument_pinned(tcx, body);
1341+
}
13431342
// Iterator::next doesn't accept a pinned argument,
13441343
// unlike for all other coroutine kinds.
13451344
CoroutineKind::Desugared(CoroutineDesugaring::Gen, _) => {}
1346-
_ => {
1347-
make_coroutine_state_argument_pinned(tcx, body);
1348-
}
13491345
}
13501346

13511347
// Make sure we remove dead blocks to remove
@@ -1408,8 +1404,7 @@ fn create_cases<'tcx>(
14081404
let mut statements = Vec::new();
14091405

14101406
// Create StorageLive instructions for locals with live storage
1411-
for i in 0..(body.local_decls.len()) {
1412-
let l = Local::new(i);
1407+
for l in body.local_decls.indices() {
14131408
let needs_storage_live = point.storage_liveness.contains(l)
14141409
&& !transform.remap.contains(l)
14151410
&& !transform.always_live_locals.contains(l);
@@ -1535,15 +1530,10 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
15351530
let coroutine_kind = body.coroutine_kind().unwrap();
15361531

15371532
// Get the discriminant type and args which typeck computed
1538-
let (discr_ty, movable) = match *coroutine_ty.kind() {
1539-
ty::Coroutine(_, args) => {
1540-
let args = args.as_coroutine();
1541-
(args.discr_ty(tcx), coroutine_kind.movability() == hir::Movability::Movable)
1542-
}
1543-
_ => {
1544-
tcx.dcx().span_bug(body.span, format!("unexpected coroutine type {coroutine_ty}"));
1545-
}
1533+
let ty::Coroutine(_, args) = coroutine_ty.kind() else {
1534+
tcx.dcx().span_bug(body.span, format!("unexpected coroutine type {coroutine_ty}"));
15461535
};
1536+
let discr_ty = args.as_coroutine().discr_ty(tcx);
15471537

15481538
let new_ret_ty = match coroutine_kind {
15491539
CoroutineKind::Desugared(CoroutineDesugaring::Async, _) => {
@@ -1610,6 +1600,7 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
16101600

16111601
let always_live_locals = always_storage_live_locals(body);
16121602

1603+
let movable = coroutine_kind.movability() == hir::Movability::Movable;
16131604
let liveness_info =
16141605
locals_live_across_suspend_points(tcx, body, &always_live_locals, movable);
16151606

Diff for: compiler/rustc_mir_transform/src/early_otherwise_branch.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,8 @@ impl<'tcx> crate::MirPass<'tcx> for EarlyOtherwiseBranch {
103103
let mut should_cleanup = false;
104104

105105
// Also consider newly generated bbs in the same pass
106-
for i in 0..body.basic_blocks.len() {
106+
for parent in body.basic_blocks.indices() {
107107
let bbs = &*body.basic_blocks;
108-
let parent = BasicBlock::from_usize(i);
109108
let Some(opt_data) = evaluate_candidate(tcx, body, parent) else { continue };
110109

111110
trace!("SUCCESS: found optimization possibility to apply: {opt_data:?}");

Diff for: compiler/rustc_mir_transform/src/match_branches.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ impl<'tcx> crate::MirPass<'tcx> for MatchBranchSimplification {
2020
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2121
let typing_env = body.typing_env(tcx);
2222
let mut should_cleanup = false;
23-
for i in 0..body.basic_blocks.len() {
24-
let bbs = &*body.basic_blocks;
25-
let bb_idx = BasicBlock::from_usize(i);
26-
match bbs[bb_idx].terminator().kind {
23+
for bb_idx in body.basic_blocks.indices() {
24+
match &body.basic_blocks[bb_idx].terminator().kind {
2725
TerminatorKind::SwitchInt {
28-
discr: ref _discr @ (Operand::Copy(_) | Operand::Move(_)),
29-
ref targets,
26+
discr: Operand::Copy(_) | Operand::Move(_),
27+
targets,
3028
..
3129
// We require that the possible target blocks don't contain this block.
3230
} if !targets.all_targets().contains(&bb_idx) => {}
@@ -66,9 +64,10 @@ trait SimplifyMatch<'tcx> {
6664
typing_env: ty::TypingEnv<'tcx>,
6765
) -> Option<()> {
6866
let bbs = &body.basic_blocks;
69-
let (discr, targets) = match bbs[switch_bb_idx].terminator().kind {
70-
TerminatorKind::SwitchInt { ref discr, ref targets, .. } => (discr, targets),
71-
_ => unreachable!(),
67+
let TerminatorKind::SwitchInt { discr, targets, .. } =
68+
&bbs[switch_bb_idx].terminator().kind
69+
else {
70+
unreachable!();
7271
};
7372

7473
let discr_ty = discr.ty(body.local_decls(), tcx);

Diff for: compiler/rustc_mir_transform/src/multiple_return_terminators.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,17 @@ impl<'tcx> crate::MirPass<'tcx> for MultipleReturnTerminators {
1818
// find basic blocks with no statement and a return terminator
1919
let mut bbs_simple_returns = DenseBitSet::new_empty(body.basic_blocks.len());
2020
let bbs = body.basic_blocks_mut();
21-
for idx in bbs.indices() {
22-
if bbs[idx].statements.is_empty()
23-
&& bbs[idx].terminator().kind == TerminatorKind::Return
24-
{
21+
for (idx, bb) in bbs.iter_enumerated() {
22+
if bb.statements.is_empty() && bb.terminator().kind == TerminatorKind::Return {
2523
bbs_simple_returns.insert(idx);
2624
}
2725
}
2826

2927
for bb in bbs {
30-
if let TerminatorKind::Goto { target } = bb.terminator().kind {
31-
if bbs_simple_returns.contains(target) {
32-
bb.terminator_mut().kind = TerminatorKind::Return;
33-
}
28+
if let TerminatorKind::Goto { target } = bb.terminator().kind
29+
&& bbs_simple_returns.contains(target)
30+
{
31+
bb.terminator_mut().kind = TerminatorKind::Return;
3432
}
3533
}
3634

Diff for: compiler/rustc_mir_transform/src/validate.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,11 @@ impl<'a, 'tcx> CfgChecker<'a, 'tcx> {
221221

222222
// Check for cycles
223223
let mut stack = FxHashSet::default();
224-
for i in 0..parent.len() {
225-
let mut bb = BasicBlock::from_usize(i);
224+
for (mut bb, parent) in parent.iter_enumerated_mut() {
226225
stack.clear();
227226
stack.insert(bb);
228227
loop {
229-
let Some(parent) = parent[bb].take() else { break };
228+
let Some(parent) = parent.take() else { break };
230229
let no_cycle = stack.insert(parent);
231230
if !no_cycle {
232231
self.fail(

0 commit comments

Comments
 (0)