Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not panic in ty::consts::Const::try_to_target_usize() in case of size mismatch #126382

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1402,13 +1402,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expr: &'tcx hir::Expr<'tcx>,
) -> Ty<'tcx> {
let element_ty = if !args.is_empty() {
let mut size_ct = None;
Copy link
Member

@compiler-errors compiler-errors Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a sufficient fix. The expectation may always be None, typically by just separating the expression from the place that gives that expectation. For example, I believe this still ICEs:

struct OppOrder<const N: u8 = 3, T = u32> {
    arr: [T; N],
}

fn main() {
    let arr = [0, 0, 0];
    let _ = OppOrder::<3, u32> { arr };
}

let coerce_to = expected
.to_option(self)
.and_then(|uty| match *uty.kind() {
ty::Array(ty, _) | ty::Slice(ty) => Some(ty),
ty::Array(ty, sz_ct) => {
size_ct = Some(sz_ct);
Some(ty)
}
ty::Slice(ty) => Some(ty),
_ => None,
})
.unwrap_or_else(|| self.next_ty_var(expr.span));

// Check if the expected type of array size is something
// other than `usize` which is clearly wrong. Fixes ICE #126359
if let Some(size_ct) = size_ct
&& let ty::ConstKind::Value(size_ty, _) = size_ct.kind()
&& !matches!(size_ty.kind(), ty::Uint(ty::UintTy::Usize))
{
let guar = self.dcx().span_delayed_bug(expr.span, "array size type is not `usize`");
self.set_tainted_by_errors(guar);

return Ty::new_error(self.tcx, guar);
}

let mut coerce = CoerceMany::with_coercion_sites(coerce_to, args);
assert_eq!(self.diverges.get(), Diverges::Maybe);
for e in args {
Expand Down
9 changes: 0 additions & 9 deletions tests/crashes/126359.rs

This file was deleted.

14 changes: 14 additions & 0 deletions tests/ui/consts/ice-const-size-relate-126359.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Regression test for ICE #126359

// Tests that there is no ICE when the generic const
// specifying the size of an array is of a non-usize type

struct OppOrder<const N: u8 = 3, T = u32> {
arr: [T; N],
//~^ ERROR the constant `N` is not of type `usize`
}

fn main() {
let _ = OppOrder::<3, u32> { arr: [0, 0, 0] };
//~^ ERROR the constant `3` is not of type `usize`
}
14 changes: 14 additions & 0 deletions tests/ui/consts/ice-const-size-relate-126359.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: the constant `N` is not of type `usize`
--> $DIR/ice-const-size-relate-126359.rs:7:10
|
LL | arr: [T; N],
| ^^^^^^ expected `usize`, found `u8`

error: the constant `3` is not of type `usize`
--> $DIR/ice-const-size-relate-126359.rs:12:39
|
LL | let _ = OppOrder::<3, u32> { arr: [0, 0, 0] };
| ^^^^^^^^^ expected `usize`, found `u8`

error: aborting due to 2 previous errors

Loading