mir: Add a new method to statement

Avoid introducing a large number of changes when adding optional initialization fields.
This commit is contained in:
dianqk
2025-06-08 15:30:18 +08:00
parent 5ca574e85b
commit 9f9cd5e283
30 changed files with 295 additions and 330 deletions

View File

@@ -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)))),
);
}
@@ -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);
}
@@ -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);
}
@@ -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);
}

View File

@@ -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

View File

@@ -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")) };

View File

@@ -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)
@@ -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))

View File

@@ -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);
@@ -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),
))),
),
);
}
@@ -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));

View File

@@ -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

View File

@@ -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
@@ -666,7 +666,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// `<expr>`.
ty::Invariant,
),
},
),
);
self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
@@ -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)
@@ -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,
),
},
),
);
}
}

View File

@@ -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 {
@@ -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 {
@@ -1124,13 +1124,13 @@ 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 => {
@@ -1138,7 +1138,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
assert!(local.index() > self.arg_count);
self.cfg.push(
block,
Statement { source_info, kind: StatementKind::StorageDead(local) },
Statement::new(source_info, StatementKind::StorageDead(local)),
);
}
}
@@ -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 => {
@@ -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)));
}
}
}