Skip to content

Commit 0cec566

Browse files
committed
Auto merge of #96591 - b-naber:transition-to-valtrees-in-type-system, r=lcnr
Use valtrees as the type-system representation for constant values This is not quite ready yet, there are still some problems with pretty printing and symbol mangling and `deref_const` seems to not work correctly in all cases. Mainly opening now for a perf-run (which should be good to go, despite the still existing problems). r? `@oli-obk` cc `@lcnr` `@RalfJung`
2 parents f491612 + 2a36b10 commit 0cec566

File tree

6 files changed

+40
-26
lines changed

6 files changed

+40
-26
lines changed

clippy_lints/src/enum_clike.rs

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

clippy_lints/src/large_const_arrays.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use if_chain::if_chain;
33
use rustc_errors::Applicability;
44
use rustc_hir::{Item, ItemKind};
55
use rustc_lint::{LateContext, LateLintPass};
6-
use rustc_middle::mir::interpret::ConstValue;
76
use rustc_middle::ty::layout::LayoutOf;
87
use rustc_middle::ty::{self, ConstKind};
98
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -53,8 +52,8 @@ impl<'tcx> LateLintPass<'tcx> for LargeConstArrays {
5352
if let ItemKind::Const(hir_ty, _) = &item.kind;
5453
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
5554
if let ty::Array(element_type, cst) = ty.kind();
56-
if let ConstKind::Value(ConstValue::Scalar(element_count)) = cst.kind();
57-
if let Ok(element_count) = element_count.to_machine_usize(&cx.tcx);
55+
if let ConstKind::Value(ty::ValTree::Leaf(element_count)) = cst.kind();
56+
if let Ok(element_count) = element_count.try_to_machine_usize(cx.tcx);
5857
if let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes());
5958
if self.maximum_allowed_size < element_count * element_size;
6059

clippy_lints/src/large_stack_arrays.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use clippy_utils::source::snippet;
33
use if_chain::if_chain;
44
use rustc_hir::{Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
6-
use rustc_middle::mir::interpret::ConstValue;
76
use rustc_middle::ty::layout::LayoutOf;
87
use rustc_middle::ty::{self, ConstKind};
98
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -43,8 +42,8 @@ impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
4342
if_chain! {
4443
if let ExprKind::Repeat(_, _) = expr.kind;
4544
if let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind();
46-
if let ConstKind::Value(ConstValue::Scalar(element_count)) = cst.kind();
47-
if let Ok(element_count) = element_count.to_machine_usize(&cx.tcx);
45+
if let ConstKind::Value(ty::ValTree::Leaf(element_count)) = cst.kind();
46+
if let Ok(element_count) = element_count.try_to_machine_usize(cx.tcx);
4847
if let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes());
4948
if self.maximum_allowed_size < element_count * element_size;
5049
then {

clippy_lints/src/matches/overlapping_arms.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_note;
33
use core::cmp::Ordering;
44
use rustc_hir::{Arm, Expr, PatKind, RangeEnd};
55
use rustc_lint::LateContext;
6+
use rustc_middle::mir;
67
use rustc_middle::ty::Ty;
78
use rustc_span::Span;
89

@@ -34,11 +35,25 @@ fn all_ranges<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>], ty: Ty<'tcx>)
3435
if let PatKind::Range(ref lhs, ref rhs, range_end) = pat.kind {
3536
let lhs_const = match lhs {
3637
Some(lhs) => constant(cx, cx.typeck_results(), lhs)?.0,
37-
None => miri_to_const(ty.numeric_min_val(cx.tcx)?)?,
38+
None => {
39+
let min_val_const = ty.numeric_min_val(cx.tcx)?;
40+
let min_constant = mir::ConstantKind::from_value(
41+
cx.tcx.valtree_to_const_val((ty, min_val_const.to_valtree())),
42+
ty,
43+
);
44+
miri_to_const(cx.tcx, min_constant)?
45+
},
3846
};
3947
let rhs_const = match rhs {
4048
Some(rhs) => constant(cx, cx.typeck_results(), rhs)?.0,
41-
None => miri_to_const(ty.numeric_max_val(cx.tcx)?)?,
49+
None => {
50+
let max_val_const = ty.numeric_max_val(cx.tcx)?;
51+
let max_constant = mir::ConstantKind::from_value(
52+
cx.tcx.valtree_to_const_val((ty, max_val_const.to_valtree())),
53+
ty,
54+
);
55+
miri_to_const(cx.tcx, max_constant)?
56+
},
4257
};
4358
let lhs_val = lhs_const.int_value(cx, ty)?;
4459
let rhs_val = rhs_const.int_value(cx, ty)?;

clippy_lints/src/non_copy_const.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ use rustc_hir::{
1313
BodyId, Expr, ExprKind, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind, UnOp,
1414
};
1515
use rustc_lint::{LateContext, LateLintPass, Lint};
16+
use rustc_middle::mir;
1617
use rustc_middle::mir::interpret::{ConstValue, ErrorHandled};
1718
use rustc_middle::ty::adjustment::Adjust;
18-
use rustc_middle::ty::{self, Const, Ty};
19+
use rustc_middle::ty::{self, Ty};
1920
use rustc_session::{declare_lint_pass, declare_tool_lint};
2021
use rustc_span::{InnerSpan, Span, DUMMY_SP};
2122
use rustc_typeck::hir_ty_to_ty;
@@ -136,19 +137,18 @@ fn is_value_unfrozen_raw<'tcx>(
136137
result: Result<ConstValue<'tcx>, ErrorHandled>,
137138
ty: Ty<'tcx>,
138139
) -> bool {
139-
fn inner<'tcx>(cx: &LateContext<'tcx>, val: Const<'tcx>) -> bool {
140+
fn inner<'tcx>(cx: &LateContext<'tcx>, val: mir::ConstantKind<'tcx>) -> bool {
140141
match val.ty().kind() {
141142
// the fact that we have to dig into every structs to search enums
142143
// leads us to the point checking `UnsafeCell` directly is the only option.
143144
ty::Adt(ty_def, ..) if Some(ty_def.did()) == cx.tcx.lang_items().unsafe_cell_type() => true,
144145
ty::Array(..) | ty::Adt(..) | ty::Tuple(..) => {
145-
let val = cx.tcx.destructure_const(cx.param_env.and(val));
146+
let val = cx.tcx.destructure_mir_constant(cx.param_env, val);
146147
val.fields.iter().any(|field| inner(cx, *field))
147148
},
148149
_ => false,
149150
}
150151
}
151-
152152
result.map_or_else(
153153
|err| {
154154
// Consider `TooGeneric` cases as being unfrozen.
@@ -174,7 +174,7 @@ fn is_value_unfrozen_raw<'tcx>(
174174
// I chose this way because unfrozen enums as assoc consts are rare (or, hopefully, none).
175175
err == ErrorHandled::TooGeneric
176176
},
177-
|val| inner(cx, Const::from_value(cx.tcx, val, ty)),
177+
|val| inner(cx, mir::ConstantKind::from_value(val, ty)),
178178
)
179179
}
180180

clippy_utils/src/consts.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_data_structures::sync::Lrc;
77
use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::{BinOp, BinOpKind, Block, Expr, ExprKind, HirId, Item, ItemKind, Node, QPath, UnOp};
99
use rustc_lint::LateContext;
10+
use rustc_middle::mir;
1011
use rustc_middle::mir::interpret::Scalar;
1112
use rustc_middle::ty::subst::{Subst, SubstsRef};
1213
use rustc_middle::ty::{self, EarlyBinder, FloatTy, ScalarInt, Ty, TyCtxt};
@@ -429,8 +430,8 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
429430
None,
430431
)
431432
.ok()
432-
.map(|val| rustc_middle::ty::Const::from_value(self.lcx.tcx, val, ty))?;
433-
let result = miri_to_const(result);
433+
.map(|val| rustc_middle::mir::ConstantKind::from_value(val, ty))?;
434+
let result = miri_to_const(self.lcx.tcx, result);
434435
if result.is_some() {
435436
self.needed_resolution = true;
436437
}
@@ -580,10 +581,10 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
580581
}
581582
}
582583

583-
pub fn miri_to_const(result: ty::Const<'_>) -> Option<Constant> {
584+
pub fn miri_to_const<'tcx>(tcx: TyCtxt<'tcx>, result: mir::ConstantKind<'tcx>) -> Option<Constant> {
584585
use rustc_middle::mir::interpret::ConstValue;
585-
match result.kind() {
586-
ty::ConstKind::Value(ConstValue::Scalar(Scalar::Int(int))) => {
586+
match result {
587+
mir::ConstantKind::Val(ConstValue::Scalar(Scalar::Int(int)), _) => {
587588
match result.ty().kind() {
588589
ty::Bool => Some(Constant::Bool(int == ScalarInt::TRUE)),
589590
ty::Uint(_) | ty::Int(_) => Some(Constant::Int(int.assert_bits(int.size()))),
@@ -603,7 +604,7 @@ pub fn miri_to_const(result: ty::Const<'_>) -> Option<Constant> {
603604
_ => None,
604605
}
605606
},
606-
ty::ConstKind::Value(ConstValue::Slice { data, start, end }) => match result.ty().kind() {
607+
mir::ConstantKind::Val(ConstValue::Slice { data, start, end }, _) => match result.ty().kind() {
607608
ty::Ref(_, tam, _) => match tam.kind() {
608609
ty::Str => String::from_utf8(
609610
data.inner()
@@ -616,10 +617,10 @@ pub fn miri_to_const(result: ty::Const<'_>) -> Option<Constant> {
616617
},
617618
_ => None,
618619
},
619-
ty::ConstKind::Value(ConstValue::ByRef { alloc, offset: _ }) => match result.ty().kind() {
620+
mir::ConstantKind::Val(ConstValue::ByRef { alloc, offset: _ }, _) => match result.ty().kind() {
620621
ty::Array(sub_type, len) => match sub_type.kind() {
621-
ty::Float(FloatTy::F32) => match miri_to_const(*len) {
622-
Some(Constant::Int(len)) => alloc
622+
ty::Float(FloatTy::F32) => match len.to_valtree().try_to_machine_usize(tcx) {
623+
Some(len) => alloc
623624
.inner()
624625
.inspect_with_uninit_and_ptr_outside_interpreter(0..(4 * len as usize))
625626
.to_owned()
@@ -633,8 +634,8 @@ pub fn miri_to_const(result: ty::Const<'_>) -> Option<Constant> {
633634
.map(Constant::Vec),
634635
_ => None,
635636
},
636-
ty::Float(FloatTy::F64) => match miri_to_const(*len) {
637-
Some(Constant::Int(len)) => alloc
637+
ty::Float(FloatTy::F64) => match len.to_valtree().try_to_machine_usize(tcx) {
638+
Some(len) => alloc
638639
.inner()
639640
.inspect_with_uninit_and_ptr_outside_interpreter(0..(8 * len as usize))
640641
.to_owned()

0 commit comments

Comments
 (0)