Skip to content

Better error messages when comparing fn ptrs #577

Closed
@RalfJung

Description

@RalfJung

The following code

fn f() -> i32 {
    42
}

fn return_fn_ptr() -> fn() -> i32 {
    f
}

fn main() {
    assert!(return_fn_ptr() != f);
}

results in the following error:

error[E0080]: constant evaluation error: tried to dereference a function pointer
  --> fnptr.rs:10:13
   |
10 |     assert!(return_fn_ptr() == f);
   |             ^^^^^^^^^^^^^^^^^^^^ tried to dereference a function pointer
   |
   = note: inside call to `main` at /home/r/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:74:34
   = note: inside call to closure at /home/r/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:62:53
   = note: inside call to closure at /home/r/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/panicking.rs:307:40
   = note: inside call to `std::panicking::try::do_call::<[closure@DefId(1/1:1898 ~ std[8e5f]::rt[0]::lang_start_internal[0]::{{closure}}[0]) 0:&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe], i32>` at /home/r/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/panicking.rs:303:5
   = note: inside call to `std::panicking::try::<i32, [closure@DefId(1/1:1898 ~ std[8e5f]::rt[0]::lang_start_internal[0]::{{closure}}[0]) 0:&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe]>` at /home/r/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/panic.rs:398:9
   = note: inside call to `std::panic::catch_unwind::<[closure@DefId(1/1:1898 ~ std[8e5f]::rt[0]::lang_start_internal[0]::{{closure}}[0]) 0:&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe], i32>` at /home/r/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:62:25
   = note: inside call to `std::rt::lang_start_internal` at /home/r/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:74:5
   = note: inside call to `std::rt::lang_start::<()>`

This error message is factually incorrect, the function pointer does not get dereferenced. We should fix that, either by making the test case pass or by at least giving a more accurate error message.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions