Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error message for duplicate names suggests invalid Rust #45829

Closed
Twey opened this issue Nov 7, 2017 · 6 comments
Closed

Error message for duplicate names suggests invalid Rust #45829

Twey opened this issue Nov 7, 2017 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. E-help-wanted Call for participation: Help is requested to fix this issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

Comments

@Twey
Copy link

Twey commented Nov 7, 2017

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
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@kennytm kennytm added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. labels Nov 7, 2017
@estebank
Copy link
Contributor

estebank commented Nov 7, 2017

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.

@Cldfire
Copy link
Contributor

Cldfire commented Nov 8, 2017

@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 👍

kennytm added a commit to kennytm/rust that referenced this issue Nov 10, 2017
Fix help for duplicated names: `extern crate (...) as (...)`

On the case of duplicated names caused by an `extern crate` statement
with a rename, don't include the inline suggestion, instead using a span
label with only the text to avoid incorrect rust code output.

Fix rust-lang#45829.
@pietroalbini
Copy link
Member

pietroalbini commented Sep 17, 2018

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};
  |              ^^^^^^^^^^^^^^^^

@pietroalbini pietroalbini reopened this Sep 17, 2018
@estebank
Copy link
Contributor

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

@estebank estebank added E-help-wanted Call for participation: Help is requested to fix this issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels Sep 17, 2018
@mockersf
Copy link
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.

@estebank
Copy link
Contributor

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

bors added a commit that referenced this issue Oct 23, 2018
#45829 when a renamed import conflict with a previous import

Fix the suggestion when a renamed import conflict.

It check if the snipped contains `" as "`, and if so uses everything before for the suggestion.
@estebank estebank closed this as completed Apr 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. E-help-wanted Call for participation: Help is requested to fix this issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
Projects
None yet
Development

No branches or pull requests

6 participants