Skip to content

Confusing error message calling indirect function pointer #29043

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

Closed
emoon opened this issue Oct 14, 2015 · 2 comments
Closed

Confusing error message calling indirect function pointer #29043

emoon opened this issue Oct 14, 2015 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@emoon
Copy link
Contributor

emoon commented Oct 14, 2015

Hi,

When compiling this code

extern crate libc;

#[repr(C)]
pub struct ApiFromC {
    some_data: *mut libc::c_void,
    some_function: extern fn(data: *mut libc::c_void),
}

struct RustAPI {
    api_from_c: *mut ApiFromC,
}

impl RustAPI {
    fn call_c_api(&self) {
        unsafe {
            (*self.api_from_c).some_function((*self.api_from_c).some_data)
        }
    }
}

Playpen link: http://is.gd/BwFkak

The error message produced

<anon>:17:23: 17:66 error: no method named `some_function` found for type `ApiFromC` in the current scope
<anon>:17           (*self.api_from_c).some_function((*self.api_from_c).some_data)
                                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:17:23: 17:66 note: did you mean to write `(*self.api_from_c).some_function`?
<anon>:17           (*self.api_from_c).some_function((*self.api_from_c).some_data)
                                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
playpen: application terminated with error code 101

So the correct way to fix this code is by changing

    (*self.api_from_c).some_function((*self.api_from_c).some_data)

to

    ((*self.api_from_c).some_function)((*self.api_from_c).some_data)

But it's from the error message not very clear that's the issue.

@huonw huonw added A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. and removed E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. labels Oct 14, 2015
@huonw
Copy link
Member

huonw commented Oct 14, 2015

Strangely enough, there is a custom error message to handle this case but it seems that it isn't emitted for fields that are extern fns, e.g. if the extern is removed from api_from_c's type, the output looks like:

<anon>:17:23: 17:66 error: no method named `some_function` found for type `ApiFromC` in the current scope
<anon>:17           (*self.api_from_c).some_function((*self.api_from_c).some_data)
                                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:17:23: 17:66 note: use `((*self.api_from_c).some_function)(...)` if you meant to call the function stored in the `some_function` field
<anon>:17           (*self.api_from_c).some_function((*self.api_from_c).some_data)

So presumably some match or if somewhere in the compiler needs to understand that extern fns are fns.

@emoon
Copy link
Contributor Author

emoon commented Oct 14, 2015

@huonw Ah, that is interesting. Thanks for the info and shouldn't be that hard to fix then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

2 participants