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

Suggestion for a typoed crate or module results in compile error #89592

Open
TaKO8Ki opened this issue Oct 6, 2021 · 2 comments
Open

Suggestion for a typoed crate or module results in compile error #89592

TaKO8Ki opened this issue Oct 6, 2021 · 2 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@TaKO8Ki
Copy link
Member

TaKO8Ki commented Oct 6, 2021

This issue is

I implemented a suggestion for typoed crate or module in #89347. However there are still some edge cases.

Given the following code:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=36133bfa20fc000bce25b7b1ea075df9

The current output is:

error[E0433]: failed to resolve: use of undeclared crate or module `my_cor`
  --> $DIR/extern-prelude-from-opaque-fail.rs:10:5
   |
LL |     my_cor::mem::drop(0);
   |     ^^^^^^ use of undeclared crate or module `my_cor`
   |
help: there is a crate or module with a similar name
   |
LL |     my_core::mem::drop(0);
   |     ~~~~~~~

error: aborting due to previous error

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

However if you apply this suggestion, you get a compiler error like the following.

error[E0433]: failed to resolve: use of undeclared crate or module `my_core`
  --> $DIR/extern-prelude-from-opaque-fail.rs:10:5
   |
LL |     my_core::mem::drop(0);
   |     ^^^^^^^ use of undeclared crate or module `my_core`

error: aborting due to previous error

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

Ideally the output should look like:

The compiler shouldn't suggest a crate or module name which you can't use.

error[E0433]: failed to resolve: use of undeclared crate or module `my_cor`
  --> $DIR/extern-prelude-from-opaque-fail.rs:10:5
   |
LL |     my_cor::mem::drop(0);
   |     ^^^^^^ use of undeclared crate or module `my_cor`
   |

error: aborting due to previous error

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

#89347 (comment) might help you.

Other things to do: #89347 (comment)

@TaKO8Ki TaKO8Ki 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 6, 2021
@PatchMixolydic
Copy link
Contributor

If you can get the span of the module, you might be able to filter candidates by Span::from_expansion. ModuleData::span seems like it could work, but I'm not sure if it'd point to the macro expansion or to the core crate in this case. If ModuleData::span points into the macro expansion, the fix might be as simple as making the following change in compiler/rustc_resolve/src/diagnostic.rs:

self.module_map
    .iter()
    .filter(|(_, module)| {
        current_module.is_ancestor_of(module) && !ptr::eq(current_module, *module)
    })
+   .filter(|(_, module)| {
+       module
+           .span
+           .map(|span| span.from_expansion())
+           .unwrap_or(false) // alternatively `.unwrap_or_default()`
+   })
    .map(|(_, module)| module.kind.name())
    .flatten(),

@TaKO8Ki TaKO8Ki self-assigned this Oct 13, 2021
@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Mar 15, 2023
@estebank
Copy link
Contributor

estebank commented Aug 3, 2023

We should still not show the typo suggestion given it's not accessible, but the suggestions have gotten better.

error[[E0433]](https://doc.rust-lang.org/nightly/error_codes/E0433.html): failed to resolve: use of undeclared crate or module `my_cor`
  --> src/main.rs:10:5
   |
10 |     my_cor::mem::drop(0);
   |     ^^^^^^ use of undeclared crate or module `my_cor`
   |
help: there is a crate or module with a similar name
   |
10 |     my_core::mem::drop(0);
   |     ~~~~~~~
help: consider importing one of these items
   |
3  + [use core::mem;](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=36133bfa20fc000bce25b7b1ea075df9#)
   |
3  + [use memoffset::__priv::mem;](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=36133bfa20fc000bce25b7b1ea075df9#)
   |
3  + [use nom::lib::std::mem;](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=36133bfa20fc000bce25b7b1ea075df9#)
   |
3  + [use pin_utils::core_reexport::mem;](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=36133bfa20fc000bce25b7b1ea075df9#)
   |
     and 1 other candidate
help: if you import `mem`, refer to it directly
   |
10 -     my_cor::mem::drop(0);
10 +     mem::drop(0);
   |

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-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants