Rollup merge of #115972 - RalfJung:const-consistency, r=oli-obk

rename mir::Constant -> mir::ConstOperand, mir::ConstKind -> mir::Const

Also, be more consistent with the `to/eval_bits` methods... we had some that take a type and some that take a size, and then sometimes the one that takes a type is called `bits_for_ty`.

Turns out that `ty::Const`/`mir::ConstKind` carry their type with them, so we don't need to even pass the type to those `eval_bits` functions at all.

However this is not properly consistent yet: in `ty` we have most of the methods on `ty::Const`, but in `mir` we have them on `mir::ConstKind`. And indeed those two types are the ones that correspond to each other. So `mir::ConstantKind` should actually be renamed to `mir::Const`. But what to do with `mir::Constant`? It carries around a span, that's really more like a constant operand that appears as a MIR operand... it's more suited for `syntax.rs` than `consts.rs`, but the bigger question is, which name should it get if we want to align the `mir` and `ty` types? `ConstOperand`? `ConstOp`? `Literal`? It's not a literal but it has a field called `literal` so it would at least be consistently wrong-ish...

``@oli-obk`` any ideas?
This commit is contained in:
Guillaume Gomez 2023-09-21 13:25:39 +02:00 committed by GitHub
commit 208f6ed95c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
76 changed files with 461 additions and 493 deletions

View file

@ -346,8 +346,8 @@ where
};
// Check the qualifs of the value of `const` items.
let uneval = match constant.literal {
ConstantKind::Ty(ct)
let uneval = match constant.const_ {
Const::Ty(ct)
if matches!(
ct.kind(),
ty::ConstKind::Param(_) | ty::ConstKind::Error(_) | ty::ConstKind::Value(_)
@ -355,11 +355,11 @@ where
{
None
}
ConstantKind::Ty(c) => {
Const::Ty(c) => {
bug!("expected ConstKind::Param or ConstKind::Value here, found {:?}", c)
}
ConstantKind::Unevaluated(uv, _) => Some(uv),
ConstantKind::Val(..) => None,
Const::Unevaluated(uv, _) => Some(uv),
Const::Val(..) => None,
};
if let Some(mir::UnevaluatedConst { def, args: _, promoted }) = uneval {
@ -383,5 +383,5 @@ where
}
// Otherwise use the qualifs of the type.
Q::in_any_value_of_ty(cx, constant.literal.ty())
Q::in_any_value_of_ty(cx, constant.const_.ty())
}

View file

@ -372,7 +372,7 @@ impl<'tcx> Validator<'_, 'tcx> {
StatementKind::Assign(box (
_,
Rvalue::Use(Operand::Constant(c)),
)) => c.literal.try_eval_target_usize(self.tcx, self.param_env),
)) => c.const_.try_eval_target_usize(self.tcx, self.param_env),
_ => None,
}
} else {
@ -554,7 +554,7 @@ impl<'tcx> Validator<'_, 'tcx> {
// Integer division: the RHS must be a non-zero const.
let const_val = match rhs {
Operand::Constant(c) => {
c.literal.try_eval_bits(self.tcx, self.param_env, lhs_ty)
c.const_.try_eval_bits(self.tcx, self.param_env)
}
_ => None,
};
@ -766,10 +766,10 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
if self.keep_original {
rhs.clone()
} else {
let unit = Rvalue::Use(Operand::Constant(Box::new(Constant {
let unit = Rvalue::Use(Operand::Constant(Box::new(ConstOperand {
span: statement.source_info.span,
user_ty: None,
literal: ConstantKind::zero_sized(self.tcx.types.unit),
const_: Const::zero_sized(self.tcx.types.unit),
})));
mem::replace(rhs, unit)
},
@ -844,10 +844,10 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
let args = tcx.erase_regions(GenericArgs::identity_for_item(tcx, def));
let uneval = mir::UnevaluatedConst { def, args, promoted: Some(promoted_id) };
Operand::Constant(Box::new(Constant {
Operand::Constant(Box::new(ConstOperand {
span,
user_ty: None,
literal: ConstantKind::Unevaluated(uneval, ty),
const_: Const::Unevaluated(uneval, ty),
}))
};
@ -1041,8 +1041,8 @@ pub fn is_const_fn_in_array_repeat_expression<'tcx>(
if let Some(Terminator { kind: TerminatorKind::Call { func, destination, .. }, .. }) =
&block.terminator
{
if let Operand::Constant(box Constant { literal, .. }) = func {
if let ty::FnDef(def_id, _) = *literal.ty().kind() {
if let Operand::Constant(box ConstOperand { const_, .. }) = func {
if let ty::FnDef(def_id, _) = *const_.ty().kind() {
if destination == place {
if ccx.tcx.is_const_fn(def_id) {
return true;