You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#[derive(Clone,Copy)]structFoo{sort_idx:usize,some_fn:unsafefn(*mutu8,u16)}unsafefndummy_fn(_:*mutu8, _:u16){}constfnsort_arr<constN:usize>(arr:[Foo;N]) -> [Foo;N]{letmut unsorted_arr:[Foo;N] = [Foo{sort_idx:0,some_fn: dummy_fn
};N];// My actual need is to construct a array of size N, copy arr into it and sort in place. This part is omitted for brevity.
unsorted_arr
}fnfoo(){}
I expected to see this happen: explanation
I expected this to work, and just compile.
Instead, this happened: explanation
function pointer casts are not allowed in constant functions
see issue #57563#57563 for more information
add #![feature(const_fn_fn_ptr_basics)] to the crate
It complains about unable being to assign the fn pointer due to a cast, which makes no sense since a type cast should not be necessary.
Interestingly enough, the following does compile and work fine:
#[derive(Clone,Copy)]structFoo{sort_idx:usize,some_fn:unsafefn(*mutu8,u16)}unsafefndummy_fn(_:*mutu8, _:u16){}constEMPTY_FOO:Foo = Foo{sort_idx:0,some_fn: dummy_fn
};constfnsome_const_fn<constN:usize>(arr:[Foo;N]) -> [Foo;N]{letmut unsorted_arr:[Foo;N] = [EMPTY_FOO;N];// My actual need is to construct a array of size N, copy arr into it and sort in place. This part is omitted for brevity.
unsorted_arr
}
It complains about unable being to assign the fn pointer due to a cast, which makes no sense since a type cast should not be necessary.
A type cast is happening here. dummy_fn is an expression denoting a function item which has a "function item type" -- a type of size 0 that uniquely denotes the function to be called (a "singleton type" in type theory lingo). This type needs to be cast to a "function pointer type", and that cast is forbidden inside const fn. For more information, see the reference.
The cast is allowed inside const, as you noted. This, too, is as expected -- due to backwards compatibility we cannot forbid these casts inside const, but they remain forbidden inside const fn until progress is made on rust-lang/rfcs#2632.
I tried this code:
I expected to see this happen: explanation
I expected this to work, and just compile.
Instead, this happened: explanation
function pointer casts are not allowed in constant functions
see issue #57563 #57563 for more information
add
#![feature(const_fn_fn_ptr_basics)]
to the crateIt complains about unable being to assign the fn pointer due to a cast, which makes no sense since a type cast should not be necessary.
Interestingly enough, the following does compile and work fine:
Meta
rustc --version --verbose
:Backtrace
The text was updated successfully, but these errors were encountered: