Skip to content

Commit 5ea5551

Browse files
authored
Rollup merge of #75984 - kornelski:typeormodule, r=matthewjasper
Improve unresolved use error message "use of undeclared type or module `foo`" doesn't mention that it could be a crate. This error can happen when users forget to add a dependency to `Cargo.toml`, so I think it's important to mention that it could be a missing crate. I've used a heuristic based on Rust's naming conventions. It complains about an unknown type if the ident starts with an upper-case letter, and crate or module otherwise. It seems to work very well. The expanded error help covers both an unknown type and a missing crate case.
2 parents 07dbe49 + 7ec1de0 commit 5ea5551

File tree

52 files changed

+126
-105
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+126
-105
lines changed
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
An undeclared type or module was used.
1+
An undeclared crate, module, or type was used.
22

33
Erroneous code example:
44

55
```compile_fail,E0433
66
let map = HashMap::new();
7-
// error: failed to resolve: use of undeclared type or module `HashMap`
7+
// error: failed to resolve: use of undeclared type `HashMap`
88
```
99

1010
Please verify you didn't misspell the type/module's name or that you didn't
1111
forget to import it:
1212

13-
1413
```
1514
use std::collections::HashMap; // HashMap has been imported.
1615
let map: HashMap<u32, u32> = HashMap::new(); // So it can be used!
1716
```
17+
18+
If you've expected to use a crate name:
19+
20+
```compile_fail
21+
use ferris_wheel::BigO;
22+
// error: failed to resolve: use of undeclared crate or module `ferris_wheel`
23+
```
24+
25+
Make sure the crate has been added as a dependency in `Cargo.toml`.
26+
27+
To use a module from your current crate, add the `crate::` prefix to the path.

compiler/rustc_resolve/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2413,7 +2413,14 @@ impl<'a> Resolver<'a> {
24132413
(format!("maybe a missing crate `{}`?", ident), None)
24142414
}
24152415
} else if i == 0 {
2416-
(format!("use of undeclared type or module `{}`", ident), None)
2416+
if ident
2417+
.name
2418+
.with(|n| n.chars().next().map_or(false, |c| c.is_ascii_uppercase()))
2419+
{
2420+
(format!("use of undeclared type `{}`", ident), None)
2421+
} else {
2422+
(format!("use of undeclared crate or module `{}`", ident), None)
2423+
}
24172424
} else {
24182425
let mut msg =
24192426
format!("could not find `{}` in `{}`", ident, path[i - 1].ident);

src/test/ui/attributes/register-attr-tool-prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#[no_implicit_prelude]
88
mod m {
99
#[attr] //~ ERROR cannot find attribute `attr` in this scope
10-
#[tool::attr] //~ ERROR failed to resolve: use of undeclared type or module `tool`
10+
#[tool::attr] //~ ERROR failed to resolve: use of undeclared crate or module `tool`
1111
fn check() {}
1212
}
1313

src/test/ui/attributes/register-attr-tool-prelude.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0433]: failed to resolve: use of undeclared type or module `tool`
1+
error[E0433]: failed to resolve: use of undeclared crate or module `tool`
22
--> $DIR/register-attr-tool-prelude.rs:10:7
33
|
44
LL | #[tool::attr]
5-
| ^^^^ use of undeclared type or module `tool`
5+
| ^^^^ use of undeclared crate or module `tool`
66

77
error: cannot find attribute `attr` in this scope
88
--> $DIR/register-attr-tool-prelude.rs:9:7

src/test/ui/bad/bad-module.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
fn main() {
22
let foo = thing::len(Vec::new());
3-
//~^ ERROR failed to resolve: use of undeclared type or module `thing`
3+
//~^ ERROR failed to resolve: use of undeclared crate or module `thing`
44

55
let foo = foo::bar::baz();
6-
//~^ ERROR failed to resolve: use of undeclared type or module `foo`
6+
//~^ ERROR failed to resolve: use of undeclared crate or module `foo`
77
}

src/test/ui/bad/bad-module.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
error[E0433]: failed to resolve: use of undeclared type or module `thing`
1+
error[E0433]: failed to resolve: use of undeclared crate or module `thing`
22
--> $DIR/bad-module.rs:2:15
33
|
44
LL | let foo = thing::len(Vec::new());
5-
| ^^^^^ use of undeclared type or module `thing`
5+
| ^^^^^ use of undeclared crate or module `thing`
66

7-
error[E0433]: failed to resolve: use of undeclared type or module `foo`
7+
error[E0433]: failed to resolve: use of undeclared crate or module `foo`
88
--> $DIR/bad-module.rs:5:15
99
|
1010
LL | let foo = foo::bar::baz();
11-
| ^^^ use of undeclared type or module `foo`
11+
| ^^^ use of undeclared crate or module `foo`
1212

1313
error: aborting due to 2 previous errors
1414

src/test/ui/coherence/conflicting-impl-with-err.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
error[E0433]: failed to resolve: use of undeclared type or module `nope`
1+
error[E0433]: failed to resolve: use of undeclared crate or module `nope`
22
--> $DIR/conflicting-impl-with-err.rs:4:11
33
|
44
LL | impl From<nope::Thing> for Error {
5-
| ^^^^ use of undeclared type or module `nope`
5+
| ^^^^ use of undeclared crate or module `nope`
66

7-
error[E0433]: failed to resolve: use of undeclared type or module `nope`
7+
error[E0433]: failed to resolve: use of undeclared crate or module `nope`
88
--> $DIR/conflicting-impl-with-err.rs:5:16
99
|
1010
LL | fn from(_: nope::Thing) -> Self {
11-
| ^^^^ use of undeclared type or module `nope`
11+
| ^^^^ use of undeclared crate or module `nope`
1212

1313
error: aborting due to 2 previous errors
1414

src/test/ui/derived-errors/issue-31997-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0433]: failed to resolve: use of undeclared type or module `HashMap`
1+
error[E0433]: failed to resolve: use of undeclared type `HashMap`
22
--> $DIR/issue-31997-1.rs:20:19
33
|
44
LL | let mut map = HashMap::new();
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
type A0 = dyn;
22
//~^ ERROR cannot find type `dyn` in this scope
33
type A1 = dyn::dyn;
4-
//~^ ERROR use of undeclared type or module `dyn`
4+
//~^ ERROR use of undeclared crate or module `dyn`
55
type A2 = dyn<dyn, dyn>;
66
//~^ ERROR cannot find type `dyn` in this scope
77
//~| ERROR cannot find type `dyn` in this scope
88
//~| ERROR cannot find type `dyn` in this scope
99
type A3 = dyn<<dyn as dyn>::dyn>;
1010
//~^ ERROR cannot find type `dyn` in this scope
1111
//~| ERROR cannot find type `dyn` in this scope
12-
//~| ERROR use of undeclared type or module `dyn`
12+
//~| ERROR use of undeclared crate or module `dyn`
1313

1414
fn main() {}

src/test/ui/dyn-trait-compatibility.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
error[E0433]: failed to resolve: use of undeclared type or module `dyn`
1+
error[E0433]: failed to resolve: use of undeclared crate or module `dyn`
22
--> $DIR/dyn-trait-compatibility.rs:3:11
33
|
44
LL | type A1 = dyn::dyn;
5-
| ^^^ use of undeclared type or module `dyn`
5+
| ^^^ use of undeclared crate or module `dyn`
66

7-
error[E0433]: failed to resolve: use of undeclared type or module `dyn`
7+
error[E0433]: failed to resolve: use of undeclared crate or module `dyn`
88
--> $DIR/dyn-trait-compatibility.rs:9:23
99
|
1010
LL | type A3 = dyn<<dyn as dyn>::dyn>;
11-
| ^^^ use of undeclared type or module `dyn`
11+
| ^^^ use of undeclared crate or module `dyn`
1212

1313
error[E0412]: cannot find type `dyn` in this scope
1414
--> $DIR/dyn-trait-compatibility.rs:1:11

0 commit comments

Comments
 (0)