1
Fork 0

Fix tests

This commit is contained in:
Michael Goulet 2024-09-27 16:15:30 -04:00
parent 9453d2cfeb
commit 5cf8107aa6
15 changed files with 47 additions and 43 deletions

View file

@ -133,6 +133,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
.expect_const() .expect_const()
.try_to_valtree() .try_to_valtree()
.expect("expected monomorphic const in codegen") .expect("expected monomorphic const in codegen")
.0
.unwrap_branch(); .unwrap_branch();
assert_eq!(x.layout(), y.layout()); assert_eq!(x.layout(), y.layout());
@ -806,8 +807,10 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => m.load_scalar(fx), ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => m.load_scalar(fx),
ty::Array(elem, len) ty::Array(elem, len)
if matches!(elem.kind(), ty::Uint(ty::UintTy::U8)) if matches!(elem.kind(), ty::Uint(ty::UintTy::U8))
&& len.try_eval_target_usize(fx.tcx, ty::ParamEnv::reveal_all()) && len
== Some(expected_bytes) => .try_to_target_usize(fx.tcx)
.expect("expected monomorphic const in codegen")
== expected_bytes =>
{ {
m.force_stack(fx).0.load( m.force_stack(fx).0.load(
fx, fx,
@ -907,8 +910,10 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => {} ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => {}
ty::Array(elem, len) ty::Array(elem, len)
if matches!(elem.kind(), ty::Uint(ty::UintTy::U8)) if matches!(elem.kind(), ty::Uint(ty::UintTy::U8))
&& len.try_eval_target_usize(fx.tcx, ty::ParamEnv::reveal_all()) && len
== Some(expected_bytes) => {} .try_to_target_usize(fx.tcx)
.expect("expected monomorphic const in codegen")
== expected_bytes => {}
_ => { _ => {
fx.tcx.dcx().span_fatal( fx.tcx.dcx().span_fatal(
span, span,

View file

@ -76,8 +76,10 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => args[0].immediate(), ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => args[0].immediate(),
ty::Array(elem, len) ty::Array(elem, len)
if matches!(*elem.kind(), ty::Uint(ty::UintTy::U8)) if matches!(*elem.kind(), ty::Uint(ty::UintTy::U8))
&& len.try_eval_target_usize(bx.tcx, ty::ParamEnv::reveal_all()) && len
== Some(expected_bytes) => .try_to_target_usize(bx.tcx)
.expect("expected monomorphic const in codegen")
== expected_bytes =>
{ {
let place = PlaceRef::alloca(bx, args[0].layout); let place = PlaceRef::alloca(bx, args[0].layout);
args[0].val.store(bx, place); args[0].val.store(bx, place);
@ -696,8 +698,10 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
} }
ty::Array(elem, len) ty::Array(elem, len)
if matches!(*elem.kind(), ty::Uint(ty::UintTy::U8)) if matches!(*elem.kind(), ty::Uint(ty::UintTy::U8))
&& len.try_eval_target_usize(bx.tcx, ty::ParamEnv::reveal_all()) && len
== Some(expected_bytes) => .try_to_target_usize(bx.tcx)
.expect("expected monomorphic const in codegen")
== expected_bytes =>
{ {
// Zero-extend iN to the array length: // Zero-extend iN to the array length:
let ze = bx.zext(result, bx.type_ix(expected_bytes * 8)); let ze = bx.zext(result, bx.type_ix(expected_bytes * 8));

View file

@ -1817,7 +1817,7 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
// Only anon consts can implicitly capture params. // Only anon consts can implicitly capture params.
// FIXME: is this correct behavior? // FIXME: is this correct behavior?
let param_env = cx.tcx.param_env(*def_id); let param_env = cx.tcx.param_env(*def_id);
ct.normalize(cx.tcx, param_env) cx.tcx.normalize_erasing_regions(param_env, ct)
} else { } else {
ct ct
}; };
@ -2033,8 +2033,8 @@ pub(crate) fn clean_middle_ty<'tcx>(
Box::new(clean_middle_ty(bound_ty.rebind(ty), cx, None, None)), Box::new(clean_middle_ty(bound_ty.rebind(ty), cx, None, None)),
format!("{pat:?}").into_boxed_str(), format!("{pat:?}").into_boxed_str(),
), ),
ty::Array(ty, mut n) => { ty::Array(ty, n) => {
n = n.normalize(cx.tcx, ty::ParamEnv::reveal_all()); let n = cx.tcx.normalize_erasing_regions(cx.param_env, n);
let n = print_const(cx, n); let n = print_const(cx, n);
Array(Box::new(clean_middle_ty(bound_ty.rebind(ty), cx, None, None)), n.into()) Array(Box::new(clean_middle_ty(bound_ty.rebind(ty), cx, None, None)), n.into())
} }

View file

@ -114,7 +114,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
if let Some(range) = higher::Range::hir(index) { if let Some(range) = higher::Range::hir(index) {
// Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..] // Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
if let ty::Array(_, s) = ty.kind() { if let ty::Array(_, s) = ty.kind() {
let size: u128 = if let Some(size) = s.try_eval_target_usize(cx.tcx, cx.param_env) { let size: u128 = if let Some(size) = s.try_to_target_usize(cx.tcx) {
size.into() size.into()
} else { } else {
return; return;
@ -183,7 +183,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
&& let ty::Uint(utype) = cx.typeck_results().expr_ty(index).kind() && let ty::Uint(utype) = cx.typeck_results().expr_ty(index).kind()
&& *utype == ty::UintTy::Usize && *utype == ty::UintTy::Usize
&& let ty::Array(_, s) = ty.kind() && let ty::Array(_, s) = ty.kind()
&& let Some(size) = s.try_eval_target_usize(cx.tcx, cx.param_env) && let Some(size) = s.try_to_target_usize(cx.tcx)
{ {
// get constant offset and check whether it is in bounds // get constant offset and check whether it is in bounds
let off = usize::try_from(off).unwrap(); let off = usize::try_from(off).unwrap();

View file

@ -30,7 +30,7 @@ pub(super) fn check(
return; return;
} }
} else if count } else if count
.try_eval_target_usize(cx.tcx, cx.param_env) .try_to_target_usize(cx.tcx)
.map_or(true, |x| x > 32) .map_or(true, |x| x > 32)
&& !msrv.meets(msrvs::ARRAY_IMPL_ANY_LEN) && !msrv.meets(msrvs::ARRAY_IMPL_ANY_LEN)
{ {

View file

@ -472,7 +472,7 @@ fn is_array_length_equal_to_range(cx: &LateContext<'_>, start: &Expr<'_>, end: &
let arr_ty = cx.typeck_results().expr_ty(arr).peel_refs(); let arr_ty = cx.typeck_results().expr_ty(arr).peel_refs();
if let ty::Array(_, s) = arr_ty.kind() { if let ty::Array(_, s) = arr_ty.kind() {
let size: u128 = if let Some(size) = s.try_eval_target_usize(cx.tcx, cx.param_env) { let size: u128 = if let Some(size) = s.try_to_target_usize(cx.tcx) {
size.into() size.into()
} else { } else {
return false; return false;

View file

@ -207,7 +207,7 @@ fn is_end_eq_array_len<'tcx>(
if let ExprKind::Lit(lit) = end.kind if let ExprKind::Lit(lit) = end.kind
&& let ast::LitKind::Int(end_int, _) = lit.node && let ast::LitKind::Int(end_int, _) = lit.node
&& let ty::Array(_, arr_len_const) = indexed_ty.kind() && let ty::Array(_, arr_len_const) = indexed_ty.kind()
&& let Some(arr_len) = arr_len_const.try_eval_target_usize(cx.tcx, cx.param_env) && let Some(arr_len) = arr_len_const.try_to_target_usize(cx.tcx)
{ {
return match limits { return match limits {
ast::RangeLimits::Closed => end_int.get() + 1 >= arr_len.into(), ast::RangeLimits::Closed => end_int.get() + 1 >= arr_len.into(),

View file

@ -11,7 +11,7 @@ use rustc_hir::def::{DefKind, Res};
use rustc_hir::intravisit::{Visitor, walk_pat}; use rustc_hir::intravisit::{Visitor, walk_pat};
use rustc_hir::{Arm, Expr, ExprKind, HirId, Node, Pat, PatKind, QPath, StmtKind}; use rustc_hir::{Arm, Expr, ExprKind, HirId, Node, Pat, PatKind, QPath, StmtKind};
use rustc_lint::LateContext; use rustc_lint::LateContext;
use rustc_middle::ty::{self, AdtDef, ParamEnv, TyCtxt, TypeckResults, VariantDef}; use rustc_middle::ty::{self, AdtDef, TyCtxt, TypeckResults, VariantDef};
use rustc_span::{Span, sym}; use rustc_span::{Span, sym};
use super::{MATCH_BOOL, SINGLE_MATCH, SINGLE_MATCH_ELSE}; use super::{MATCH_BOOL, SINGLE_MATCH, SINGLE_MATCH_ELSE};
@ -67,7 +67,6 @@ pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &'tcx Expr<'_>, arms: &'tc
if v.has_enum { if v.has_enum {
let cx = PatCtxt { let cx = PatCtxt {
tcx: cx.tcx, tcx: cx.tcx,
param_env: cx.param_env,
typeck, typeck,
arena: DroplessArena::default(), arena: DroplessArena::default(),
}; };
@ -185,7 +184,6 @@ impl<'tcx> Visitor<'tcx> for PatVisitor<'tcx> {
/// The context needed to manipulate a `PatState`. /// The context needed to manipulate a `PatState`.
struct PatCtxt<'tcx> { struct PatCtxt<'tcx> {
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
typeck: &'tcx TypeckResults<'tcx>, typeck: &'tcx TypeckResults<'tcx>,
arena: DroplessArena, arena: DroplessArena,
} }
@ -334,7 +332,7 @@ impl<'a> PatState<'a> {
if match *cx.typeck.pat_ty(pat).peel_refs().kind() { if match *cx.typeck.pat_ty(pat).peel_refs().kind() {
ty::Adt(adt, _) => adt.is_enum() || (adt.is_struct() && !adt.non_enum_variant().fields.is_empty()), ty::Adt(adt, _) => adt.is_enum() || (adt.is_struct() && !adt.non_enum_variant().fields.is_empty()),
ty::Tuple(tys) => !tys.is_empty(), ty::Tuple(tys) => !tys.is_empty(),
ty::Array(_, len) => len.try_eval_target_usize(cx.tcx, cx.param_env) != Some(1), ty::Array(_, len) => len.try_to_target_usize(cx.tcx) != Some(1),
ty::Slice(..) => true, ty::Slice(..) => true,
_ => false, _ => false,
} => } =>
@ -353,7 +351,7 @@ impl<'a> PatState<'a> {
}, },
PatKind::Slice([sub_pat], _, []) | PatKind::Slice([], _, [sub_pat]) PatKind::Slice([sub_pat], _, []) | PatKind::Slice([], _, [sub_pat])
if let ty::Array(_, len) = *cx.typeck.pat_ty(pat).kind() if let ty::Array(_, len) = *cx.typeck.pat_ty(pat).kind()
&& len.try_eval_target_usize(cx.tcx, cx.param_env) == Some(1) => && len.try_to_target_usize(cx.tcx) == Some(1) =>
{ {
self.add_pat(cx, sub_pat) self.add_pat(cx, sub_pat)
}, },

View file

@ -31,14 +31,14 @@ fn get_iterator_length<'tcx>(cx: &LateContext<'tcx>, iter: &'tcx Expr<'tcx>) ->
// parameter. // parameter.
substs substs
.const_at(1) .const_at(1)
.try_eval_target_usize(cx.tcx, cx.param_env) .try_to_target_usize(cx.tcx)
.map(u128::from) .map(u128::from)
} else if cx.tcx.is_diagnostic_item(sym::SliceIter, did) } else if cx.tcx.is_diagnostic_item(sym::SliceIter, did)
&& let ExprKind::MethodCall(_, recv, ..) = iter.kind && let ExprKind::MethodCall(_, recv, ..) = iter.kind
{ {
if let ty::Array(_, len) = cx.typeck_results().expr_ty(recv).peel_refs().kind() { if let ty::Array(_, len) = cx.typeck_results().expr_ty(recv).peel_refs().kind() {
// For slice::Iter<'_, T>, the receiver might be an array literal: [1,2,3].iter().skip(..) // For slice::Iter<'_, T>, the receiver might be an array literal: [1,2,3].iter().skip(..)
len.try_eval_target_usize(cx.tcx, cx.param_env).map(u128::from) len.try_to_target_usize(cx.tcx).map(u128::from)
} else if let Some(args) = VecArgs::hir(cx, expr_or_init(cx, recv)) { } else if let Some(args) = VecArgs::hir(cx, expr_or_init(cx, recv)) {
match args { match args {
VecArgs::Vec(vec) => vec.len().try_into().ok(), VecArgs::Vec(vec) => vec.len().try_into().ok(),

View file

@ -18,7 +18,7 @@ pub(super) fn derefs_to_slice<'tcx>(
ty::Slice(_) => true, ty::Slice(_) => true,
ty::Adt(..) if let Some(boxed) = ty.boxed_ty() => may_slice(cx, boxed), ty::Adt(..) if let Some(boxed) = ty.boxed_ty() => may_slice(cx, boxed),
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::Vec), ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::Vec),
ty::Array(_, size) => size.try_eval_target_usize(cx.tcx, cx.param_env).is_some(), ty::Array(_, size) => size.try_to_target_usize(cx.tcx).is_some(),
ty::Ref(_, inner, _) => may_slice(cx, *inner), ty::Ref(_, inner, _) => may_slice(cx, *inner),
_ => false, _ => false,
} }

View file

@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::has_repr_attr; use clippy_utils::has_repr_attr;
use rustc_hir::{Item, ItemKind}; use rustc_hir::{Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{Const, FeedConstTy}; use rustc_middle::ty;
use rustc_session::declare_lint_pass; use rustc_session::declare_lint_pass;
declare_clippy_lint! { declare_clippy_lint! {
@ -55,16 +55,14 @@ impl<'tcx> LateLintPass<'tcx> for TrailingEmptyArray {
fn is_struct_with_trailing_zero_sized_array<'tcx>(cx: &LateContext<'tcx>, item: &Item<'tcx>) -> bool { fn is_struct_with_trailing_zero_sized_array<'tcx>(cx: &LateContext<'tcx>, item: &Item<'tcx>) -> bool {
if let ItemKind::Struct(data, _) = &item.kind if let ItemKind::Struct(data, _) = &item.kind
// First check if last field is an array
&& let Some(last_field) = data.fields().last() && let Some(last_field) = data.fields().last()
&& let rustc_hir::TyKind::Array(_, rustc_hir::ArrayLen::Body(length)) = last_field.ty.kind && let field_ty = cx
.tcx
// Then check if that array is zero-sized .normalize_erasing_regions(cx.param_env, cx.tcx.type_of(last_field.def_id).instantiate_identity())
&& let length = Const::from_const_arg(cx.tcx, length, FeedConstTy::No) && let ty::Array(_, array_len) = *field_ty.kind()
&& let length = length.try_eval_target_usize(cx.tcx, cx.param_env) && let Some(0) = array_len.try_to_target_usize(cx.tcx)
&& let Some(length) = length
{ {
length == 0 true
} else { } else {
false false
} }

View file

@ -190,7 +190,7 @@ fn all_bindings_are_for_conv<'tcx>(
tys.len() == elements.len() && tys.iter().chain(final_tys.iter().copied()).all_equal() tys.len() == elements.len() && tys.iter().chain(final_tys.iter().copied()).all_equal()
}, },
(ToType::Tuple, ty::Array(ty, len)) => { (ToType::Tuple, ty::Array(ty, len)) => {
let Some(len) = len.try_eval_target_usize(cx.tcx, cx.param_env) else { return false }; let Some(len) = len.try_to_target_usize(cx.tcx) else { return false };
len as usize == elements.len() && final_tys.iter().chain(once(ty)).all_equal() len as usize == elements.len() && final_tys.iter().chain(once(ty)).all_equal()
}, },
_ => false, _ => false,

View file

@ -472,7 +472,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
ExprKind::Tup(tup) => self.multi(tup).map(Constant::Tuple), ExprKind::Tup(tup) => self.multi(tup).map(Constant::Tuple),
ExprKind::Repeat(value, _) => { ExprKind::Repeat(value, _) => {
let n = match self.typeck.expr_ty(e).kind() { let n = match self.typeck.expr_ty(e).kind() {
ty::Array(_, n) => n.try_eval_target_usize(self.tcx, self.param_env)?, ty::Array(_, n) => n.try_to_target_usize(self.tcx)?,
_ => span_bug!(e.span, "typeck error"), _ => span_bug!(e.span, "typeck error"),
}; };
self.expr(value).map(|v| Constant::Repeat(Box::new(v), n)) self.expr(value).map(|v| Constant::Repeat(Box::new(v), n))
@ -553,7 +553,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
ExprKind::Array(vec) => self.multi(vec).map(|v| v.is_empty()), ExprKind::Array(vec) => self.multi(vec).map(|v| v.is_empty()),
ExprKind::Repeat(..) => { ExprKind::Repeat(..) => {
if let ty::Array(_, n) = self.typeck.expr_ty(e).kind() { if let ty::Array(_, n) = self.typeck.expr_ty(e).kind() {
Some(n.try_eval_target_usize(self.tcx, self.param_env)? == 0) Some(n.try_to_target_usize(self.tcx)? == 0)
} else { } else {
span_bug!(e.span, "typeck error"); span_bug!(e.span, "typeck error");
} }

View file

@ -989,7 +989,7 @@ pub fn approx_ty_size<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> u64 {
(Ok(size), _) => size, (Ok(size), _) => size,
(Err(_), ty::Tuple(list)) => list.iter().map(|t| approx_ty_size(cx, t)).sum(), (Err(_), ty::Tuple(list)) => list.iter().map(|t| approx_ty_size(cx, t)).sum(),
(Err(_), ty::Array(t, n)) => { (Err(_), ty::Array(t, n)) => {
n.try_eval_target_usize(cx.tcx, cx.param_env).unwrap_or_default() * approx_ty_size(cx, *t) n.try_to_target_usize(cx.tcx).unwrap_or_default() * approx_ty_size(cx, *t)
}, },
(Err(_), ty::Adt(def, subst)) if def.is_struct() => def (Err(_), ty::Adt(def, subst)) if def.is_struct() => def
.variants() .variants()
@ -1207,7 +1207,7 @@ impl<'tcx> InteriorMut<'tcx> {
let chain = match *ty.kind() { let chain = match *ty.kind() {
ty::RawPtr(inner_ty, _) if !self.ignore_pointers => self.interior_mut_ty_chain(cx, inner_ty), ty::RawPtr(inner_ty, _) if !self.ignore_pointers => self.interior_mut_ty_chain(cx, inner_ty),
ty::Ref(_, inner_ty, _) | ty::Slice(inner_ty) => self.interior_mut_ty_chain(cx, inner_ty), ty::Ref(_, inner_ty, _) | ty::Slice(inner_ty) => self.interior_mut_ty_chain(cx, inner_ty),
ty::Array(inner_ty, size) if size.try_eval_target_usize(cx.tcx, cx.param_env) != Some(0) => { ty::Array(inner_ty, size) if size.try_to_target_usize(cx.tcx) != Some(0) => {
self.interior_mut_ty_chain(cx, inner_ty) self.interior_mut_ty_chain(cx, inner_ty)
}, },
ty::Tuple(fields) => fields.iter().find_map(|ty| self.interior_mut_ty_chain(cx, ty)), ty::Tuple(fields) => fields.iter().find_map(|ty| self.interior_mut_ty_chain(cx, ty)),

View file

@ -1,8 +1,7 @@
use either::Either; use either::Either;
use rustc_apfloat::{Float, Round}; use rustc_apfloat::{Float, Round};
use rustc_middle::ty::FloatTy; use rustc_middle::ty::layout::LayoutOf;
use rustc_middle::ty::layout::{HasParamEnv, LayoutOf}; use rustc_middle::{mir, ty, ty::FloatTy};
use rustc_middle::{mir, ty};
use rustc_span::{Symbol, sym}; use rustc_span::{Symbol, sym};
use rustc_target::abi::{Endian, HasDataLayout}; use rustc_target::abi::{Endian, HasDataLayout};
@ -633,9 +632,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let index = generic_args[2] let index = generic_args[2]
.expect_const() .expect_const()
.eval(*this.tcx, this.param_env(), this.tcx.span) .try_to_valtree()
.unwrap() .unwrap()
.1 .0
.unwrap_branch(); .unwrap_branch();
let index_len = index.len(); let index_len = index.len();