Skip to content

Crate name found in --extern gets suggested verbatim on unresolved import #113035

Description

@fmease

Given the code in file user.rs as shown below:

use a_great_crate::item;

rustc outputs the following when invoked via rustc user.rs --crate-type=lib --edition=2021 --extern=a-great-crate=liba_great_crate.rlib:

error[E0432]: unresolved import `a_great_crate`
 --> user.rs:1:5
  |
1 | use a_great_crate::item;
  |     ^^^^^^^^^^^^^ use of undeclared crate or module `a_great_crate`
  |
help: there is a crate or module with a similar name
  |
1 | use a-great-crate::item;
  |     ~~~~~~~~~~~~~

error: aborting due to previous error

For more information about this error, try `rustc --explain E0432`.

While ideally, in this specific case it should either suggest nothing of if feasible suggest changing the --extern flag to --extern=a_great_crate=liba_great_crate.rlib (the latter is probably P-low). I did actually get confused by the suggestion when I originally faced it.

In fact, rustc suggests whatever garbage you put in the flag (e.g. on rustc … '--extern=a&-gre?t#crate=xxx'). In these cases it shouldn't consider the name at all (I realize though that this might be a case of garbage in, garbage out).

@rustbot label T-compiler A-diagnostics A-suggestion-diagnostics D-invalid-suggestion

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jun 25, 2023
  2. est31 commented on Jun 26, 2023

    @est31
    Member

    Rustc could in fact issue an error if an invalid identifier is passed as extern name, because there is no way this identifier can be used by the program anyways (outside of the force option on --extern added by #109421 there is no use case). The only issue with that is that it's going to be a breaking change so needs to be phased in gradually. Alternatively, one could just issue a warning lint.

  3. fmease commented on Jun 30, 2023

    @fmease
    MemberAuthor

    Sounds even better! For some reason that didn't come to my mind when opening this issue

  4. est31 commented on Jul 6, 2023

    @est31
    Member

    I've filed an MCP (zulip).

  5. est31 commented on Aug 10, 2023

    @est31
    Member

    MCP is accepted now. Now, one could file a pull request. It should be pretty simple.

  6. fmease commented on Aug 18, 2023

    @fmease
    MemberAuthor

    @rustbot claim

  7. est31 commented on Aug 18, 2023

    @est31
    Member

    A low-level check would be to use rustc_lexer::is_ident. Of course, if you have any better ideas, write me.

  8. fmease commented on Sep 10, 2023

    @fmease
    MemberAuthor

    I'm sorry for not getting back to you. There's validate_crate_name which I plan on using if possible. Hoping to send a PR soon-ish, couldn't be as active in the project in the last few weeks as I wanted.

  9. est31 commented on Sep 10, 2023

    @est31
    Member

    @fmease no pressure! I'm in a similar situation in fact. Thanks for the heads up. Yeah, validate_crate_name looks like a good candidate, definitely worth to explore it. I think in addition the is_ident check would still help, as validate_crate_name does not reject names starting with numbers. But we can discuss details when you send the PR.

  10. added a commit that references this issue on Sep 22, 2023
    aadb571
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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