error on unsized AnonConsts

This commit is contained in:
khyperia 2026-02-02 20:48:14 +01:00
parent f60a0f1bcc
commit c7c3266b8a
10 changed files with 269 additions and 16 deletions

View file

@ -200,6 +200,14 @@ fn typeck_with_inspect<'tcx>(
let wf_code = ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(def_id)));
fcx.register_wf_obligation(expected_type.into(), body.value.span, wf_code);
if let hir::Node::AnonConst(_) = node {
fcx.require_type_is_sized(
expected_type,
body.value.span,
ObligationCauseCode::SizedConstOrStatic,
);
}
fcx.check_expr_coercible_to_type(body.value, expected_type, None);
fcx.write_ty(id, expected_type);

View file

@ -1,16 +0,0 @@
//@ known-bug: #137582
#![feature(adt_const_params)]
mod lib {
pub type Matrix = [&'static u32];
const EMPTY_MATRIX: Matrix = [[0; 4]; 4];
pub struct Walk<const CURRENT: usize, const REMAINING: Matrix> {
_p: (),
}
impl<const CURRENT: usize> Walk<CURRENT, EMPTY_MATRIX> {}
}
fn main() {}

View file

@ -0,0 +1,22 @@
// regression test for issue #137582, where constant evaluating an unsized AnonConst would ICE
#![feature(adt_const_params)]
mod lib {
pub type Matrix = [&'static u32];
const EMPTY_MATRIX: Matrix = [[0; 4]; 4];
//~^ ERROR the size for values of type `[&'static u32]` cannot be known at compilation time
//~| ERROR mismatched types
//~| ERROR mismatched types
pub struct Walk<const CURRENT: usize, const REMAINING: Matrix> {
//~^ ERROR use of unstable library feature `unsized_const_params`
_p: (),
}
impl<const CURRENT: usize> Walk<CURRENT, EMPTY_MATRIX> {}
//~^ ERROR the size for values of type `[&'static u32]` cannot be known at compilation time
}
fn main() {}

View file

@ -0,0 +1,44 @@
error[E0277]: the size for values of type `[&'static u32]` cannot be known at compilation time
--> $DIR/unsized-anon-const-err-1.rs:8:25
|
LL | const EMPTY_MATRIX: Matrix = [[0; 4]; 4];
| ^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[&'static u32]`
= note: statics and constants must have a statically known size
error[E0658]: use of unstable library feature `unsized_const_params`
--> $DIR/unsized-anon-const-err-1.rs:13:43
|
LL | pub struct Walk<const CURRENT: usize, const REMAINING: Matrix> {
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(unsized_const_params)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= note: required for `[&'static u32]` to implement `ConstParamTy_`
error[E0277]: the size for values of type `[&'static u32]` cannot be known at compilation time
--> $DIR/unsized-anon-const-err-1.rs:18:46
|
LL | impl<const CURRENT: usize> Walk<CURRENT, EMPTY_MATRIX> {}
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[&'static u32]`
= note: statics and constants must have a statically known size
error[E0308]: mismatched types
--> $DIR/unsized-anon-const-err-1.rs:8:35
|
LL | const EMPTY_MATRIX: Matrix = [[0; 4]; 4];
| ^^^^^^ expected `&u32`, found `[{integer}; 4]`
error[E0308]: mismatched types
--> $DIR/unsized-anon-const-err-1.rs:8:34
|
LL | const EMPTY_MATRIX: Matrix = [[0; 4]; 4];
| ^^^^^^^^^^^ expected `[&u32]`, found `[&u32; 4]`
error: aborting due to 5 previous errors
Some errors have detailed explanations: E0277, E0308, E0658.
For more information about an error, try `rustc --explain E0277`.

View file

@ -0,0 +1,21 @@
// regression test for issue #151591, where constant evaluating an unsized AnonConst would ICE
#![feature(adt_const_params)]
#![feature(unsized_const_params)]
//~^ WARN the feature `unsized_const_params` is incomplete and may not be safe to use and/or cause compiler crashes
#[derive(Clone)]
struct S<const L: [u8]>;
const A: [u8];
//~^ ERROR free constant item without body
//~| ERROR the size for values of type `[u8]` cannot be known at compilation time
impl<const N: i32> Copy for S<A> {}
//~^ ERROR the size for values of type `[u8]` cannot be known at compilation time
//~| ERROR the const parameter `N` is not constrained by the impl trait, self type, or predicates
impl<const M: usize> Copy for S<A> {}
//~^ ERROR the size for values of type `[u8]` cannot be known at compilation time
//~| ERROR the const parameter `M` is not constrained by the impl trait, self type, or predicates
fn main() {}

View file

@ -0,0 +1,66 @@
error: free constant item without body
--> $DIR/unsized-anon-const-err-2.rs:10:1
|
LL | const A: [u8];
| ^^^^^^^^^^^^^-
| |
| help: provide a definition for the constant: `= <expr>;`
warning: the feature `unsized_const_params` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/unsized-anon-const-err-2.rs:4:12
|
LL | #![feature(unsized_const_params)]
| ^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #95174 <https://github.com/rust-lang/rust/issues/95174> for more information
= note: `#[warn(incomplete_features)]` on by default
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/unsized-anon-const-err-2.rs:10:10
|
LL | const A: [u8];
| ^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u8]`
= note: statics and constants must have a statically known size
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/unsized-anon-const-err-2.rs:14:31
|
LL | impl<const N: i32> Copy for S<A> {}
| ^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u8]`
= note: statics and constants must have a statically known size
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/unsized-anon-const-err-2.rs:17:33
|
LL | impl<const M: usize> Copy for S<A> {}
| ^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u8]`
= note: statics and constants must have a statically known size
error[E0207]: the const parameter `N` is not constrained by the impl trait, self type, or predicates
--> $DIR/unsized-anon-const-err-2.rs:14:6
|
LL | impl<const N: i32> Copy for S<A> {}
| ^^^^^^^^^^^^ unconstrained const parameter
|
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported
error[E0207]: the const parameter `M` is not constrained by the impl trait, self type, or predicates
--> $DIR/unsized-anon-const-err-2.rs:17:6
|
LL | impl<const M: usize> Copy for S<A> {}
| ^^^^^^^^^^^^^^ unconstrained const parameter
|
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported
error: aborting due to 6 previous errors; 1 warning emitted
Some errors have detailed explanations: E0207, E0277.
For more information about an error, try `rustc --explain E0207`.

View file

@ -0,0 +1,16 @@
// manually reduced reproduction of issue #137582, where constant evaluating an unsized AnonConst
// would ICE
#![feature(adt_const_params)]
fn func<const V: [u32]>() {}
//~^ ERROR use of unstable library feature `unsized_const_params`
const VALUE: [u32] = [0; 4];
//~^ ERROR mismatched types
//~| ERROR the size for values of type `[u32]` cannot be known at compilation time
fn main() {
func::<VALUE>();
//~^ ERROR the size for values of type `[u32]` cannot be known at compilation time
}

View file

@ -0,0 +1,38 @@
error[E0658]: use of unstable library feature `unsized_const_params`
--> $DIR/unsized-anon-const-func-err.rs:6:9
|
LL | fn func<const V: [u32]>() {}
| ^^^^^^^^^^^^^^
|
= help: add `#![feature(unsized_const_params)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= note: required for `[u32]` to implement `ConstParamTy_`
error[E0277]: the size for values of type `[u32]` cannot be known at compilation time
--> $DIR/unsized-anon-const-func-err.rs:9:14
|
LL | const VALUE: [u32] = [0; 4];
| ^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u32]`
= note: statics and constants must have a statically known size
error[E0308]: mismatched types
--> $DIR/unsized-anon-const-func-err.rs:9:22
|
LL | const VALUE: [u32] = [0; 4];
| ^^^^^^ expected `[u32]`, found `[u32; 4]`
error[E0277]: the size for values of type `[u32]` cannot be known at compilation time
--> $DIR/unsized-anon-const-func-err.rs:14:12
|
LL | func::<VALUE>();
| ^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u32]`
= note: statics and constants must have a statically known size
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0277, E0308, E0658.
For more information about an error, try `rustc --explain E0277`.

View file

@ -0,0 +1,16 @@
// manually reduced reproduction of issue #137582, where constant evaluating an unsized AnonConst
// would ICE
#![feature(adt_const_params)]
const VALUE: [u32] = [0; 4];
//~^ ERROR the size for values of type `[u32]` cannot be known at compilation time
//~| ERROR mismatched types
struct SomeStruct<const V: [u32]> {}
//~^ ERROR use of unstable library feature `unsized_const_params`
impl SomeStruct<VALUE> {}
//~^ ERROR the size for values of type `[u32]` cannot be known at compilation time
fn main() {}

View file

@ -0,0 +1,38 @@
error[E0277]: the size for values of type `[u32]` cannot be known at compilation time
--> $DIR/unsized-anon-const-struct-err.rs:6:14
|
LL | const VALUE: [u32] = [0; 4];
| ^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u32]`
= note: statics and constants must have a statically known size
error[E0658]: use of unstable library feature `unsized_const_params`
--> $DIR/unsized-anon-const-struct-err.rs:10:19
|
LL | struct SomeStruct<const V: [u32]> {}
| ^^^^^^^^^^^^^^
|
= help: add `#![feature(unsized_const_params)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= note: required for `[u32]` to implement `ConstParamTy_`
error[E0277]: the size for values of type `[u32]` cannot be known at compilation time
--> $DIR/unsized-anon-const-struct-err.rs:13:17
|
LL | impl SomeStruct<VALUE> {}
| ^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u32]`
= note: statics and constants must have a statically known size
error[E0308]: mismatched types
--> $DIR/unsized-anon-const-struct-err.rs:6:22
|
LL | const VALUE: [u32] = [0; 4];
| ^^^^^^ expected `[u32]`, found `[u32; 4]`
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0277, E0308, E0658.
For more information about an error, try `rustc --explain E0277`.