Skip to content
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

Overly aggressive const_fn function pointer check. #83033

Closed
HindrikStegenga opened this issue Mar 11, 2021 · 1 comment
Closed

Overly aggressive const_fn function pointer check. #83033

HindrikStegenga opened this issue Mar 11, 2021 · 1 comment
Labels
C-bug Category: This is a bug.

Comments

@HindrikStegenga
Copy link

HindrikStegenga commented Mar 11, 2021

I tried this code:

#[derive(Clone, Copy)]
struct Foo {
    sort_idx: usize,
    some_fn: unsafe fn(*mut u8, u16)
}

unsafe fn dummy_fn(_: *mut u8, _: u16) {}


const fn sort_arr<const N: usize>(arr: [Foo; N]) -> [Foo; N] {
    let mut 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
}

fn foo() {

}

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)]
struct Foo {
    sort_idx: usize,
    some_fn: unsafe fn(*mut u8, u16)
}

unsafe fn dummy_fn(_: *mut u8, _: u16) {}

const EMPTY_FOO: Foo = Foo {
    sort_idx: 0,
    some_fn: dummy_fn
};

const fn some_const_fn<const N: usize>(arr: [Foo; N]) -> [Foo; N] {
    let mut 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
}

Meta

rustc --version --verbose:

rustc 1.51.0-nightly (e38fb306b 2021-01-14)
binary: rustc
commit-hash: e38fb306b7f5e65cca34df2dab1f0db15e1defb4
commit-date: 2021-01-14
host: x86_64-apple-darwin
release: 1.51.0-nightly
LLVM version: 11.0
Backtrace

<backtrace>

@RalfJung
Copy link
Member

RalfJung commented May 3, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants