Skip to content

Use the new method for BasicBlockData and Statement #143190

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

Merged
merged 2 commits into from
Jun 30, 2025
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
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_ssa/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,15 @@ fn optimize_use_clone<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(

let destination_block = target.unwrap();

bb.statements.push(mir::Statement {
source_info: bb.terminator().source_info,
kind: mir::StatementKind::Assign(Box::new((
bb.statements.push(mir::Statement::new(
bb.terminator().source_info,
mir::StatementKind::Assign(Box::new((
*destination,
mir::Rvalue::Use(mir::Operand::Copy(
arg_place.project_deeper(&[mir::ProjectionElem::Deref], tcx),
)),
))),
});
));

bb.terminator_mut().kind = mir::TerminatorKind::Goto { target: destination_block };
}
Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,15 @@ pub struct BasicBlockData<'tcx> {

impl<'tcx> BasicBlockData<'tcx> {
pub fn new(terminator: Option<Terminator<'tcx>>, is_cleanup: bool) -> BasicBlockData<'tcx> {
BasicBlockData { statements: vec![], terminator, is_cleanup }
BasicBlockData::new_stmts(Vec::new(), terminator, is_cleanup)
}

pub fn new_stmts(
statements: Vec<Statement<'tcx>>,
terminator: Option<Terminator<'tcx>>,
is_cleanup: bool,
) -> BasicBlockData<'tcx> {
BasicBlockData { statements, terminator, is_cleanup }
}

/// Accessor for terminator.
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_middle/src/mir/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ pub struct Statement<'tcx> {
pub kind: StatementKind<'tcx>,
}

impl Statement<'_> {
impl<'tcx> Statement<'tcx> {
/// Changes a statement to a nop. This is both faster than deleting instructions and avoids
/// invalidating statement indices in `Location`s.
pub fn make_nop(&mut self) {
self.kind = StatementKind::Nop
}

pub fn new(source_info: SourceInfo, kind: StatementKind<'tcx>) -> Self {
Statement { source_info, kind }
}
}

impl<'tcx> StatementKind<'tcx> {
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_mir_build/src/builder/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<'tcx> CFG<'tcx> {
) {
self.push(
block,
Statement { source_info, kind: StatementKind::Assign(Box::new((place, rvalue))) },
Statement::new(source_info, StatementKind::Assign(Box::new((place, rvalue)))),
);
}

Expand Down Expand Up @@ -88,7 +88,7 @@ impl<'tcx> CFG<'tcx> {
place: Place<'tcx>,
) {
let kind = StatementKind::FakeRead(Box::new((cause, place)));
let stmt = Statement { source_info, kind };
let stmt = Statement::new(source_info, kind);
self.push(block, stmt);
}

Expand All @@ -99,7 +99,7 @@ impl<'tcx> CFG<'tcx> {
place: Place<'tcx>,
) {
let kind = StatementKind::PlaceMention(Box::new(place));
let stmt = Statement { source_info, kind };
let stmt = Statement::new(source_info, kind);
self.push(block, stmt);
}

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

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_mir_build/src/builder/coverageinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ impl BlockMarkerGen {
block: BasicBlock,
) -> BlockMarkerId {
let id = self.next_block_marker_id();
let marker_statement = mir::Statement {
let marker_statement = mir::Statement::new(
source_info,
kind: mir::StatementKind::Coverage(CoverageKind::BlockMarker { id }),
};
mir::StatementKind::Coverage(CoverageKind::BlockMarker { id }),
);
cfg.push(block, marker_statement);

id
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_mir_build/src/builder/custom/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,8 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
let stmt = self.statement_as_expr(*stmt_id)?;
let span = self.thir[stmt].span;
let statement = self.parse_statement(stmt)?;
data.statements.push(Statement {
source_info: SourceInfo { span, scope: self.source_scope },
kind: statement,
});
data.statements
.push(Statement::new(SourceInfo { span, scope: self.source_scope }, statement));
}

let Some(trailing) = block.expr else { return Err(self.expr_error(expr_id, "terminator")) };
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_mir_build/src/builder/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,16 +489,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let place = place_builder.to_place(this);
this.cfg.push(
block,
Statement {
source_info: ty_source_info,
kind: StatementKind::AscribeUserType(
Statement::new(
ty_source_info,
StatementKind::AscribeUserType(
Box::new((
place,
UserTypeProjection { base: annotation_index, projs: vec![] },
)),
Variance::Invariant,
),
},
),
);
}
block.and(place_builder)
Expand All @@ -518,16 +518,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
});
this.cfg.push(
block,
Statement {
source_info: ty_source_info,
kind: StatementKind::AscribeUserType(
Statement::new(
ty_source_info,
StatementKind::AscribeUserType(
Box::new((
Place::from(temp),
UserTypeProjection { base: annotation_index, projs: vec![] },
)),
Variance::Invariant,
),
},
),
);
}
block.and(PlaceBuilder::from(temp))
Expand Down
18 changes: 8 additions & 10 deletions compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// and therefore is not considered during coroutine auto-trait
// determination. See the comment about `box` at `yield_in_scope`.
let result = this.local_decls.push(LocalDecl::new(expr.ty, expr_span));
this.cfg.push(
block,
Statement { source_info, kind: StatementKind::StorageLive(result) },
);
this.cfg
.push(block, Statement::new(source_info, StatementKind::StorageLive(result)));
if let Some(scope) = scope.temp_lifetime {
// schedule a shallow free of that memory, lest we unwind:
this.schedule_drop_storage_and_value(expr_span, scope, result);
Expand Down Expand Up @@ -278,12 +276,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
};
this.cfg.push(
block,
Statement {
Statement::new(
source_info,
kind: StatementKind::Intrinsic(Box::new(
NonDivergingIntrinsic::Assume(Operand::Move(assert_place)),
)),
},
StatementKind::Intrinsic(Box::new(NonDivergingIntrinsic::Assume(
Operand::Move(assert_place),
))),
),
);
}

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

this.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
this.cfg.push(block, Statement::new(source_info, StatementKind::StorageLive(temp)));

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

Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_mir_build/src/builder/expr/as_temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
if let Block { expr: None, targeted_by_break: false, .. } = this.thir[block]
&& expr_ty.is_never() => {}
_ => {
this.cfg
.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
this.cfg.push(block, Statement::new(source_info, StatementKind::StorageLive(temp)));

// In constants, `temp_lifetime` is `None` for temporaries that
// live for the `'static` lifetime. Thus we do not drop these
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_mir_build/src/builder/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let base = self.canonical_user_type_annotations.push(annotation.clone());
self.cfg.push(
block,
Statement {
source_info: ty_source_info,
kind: StatementKind::AscribeUserType(
Statement::new(
ty_source_info,
StatementKind::AscribeUserType(
Box::new((place, UserTypeProjection { base, projs: Vec::new() })),
// We always use invariant as the variance here. This is because the
// variance field from the ascription refers to the variance to use
Expand All @@ -666,7 +666,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// `<expr>`.
ty::Invariant,
),
},
),
);

self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
Expand Down Expand Up @@ -828,7 +828,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
) -> Place<'tcx> {
let local_id = self.var_local_id(var, for_guard);
let source_info = self.source_info(span);
self.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(local_id) });
self.cfg.push(block, Statement::new(source_info, StatementKind::StorageLive(local_id)));
// Although there is almost always scope for given variable in corner cases
// like #92893 we might get variable with no scope.
if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id)
Expand Down Expand Up @@ -2578,16 +2578,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let base = self.canonical_user_type_annotations.push(ascription.annotation);
self.cfg.push(
block,
Statement {
Statement::new(
source_info,
kind: StatementKind::AscribeUserType(
StatementKind::AscribeUserType(
Box::new((
ascription.source,
UserTypeProjection { base, projs: Vec::new() },
)),
ascription.variance,
),
},
),
);
}
}
Expand Down
32 changes: 16 additions & 16 deletions compiler/rustc_mir_build/src/builder/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,13 @@ impl DropTree {
cfg.terminate(block, drop_node.data.source_info, terminator);
}
DropKind::ForLint => {
let stmt = Statement {
source_info: drop_node.data.source_info,
kind: StatementKind::BackwardIncompatibleDropHint {
let stmt = Statement::new(
drop_node.data.source_info,
StatementKind::BackwardIncompatibleDropHint {
place: Box::new(drop_node.data.local.into()),
reason: BackwardIncompatibleDropReason::Edition2024,
},
};
);
cfg.push(block, stmt);
let target = blocks[drop_node.next].unwrap();
if target != block {
Expand All @@ -454,10 +454,10 @@ impl DropTree {
// Root nodes don't correspond to a drop.
DropKind::Storage if drop_idx == ROOT_NODE => {}
DropKind::Storage => {
let stmt = Statement {
source_info: drop_node.data.source_info,
kind: StatementKind::StorageDead(drop_node.data.local),
};
let stmt = Statement::new(
drop_node.data.source_info,
StatementKind::StorageDead(drop_node.data.local),
);
cfg.push(block, stmt);
let target = blocks[drop_node.next].unwrap();
if target != block {
Expand Down Expand Up @@ -1124,21 +1124,21 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
DropKind::ForLint => {
self.cfg.push(
block,
Statement {
Statement::new(
source_info,
kind: StatementKind::BackwardIncompatibleDropHint {
StatementKind::BackwardIncompatibleDropHint {
place: Box::new(local.into()),
reason: BackwardIncompatibleDropReason::Edition2024,
},
},
),
);
}
DropKind::Storage => {
// Only temps and vars need their storage dead.
assert!(local.index() > self.arg_count);
self.cfg.push(
block,
Statement { source_info, kind: StatementKind::StorageDead(local) },
Statement::new(source_info, StatementKind::StorageDead(local)),
);
}
}
Expand Down Expand Up @@ -1880,13 +1880,13 @@ where

cfg.push(
block,
Statement {
Statement::new(
source_info,
kind: StatementKind::BackwardIncompatibleDropHint {
StatementKind::BackwardIncompatibleDropHint {
place: Box::new(local.into()),
reason: BackwardIncompatibleDropReason::Edition2024,
},
},
),
);
}
DropKind::Storage => {
Expand All @@ -1910,7 +1910,7 @@ where
}
// Only temps and vars need their storage dead.
assert!(local.index() > arg_count);
cfg.push(block, Statement { source_info, kind: StatementKind::StorageDead(local) });
cfg.push(block, Statement::new(source_info, StatementKind::StorageDead(local)));
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_mir_dataflow/src/framework/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ fn mock_body<'tcx>() -> mir::Body<'tcx> {

let mut blocks = IndexVec::new();
let mut block = |n, kind| {
let nop = mir::Statement { source_info, kind: mir::StatementKind::Nop };
let nop = mir::Statement::new(source_info, mir::StatementKind::Nop);

blocks.push(mir::BasicBlockData {
statements: std::iter::repeat(&nop).cloned().take(n).collect(),
terminator: Some(mir::Terminator { source_info, kind }),
is_cleanup: false,
})
blocks.push(mir::BasicBlockData::new_stmts(
std::iter::repeat(&nop).cloned().take(n).collect(),
Some(mir::Terminator { source_info, kind }),
false,
))
};

let dummy_place = mir::Place { local: mir::RETURN_PLACE, projection: ty::List::empty() };
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_mir_transform/src/add_call_guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ impl<'tcx> crate::MirPass<'tcx> for AddCallGuards {

let cur_len = body.basic_blocks.len();
let mut new_block = |source_info: SourceInfo, is_cleanup: bool, target: BasicBlock| {
let block = BasicBlockData {
statements: vec![],
let block = BasicBlockData::new(
Some(Terminator { source_info, kind: TerminatorKind::Goto { target } }),
is_cleanup,
terminator: Some(Terminator { source_info, kind: TerminatorKind::Goto { target } }),
};
);
let idx = cur_len + new_blocks.len();
new_blocks.push(block);
BasicBlock::new(idx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ fn add_move_for_packed_drop<'tcx>(
let ty = place.ty(body, tcx).ty;
let temp = patch.new_temp(ty, source_info.span);

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

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