Skip to content

Confusing error message calling indirect function pointer #29043

Closed
@emoon

Description

@emoon

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions