Skip to content

Misleading note for accessing closures within structs. #32128

Closed
@ix

Description

@ix

Hi, I encountered an issue the other day wherein I was attempting to call a closure stored within a struct.

The compiler gives a misleading error which doesn't give the user an idea on how to fix the problem.
I've prepared an example which reproduces the misleading text:

struct Example {
    example: Box<Fn(i32) -> i32>
}

fn main() {
    let demo = Example {
        example: Box::new(|x| {
            x + 1
        })
    };

    demo.example(1);
    // (demo.example)(1);
}

The error this yields from the compiler is as follows:

structs.rs:12:10: 12:20 error: no method named `example` found for type `Example` in the current scope
structs.rs:12     demo.example(1);
                       ^~~~~~~~~~
structs.rs:12:10: 12:20 note: did you mean to write `demo.example`?
structs.rs:12     demo.example(1);
                       ^~~~~~~~~~
error: aborting due to previous error

Note that in the source code I've provided, underneath the line which yields this error, I have provided a comment demonstrating the correct way to access and call the example member of the structure, however the compiler's example is identical to the erroneous code and thus doesn't help the user in working out how to properly call the member.

Would it be at all possible to improve this error message?

I've confirmed this is reproducible on rustc 1.7.0 (a5d1e7a59 2016-02-29).

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