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

Better error messages when comparing fn ptrs #577

Closed
RalfJung opened this issue Dec 18, 2018 · 1 comment
Closed

Better error messages when comparing fn ptrs #577

RalfJung opened this issue Dec 18, 2018 · 1 comment

Comments

@RalfJung
Copy link
Member

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.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 8, 2019

The test passes now

@oli-obk oli-obk closed this as completed Jan 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants