rust icon indicating copy to clipboard operation
rust copied to clipboard

Suggest `use` if `import`, `using`, or `require` is used instead

Open bindsdev opened this issue 3 years ago • 9 comments

I was looking at namely issue #99751 when I thought about this. If work is being put into improving error messages when somebody uses a keyword that defines a function in another language, there should also be improved error messages if someone uses the incorrect directive to bring something into the scope of their module or namespace/

Given the following code: Playground Link

import std::mem;
using std::mem;
require std::mem;

The current output is:

error: expected one of `!` or `::`, found `std`
 --> src/main.rs:1:8
  |
1 | import std::mem;
  |        ^^^ expected one of `!` or `::`

error: could not compile `playground` due to previous error

The error message should preferably suggest the user to use the use directive to bring something into scope of their module.

bindsdev avatar Aug 04 '22 13:08 bindsdev

@rustbot claim

gimbling-away avatar Aug 04 '22 13:08 gimbling-away

For the record, on nightly, both import and using suggest using use. I'm sure it's not that difficult to add require.

compiler-errors avatar Aug 04 '22 13:08 compiler-errors

For the record, on nightly, both import and using suggest using use. I'm sure it's not that difficult to add require.

ah. I was not aware of this. Less work to do then!

bindsdev avatar Aug 04 '22 13:08 bindsdev

[Ignore this, just notes]

https://github.com/rust-lang/rust/blob/caee496150a551fe1b9f77b3a58f7e66d54bc824/compiler/rustc_parse/src/parser/item.rs#L274

https://github.com/rust-lang/rust/blob/6f18f0a9d4548bc87afff1e4c0fe9081c35002c2/compiler/rustc_span/src/symbol.rs#L1172

gimbling-away avatar Aug 04 '22 15:08 gimbling-away

@gimbles And also add include?
Rust may go tooooo friendly in diagnostic message 😊. Do you mind that I make a PR for it? I'm a newbie in hacking Rust compiler.

chenyukang avatar Aug 05 '22 09:08 chenyukang

Sure! The earlier message should clarify where the edits are needed to be made. Ain't nothing as a diagnostic too friendly. =)

gimbling-away avatar Aug 05 '22 09:08 gimbling-away

@chenyukang Remember to assign yourself using rustbot. 😄

Just a note for the future, usually if someone has already claimed an issue, it's not exactly common for someone else to fix it [aside from if the issue is abandoned by the claimee].

Since you're a newcomer here, I removed my assignment (and because I was working on another). However, you can find loads of easy issues with the E-Easy and/or E-Mentor tag for gaining experience.

A few resources that might be helpful -> https://github.com/rust-lang/triagebot/wiki https://github.com/rust-lang/rustc-dev-guide

Have fun hacking on Rust! 🦀 🤎

gimbling-away avatar Aug 05 '22 09:08 gimbling-away

@rustbot claim

chenyukang avatar Aug 05 '22 10:08 chenyukang

@gimbles And also add include? Rust may go tooooo friendly in diagnostic message 😊. Do you mind that I make a PR for it? I'm a newbie in hacking Rust compiler.

Totally forgot about include! Good catch!

bindsdev avatar Aug 05 '22 13:08 bindsdev