Skip to content

Commit 5c95a3d

Browse files
committed
fix clippy test failures
1 parent 705d818 commit 5c95a3d

File tree

6 files changed

+111
-65
lines changed

6 files changed

+111
-65
lines changed

compiler/rustc_middle/src/ty/consts.rs

+8
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ impl<'tcx> Const<'tcx> {
199199
tcx.mk_const(ConstS { kind: ConstKind::Value(val), ty })
200200
}
201201

202+
/// Panics if self.kind != ty::ConstKind::Value
203+
pub fn to_valtree(self) -> ty::ValTree<'tcx> {
204+
match self.val() {
205+
ty::ConstKind::Value(valtree) => valtree,
206+
_ => bug!("expected ConstKind::Value"),
207+
}
208+
}
209+
202210
pub fn from_scalar_int(tcx: TyCtxt<'tcx>, i: ScalarInt, ty: Ty<'tcx>) -> Self {
203211
let valtree = ty::ValTree::from_scalar_int(i);
204212
Self::from_value(tcx, valtree, ty)

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ pub(crate) fn compare_const_vals<'tcx>(
798798
if let ty::Str = ty.kind() && let (
799799
Some(a_val @ ConstValue::Slice { .. }),
800800
Some(b_val @ ConstValue::Slice { .. }),
801-
) = (a.try_val(tcx), b.try_val(tcx))
801+
) = (a.try_to_value(tcx), b.try_to_value(tcx))
802802
{
803803
let a_bytes = get_slice_bytes(&tcx, a_val);
804804
let b_bytes = get_slice_bytes(&tcx, b_val);

src/tools/clippy/clippy_lints/src/enum_clike.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ impl<'tcx> LateLintPass<'tcx> for UnportableVariant {
4848
let mut ty = cx.tcx.type_of(def_id.to_def_id());
4949
let constant = cx
5050
.tcx
51-
.const_eval_poly_for_typeck(def_id.to_def_id())
51+
.const_eval_poly(def_id.to_def_id())
5252
.ok()
53-
.and_then(|val| val.map(|valtree| rustc_middle::ty::Const::from_value(cx.tcx, valtree, ty)));
53+
.and_then(|val| Some(rustc_middle::mir::ConstantKind::from_value(val, ty)));
5454
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() {

src/tools/clippy/clippy_lints/src/matches/overlapping_arms.rs

+17-2
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(cx.tcx, 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(cx.tcx, 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)?;

src/tools/clippy/clippy_lints/src/non_copy_const.rs

+9-9
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::interpret::ErrorHandled;
16+
use rustc_middle::mir;
17+
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;
@@ -133,22 +134,21 @@ fn is_unfrozen<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
133134

134135
fn is_value_unfrozen_raw<'tcx>(
135136
cx: &LateContext<'tcx>,
136-
result: Result<Option<ty::ValTree<'tcx>>, ErrorHandled>,
137+
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(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,19 +174,19 @@ 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| val.map_or(false, |val| inner(cx, Const::from_value(cx.tcx, val, ty))),
177+
|val| inner(cx, mir::ConstantKind::from_value(val, ty)),
178178
)
179179
}
180180

181181
fn is_value_unfrozen_poly<'tcx>(cx: &LateContext<'tcx>, body_id: BodyId, ty: Ty<'tcx>) -> bool {
182-
let result = cx.tcx.const_eval_poly_for_typeck(body_id.hir_id.owner.to_def_id());
182+
let result = cx.tcx.const_eval_poly(body_id.hir_id.owner.to_def_id());
183183
is_value_unfrozen_raw(cx, result, ty)
184184
}
185185

186186
fn is_value_unfrozen_expr<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId, def_id: DefId, ty: Ty<'tcx>) -> bool {
187187
let substs = cx.typeck_results().node_substs(hir_id);
188188

189-
let result = cx.tcx.const_eval_resolve_for_typeck(
189+
let result = cx.tcx.const_eval_resolve(
190190
cx.param_env,
191191
ty::Unevaluated::new(ty::WithOptConstParam::unknown(def_id), substs),
192192
None,

src/tools/clippy/clippy_utils/src/consts.rs

+74-51
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ 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;
11+
use rustc_middle::mir::interpret::Scalar;
1012
use rustc_middle::ty::subst::{Subst, SubstsRef};
1113
use rustc_middle::ty::{self, EarlyBinder, FloatTy, ScalarInt, Ty, TyCtxt};
1214
use rustc_middle::{bug, span_bug};
@@ -422,13 +424,13 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
422424
let result = self
423425
.lcx
424426
.tcx
425-
.const_eval_resolve_for_typeck(
427+
.const_eval_resolve(
426428
self.param_env,
427429
ty::Unevaluated::new(ty::WithOptConstParam::unknown(def_id), substs),
428430
None,
429431
)
430432
.ok()
431-
.and_then(|val| val.map(|val| rustc_middle::ty::Const::from_value(self.lcx.tcx, val, ty)))?;
433+
.and_then(|val| Some(rustc_middle::mir::ConstantKind::from_value(val, ty)))?;
432434
let result = miri_to_const(self.lcx.tcx, result);
433435
if result.is_some() {
434436
self.needed_resolution = true;
@@ -579,69 +581,90 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
579581
}
580582
}
581583

582-
pub fn miri_to_const<'tcx>(tcx: TyCtxt<'tcx>, result: ty::Const<'tcx>) -> Option<Constant> {
583-
match result.kind() {
584+
fn try_const_to_constant<'tcx>(tcx: TyCtxt<'tcx>, c: ty::Const<'tcx>) -> Option<Constant> {
585+
match c.kind() {
584586
ty::ConstKind::Value(valtree) => {
585-
match (valtree, result.ty().kind()) {
586-
(ty::ValTree::Leaf(int), ty::Bool) => Some(Constant::Bool(int == ScalarInt::TRUE)),
587-
(ty::ValTree::Leaf(int), ty::Uint(_) | ty::Int(_)) => Some(Constant::Int(int.assert_bits(int.size()))),
588-
(ty::ValTree::Leaf(int), ty::Float(FloatTy::F32)) => Some(Constant::F32(f32::from_bits(
587+
let const_val = tcx.valtree_to_const_val((c.ty(), valtree));
588+
miri_to_const(tcx, mir::ConstantKind::from_value(const_val, c.ty()))
589+
},
590+
_ => None,
591+
}
592+
}
593+
594+
pub fn miri_to_const<'tcx>(tcx: TyCtxt<'tcx>, result: mir::ConstantKind<'tcx>) -> Option<Constant> {
595+
use rustc_middle::mir::interpret::ConstValue;
596+
match result {
597+
mir::ConstantKind::Val(ConstValue::Scalar(Scalar::Int(int)), _) => {
598+
match result.ty().kind() {
599+
ty::Bool => Some(Constant::Bool(int == ScalarInt::TRUE)),
600+
ty::Uint(_) | ty::Int(_) => Some(Constant::Int(int.assert_bits(int.size()))),
601+
ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(
589602
int.try_into().expect("invalid f32 bit representation"),
590603
))),
591-
(ty::ValTree::Leaf(int), ty::Float(FloatTy::F64)) => Some(Constant::F64(f64::from_bits(
604+
ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(
592605
int.try_into().expect("invalid f64 bit representation"),
593606
))),
594-
(ty::ValTree::Leaf(int), ty::RawPtr(type_and_mut)) => {
607+
ty::RawPtr(type_and_mut) => {
595608
if let ty::Uint(_) = type_and_mut.ty.kind() {
596609
return Some(Constant::RawPtr(int.assert_bits(int.size())));
597610
}
598611
None
599612
},
600-
(ty::ValTree::Branch(_), ty::Ref(_, inner_ty, _)) if *inner_ty == tcx.types.str_ => valtree
601-
.try_to_raw_bytes(tcx, result.ty())
602-
.and_then(|bytes| String::from_utf8(bytes.to_owned()).ok().map(Constant::Str)),
603-
(ty::ValTree::Branch(_), ty::Array(arr_ty, len)) => match arr_ty.kind() {
604-
ty::Float(float_ty) => {
605-
let chunk_size = match float_ty {
606-
FloatTy::F32 => 4,
607-
FloatTy::F64 => 8,
608-
};
609-
610-
match miri_to_const(tcx, *len) {
611-
Some(Constant::Int(_)) => valtree.try_to_raw_bytes(tcx, result.ty()).and_then(|bytes| {
612-
bytes
613-
.to_owned()
614-
.chunks(chunk_size)
615-
.map(|chunk| match float_ty {
616-
FloatTy::F32 => {
617-
let float = f32::from_le_bytes(
618-
chunk
619-
.try_into()
620-
.expect(&format!("expected to construct f32 from {:?}", chunk)),
621-
);
622-
Some(Constant::F32(float))
623-
},
624-
FloatTy::F64 => {
625-
let float = f64::from_le_bytes(
626-
chunk
627-
.try_into()
628-
.expect(&format!("expected to construct f64 from {:?}", chunk)),
629-
);
630-
Some(Constant::F64(float))
631-
},
632-
})
633-
.collect::<Option<Vec<Constant>>>()
634-
.map(Constant::Vec)
635-
}),
636-
_ => None,
637-
}
638-
},
639-
_ => None,
640-
},
641613
// FIXME: implement other conversions.
642614
_ => None,
643615
}
644616
},
617+
mir::ConstantKind::Val(ConstValue::Slice { data, start, end }, _) => match result.ty().kind() {
618+
ty::Ref(_, tam, _) => match tam.kind() {
619+
ty::Str => String::from_utf8(
620+
data.inner()
621+
.inspect_with_uninit_and_ptr_outside_interpreter(start..end)
622+
.to_owned(),
623+
)
624+
.ok()
625+
.map(Constant::Str),
626+
_ => None,
627+
},
628+
_ => None,
629+
},
630+
mir::ConstantKind::Val(ConstValue::ByRef { alloc, offset: _ }, _) => match result.ty().kind() {
631+
ty::Array(sub_type, len) => match sub_type.kind() {
632+
ty::Float(FloatTy::F32) => match try_const_to_constant(tcx, *len) {
633+
Some(Constant::Int(len)) => alloc
634+
.inner()
635+
.inspect_with_uninit_and_ptr_outside_interpreter(0..(4 * len as usize))
636+
.to_owned()
637+
.chunks(4)
638+
.map(|chunk| {
639+
Some(Constant::F32(f32::from_le_bytes(
640+
chunk.try_into().expect("this shouldn't happen"),
641+
)))
642+
})
643+
.collect::<Option<Vec<Constant>>>()
644+
.map(Constant::Vec),
645+
_ => None,
646+
},
647+
ty::Float(FloatTy::F64) => match try_const_to_constant(tcx, *len) {
648+
Some(Constant::Int(len)) => alloc
649+
.inner()
650+
.inspect_with_uninit_and_ptr_outside_interpreter(0..(8 * len as usize))
651+
.to_owned()
652+
.chunks(8)
653+
.map(|chunk| {
654+
Some(Constant::F64(f64::from_le_bytes(
655+
chunk.try_into().expect("this shouldn't happen"),
656+
)))
657+
})
658+
.collect::<Option<Vec<Constant>>>()
659+
.map(Constant::Vec),
660+
_ => None,
661+
},
662+
// FIXME: implement other array type conversions.
663+
_ => None,
664+
},
665+
_ => None,
666+
},
667+
// FIXME: implement other conversions.
645668
_ => None,
646669
}
647670
}

0 commit comments

Comments
 (0)