Skip to content

Commit edfee16

Browse files
committed
Auto merge of rust-lang#11379 - popzxc:fix-tuple-array-conversions, r=xFrednet
Fix tuple_array_conversions lint on nightly ``` changelog: ICE: [`tuple_array_conversions`]: Don't expect array length to always be usize ``` tl;dr: changed [`Const::eval_target_usize`](https://github.com/rust-lang/rust/blob/master/compiler/rustc_middle/src/ty/consts.rs#L359) to [`Consts::try_eval_target_usize`](https://github.com/rust-lang/rust/blob/master/compiler/rustc_middle/src/ty/consts.rs#L327) to get rid of ICE. I have encountered a problem with clippy: it caught ICE when working with a codebase that uses a lot of nightly features. Here's a (stripped) ICE info: ``` error: internal compiler error: /rustc/5c6a7e71cd66705c31c9af94077901a220f0870c/compiler/rustc_middle/src/ty/consts.rs:361:32: expected usize, got Const { ty: usize, kind: N/#1 } thread 'rustc' panicked at /rustc/5c6a7e71cd66705c31c9af94077901a220f0870c/compiler/rustc_errors/src/lib.rs:1635:9: Box<dyn Any> stack backtrace: ... 16: 0x110b9c590 - rustc_middle[449edf845976488d]::util::bug::bug_fmt 17: 0x102f76ae0 - clippy_lints[71754038dd04c2d2]::tuple_array_conversions::all_bindings_are_for_conv ... ``` I don't really know what's going on low-level-wise, but seems like this lin assumed that the length of the array can always be treated as `usize`, and *I assume* this doesn't play well with `feat(generic_const_exprs)`. I wasn't able to build a minimal reproducible example, but locally this fix does resolve the issue.
2 parents 4be90d0 + f0eaa66 commit edfee16

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

clippy_lints/src/tuple_array_conversions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ fn all_bindings_are_for_conv<'tcx>(
189189
tys.len() == elements.len() && tys.iter().chain(final_tys.iter().copied()).all_equal()
190190
},
191191
(ToType::Tuple, ty::Array(ty, len)) => {
192-
len.eval_target_usize(cx.tcx, cx.param_env) as usize == elements.len()
193-
&& final_tys.iter().chain(once(ty)).all_equal()
192+
let Some(len) = len.try_eval_target_usize(cx.tcx, cx.param_env) else { return false };
193+
len as usize == elements.len() && final_tys.iter().chain(once(ty)).all_equal()
194194
},
195195
_ => false,
196196
}

tests/ui/tuple_array_conversions.rs

+5
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ fn main() {
8282
[a, c];
8383
let [[a, b], [c, d]] = [[1, 2], [3, 4]];
8484
(a, c);
85+
// Array length is not usize (#11144)
86+
fn generic_array_length<const N: usize>() {
87+
let src = [0; N];
88+
let dest: (u8,) = (src[0],);
89+
}
8590
}
8691

8792
#[clippy::msrv = "1.70.0"]

tests/ui/tuple_array_conversions.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ LL | (src, dest);
6464
= help: use `.into()` instead, or `<(T0, T1, ..., Tn)>::from` if type annotations are needed
6565

6666
error: it looks like you're trying to convert an array to a tuple
67-
--> $DIR/tuple_array_conversions.rs:99:13
67+
--> $DIR/tuple_array_conversions.rs:104:13
6868
|
6969
LL | let x = (x[0], x[1]);
7070
| ^^^^^^^^^^^^
7171
|
7272
= help: use `.into()` instead, or `<(T0, T1, ..., Tn)>::from` if type annotations are needed
7373

7474
error: it looks like you're trying to convert a tuple to an array
75-
--> $DIR/tuple_array_conversions.rs:100:13
75+
--> $DIR/tuple_array_conversions.rs:105:13
7676
|
7777
LL | let x = [x.0, x.1];
7878
| ^^^^^^^^^^

0 commit comments

Comments
 (0)