rust-lang / rust-lang/rust

Suggest declaring module when trying to use it

Open
#124,128 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
After cargo new:

$ git diff --cached
diff --git a/src/foo.rs b/src/foo.rs
new file mode 100644
index 0000000..626450e
--- /dev/null
+++ b/src/foo.rs
@@ -0,0 +1,3 @@
+pub fn hello_world() {
+    println!("hello world");
+}
diff --git a/src/main.rs b/src/main.rs
index e7a11a9..1f8c3d2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,5 @@
+use crate::foo::hello_world;
+
 fn main() {
-    println!("Hello, world!");
+    hello_world()
 }


### Current output

```Shell
$ cargo run
   Compiling import v0.1.0 (/Users/iguridi/repro/import)
error[E0432]: unresolved import `crate::foo`
 --> src/main.rs:1:12
  |
1 | use crate::foo::hello_world;
  |            ^^^ could not find `foo` in the crate root

For more information about this error, try `rustc --explain E0432`.
error: could not compile `import` (bin "import") due to 1 previous error
Desired output
$ cargo run
   Compiling import v0.1.0 (/Users/iguridi/repro/import)
error[E0432]: unresolved import `crate::foo`
 --> src/main.rs:1:12
  |
1 | use crate::foo::hello_world;
  |            ^^^ could not find `foo` in the crate root. Have you declared this module e.g. `mod foo`?

For more information about this error, try `rustc --explain E0432`.
error: could not compile `import` (bin "import") due to 1 previous error
Rationale and extra context

The fix is very easy, but a beginner may miss it

Other cases

No response

Rust Version
$ rustc --version --verbose
rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: aarch64-apple-darwin
release: 1.76.0
LLVM version: 17.0.6
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue from the src/main.rs and src/foo.rs example by running cargo run, then trace the compiler diagnostic for unresolved import E0432. Done means the diagnostic suggests declaring the missing module, matching the desired output while preserving the existing error information.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.