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
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

+8-1
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

+1-1
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

+2-2
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

+2-2
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

+4-4
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

+4-4
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

+1-1
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();
+2-2
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

+4-4
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

src/test/ui/error-codes/E0433.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0433]: failed to resolve: use of undeclared type or module `NonExistingMap`
1+
error[E0433]: failed to resolve: use of undeclared type `NonExistingMap`
22
--> $DIR/E0433.rs:2:15
33
|
44
LL | let map = NonExistingMap::new();
5-
| ^^^^^^^^^^^^^^ use of undeclared type or module `NonExistingMap`
5+
| ^^^^^^^^^^^^^^ use of undeclared type `NonExistingMap`
66

77
error: aborting due to previous error
88

src/test/ui/export-fully-qualified.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// ignore-tidy-linelength
2+
13
// In this test baz isn't resolved when called as foo.baz even though
24
// it's called from inside foo. This is somewhat surprising and may
35
// want to change eventually.
46

57
mod foo {
6-
pub fn bar() { foo::baz(); } //~ ERROR failed to resolve: use of undeclared type or module `foo`
8+
pub fn bar() { foo::baz(); } //~ ERROR failed to resolve: use of undeclared crate or module `foo`
79

810
fn baz() { }
911
}

src/test/ui/export-fully-qualified.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0433]: failed to resolve: use of undeclared type or module `foo`
2-
--> $DIR/export-fully-qualified.rs:6:20
1+
error[E0433]: failed to resolve: use of undeclared crate or module `foo`
2+
--> $DIR/export-fully-qualified.rs:8:20
33
|
44
LL | pub fn bar() { foo::baz(); }
5-
| ^^^ use of undeclared type or module `foo`
5+
| ^^^ use of undeclared crate or module `foo`
66

77
error: aborting due to previous error
88

src/test/ui/export2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod foo {
2-
pub fn x() { bar::x(); } //~ ERROR failed to resolve: use of undeclared type or module `bar`
2+
pub fn x() { bar::x(); } //~ ERROR failed to resolve: use of undeclared crate or module `bar`
33
}
44

55
mod bar {

src/test/ui/export2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0433]: failed to resolve: use of undeclared type or module `bar`
1+
error[E0433]: failed to resolve: use of undeclared crate or module `bar`
22
--> $DIR/export2.rs:2:18
33
|
44
LL | pub fn x() { bar::x(); }
5-
| ^^^ use of undeclared type or module `bar`
5+
| ^^^ use of undeclared crate or module `bar`
66

77
error: aborting due to previous error
88

src/test/ui/extern-flag/multiple-opts.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0433]: failed to resolve: use of undeclared type or module `somedep`
1+
error[E0433]: failed to resolve: use of undeclared crate or module `somedep`
22
--> $DIR/multiple-opts.rs:19:5
33
|
44
LL | somedep::somefun();
5-
| ^^^^^^^ use of undeclared type or module `somedep`
5+
| ^^^^^^^ use of undeclared crate or module `somedep`
66

77
error: aborting due to previous error
88

src/test/ui/extern-flag/noprelude.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0433]: failed to resolve: use of undeclared type or module `somedep`
1+
error[E0433]: failed to resolve: use of undeclared crate or module `somedep`
22
--> $DIR/noprelude.rs:6:5
33
|
44
LL | somedep::somefun();
5-
| ^^^^^^^ use of undeclared type or module `somedep`
5+
| ^^^^^^^ use of undeclared crate or module `somedep`
66

77
error: aborting due to previous error
88

src/test/ui/hygiene/extern-prelude-from-opaque-fail.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ macro a() {
99
mod u {
1010
// Late resolution.
1111
fn f() { my_core::mem::drop(0); }
12-
//~^ ERROR failed to resolve: use of undeclared type or module `my_core`
12+
//~^ ERROR failed to resolve: use of undeclared crate or module `my_core`
1313
}
1414
}
1515

@@ -22,7 +22,7 @@ mod v {
2222
mod u {
2323
// Late resolution.
2424
fn f() { my_core::mem::drop(0); }
25-
//~^ ERROR failed to resolve: use of undeclared type or module `my_core`
25+
//~^ ERROR failed to resolve: use of undeclared crate or module `my_core`
2626
}
2727

2828
fn main() {}

src/test/ui/hygiene/extern-prelude-from-opaque-fail.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ LL | a!();
1818
|
1919
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
2020

21-
error[E0433]: failed to resolve: use of undeclared type or module `my_core`
21+
error[E0433]: failed to resolve: use of undeclared crate or module `my_core`
2222
--> $DIR/extern-prelude-from-opaque-fail.rs:11:18
2323
|
2424
LL | fn f() { my_core::mem::drop(0); }
25-
| ^^^^^^^ use of undeclared type or module `my_core`
25+
| ^^^^^^^ use of undeclared crate or module `my_core`
2626
...
2727
LL | a!();
2828
| ----- in this macro invocation
2929
|
3030
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
3131

32-
error[E0433]: failed to resolve: use of undeclared type or module `my_core`
32+
error[E0433]: failed to resolve: use of undeclared crate or module `my_core`
3333
--> $DIR/extern-prelude-from-opaque-fail.rs:24:14
3434
|
3535
LL | fn f() { my_core::mem::drop(0); }
36-
| ^^^^^^^ use of undeclared type or module `my_core`
36+
| ^^^^^^^ use of undeclared crate or module `my_core`
3737

3838
error: aborting due to 4 previous errors
3939

src/test/ui/hygiene/no_implicit_prelude.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | assert_eq!(0, 0);
66
|
77
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
88

9-
error[E0433]: failed to resolve: use of undeclared type or module `Vec`
9+
error[E0433]: failed to resolve: use of undeclared type `Vec`
1010
--> $DIR/no_implicit_prelude.rs:11:9
1111
|
1212
LL | fn f() { ::bar::m!(); }

src/test/ui/impl-trait/issue-72911.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
error[E0433]: failed to resolve: use of undeclared type or module `foo`
1+
error[E0433]: failed to resolve: use of undeclared crate or module `foo`
22
--> $DIR/issue-72911.rs:12:33
33
|
44
LL | fn gather_from_file(dir_entry: &foo::MissingItem) -> impl Iterator<Item = Lint> {
5-
| ^^^ use of undeclared type or module `foo`
5+
| ^^^ use of undeclared crate or module `foo`
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/issue-72911.rs:17:41
99
|
1010
LL | fn lint_files() -> impl Iterator<Item = foo::MissingItem> {
11-
| ^^^ use of undeclared type or module `foo`
11+
| ^^^ use of undeclared crate or module `foo`
1212

1313
error[E0720]: cannot resolve opaque type
1414
--> $DIR/issue-72911.rs:7:24

src/test/ui/imports/extern-prelude-extern-crate-fail.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore-tidy-linelength
2+
13
// aux-build:two_macros.rs
24
// compile-flags:--extern non_existent
35

@@ -7,7 +9,7 @@ mod n {
79

810
mod m {
911
fn check() {
10-
two_macros::m!(); //~ ERROR failed to resolve: use of undeclared type or module `two_macros`
12+
two_macros::m!(); //~ ERROR failed to resolve: use of undeclared crate or module `two_macros`
1113
}
1214
}
1315

src/test/ui/imports/extern-prelude-extern-crate-fail.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: macro-expanded `extern crate` items cannot shadow names passed with `--extern`
2-
--> $DIR/extern-prelude-extern-crate-fail.rs:16:9
2+
--> $DIR/extern-prelude-extern-crate-fail.rs:18:9
33
|
44
LL | extern crate std as non_existent;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -9,11 +9,11 @@ LL | define_std_as_non_existent!();
99
|
1010
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
1111

12-
error[E0433]: failed to resolve: use of undeclared type or module `two_macros`
13-
--> $DIR/extern-prelude-extern-crate-fail.rs:10:9
12+
error[E0433]: failed to resolve: use of undeclared crate or module `two_macros`
13+
--> $DIR/extern-prelude-extern-crate-fail.rs:12:9
1414
|
1515
LL | two_macros::m!();
16-
| ^^^^^^^^^^ use of undeclared type or module `two_macros`
16+
| ^^^^^^^^^^ use of undeclared crate or module `two_macros`
1717

1818
error: aborting due to 2 previous errors
1919

src/test/ui/issues/issue-33293.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn main() {
22
match 0 {
33
aaa::bbb(_) => ()
4-
//~^ ERROR failed to resolve: use of undeclared type or module `aaa`
4+
//~^ ERROR failed to resolve: use of undeclared crate or module `aaa`
55
};
66
}

src/test/ui/issues/issue-33293.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0433]: failed to resolve: use of undeclared type or module `aaa`
1+
error[E0433]: failed to resolve: use of undeclared crate or module `aaa`
22
--> $DIR/issue-33293.rs:3:9
33
|
44
LL | aaa::bbb(_) => ()
5-
| ^^^ use of undeclared type or module `aaa`
5+
| ^^^ use of undeclared crate or module `aaa`
66

77
error: aborting due to previous error
88

src/test/ui/macros/builtin-prelude-no-accidents.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// because macros with the same names are in prelude.
33

44
fn main() {
5-
env::current_dir; //~ ERROR use of undeclared type or module `env`
6-
type A = panic::PanicInfo; //~ ERROR use of undeclared type or module `panic`
7-
type B = vec::Vec<u8>; //~ ERROR use of undeclared type or module `vec`
5+
env::current_dir; //~ ERROR use of undeclared crate or module `env`
6+
type A = panic::PanicInfo; //~ ERROR use of undeclared crate or module `panic`
7+
type B = vec::Vec<u8>; //~ ERROR use of undeclared crate or module `vec`
88
}

src/test/ui/macros/builtin-prelude-no-accidents.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
error[E0433]: failed to resolve: use of undeclared type or module `env`
1+
error[E0433]: failed to resolve: use of undeclared crate or module `env`
22
--> $DIR/builtin-prelude-no-accidents.rs:5:5
33
|
44
LL | env::current_dir;
5-
| ^^^ use of undeclared type or module `env`
5+
| ^^^ use of undeclared crate or module `env`
66

7-
error[E0433]: failed to resolve: use of undeclared type or module `panic`
7+
error[E0433]: failed to resolve: use of undeclared crate or module `panic`
88
--> $DIR/builtin-prelude-no-accidents.rs:6:14
99
|
1010
LL | type A = panic::PanicInfo;
11-
| ^^^^^ use of undeclared type or module `panic`
11+
| ^^^^^ use of undeclared crate or module `panic`
1212

13-
error[E0433]: failed to resolve: use of undeclared type or module `vec`
13+
error[E0433]: failed to resolve: use of undeclared crate or module `vec`
1414
--> $DIR/builtin-prelude-no-accidents.rs:7:14
1515
|
1616
LL | type B = vec::Vec<u8>;
17-
| ^^^ use of undeclared type or module `vec`
17+
| ^^^ use of undeclared crate or module `vec`
1818

1919
error: aborting due to 3 previous errors
2020

0 commit comments

Comments
 (0)