-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Array lengths don't support generic parameters. #43408
Comments
cc @eddyb -- was this intentional? |
This is a limitation of array length - it can't use any parameters in scope - which also came up during the stabilization of associated consts (cc @withoutboats do we have a common issue to use for this?) and the decision taken was to stabilize associated consts without it. The exact error here comes from the type parameter being resolved, but the |
This example triggers a const-evaluation error instead (try on playpen): fn test<T: ?Sized>() {
[0u8; std::mem::size_of::<&T>()];
} cc @GuillaumeGomez @oli-obk Why is the const-eval error printing broken still 😢? |
This makes me sad, as it's the one thing I wanted to do with #42859 😢 |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I think we shoud change the error message, this is very confusing now. |
@ExpHP The problem is using type parameters (e.g. your @juchiast The error message is "emergent" from the same reason we can't "just" allow this to work right now, the the only other solution I can think of is checking if type-parameters are "really in scope" but that would probably break existing code that doesn't need to look at type parameters. |
#![feature(const_fn)]
pub const fn sof<T:?Sized>() -> usize {
10
}
fn to_byte_array<T>() -> [u8; sof::<T>()] {
panic!()
} Trying this way results in compiler crash in nightly.
Any workarounds known? |
@MageSlayer Nope, it just can't be supported yet, see #43408 (comment) and previous. |
Triage: The current output of #43408 (comment) with the latest nightly:
And with
It's no longer ICE but rejected by |
This now appear to work-ish, but is a bit awkward to use: #![feature(generic_const_exprs)]
unsafe fn zeroed<T: Sized>() -> T
where
[(); std::mem::size_of::<T>()]:
{
// First create an array that has the same size as T, initialized at 0
let x = [0u8; std::mem::size_of::<T>()];
// Then, transmute it to type T, and return it
std::mem::transmute_copy(&x)
} Two things of note:
|
This comment has been minimized.
This comment has been minimized.
@ExpHP I believe this is wrong, and my implementation is safe even if
(Emphasis mine) I believe this means that it is correct even when &U (our output type) has a "stricter alignment" (higher alignment) than |
@roblabla is right, The docs for |
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
@rustbot label -I-ICE |
Current Rust 1.45.2 doesn't support this. So need to wait until Rust is improved. rust-lang/rust#43408
EDIT: This can now be made to work on nightly compilers, although it's a bit awkward to use. See this comment.
It would seem that when using size_of in const fn context, it fails to properly compute the size of generic types.
The following function fails
The error is the following :
This is very confusing because it complains that
T
doesn't havestd::marker::Sized
, even though it does have that bound.Meta
rustc_version : rustc 1.20.0-nightly (ae98ebf 2017-07-20)
The text was updated successfully, but these errors were encountered: