Skip to content

mir: Mark Statement and BasicBlockData as #[non_exhaustive] #143262

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 1 commit into from
Jul 1, 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
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
_,
mir::Rvalue::Use(mir::Operand::Copy(place)),
)),
..
}) = self.body[location.block].statements.get(location.statement_index)
{
self.body.local_decls[place.local].source_info.span
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,7 @@ impl BasicBlock {
///
/// See [`BasicBlock`] for documentation on what basic blocks are at a high level.
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
#[non_exhaustive]
pub struct BasicBlockData<'tcx> {
/// List of statements in this block.
pub statements: Vec<Statement<'tcx>>,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/mir/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::ty::CoroutineArgsExt;

/// A statement in a basic block, including information about its source code.
#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
#[non_exhaustive]
pub struct Statement<'tcx> {
pub source_info: SourceInfo,
pub kind: StatementKind<'tcx>,
Expand Down
49 changes: 24 additions & 25 deletions compiler/rustc_mir_transform/src/check_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ fn split_block(
let block_data = &mut basic_blocks[location.block];

// Drain every statement after this one and move the current terminator to a new basic block.
let new_block = BasicBlockData {
statements: block_data.statements.split_off(location.statement_index),
terminator: block_data.terminator.take(),
is_cleanup: block_data.is_cleanup,
};
let new_block = BasicBlockData::new_stmts(
block_data.statements.split_off(location.statement_index),
block_data.terminator.take(),
block_data.is_cleanup,
);

basic_blocks.push(new_block)
}
Expand Down Expand Up @@ -270,10 +270,9 @@ fn insert_discr_cast_to_u128<'tcx>(
let mu_array =
local_decls.push(LocalDecl::with_source_info(mu_array_ty, source_info)).into();
let rvalue = Rvalue::Cast(CastKind::Transmute, source_op, mu_array_ty);
block_data.statements.push(Statement {
source_info,
kind: StatementKind::Assign(Box::new((mu_array, rvalue))),
});
block_data
.statements
.push(Statement::new(source_info, StatementKind::Assign(Box::new((mu_array, rvalue)))));

// Index into the array of MaybeUninit to get something that is actually
// as wide as the discriminant.
Expand All @@ -294,10 +293,10 @@ fn insert_discr_cast_to_u128<'tcx>(
let op_as_int =
local_decls.push(LocalDecl::with_source_info(operand_int_ty, source_info)).into();
let rvalue = Rvalue::Cast(CastKind::Transmute, source_op, operand_int_ty);
block_data.statements.push(Statement {
block_data.statements.push(Statement::new(
source_info,
kind: StatementKind::Assign(Box::new((op_as_int, rvalue))),
});
StatementKind::Assign(Box::new((op_as_int, rvalue))),
));

(CastKind::IntToInt, Operand::Copy(op_as_int))
};
Expand All @@ -306,18 +305,18 @@ fn insert_discr_cast_to_u128<'tcx>(
let rvalue = Rvalue::Cast(cast_kind, discr_ty_bits, discr.ty);
let discr_in_discr_ty =
local_decls.push(LocalDecl::with_source_info(discr.ty, source_info)).into();
block_data.statements.push(Statement {
block_data.statements.push(Statement::new(
source_info,
kind: StatementKind::Assign(Box::new((discr_in_discr_ty, rvalue))),
});
StatementKind::Assign(Box::new((discr_in_discr_ty, rvalue))),
));

// Cast the discriminant to a u128 (base for comparisions of enum discriminants).
let const_u128 = Ty::new_uint(tcx, ty::UintTy::U128);
let rvalue = Rvalue::Cast(CastKind::IntToInt, Operand::Copy(discr_in_discr_ty), const_u128);
let discr = local_decls.push(LocalDecl::with_source_info(const_u128, source_info)).into();
block_data
.statements
.push(Statement { source_info, kind: StatementKind::Assign(Box::new((discr, rvalue))) });
.push(Statement::new(source_info, StatementKind::Assign(Box::new((discr, rvalue)))));

discr
}
Expand Down Expand Up @@ -390,17 +389,17 @@ fn insert_uninhabited_enum_check<'tcx>(
) {
let is_ok: Place<'_> =
local_decls.push(LocalDecl::with_source_info(tcx.types.bool, source_info)).into();
block_data.statements.push(Statement {
block_data.statements.push(Statement::new(
source_info,
kind: StatementKind::Assign(Box::new((
StatementKind::Assign(Box::new((
is_ok,
Rvalue::Use(Operand::Constant(Box::new(ConstOperand {
span: source_info.span,
user_ty: None,
const_: Const::Val(ConstValue::from_bool(false), tcx.types.bool),
}))),
))),
});
));

block_data.terminator = Some(Terminator {
source_info,
Expand Down Expand Up @@ -463,27 +462,27 @@ fn insert_niche_check<'tcx>(

let discr_diff: Place<'_> =
local_decls.push(LocalDecl::with_source_info(tcx.types.u128, source_info)).into();
block_data.statements.push(Statement {
block_data.statements.push(Statement::new(
source_info,
kind: StatementKind::Assign(Box::new((
StatementKind::Assign(Box::new((
discr_diff,
Rvalue::BinaryOp(BinOp::Sub, Box::new((Operand::Copy(discr), start_const))),
))),
});
));

let is_ok: Place<'_> =
local_decls.push(LocalDecl::with_source_info(tcx.types.bool, source_info)).into();
block_data.statements.push(Statement {
block_data.statements.push(Statement::new(
source_info,
kind: StatementKind::Assign(Box::new((
StatementKind::Assign(Box::new((
is_ok,
Rvalue::BinaryOp(
// This is a `WrappingRange`, so make sure to get the wrapping right.
BinOp::Le,
Box::new((Operand::Copy(discr_diff), end_start_diff_const)),
),
))),
});
));

block_data.terminator = Some(Terminator {
source_info,
Expand Down
Loading