Skip to content

Commit b47008a

Browse files
Rollup merge of #143262 - dianqk:non_exhaustive, r=oli-obk
mir: Mark `Statement` and `BasicBlockData` as `#[non_exhaustive]` Ensure they are always created using constructors. r? oli-obk
2 parents 8b0e7d7 + 90e0835 commit b47008a

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11681168
_,
11691169
mir::Rvalue::Use(mir::Operand::Copy(place)),
11701170
)),
1171+
..
11711172
}) = self.body[location.block].statements.get(location.statement_index)
11721173
{
11731174
self.body.local_decls[place.local].source_info.span

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,7 @@ impl BasicBlock {
13361336
///
13371337
/// See [`BasicBlock`] for documentation on what basic blocks are at a high level.
13381338
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
1339+
#[non_exhaustive]
13391340
pub struct BasicBlockData<'tcx> {
13401341
/// List of statements in this block.
13411342
pub statements: Vec<Statement<'tcx>>,

compiler/rustc_middle/src/mir/statement.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::ty::CoroutineArgsExt;
1111

1212
/// A statement in a basic block, including information about its source code.
1313
#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
14+
#[non_exhaustive]
1415
pub struct Statement<'tcx> {
1516
pub source_info: SourceInfo,
1617
pub kind: StatementKind<'tcx>,

compiler/rustc_mir_transform/src/check_enums.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,11 @@ fn split_block(
230230
let block_data = &mut basic_blocks[location.block];
231231

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

239239
basic_blocks.push(new_block)
240240
}
@@ -270,10 +270,9 @@ fn insert_discr_cast_to_u128<'tcx>(
270270
let mu_array =
271271
local_decls.push(LocalDecl::with_source_info(mu_array_ty, source_info)).into();
272272
let rvalue = Rvalue::Cast(CastKind::Transmute, source_op, mu_array_ty);
273-
block_data.statements.push(Statement {
274-
source_info,
275-
kind: StatementKind::Assign(Box::new((mu_array, rvalue))),
276-
});
273+
block_data
274+
.statements
275+
.push(Statement::new(source_info, StatementKind::Assign(Box::new((mu_array, rvalue)))));
277276

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

302301
(CastKind::IntToInt, Operand::Copy(op_as_int))
303302
};
@@ -306,18 +305,18 @@ fn insert_discr_cast_to_u128<'tcx>(
306305
let rvalue = Rvalue::Cast(cast_kind, discr_ty_bits, discr.ty);
307306
let discr_in_discr_ty =
308307
local_decls.push(LocalDecl::with_source_info(discr.ty, source_info)).into();
309-
block_data.statements.push(Statement {
308+
block_data.statements.push(Statement::new(
310309
source_info,
311-
kind: StatementKind::Assign(Box::new((discr_in_discr_ty, rvalue))),
312-
});
310+
StatementKind::Assign(Box::new((discr_in_discr_ty, rvalue))),
311+
));
313312

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

322321
discr
323322
}
@@ -390,17 +389,17 @@ fn insert_uninhabited_enum_check<'tcx>(
390389
) {
391390
let is_ok: Place<'_> =
392391
local_decls.push(LocalDecl::with_source_info(tcx.types.bool, source_info)).into();
393-
block_data.statements.push(Statement {
392+
block_data.statements.push(Statement::new(
394393
source_info,
395-
kind: StatementKind::Assign(Box::new((
394+
StatementKind::Assign(Box::new((
396395
is_ok,
397396
Rvalue::Use(Operand::Constant(Box::new(ConstOperand {
398397
span: source_info.span,
399398
user_ty: None,
400399
const_: Const::Val(ConstValue::from_bool(false), tcx.types.bool),
401400
}))),
402401
))),
403-
});
402+
));
404403

405404
block_data.terminator = Some(Terminator {
406405
source_info,
@@ -463,27 +462,27 @@ fn insert_niche_check<'tcx>(
463462

464463
let discr_diff: Place<'_> =
465464
local_decls.push(LocalDecl::with_source_info(tcx.types.u128, source_info)).into();
466-
block_data.statements.push(Statement {
465+
block_data.statements.push(Statement::new(
467466
source_info,
468-
kind: StatementKind::Assign(Box::new((
467+
StatementKind::Assign(Box::new((
469468
discr_diff,
470469
Rvalue::BinaryOp(BinOp::Sub, Box::new((Operand::Copy(discr), start_const))),
471470
))),
472-
});
471+
));
473472

474473
let is_ok: Place<'_> =
475474
local_decls.push(LocalDecl::with_source_info(tcx.types.bool, source_info)).into();
476-
block_data.statements.push(Statement {
475+
block_data.statements.push(Statement::new(
477476
source_info,
478-
kind: StatementKind::Assign(Box::new((
477+
StatementKind::Assign(Box::new((
479478
is_ok,
480479
Rvalue::BinaryOp(
481480
// This is a `WrappingRange`, so make sure to get the wrapping right.
482481
BinOp::Le,
483482
Box::new((Operand::Copy(discr_diff), end_start_diff_const)),
484483
),
485484
))),
486-
});
485+
));
487486

488487
block_data.terminator = Some(Terminator {
489488
source_info,

0 commit comments

Comments
 (0)