Skip to content
Merged
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
31 changes: 8 additions & 23 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2492,8 +2492,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
) -> Const<'tcx> {
let tcx = self.tcx();

let (elem_ty, len) = match ty.kind() {
ty::Array(elem_ty, len) => (elem_ty, len),
let elem_ty = match ty.kind() {
ty::Array(elem_ty, _) => elem_ty,
ty::Error(e) => return Const::new_error(tcx, *e),
_ => {
let e = tcx
Expand All @@ -2509,28 +2509,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
.map(|elem| self.lower_const_arg(elem, *elem_ty))
.collect::<Vec<_>>();

let len = tcx
.try_normalize_erasing_regions(
ty::TypingEnv::new(ty::ParamEnv::empty(), TypingMode::non_body_analysis()),
Unnormalized::new_wip(*len),
)
.unwrap_or(*len);
if let Some(expected_len) = len.try_to_target_usize(tcx)
&& expected_len != elems.len() as u64
{
let e = tcx.dcx().span_err(
array_expr.span,
format!(
"expected array with {expected_len} elements, found {} elements",
array_expr.elems.len()
),
);
return Const::new_error(tcx, e);
}

// The array len passed in the type might be an infer var, or a const param, or it could
// just be an incorrect constant. So, construct the resulting valtree's type based on the
// provided syntax rather than the expected type. The surrounding typeck will catch any
// mismatches.
let valtree_ty = Ty::new_array(tcx, *elem_ty, elems.len() as u64);
let valtree = ty::ValTree::from_branches(tcx, elems);

ty::Const::new_value(tcx, valtree, ty)
ty::Const::new_value(tcx, valtree, valtree_ty)
}

fn try_recover_misrepresented_function_call(
Expand Down
10 changes: 5 additions & 5 deletions tests/ui/const-generics/mgca/array-const-arg-len-mismatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ fn baz<const A: [u8; <S as Trait>::LEN]>() {}

fn main() {
foo::<u8, 2, { [] }>();
//~^ ERROR: expected array with 2 elements, found 0 elements
//~^ ERROR: the constant `*b""` is not of type `[u8; 2]`
foo::<u8, 2, { [0, 0, 0] }>();
//~^ ERROR: expected array with 2 elements, found 3 elements
//~^ ERROR: the constant `*b"\x00\x00\x00"` is not of type `[u8; 2]`
bar::<{ [] }>();
//~^ ERROR: expected array with 2 elements, found 0 elements
//~^ ERROR: the constant `*b""` is not of type `[u8; 2]`
bar::<{ [1, 2, 3] }>();
//~^ ERROR: expected array with 2 elements, found 3 elements
//~^ ERROR: the constant `*b"\x01\x02\x03"` is not of type `[u8; 2]`
baz::<{ [42] }>();
//~^ ERROR: expected array with 3 elements, found 1 elements
//~^ ERROR: the constant `*b"*"` is not of type `[u8; 3]`
}
54 changes: 42 additions & 12 deletions tests/ui/const-generics/mgca/array-const-arg-len-mismatch.stderr
Original file line number Diff line number Diff line change
@@ -1,32 +1,62 @@
error: expected array with 2 elements, found 0 elements
--> $DIR/array-const-arg-len-mismatch.rs:30:20
error: the constant `*b""` is not of type `[u8; 2]`
--> $DIR/array-const-arg-len-mismatch.rs:30:11
|
LL | foo::<u8, 2, { [] }>();
| ^^
| ^^ expected `[u8; 2]`, found `[u8; 0]`
|
note: required by a const generic parameter in `foo`
--> $DIR/array-const-arg-len-mismatch.rs:11:42
|
LL | fn foo<T: ConstParamTy_, const N: usize, const M: [T; N]>() -> [T; N] {
| ^^^^^^^^^^^^^^^ required by this const generic parameter in `foo`

error: expected array with 2 elements, found 3 elements
--> $DIR/array-const-arg-len-mismatch.rs:32:20
error: the constant `*b"\x00\x00\x00"` is not of type `[u8; 2]`
--> $DIR/array-const-arg-len-mismatch.rs:32:11
|
LL | foo::<u8, 2, { [0, 0, 0] }>();
| ^^^^^^^^^
| ^^ expected `[u8; 2]`, found `[u8; 3]`
|
note: required by a const generic parameter in `foo`
--> $DIR/array-const-arg-len-mismatch.rs:11:42
|
LL | fn foo<T: ConstParamTy_, const N: usize, const M: [T; N]>() -> [T; N] {
| ^^^^^^^^^^^^^^^ required by this const generic parameter in `foo`

error: expected array with 2 elements, found 0 elements
error: the constant `*b""` is not of type `[u8; 2]`
--> $DIR/array-const-arg-len-mismatch.rs:34:13
|
LL | bar::<{ [] }>();
| ^^
| ^^ expected `[u8; 2]`, found `[u8; 0]`
|
note: required by a const generic parameter in `bar`
--> $DIR/array-const-arg-len-mismatch.rs:15:8
|
LL | fn bar<const A: [u8; 2]>() {}
| ^^^^^^^^^^^^^^^^ required by this const generic parameter in `bar`

error: expected array with 2 elements, found 3 elements
error: the constant `*b"\x01\x02\x03"` is not of type `[u8; 2]`
--> $DIR/array-const-arg-len-mismatch.rs:36:13
|
LL | bar::<{ [1, 2, 3] }>();
| ^^^^^^^^^
| ^^^^^^^^^ expected `[u8; 2]`, found `[u8; 3]`
|
note: required by a const generic parameter in `bar`
--> $DIR/array-const-arg-len-mismatch.rs:15:8
|
LL | fn bar<const A: [u8; 2]>() {}
| ^^^^^^^^^^^^^^^^ required by this const generic parameter in `bar`

error: expected array with 3 elements, found 1 elements
error: the constant `*b"*"` is not of type `[u8; 3]`
--> $DIR/array-const-arg-len-mismatch.rs:38:13
|
LL | baz::<{ [42] }>();
| ^^^^
| ^^^^ expected `[u8; 3]`, found `[u8; 1]`
|
note: required by a const generic parameter in `baz`
--> $DIR/array-const-arg-len-mismatch.rs:27:8
|
LL | fn baz<const A: [u8; <S as Trait>::LEN]>() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this const generic parameter in `baz`

error: aborting due to 5 previous errors

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@ known-bug: #160553
//@ compile-flags: -Copt-level=0
//! Regression test for #160553 (used to ICE)
#![allow(incomplete_features)]
#![feature(adt_const_params, min_generic_const_args, macroless_generic_const_args)]
#![feature(generic_const_parameter_types)]
Expand All @@ -20,6 +19,7 @@ fn foo<T: Trait, const A: [u8; <T as Trait>::LEN]>() -> [u8; <T as Trait>::LEN]

fn bar<T: Trait>() -> [u8; <T as Trait>::LEN] {
foo::<T, { [1, 2, 3] }>()
//~^ ERROR the constant `*b"\x01\x02\x03"` is not of type `[u8; <T as Trait>::LEN]`
}

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: the constant `*b"\x01\x02\x03"` is not of type `[u8; <T as Trait>::LEN]`
--> $DIR/generic_const_items-mismatched-array-len.rs:21:11
|
LL | foo::<T, { [1, 2, 3] }>()
| ^ expected `[u8; <T as Trait>::LEN]`, found `[u8; 3]`
|
note: required by a const generic parameter in `foo`
--> $DIR/generic_const_items-mismatched-array-len.rs:16:18
|
LL | fn foo<T: Trait, const A: [u8; <T as Trait>::LEN]>() -> [u8; <T as Trait>::LEN] {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this const generic parameter in `foo`

error: aborting due to 1 previous error

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@ check-pass

#![feature(min_adt_const_params, min_generic_const_args, generic_const_parameter_types)]
fn foo<const N: usize, const A: [u8; N]>() {}

fn main() {
foo::<_, core::direct_const_arg!([0, 1, 2, 3])>();
}
Loading