Rollup merge of #126410 - RalfJung:smir-const-operand, r=oli-obk

smir: merge identical Constant and ConstOperand types

The first commit renames the const operand visitor functions on regular MIR to match the type name, that was forgotten in the original rename.

The second commit changes stable MIR, fixing https://github.com/rust-lang/project-stable-mir/issues/71. Previously there were two different smir types for the MIR type `ConstOperand`, one used in `Operand` and one in `VarDebugInfoContents`.

Maybe we should have done this with https://github.com/rust-lang/rust/pull/125967, so there's only a single breaking change... but I saw that PR too late.

Fixes https://github.com/rust-lang/project-stable-mir/issues/71
This commit is contained in:
Guillaume Gomez
2024-06-15 19:51:35 +02:00
committed by GitHub
16 changed files with 52 additions and 55 deletions
+1 -1
View File
@@ -113,7 +113,7 @@ fn visit_ty_const(&mut self, ct: &mut ty::Const<'tcx>, location: Location) {
}
#[instrument(skip(self), level = "debug")]
fn visit_constant(&mut self, constant: &mut ConstOperand<'tcx>, location: Location) {
fn visit_const_operand(&mut self, constant: &mut ConstOperand<'tcx>, location: Location) {
let const_ = constant.const_;
constant.const_ = self.renumber_regions(const_, || RegionCtxt::Location(location));
debug!("constant: {:#?}", constant);
@@ -301,10 +301,10 @@ fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location:
self.sanitize_place(place, location, context);
}
fn visit_constant(&mut self, constant: &ConstOperand<'tcx>, location: Location) {
debug!(?constant, ?location, "visit_constant");
fn visit_const_operand(&mut self, constant: &ConstOperand<'tcx>, location: Location) {
debug!(?constant, ?location, "visit_const_operand");
self.super_constant(constant, location);
self.super_const_operand(constant, location);
let ty = self.sanitize_type(constant, constant.const_.ty());
self.cx.infcx.tcx.for_each_free_region(&ty, |live_region| {
+2 -2
View File
@@ -1287,7 +1287,7 @@ fn use_verbose(ty: Ty<'_>, fn_def: bool) -> bool {
}
impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> {
fn visit_constant(&mut self, constant: &ConstOperand<'tcx>, _location: Location) {
fn visit_const_operand(&mut self, constant: &ConstOperand<'tcx>, _location: Location) {
let ConstOperand { span, user_ty, const_ } = constant;
if use_verbose(const_.ty(), true) {
self.push("mir::ConstOperand");
@@ -1415,7 +1415,7 @@ fn alloc_ids_from_const_val(val: ConstValue<'_>) -> impl Iterator<Item = AllocId
struct CollectAllocIds(BTreeSet<AllocId>);
impl<'tcx> Visitor<'tcx> for CollectAllocIds {
fn visit_constant(&mut self, c: &ConstOperand<'tcx>, _: Location) {
fn visit_const_operand(&mut self, c: &ConstOperand<'tcx>, _: Location) {
match c.const_ {
Const::Ty(_, _) | Const::Unevaluated(..) => {}
Const::Val(val, _) => {
+7 -7
View File
@@ -184,12 +184,12 @@ fn visit_place(
/// This is called for every constant in the MIR body and every `required_consts`
/// (i.e., including consts that have been dead-code-eliminated).
fn visit_constant(
fn visit_const_operand(
&mut self,
constant: & $($mutability)? ConstOperand<'tcx>,
location: Location,
) {
self.super_constant(constant, location);
self.super_const_operand(constant, location);
}
fn visit_ty_const(
@@ -597,7 +597,7 @@ fn super_terminator(&mut self,
}
InlineAsmOperand::Const { value }
| InlineAsmOperand::SymFn { value } => {
self.visit_constant(value, location);
self.visit_const_operand(value, location);
}
InlineAsmOperand::Out { place: None, .. }
| InlineAsmOperand::SymStatic { def_id: _ }
@@ -788,7 +788,7 @@ fn super_operand(&mut self,
);
}
Operand::Constant(constant) => {
self.visit_constant(constant, location);
self.visit_const_operand(constant, location);
}
}
}
@@ -867,7 +867,7 @@ fn super_var_debug_info(
}
}
match value {
VarDebugInfoContents::Const(c) => self.visit_constant(c, location),
VarDebugInfoContents::Const(c) => self.visit_const_operand(c, location),
VarDebugInfoContents::Place(place) =>
self.visit_place(
place,
@@ -882,7 +882,7 @@ fn super_source_scope(
_scope: $(& $mutability)? SourceScope
) {}
fn super_constant(
fn super_const_operand(
&mut self,
constant: & $($mutability)? ConstOperand<'tcx>,
location: Location
@@ -1057,7 +1057,7 @@ macro_rules! type_annotations {
for const_ in &$($mutability)? $body.required_consts {
let location = Location::START;
$self.visit_constant(const_, location);
$self.visit_const_operand(const_, location);
}
}
}
@@ -706,9 +706,9 @@ fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
self.super_operand(operand, location);
}
fn visit_constant(&mut self, constant: &ConstOperand<'tcx>, location: Location) {
trace!("visit_constant: {:?}", constant);
self.super_constant(constant, location);
fn visit_const_operand(&mut self, constant: &ConstOperand<'tcx>, location: Location) {
trace!("visit_const_operand: {:?}", constant);
self.super_const_operand(constant, location);
self.eval_constant(constant);
}
@@ -956,7 +956,7 @@ fn visit_local(&mut self, local: &mut Local, _: PlaceContext, _: Location) {
}
}
fn visit_constant(&mut self, constant: &mut ConstOperand<'tcx>, _location: Location) {
fn visit_const_operand(&mut self, constant: &mut ConstOperand<'tcx>, _location: Location) {
if constant.const_.is_required_const() {
self.promoted.required_consts.push(*constant);
}
@@ -12,7 +12,7 @@ pub fn new(required_consts: &'a mut Vec<ConstOperand<'tcx>>) -> Self {
}
impl<'tcx> Visitor<'tcx> for RequiredConstsVisitor<'_, 'tcx> {
fn visit_constant(&mut self, constant: &ConstOperand<'tcx>, _: Location) {
fn visit_const_operand(&mut self, constant: &ConstOperand<'tcx>, _: Location) {
if constant.const_.is_required_const() {
self.required_consts.push(*constant);
}
@@ -49,14 +49,14 @@ fn visit_place(
}
#[inline]
fn visit_constant(&mut self, constant: &mut ConstOperand<'tcx>, location: Location) {
fn visit_const_operand(&mut self, constant: &mut ConstOperand<'tcx>, location: Location) {
// We have to use `try_normalize_erasing_regions` here, since it's
// possible that we visit impossible-to-satisfy where clauses here,
// see #91745
if let Ok(c) = self.tcx.try_normalize_erasing_regions(self.param_env, constant.const_) {
constant.const_ = c;
}
self.super_constant(constant, location);
self.super_const_operand(constant, location);
}
#[inline]
+1 -1
View File
@@ -799,7 +799,7 @@ fn visit_rvalue(&mut self, rvalue: &mir::Rvalue<'tcx>, location: Location) {
/// This does not walk the MIR of the constant as that is not needed for codegen, all we need is
/// to ensure that the constant evaluates successfully and walk the result.
#[instrument(skip(self), level = "debug")]
fn visit_constant(&mut self, constant: &mir::ConstOperand<'tcx>, location: Location) {
fn visit_const_operand(&mut self, constant: &mir::ConstOperand<'tcx>, location: Location) {
// No `super_constant` as we don't care about `visit_ty`/`visit_ty_const`.
let Some(val) = self.eval_constant(constant) else { return };
collect_const_value(self.tcx, val, self.used_items);
@@ -261,7 +261,7 @@ fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
self.super_local_decl(local, local_decl);
}
fn visit_constant(&mut self, ct: &mir::ConstOperand<'tcx>, location: Location) {
fn visit_const_operand(&mut self, ct: &mir::ConstOperand<'tcx>, location: Location) {
match ct.const_ {
mir::Const::Ty(_, c) => {
c.visit_with(self);
@@ -52,7 +52,11 @@ pub fn build(mut self, tables: &mut Tables<'tcx>) -> stable_mir::mir::Body {
}
impl<'tcx> MutVisitor<'tcx> for BodyBuilder<'tcx> {
fn visit_constant(&mut self, constant: &mut mir::ConstOperand<'tcx>, location: mir::Location) {
fn visit_const_operand(
&mut self,
constant: &mut mir::ConstOperand<'tcx>,
location: mir::Location,
) {
let const_ = constant.const_;
let val = match const_.eval(self.tcx, ty::ParamEnv::reveal_all(), constant.span) {
Ok(v) => v,
@@ -63,7 +67,7 @@ fn visit_constant(&mut self, constant: &mut mir::ConstOperand<'tcx>, location: m
};
let ty = constant.ty();
constant.const_ = mir::Const::Val(val, ty);
self.super_constant(constant, location);
self.super_const_operand(constant, location);
}
fn tcx(&self) -> TyCtxt<'tcx> {
@@ -328,13 +328,13 @@ fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
}
impl<'tcx> Stable<'tcx> for mir::ConstOperand<'tcx> {
type T = stable_mir::mir::Constant;
type T = stable_mir::mir::ConstOperand;
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
stable_mir::mir::Constant {
stable_mir::mir::ConstOperand {
span: self.span.stable(tables),
user_ty: self.user_ty.map(|u| u.as_usize()).or(None),
literal: self.const_.stable(tables),
const_: self.const_.stable(tables),
}
}
}
+10 -17
View File
@@ -637,7 +637,7 @@ pub enum AggregateKind {
pub enum Operand {
Copy(Place),
Move(Place),
Constant(Constant),
Constant(ConstOperand),
}
#[derive(Clone, Eq, PartialEq)]
@@ -653,6 +653,13 @@ fn from(local: Local) -> Self {
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ConstOperand {
pub span: Span,
pub user_ty: Option<UserTypeAnnotationIndex>,
pub const_: MirConst,
}
/// Debug information pertaining to a user variable.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct VarDebugInfo {
@@ -714,13 +721,6 @@ pub enum VarDebugInfoContents {
Const(ConstOperand),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ConstOperand {
pub span: Span,
pub user_ty: Option<UserTypeAnnotationIndex>,
pub const_: MirConst,
}
// In MIR ProjectionElem is parameterized on the second Field argument and the Index argument. This
// is so it can be used for both Places (for which the projection elements are of type
// ProjectionElem<Local, Ty>) and user-provided type annotations (for which the projection elements
@@ -829,13 +829,6 @@ pub struct UserTypeProjection {
type UserTypeAnnotationIndex = usize;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Constant {
pub span: Span,
pub user_ty: Option<UserTypeAnnotationIndex>,
pub literal: MirConst,
}
/// The possible branch sites of a [TerminatorKind::SwitchInt].
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SwitchTargets {
@@ -1001,9 +994,9 @@ pub fn ty(&self, locals: &[LocalDecl]) -> Result<Ty, Error> {
}
}
impl Constant {
impl ConstOperand {
pub fn ty(&self) -> Ty {
self.literal.ty()
self.const_.ty()
}
}
+1 -1
View File
@@ -310,7 +310,7 @@ fn pretty_operand(operand: &Operand) -> String {
Operand::Move(mv) => {
format!("move {:?}", mv)
}
Operand::Constant(cnst) => pretty_mir_const(&cnst.literal),
Operand::Constant(cnst) => pretty_mir_const(&cnst.const_),
}
}
+6 -6
View File
@@ -108,8 +108,8 @@ fn visit_ty(&mut self, ty: &Ty, location: Location) {
self.super_ty(ty)
}
fn visit_constant(&mut self, constant: &Constant, location: Location) {
self.super_constant(constant, location)
fn visit_const_operand(&mut self, constant: &ConstOperand, location: Location) {
self.super_const_operand(constant, location)
}
fn visit_mir_const(&mut self, constant: &MirConst, location: Location) {
@@ -366,7 +366,7 @@ fn super_operand(&mut self, operand: &Operand, location: Location) {
self.visit_place(place, PlaceContext::NON_MUTATING, location)
}
Operand::Constant(constant) => {
self.visit_constant(constant, location);
self.visit_const_operand(constant, location);
}
}
}
@@ -380,10 +380,10 @@ fn super_ty(&mut self, ty: &Ty) {
let _ = ty;
}
fn super_constant(&mut self, constant: &Constant, location: Location) {
let Constant { span, user_ty: _, literal } = constant;
fn super_const_operand(&mut self, constant: &ConstOperand, location: Location) {
let ConstOperand { span, user_ty: _, const_ } = constant;
self.visit_span(span);
self.visit_mir_const(literal, location);
self.visit_mir_const(const_, location);
}
fn super_mir_const(&mut self, constant: &MirConst, location: Location) {
@@ -21,7 +21,7 @@
use rustc_smir::rustc_internal;
use stable_mir::mir::alloc::GlobalAlloc;
use stable_mir::mir::mono::Instance;
use stable_mir::mir::{Body, Constant, Operand, Rvalue, StatementKind, TerminatorKind};
use stable_mir::mir::{Body, ConstOperand, Operand, Rvalue, StatementKind, TerminatorKind};
use stable_mir::ty::{ConstantKind, MirConst};
use stable_mir::{CrateDef, CrateItems, ItemKind};
use std::convert::TryFrom;
@@ -72,7 +72,7 @@ fn check_msg(body: &Body, expected: &str) {
.unwrap()
}
};
let ConstantKind::Allocated(alloc) = msg_const.literal.kind() else {
let ConstantKind::Allocated(alloc) = msg_const.const_.kind() else {
unreachable!()
};
assert_eq!(alloc.provenance.ptrs.len(), 1);
@@ -96,8 +96,8 @@ fn change_panic_msg(mut body: Body, new_msg: &str) -> Body {
match &mut bb.terminator.kind {
TerminatorKind::Call { args, .. } => {
let new_const = MirConst::from_str(new_msg);
args[0] = Operand::Constant(Constant {
literal: new_const,
args[0] = Operand::Constant(ConstOperand {
const_: new_const,
span: bb.terminator.span,
user_ty: None,
});