Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upArray lengths don't support generic parameters. #43408
Comments
Mark-Simulacrum
added
A-const-fn
C-bug
labels
Jul 26, 2017
This comment has been minimized.
This comment has been minimized.
|
cc @eddyb -- was this intentional? |
Mark-Simulacrum
added
the
T-compiler
label
Jul 26, 2017
This comment has been minimized.
This comment has been minimized.
|
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 comment has been minimized.
This comment has been minimized.
|
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 comment has been minimized.
This comment has been minimized.
|
This makes me sad, as it's the one thing I wanted to do with #42859 |
frewsxcv
referenced this issue
Oct 28, 2017
Closed
Calling std::mem::size_of::<T> in a generic constexpr context errors. #45598
npmccallum
referenced this issue
Feb 24, 2018
Closed
Cannot resolve size of either Self or generic type in method signature #48505
This comment was marked as outdated.
This comment was marked as outdated.
|
Weird thing: You can... um... kind of work around it. (but perhaps you shouldn't) pub trait ConstSizeOf: Sized {
const SIZE: usize = ::std::mem::size_of::<Self>();
}
impl<T> ConstSizeOf for T { }You get a, uh... warning... that sounds extremely serious and that seems to suggest that this will have a very high probability of blowing up in your face.
But, um... it, uh...seems to work. fn main() {
// using println instead of assert_eq! to make sure it isn't just optimized
// away as undefined behavior
println!("{:?}", <i8>::SIZE); // prints 1
println!("{:?}", <i16>::SIZE); // prints 2
println!("{:?}", <i32>::SIZE); // prints 4
println!("{:?}", <i64>::SIZE); // prints 8
println!("{:?}", <Vec<()>>::SIZE); // prints 24
println!("{:?}", <Vec<i8>>::SIZE); // prints 24
fn test_vec<T>() {
println!("{:?}", Vec::<T>::SIZE);
}
fn test_tuple<T>() {
println!("{:?}", <(T, T)>::SIZE);
}
test_vec::<()>(); // prints 24
test_vec::<i8>(); // prints 24
test_tuple::<()>(); // prints 0
test_tuple::<i8>(); // prints 2
} |
This comment was marked as outdated.
This comment was marked as outdated.
|
Oops, never mind. // error: T: Sized is not satisfied
fn to_byte_array<T>() -> [u8; T::SIZE] {
panic!()
}I could have sworn I've done something before with associated |
This comment has been minimized.
This comment has been minimized.
|
I think we shoud change the error message, this is very confusing now. |
This comment has been minimized.
This comment has been minimized.
|
@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. |
eddyb
changed the title
const fn size_of doesn't work on generic types
Array lengths don't support generic type parameters.
Apr 17, 2018
jannschu
referenced this issue
May 13, 2018
Closed
Incorrect error when using `mem::size_of` with generics, and "const_size_of" feature enabled. #50651
bgeron
referenced this issue
May 19, 2018
Open
Cannot use associated type as type of associated const #46969
This comment has been minimized.
This comment has been minimized.
MageSlayer
commented
May 24, 2018
•
#![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? |
This comment has been minimized.
This comment has been minimized.
|
@MageSlayer Nope, it just can't be supported yet, see #43408 (comment) and previous. |
bergus
referenced this issue
Jun 5, 2018
Closed
Misleading error message when using associated const in where clause for fixed size array #44774
dtolnay
referenced this issue
Jun 9, 2018
Closed
Error message should be improved for associated consts in array lengths #44168
kennytm
referenced this issue
Jun 28, 2018
Merged
Do not allow LLVM to increase a TLS's alignment on macOS. #51828
mjbshaw
referenced this issue
Jun 29, 2018
Open
ICE when computing the size of a specialized associated trait type #51892
jeehoonkang
referenced this issue
Jul 26, 2018
Closed
spawn_unchecked and builder_spawn_unchecked should return ScopedJoinHandle<T> #25
vitalyd
referenced this issue
Jul 30, 2018
Closed
Cannot use associated const to specify static array length #52070
This was referenced Sep 16, 2018
bors
added a commit
that referenced
this issue
Sep 19, 2018
kennytm
added a commit
to kennytm/rust
that referenced
this issue
Sep 20, 2018
kennytm
added a commit
to kennytm/rust
that referenced
this issue
Sep 20, 2018
varkor
referenced this issue
Sep 27, 2018
Closed
Failed to compile [T; std::mem::size_of::<U>()] #54611
dtolnay
referenced this issue
Oct 8, 2018
Closed
Confusing diagnostic with assocaited constants #54849
bors bot
added a commit
to tock/tock
that referenced
this issue
Oct 17, 2018
This was referenced Oct 22, 2018
andreytkachenko
referenced this issue
Nov 2, 2018
Open
[WIP] The Genesis of Generic Germination #53645
nikic
referenced this issue
Dec 4, 2018
Closed
mem::size_of does not work with generic struct parameter #56512
Palladinium
referenced this issue
Jan 1, 2019
Open
Confusing error message for associated const #56589
oli-obk
referenced this issue
Jan 28, 2019
Closed
Generic bounds don't seem to be used in consteval #50308
This comment has been minimized.
This comment has been minimized.
|
Here is my workaround in Serde for |
roblabla commentedJul 22, 2017
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
Tdoesn't havestd::marker::Sized, even though it does have that bound.Meta
rustc_version : rustc 1.20.0-nightly (ae98ebf 2017-07-20)