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

Rustc suggests the same use statement that caused the error when two modules share their name #116970

Closed
CastilloDel opened this issue Oct 20, 2023 · 1 comment · Fixed by #117009
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name resolution A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@CastilloDel
Copy link
Contributor

CastilloDel commented Oct 20, 2023

Code

I tried to reproduce the problem in the playground

mod a {
    pub mod log {
        pub struct S {}
    }
}

use a::log::{self};

use log::SetLoggerError;

fn main() {}

Current output

The output for the playground code is the following:

error[E0432]: unresolved import `log::SetLoggerError`
  --> src/main.rs:12:5
   |
12 | use log::SetLoggerError;
   |     ^^^^^^^^^^^^^^^^^^^ no `SetLoggerError` in `a::log`
   |
help: consider importing this struct instead
   |
12 | use log::SetLoggerError;
   |     ~~~~~~~~~~~~~~~~~~~

The one I actually saw:

 unresolved import `proto::LogLevelValue`
 --> tests/tests/server_api.rs:3:5
  |
3 | use proto::LogLevelValue;
  |     ^^^^^^^^^^^^^^^^^^^^ no `LogLevelValue` in `proto`
  |
help: consider importing this enum instead
  |
3 | use proto::LogLevelValue;
  |     ~~~~~~~~~~~~~~~~~~~~

The second one is worse, because "no LogLevelValue in proto" doesn't indicate that this proto comes is a different proto than the one in the suggestion. I think this happens because the modules aren't in the same file or crate, so I couldn't reproduce it in the playground.

Desired output

Ideally, rustc should be able to notify the user that the error comes from the fact that there are two modules with the same name, instead of suggesting the same code as a fix.

Rationale and extra context

rustc 1.73.0 (cc66ad468 2023-10-03)

Other cases

No response

Anything else?

Sorry if this is duplicated, but I couldn't find an issue regarding this problem

@CastilloDel CastilloDel added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 20, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 20, 2023
@fmease
Copy link
Member

fmease commented Oct 20, 2023

MCVE:

// === dep.rs ===   rustc dep.rs --crate-type=lib
pub struct SomeUsefulType;

// === usr.rs ===   rustc usr.rs --edition=2021 --extern=dep=libdep.rlib
mod dep {}
use dep::SomeUsefulType;

fn main() {}
error[E0432]: unresolved import `dep::SomeUsefulType`
 --> usr.rs:2:5
  |
2 | use dep::SomeUsefulType;
  |     ^^^^^^^^^^^^^^^^^^^ no `SomeUsefulType` in `dep`
  |
help: consider importing this struct instead
  |
2 | use dep::SomeUsefulType;
  |     ~~~~~~~~~~~~~~~~~~~

Explanation: The suggestion doesn't account for the case where a local module (here dep) has the same name as the crate found in the external prelude that contains the wanted item. If this situation arises, it should instead suggest:

help: consider importing this struct instead
  |
2 | use ::dep::SomeUsefulType;
  |     ~~~~~~~~~~~~~~~~~~~~~

@fmease fmease added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue A-resolve Area: Name resolution and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 20, 2023
@fmease fmease self-assigned this Oct 20, 2023
@bors bors closed this as completed in 2a027fa Oct 26, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 26, 2023
Rollup merge of rust-lang#117009 - fmease:diag-disambig-sugg-crate, r=b-naber

On unresolved imports, suggest a disambiguated path if necessary to avoid collision with local items

Fixes rust-lang#116970.
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 A-resolve Area: Name resolution A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
3 participants