Skip to content

Confusing error message when trying to implement a function in an extern block #62109

Closed
@Gaelan

Description

@Gaelan

A programmer new to Rust (yours truly, for example) may assume that these two syntaxes are equivalent:

extern "C" {
    fn foo() -> i32 {
        return 0;
    }
}

extern "C" fn foo() -> i32 {
    return 0;
}

This is not the case, of course—the first is a syntax error. However, the compiler points this out rather unhelpfully:

error: expected one of `!`, `(`, `+`, `::`, `;`, `<`, or `where`, found `{`
 --> src/lib.rs:2:21
  |
2 |     fn foo() -> i32 {
  |                     ^ expected one of 7 possible tokens here

A message such as "If you're trying to declare a function callable from other languages, use extern "C" fn foo() (wording is bikesheddable, obviously) would have saved me a lot of hair-tearing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions