Skip to content

Commit f3b7c5a

Browse files
authored
Unrolled build for #143190
Rollup merge of #143190 - dianqk:new-method, r=oli-obk Use the `new` method for `BasicBlockData` and `Statement` This is the NFC part of #142771. I split it to make it easier to review for #142771. Even without #142771, I think this change is worthwhile.
2 parents ad3b725 + 24e553e commit f3b7c5a

34 files changed

+473
-537
lines changed

compiler/rustc_codegen_ssa/src/mir/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,15 +354,15 @@ fn optimize_use_clone<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
354354

355355
let destination_block = target.unwrap();
356356

357-
bb.statements.push(mir::Statement {
358-
source_info: bb.terminator().source_info,
359-
kind: mir::StatementKind::Assign(Box::new((
357+
bb.statements.push(mir::Statement::new(
358+
bb.terminator().source_info,
359+
mir::StatementKind::Assign(Box::new((
360360
*destination,
361361
mir::Rvalue::Use(mir::Operand::Copy(
362362
arg_place.project_deeper(&[mir::ProjectionElem::Deref], tcx),
363363
)),
364364
))),
365-
});
365+
));
366366

367367
bb.terminator_mut().kind = mir::TerminatorKind::Goto { target: destination_block };
368368
}

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,15 @@ pub struct BasicBlockData<'tcx> {
13591359

13601360
impl<'tcx> BasicBlockData<'tcx> {
13611361
pub fn new(terminator: Option<Terminator<'tcx>>, is_cleanup: bool) -> BasicBlockData<'tcx> {
1362-
BasicBlockData { statements: vec![], terminator, is_cleanup }
1362+
BasicBlockData::new_stmts(Vec::new(), terminator, is_cleanup)
1363+
}
1364+
1365+
pub fn new_stmts(
1366+
statements: Vec<Statement<'tcx>>,
1367+
terminator: Option<Terminator<'tcx>>,
1368+
is_cleanup: bool,
1369+
) -> BasicBlockData<'tcx> {
1370+
BasicBlockData { statements, terminator, is_cleanup }
13631371
}
13641372

13651373
/// Accessor for terminator.

compiler/rustc_middle/src/mir/statement.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ pub struct Statement<'tcx> {
1616
pub kind: StatementKind<'tcx>,
1717
}
1818

19-
impl Statement<'_> {
19+
impl<'tcx> Statement<'tcx> {
2020
/// Changes a statement to a nop. This is both faster than deleting instructions and avoids
2121
/// invalidating statement indices in `Location`s.
2222
pub fn make_nop(&mut self) {
2323
self.kind = StatementKind::Nop
2424
}
25+
26+
pub fn new(source_info: SourceInfo, kind: StatementKind<'tcx>) -> Self {
27+
Statement { source_info, kind }
28+
}
2529
}
2630

2731
impl<'tcx> StatementKind<'tcx> {

compiler/rustc_mir_build/src/builder/cfg.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<'tcx> CFG<'tcx> {
4242
) {
4343
self.push(
4444
block,
45-
Statement { source_info, kind: StatementKind::Assign(Box::new((place, rvalue))) },
45+
Statement::new(source_info, StatementKind::Assign(Box::new((place, rvalue)))),
4646
);
4747
}
4848

@@ -88,7 +88,7 @@ impl<'tcx> CFG<'tcx> {
8888
place: Place<'tcx>,
8989
) {
9090
let kind = StatementKind::FakeRead(Box::new((cause, place)));
91-
let stmt = Statement { source_info, kind };
91+
let stmt = Statement::new(source_info, kind);
9292
self.push(block, stmt);
9393
}
9494

@@ -99,7 +99,7 @@ impl<'tcx> CFG<'tcx> {
9999
place: Place<'tcx>,
100100
) {
101101
let kind = StatementKind::PlaceMention(Box::new(place));
102-
let stmt = Statement { source_info, kind };
102+
let stmt = Statement::new(source_info, kind);
103103
self.push(block, stmt);
104104
}
105105

@@ -110,7 +110,7 @@ impl<'tcx> CFG<'tcx> {
110110
/// syntax (e.g. `continue` or `if !`) that would otherwise not appear in MIR.
111111
pub(crate) fn push_coverage_span_marker(&mut self, block: BasicBlock, source_info: SourceInfo) {
112112
let kind = StatementKind::Coverage(coverage::CoverageKind::SpanMarker);
113-
let stmt = Statement { source_info, kind };
113+
let stmt = Statement::new(source_info, kind);
114114
self.push(block, stmt);
115115
}
116116

compiler/rustc_mir_build/src/builder/coverageinfo.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ impl BlockMarkerGen {
6161
block: BasicBlock,
6262
) -> BlockMarkerId {
6363
let id = self.next_block_marker_id();
64-
let marker_statement = mir::Statement {
64+
let marker_statement = mir::Statement::new(
6565
source_info,
66-
kind: mir::StatementKind::Coverage(CoverageKind::BlockMarker { id }),
67-
};
66+
mir::StatementKind::Coverage(CoverageKind::BlockMarker { id }),
67+
);
6868
cfg.push(block, marker_statement);
6969

7070
id

compiler/rustc_mir_build/src/builder/custom/parse.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,8 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
315315
let stmt = self.statement_as_expr(*stmt_id)?;
316316
let span = self.thir[stmt].span;
317317
let statement = self.parse_statement(stmt)?;
318-
data.statements.push(Statement {
319-
source_info: SourceInfo { span, scope: self.source_scope },
320-
kind: statement,
321-
});
318+
data.statements
319+
.push(Statement::new(SourceInfo { span, scope: self.source_scope }, statement));
322320
}
323321

324322
let Some(trailing) = block.expr else { return Err(self.expr_error(expr_id, "terminator")) };

compiler/rustc_mir_build/src/builder/expr/as_place.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -489,16 +489,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
489489
let place = place_builder.to_place(this);
490490
this.cfg.push(
491491
block,
492-
Statement {
493-
source_info: ty_source_info,
494-
kind: StatementKind::AscribeUserType(
492+
Statement::new(
493+
ty_source_info,
494+
StatementKind::AscribeUserType(
495495
Box::new((
496496
place,
497497
UserTypeProjection { base: annotation_index, projs: vec![] },
498498
)),
499499
Variance::Invariant,
500500
),
501-
},
501+
),
502502
);
503503
}
504504
block.and(place_builder)
@@ -518,16 +518,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
518518
});
519519
this.cfg.push(
520520
block,
521-
Statement {
522-
source_info: ty_source_info,
523-
kind: StatementKind::AscribeUserType(
521+
Statement::new(
522+
ty_source_info,
523+
StatementKind::AscribeUserType(
524524
Box::new((
525525
Place::from(temp),
526526
UserTypeProjection { base: annotation_index, projs: vec![] },
527527
)),
528528
Variance::Invariant,
529529
),
530-
},
530+
),
531531
);
532532
}
533533
block.and(PlaceBuilder::from(temp))

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
175175
// and therefore is not considered during coroutine auto-trait
176176
// determination. See the comment about `box` at `yield_in_scope`.
177177
let result = this.local_decls.push(LocalDecl::new(expr.ty, expr_span));
178-
this.cfg.push(
179-
block,
180-
Statement { source_info, kind: StatementKind::StorageLive(result) },
181-
);
178+
this.cfg
179+
.push(block, Statement::new(source_info, StatementKind::StorageLive(result)));
182180
if let Some(scope) = scope.temp_lifetime {
183181
// schedule a shallow free of that memory, lest we unwind:
184182
this.schedule_drop_storage_and_value(expr_span, scope, result);
@@ -278,12 +276,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
278276
};
279277
this.cfg.push(
280278
block,
281-
Statement {
279+
Statement::new(
282280
source_info,
283-
kind: StatementKind::Intrinsic(Box::new(
284-
NonDivergingIntrinsic::Assume(Operand::Move(assert_place)),
285-
)),
286-
},
281+
StatementKind::Intrinsic(Box::new(NonDivergingIntrinsic::Assume(
282+
Operand::Move(assert_place),
283+
))),
284+
),
287285
);
288286
}
289287

@@ -789,7 +787,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
789787
let source_info = this.source_info(upvar_span);
790788
let temp = this.local_decls.push(LocalDecl::new(upvar_ty, upvar_span));
791789

792-
this.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
790+
this.cfg.push(block, Statement::new(source_info, StatementKind::StorageLive(temp)));
793791

794792
let arg_place_builder = unpack!(block = this.as_place_builder(block, arg));
795793

compiler/rustc_mir_build/src/builder/expr/as_temp.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
102102
if let Block { expr: None, targeted_by_break: false, .. } = this.thir[block]
103103
&& expr_ty.is_never() => {}
104104
_ => {
105-
this.cfg
106-
.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
105+
this.cfg.push(block, Statement::new(source_info, StatementKind::StorageLive(temp)));
107106

108107
// In constants, `temp_lifetime` is `None` for temporaries that
109108
// live for the `'static` lifetime. Thus we do not drop these

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -646,9 +646,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
646646
let base = self.canonical_user_type_annotations.push(annotation.clone());
647647
self.cfg.push(
648648
block,
649-
Statement {
650-
source_info: ty_source_info,
651-
kind: StatementKind::AscribeUserType(
649+
Statement::new(
650+
ty_source_info,
651+
StatementKind::AscribeUserType(
652652
Box::new((place, UserTypeProjection { base, projs: Vec::new() })),
653653
// We always use invariant as the variance here. This is because the
654654
// variance field from the ascription refers to the variance to use
@@ -666,7 +666,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
666666
// `<expr>`.
667667
ty::Invariant,
668668
),
669-
},
669+
),
670670
);
671671

672672
self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
@@ -828,7 +828,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
828828
) -> Place<'tcx> {
829829
let local_id = self.var_local_id(var, for_guard);
830830
let source_info = self.source_info(span);
831-
self.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(local_id) });
831+
self.cfg.push(block, Statement::new(source_info, StatementKind::StorageLive(local_id)));
832832
// Although there is almost always scope for given variable in corner cases
833833
// like #92893 we might get variable with no scope.
834834
if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id)
@@ -2578,16 +2578,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
25782578
let base = self.canonical_user_type_annotations.push(ascription.annotation);
25792579
self.cfg.push(
25802580
block,
2581-
Statement {
2581+
Statement::new(
25822582
source_info,
2583-
kind: StatementKind::AscribeUserType(
2583+
StatementKind::AscribeUserType(
25842584
Box::new((
25852585
ascription.source,
25862586
UserTypeProjection { base, projs: Vec::new() },
25872587
)),
25882588
ascription.variance,
25892589
),
2590-
},
2590+
),
25912591
);
25922592
}
25932593
}

compiler/rustc_mir_build/src/builder/scope.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -431,13 +431,13 @@ impl DropTree {
431431
cfg.terminate(block, drop_node.data.source_info, terminator);
432432
}
433433
DropKind::ForLint => {
434-
let stmt = Statement {
435-
source_info: drop_node.data.source_info,
436-
kind: StatementKind::BackwardIncompatibleDropHint {
434+
let stmt = Statement::new(
435+
drop_node.data.source_info,
436+
StatementKind::BackwardIncompatibleDropHint {
437437
place: Box::new(drop_node.data.local.into()),
438438
reason: BackwardIncompatibleDropReason::Edition2024,
439439
},
440-
};
440+
);
441441
cfg.push(block, stmt);
442442
let target = blocks[drop_node.next].unwrap();
443443
if target != block {
@@ -454,10 +454,10 @@ impl DropTree {
454454
// Root nodes don't correspond to a drop.
455455
DropKind::Storage if drop_idx == ROOT_NODE => {}
456456
DropKind::Storage => {
457-
let stmt = Statement {
458-
source_info: drop_node.data.source_info,
459-
kind: StatementKind::StorageDead(drop_node.data.local),
460-
};
457+
let stmt = Statement::new(
458+
drop_node.data.source_info,
459+
StatementKind::StorageDead(drop_node.data.local),
460+
);
461461
cfg.push(block, stmt);
462462
let target = blocks[drop_node.next].unwrap();
463463
if target != block {
@@ -1124,21 +1124,21 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
11241124
DropKind::ForLint => {
11251125
self.cfg.push(
11261126
block,
1127-
Statement {
1127+
Statement::new(
11281128
source_info,
1129-
kind: StatementKind::BackwardIncompatibleDropHint {
1129+
StatementKind::BackwardIncompatibleDropHint {
11301130
place: Box::new(local.into()),
11311131
reason: BackwardIncompatibleDropReason::Edition2024,
11321132
},
1133-
},
1133+
),
11341134
);
11351135
}
11361136
DropKind::Storage => {
11371137
// Only temps and vars need their storage dead.
11381138
assert!(local.index() > self.arg_count);
11391139
self.cfg.push(
11401140
block,
1141-
Statement { source_info, kind: StatementKind::StorageDead(local) },
1141+
Statement::new(source_info, StatementKind::StorageDead(local)),
11421142
);
11431143
}
11441144
}
@@ -1880,13 +1880,13 @@ where
18801880

18811881
cfg.push(
18821882
block,
1883-
Statement {
1883+
Statement::new(
18841884
source_info,
1885-
kind: StatementKind::BackwardIncompatibleDropHint {
1885+
StatementKind::BackwardIncompatibleDropHint {
18861886
place: Box::new(local.into()),
18871887
reason: BackwardIncompatibleDropReason::Edition2024,
18881888
},
1889-
},
1889+
),
18901890
);
18911891
}
18921892
DropKind::Storage => {
@@ -1910,7 +1910,7 @@ where
19101910
}
19111911
// Only temps and vars need their storage dead.
19121912
assert!(local.index() > arg_count);
1913-
cfg.push(block, Statement { source_info, kind: StatementKind::StorageDead(local) });
1913+
cfg.push(block, Statement::new(source_info, StatementKind::StorageDead(local)));
19141914
}
19151915
}
19161916
}

compiler/rustc_mir_dataflow/src/framework/tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ fn mock_body<'tcx>() -> mir::Body<'tcx> {
1717

1818
let mut blocks = IndexVec::new();
1919
let mut block = |n, kind| {
20-
let nop = mir::Statement { source_info, kind: mir::StatementKind::Nop };
20+
let nop = mir::Statement::new(source_info, mir::StatementKind::Nop);
2121

22-
blocks.push(mir::BasicBlockData {
23-
statements: std::iter::repeat(&nop).cloned().take(n).collect(),
24-
terminator: Some(mir::Terminator { source_info, kind }),
25-
is_cleanup: false,
26-
})
22+
blocks.push(mir::BasicBlockData::new_stmts(
23+
std::iter::repeat(&nop).cloned().take(n).collect(),
24+
Some(mir::Terminator { source_info, kind }),
25+
false,
26+
))
2727
};
2828

2929
let dummy_place = mir::Place { local: mir::RETURN_PLACE, projection: ty::List::empty() };

compiler/rustc_mir_transform/src/add_call_guards.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,10 @@ impl<'tcx> crate::MirPass<'tcx> for AddCallGuards {
4444

4545
let cur_len = body.basic_blocks.len();
4646
let mut new_block = |source_info: SourceInfo, is_cleanup: bool, target: BasicBlock| {
47-
let block = BasicBlockData {
48-
statements: vec![],
47+
let block = BasicBlockData::new(
48+
Some(Terminator { source_info, kind: TerminatorKind::Goto { target } }),
4949
is_cleanup,
50-
terminator: Some(Terminator { source_info, kind: TerminatorKind::Goto { target } }),
51-
};
50+
);
5251
let idx = cur_len + new_blocks.len();
5352
new_blocks.push(block);
5453
BasicBlock::new(idx)

compiler/rustc_mir_transform/src/add_moves_for_packed_drops.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ fn add_move_for_packed_drop<'tcx>(
9393
let ty = place.ty(body, tcx).ty;
9494
let temp = patch.new_temp(ty, source_info.span);
9595

96-
let storage_dead_block = patch.new_block(BasicBlockData {
97-
statements: vec![Statement { source_info, kind: StatementKind::StorageDead(temp) }],
98-
terminator: Some(Terminator { source_info, kind: TerminatorKind::Goto { target } }),
96+
let storage_dead_block = patch.new_block(BasicBlockData::new_stmts(
97+
vec![Statement::new(source_info, StatementKind::StorageDead(temp))],
98+
Some(Terminator { source_info, kind: TerminatorKind::Goto { target } }),
9999
is_cleanup,
100-
});
100+
));
101101

102102
patch.add_statement(loc, StatementKind::StorageLive(temp));
103103
patch.add_assign(loc, Place::from(temp), Rvalue::Use(Operand::Move(*place)));

0 commit comments

Comments
 (0)