Skip to content

Commit 12815e9

Browse files
Rollup merge of rust-lang#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?
2 parents 238dc28 + 2ea6ac5 commit 12815e9

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

clippy_lints/src/enum_clike.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'tcx> LateLintPass<'tcx> for UnportableVariant {
5050
.tcx
5151
.const_eval_poly(def_id.to_def_id())
5252
.ok()
53-
.map(|val| rustc_middle::mir::ConstantKind::from_value(val, ty));
53+
.map(|val| rustc_middle::mir::Const::from_value(val, ty));
5454
if let Some(Constant::Int(val)) = constant.and_then(|c| miri_to_const(cx, c)) {
5555
if let ty::Adt(adt, _) = ty.kind() {
5656
if adt.is_enum() {

clippy_lints/src/matches/overlapping_arms.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ fn all_ranges<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>], ty: Ty<'tcx>)
3737
Some(lhs) => constant(cx, cx.typeck_results(), lhs)?,
3838
None => {
3939
let min_val_const = ty.numeric_min_val(cx.tcx)?;
40-
miri_to_const(cx, mir::ConstantKind::from_ty_const(min_val_const, cx.tcx))?
40+
miri_to_const(cx, mir::Const::from_ty_const(min_val_const, cx.tcx))?
4141
},
4242
};
4343
let rhs_const = match rhs {
4444
Some(rhs) => constant(cx, cx.typeck_results(), rhs)?,
4545
None => {
4646
let max_val_const = ty.numeric_max_val(cx.tcx)?;
47-
miri_to_const(cx, mir::ConstantKind::from_ty_const(max_val_const, cx.tcx))?
47+
miri_to_const(cx, mir::Const::from_ty_const(max_val_const, cx.tcx))?
4848
},
4949
};
5050
let lhs_val = lhs_const.int_value(cx, ty)?;

clippy_utils/src/consts.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::iter;
2121
/// A `LitKind`-like enum to fold constant `Expr`s into.
2222
#[derive(Debug, Clone)]
2323
pub enum Constant<'tcx> {
24-
Adt(rustc_middle::mir::ConstantKind<'tcx>),
24+
Adt(rustc_middle::mir::Const<'tcx>),
2525
/// A `String` (e.g., "abc").
2626
Str(String),
2727
/// A binary string (e.g., `b"abc"`).
@@ -482,7 +482,7 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
482482
.tcx
483483
.const_eval_resolve(self.param_env, mir::UnevaluatedConst::new(def_id, args), None)
484484
.ok()
485-
.map(|val| rustc_middle::mir::ConstantKind::from_value(val, ty))?;
485+
.map(|val| rustc_middle::mir::Const::from_value(val, ty))?;
486486
let result = miri_to_const(self.lcx, result)?;
487487
self.source = ConstantSource::Constant;
488488
Some(result)
@@ -655,10 +655,10 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
655655
}
656656
}
657657

658-
pub fn miri_to_const<'tcx>(lcx: &LateContext<'tcx>, result: mir::ConstantKind<'tcx>) -> Option<Constant<'tcx>> {
658+
pub fn miri_to_const<'tcx>(lcx: &LateContext<'tcx>, result: mir::Const<'tcx>) -> Option<Constant<'tcx>> {
659659
use rustc_middle::mir::ConstValue;
660660
match result {
661-
mir::ConstantKind::Val(ConstValue::Scalar(Scalar::Int(int)), _) => match result.ty().kind() {
661+
mir::Const::Val(ConstValue::Scalar(Scalar::Int(int)), _) => match result.ty().kind() {
662662
ty::Adt(adt_def, _) if adt_def.is_struct() => Some(Constant::Adt(result)),
663663
ty::Bool => Some(Constant::Bool(int == ScalarInt::TRUE)),
664664
ty::Uint(_) | ty::Int(_) => Some(Constant::Int(int.assert_bits(int.size()))),
@@ -671,11 +671,11 @@ pub fn miri_to_const<'tcx>(lcx: &LateContext<'tcx>, result: mir::ConstantKind<'t
671671
ty::RawPtr(_) => Some(Constant::RawPtr(int.assert_bits(int.size()))),
672672
_ => None,
673673
},
674-
mir::ConstantKind::Val(cv, _) if matches!(result.ty().kind(), ty::Ref(_, inner_ty, _) if matches!(inner_ty.kind(), ty::Str)) => {
674+
mir::Const::Val(cv, _) if matches!(result.ty().kind(), ty::Ref(_, inner_ty, _) if matches!(inner_ty.kind(), ty::Str)) => {
675675
let data = cv.try_get_slice_bytes_for_diagnostics(lcx.tcx)?;
676676
String::from_utf8(data.to_owned()).ok().map(Constant::Str)
677677
}
678-
mir::ConstantKind::Val(ConstValue::Indirect { alloc_id, offset: _ }, _) => {
678+
mir::Const::Val(ConstValue::Indirect { alloc_id, offset: _ }, _) => {
679679
let alloc = lcx.tcx.global_alloc(alloc_id).unwrap_memory();
680680
match result.ty().kind() {
681681
ty::Adt(adt_def, _) if adt_def.is_struct() => Some(Constant::Adt(result)),
@@ -714,17 +714,17 @@ pub fn miri_to_const<'tcx>(lcx: &LateContext<'tcx>, result: mir::ConstantKind<'t
714714
fn field_of_struct<'tcx>(
715715
adt_def: ty::AdtDef<'tcx>,
716716
lcx: &LateContext<'tcx>,
717-
result: mir::ConstantKind<'tcx>,
717+
result: mir::Const<'tcx>,
718718
field: &Ident,
719-
) -> Option<mir::ConstantKind<'tcx>> {
720-
if let mir::ConstantKind::Val(result, ty) = result
719+
) -> Option<mir::Const<'tcx>> {
720+
if let mir::Const::Val(result, ty) = result
721721
&& let Some(dc) = lcx.tcx.try_destructure_mir_constant_for_diagnostics((result, ty))
722722
&& let Some(dc_variant) = dc.variant
723723
&& let Some(variant) = adt_def.variants().get(dc_variant)
724724
&& let Some(field_idx) = variant.fields.iter().position(|el| el.name == field.name)
725725
&& let Some(&(val, ty)) = dc.fields.get(field_idx)
726726
{
727-
Some(mir::ConstantKind::Val(val, ty))
727+
Some(mir::Const::Val(val, ty))
728728
}
729729
else {
730730
None

clippy_utils/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ use rustc_hir::{
9797
use rustc_lexer::{tokenize, TokenKind};
9898
use rustc_lint::{LateContext, Level, Lint, LintContext};
9999
use rustc_middle::hir::place::PlaceBase;
100-
use rustc_middle::mir::ConstantKind;
100+
use rustc_middle::mir::Const;
101101
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
102102
use rustc_middle::ty::binding::BindingMode;
103103
use rustc_middle::ty::fast_reject::SimplifiedType;
@@ -1510,7 +1510,7 @@ pub fn is_range_full(cx: &LateContext<'_>, expr: &Expr<'_>, container_path: Opti
15101510
&& let bnd_ty = subst.type_at(0)
15111511
&& let Some(min_val) = bnd_ty.numeric_min_val(cx.tcx)
15121512
&& let const_val = cx.tcx.valtree_to_const_val((bnd_ty, min_val.to_valtree()))
1513-
&& let min_const_kind = ConstantKind::from_value(const_val, bnd_ty)
1513+
&& let min_const_kind = Const::from_value(const_val, bnd_ty)
15141514
&& let Some(min_const) = miri_to_const(cx, min_const_kind)
15151515
&& let Some(start_const) = constant(cx, cx.typeck_results(), start)
15161516
{
@@ -1526,7 +1526,7 @@ pub fn is_range_full(cx: &LateContext<'_>, expr: &Expr<'_>, container_path: Opti
15261526
&& let bnd_ty = subst.type_at(0)
15271527
&& let Some(max_val) = bnd_ty.numeric_max_val(cx.tcx)
15281528
&& let const_val = cx.tcx.valtree_to_const_val((bnd_ty, max_val.to_valtree()))
1529-
&& let max_const_kind = ConstantKind::from_value(const_val, bnd_ty)
1529+
&& let max_const_kind = Const::from_value(const_val, bnd_ty)
15301530
&& let Some(max_const) = miri_to_const(cx, max_const_kind)
15311531
&& let Some(end_const) = constant(cx, cx.typeck_results(), end)
15321532
{

0 commit comments

Comments
 (0)