Skip to content

Error message for duplicate names suggests invalid Rust #45829

Closed

Description

@Twey

Consider the following library (with hello and aleph externs provided):

extern crate hello;
extern crate aleph as hello;

rustc will reasonably identify a problem:

error[E0259]: the name `hello` is defined multiple times
 --> lib.rs:2:1
  |
1 | extern crate hello;
  | ------------------- previous import of the extern crate `hello` here
2 | extern crate aleph as hello;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `hello` reimported here
  |
  = note: `hello` must be defined only once in the type namespace of this module

but will then go on to suggest a rather unhelpful solution that isn't even valid Rust code:

help: You can use `as` to change the binding name of the import
  |
2 | extern crate aleph as hello; as Otherhello
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    C-bugCategory: This is a bug.
    on Nov 7, 2017
  2. estebank commented on Nov 7, 2017

    @estebank
    Contributor

    Related to #45799.

    #45660 should verify wether the crate has already been renamed before providing the help. I believe this can be done by checking wether the NameBinding.kind::<Import> fields target and source are the same.

  3. Cldfire commented on Nov 8, 2017

    @Cldfire
    Contributor

    @Twey, thanks for noticing that and filing an issue 😄

    And @estebank, thanks for the instructions. I'll add a commit that fixes this as a part of #45820 when I get the chance.

    EDIT: Looks like you've got it handled 👍

  4. added a commit that references this issue on Nov 10, 2017
    de083eb
  5. emilyalbini commented on Sep 17, 2018

    @emilyalbini
    Member

    This also happens with uses:

    #![allow(unused_imports)]
    
    mod foo {
        pub struct A;
        pub struct B;
    }
    
    use foo::{A, B as A};
    
    fn main() {}
    error[E0252]: the name `A` is defined multiple times
     --> src/main.rs:8:14
      |
    8 | use foo::{A, B as A};
      |           -  ^^^^^^ `A` reimported here
      |           |
      |           previous import of the type `A` here
      |
      = note: `A` must be defined only once in the type namespace of this module
    help: You can use `as` to change the binding name of the import
      |
    8 | use foo::{A, B as A as OtherA};
      |              ^^^^^^^^^^^^^^^^
    
  6. estebank commented on Sep 17, 2018

    @estebank
    Contributor

    @pietroalbini thanks for fishing this out! The fix for use will likely be very similar to #45856.

  7. added
    E-help-wantedCall for participation: Help is requested to fix this issue.
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    on Sep 17, 2018
  8. mockersf commented on Oct 16, 2018

    @mockersf
    Contributor

    Hello ! I took a shot at this.

    I noticed that rust no longer suggests a new name for an extern crate import that has already been renamed.
    This is because the suggestion is behind this check (https://github.com/rust-lang/rust/blob/master/src/librustc_resolve/lib.rs#L4788):

               if let (Ok(snippet), false) = (cm.span_to_snippet(binding.span),
                                               binding.is_renamed_extern_crate()) {

    I didn't change that in #55113 in case it was done willingly, but that should be easy to change.

  9. estebank commented on Oct 16, 2018

    @estebank
    Contributor

    @mockersf commented on the PR. If you can apply the fix to extern statements as well, go ahead :)

  10. added a commit that references this issue on Oct 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.E-help-wantedCall for participation: Help is requested to fix this issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions