Skip to content

Commit 2428057

Browse files
committed
mir: Add a new method to statement
Avoid introducing a large number of changes when adding optional initialization fields.
1 parent 15c701f commit 2428057

File tree

30 files changed

+295
-330
lines changed

30 files changed

+295
-330
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/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

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

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

792790
let arg_place_builder = unpack!(block = this.as_place_builder(block, arg));
793791

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
@@ -644,9 +644,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
644644
let base = self.canonical_user_type_annotations.push(annotation.clone());
645645
self.cfg.push(
646646
block,
647-
Statement {
648-
source_info: ty_source_info,
649-
kind: StatementKind::AscribeUserType(
647+
Statement::new(
648+
ty_source_info,
649+
StatementKind::AscribeUserType(
650650
Box::new((place, UserTypeProjection { base, projs: Vec::new() })),
651651
// We always use invariant as the variance here. This is because the
652652
// variance field from the ascription refers to the variance to use
@@ -664,7 +664,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
664664
// `<expr>`.
665665
ty::Invariant,
666666
),
667-
},
667+
),
668668
);
669669

670670
self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
@@ -826,7 +826,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
826826
) -> Place<'tcx> {
827827
let local_id = self.var_local_id(var, for_guard);
828828
let source_info = self.source_info(span);
829-
self.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(local_id) });
829+
self.cfg.push(block, Statement::new(source_info, StatementKind::StorageLive(local_id)));
830830
// Although there is almost always scope for given variable in corner cases
831831
// like #92893 we might get variable with no scope.
832832
if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id)
@@ -2577,16 +2577,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
25772577
let base = self.canonical_user_type_annotations.push(ascription.annotation);
25782578
self.cfg.push(
25792579
block,
2580-
Statement {
2580+
Statement::new(
25812581
source_info,
2582-
kind: StatementKind::AscribeUserType(
2582+
StatementKind::AscribeUserType(
25832583
Box::new((
25842584
ascription.source,
25852585
UserTypeProjection { base, projs: Vec::new() },
25862586
)),
25872587
ascription.variance,
25882588
),
2589-
},
2589+
),
25902590
);
25912591
}
25922592
}

compiler/rustc_mir_build/src/builder/scope.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -411,13 +411,13 @@ impl DropTree {
411411
cfg.terminate(block, drop_node.data.source_info, terminator);
412412
}
413413
DropKind::ForLint => {
414-
let stmt = Statement {
415-
source_info: drop_node.data.source_info,
416-
kind: StatementKind::BackwardIncompatibleDropHint {
414+
let stmt = Statement::new(
415+
drop_node.data.source_info,
416+
StatementKind::BackwardIncompatibleDropHint {
417417
place: Box::new(drop_node.data.local.into()),
418418
reason: BackwardIncompatibleDropReason::Edition2024,
419419
},
420-
};
420+
);
421421
cfg.push(block, stmt);
422422
let target = blocks[drop_node.next].unwrap();
423423
if target != block {
@@ -434,10 +434,10 @@ impl DropTree {
434434
// Root nodes don't correspond to a drop.
435435
DropKind::Storage if drop_idx == ROOT_NODE => {}
436436
DropKind::Storage => {
437-
let stmt = Statement {
438-
source_info: drop_node.data.source_info,
439-
kind: StatementKind::StorageDead(drop_node.data.local),
440-
};
437+
let stmt = Statement::new(
438+
drop_node.data.source_info,
439+
StatementKind::StorageDead(drop_node.data.local),
440+
);
441441
cfg.push(block, stmt);
442442
let target = blocks[drop_node.next].unwrap();
443443
if target != block {
@@ -866,21 +866,21 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
866866
DropKind::ForLint => {
867867
self.cfg.push(
868868
block,
869-
Statement {
869+
Statement::new(
870870
source_info,
871-
kind: StatementKind::BackwardIncompatibleDropHint {
871+
StatementKind::BackwardIncompatibleDropHint {
872872
place: Box::new(local.into()),
873873
reason: BackwardIncompatibleDropReason::Edition2024,
874874
},
875-
},
875+
),
876876
);
877877
}
878878
DropKind::Storage => {
879879
// Only temps and vars need their storage dead.
880880
assert!(local.index() > self.arg_count);
881881
self.cfg.push(
882882
block,
883-
Statement { source_info, kind: StatementKind::StorageDead(local) },
883+
Statement::new(source_info, StatementKind::StorageDead(local)),
884884
);
885885
}
886886
}
@@ -1622,13 +1622,13 @@ where
16221622

16231623
cfg.push(
16241624
block,
1625-
Statement {
1625+
Statement::new(
16261626
source_info,
1627-
kind: StatementKind::BackwardIncompatibleDropHint {
1627+
StatementKind::BackwardIncompatibleDropHint {
16281628
place: Box::new(local.into()),
16291629
reason: BackwardIncompatibleDropReason::Edition2024,
16301630
},
1631-
},
1631+
),
16321632
);
16331633
}
16341634
DropKind::Storage => {
@@ -1652,7 +1652,7 @@ where
16521652
}
16531653
// Only temps and vars need their storage dead.
16541654
assert!(local.index() > arg_count);
1655-
cfg.push(block, Statement { source_info, kind: StatementKind::StorageDead(local) });
1655+
cfg.push(block, Statement::new(source_info, StatementKind::StorageDead(local)));
16561656
}
16571657
}
16581658
}

0 commit comments

Comments
 (0)