-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
&Vec to function expecting a &[u8; 32] #32559
Comments
Inlining the test for future reference: pub fn test(a: &[u8; 32]) {}
fn main() {
test(&vec![]);
} |
ping @nikomatsakis, this is the same issue (I think) as #32320 backtrace:
|
I've done some testing: the important parts are: the passed type must have at least one type parameter, it must implement fn main() {
// may have any number of type parameters greater than zero
struct WithParam<T>(std::marker::PhantomData<T>);
// Target can be anything
impl<T> std::ops::Deref for WithParam<T> {
type Target = ();
fn deref(&self) -> &() {
panic!()
}
}
// can be literally anything
struct OtherType;
fn doop(_: &OtherType) { }
// must not be able to infer the inner type
doop(&WithParam(std::marker::PhantomData))
// the references must be able to coerce,
// so either &mut -> &, & -> &, or &mut -> &mut
// passing an &WithParam to a function expecting an &mut OtherType
// will not create ICE conditions
} https://play.rust-lang.org/?gist=1ff04f679e568535f7c9&version=stable |
On nightly, this is fixed. |
duplicate of that issue. |
Your example is broken now! |
no, its supposed to throw an error, it just wasnt supposed to cause an internal compiler error.
this is due to the function having the explicit length 32 in its signature. since the vector is dynamically sized, it cannot be coerced into a fixed size array. the function should take &[u32] as an argument, a slice (of, at compile-time, unknown length). I hope this answers your question, otherwise have a look at the book regarding slices. |
how to easily convert an |
you could try this solution by implementing your own function to convert it or use the recently stabilized TryInto trait like suggested in this answer. Although I love answering your questions, please dont use the bug tracker for them but rather one of the other chat platforms like discord or IRC. |
I tried passing an empty vector to a function and the compiler crashed.
Here is a working example:
http://is.gd/EOfLkC
I think it has to do with the compiler trying to deref the vec.
Meta
rustc 1.7.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.7.0
But the bug is in beta and nightly, too.
I don't have a backtrace. Should be easy for you to get one though.
The text was updated successfully, but these errors were encountered: