Skip to content

Commit

Permalink
Rollup merge of #75984 - kornelski:typeormodule, r=matthewjasper
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tmandry authored Sep 9, 2020
2 parents 07dbe49 + 7ec1de0 commit 5ea5551
Show file tree
Hide file tree
Showing 52 changed files with 126 additions and 105 deletions.
16 changes: 13 additions & 3 deletions compiler/rustc_error_codes/src/error_codes/E0433.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
An undeclared type or module was used.
An undeclared crate, module, or type was used.

Erroneous code example:

```compile_fail,E0433
let map = HashMap::new();
// error: failed to resolve: use of undeclared type or module `HashMap`
// error: failed to resolve: use of undeclared type `HashMap`
```

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


```
use std::collections::HashMap; // HashMap has been imported.
let map: HashMap<u32, u32> = HashMap::new(); // So it can be used!
```

If you've expected to use a crate name:

```compile_fail
use ferris_wheel::BigO;
// error: failed to resolve: use of undeclared crate or module `ferris_wheel`
```

Make sure the crate has been added as a dependency in `Cargo.toml`.

To use a module from your current crate, add the `crate::` prefix to the path.
9 changes: 8 additions & 1 deletion compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2413,7 +2413,14 @@ impl<'a> Resolver<'a> {
(format!("maybe a missing crate `{}`?", ident), None)
}
} else if i == 0 {
(format!("use of undeclared type or module `{}`", ident), None)
if ident
.name
.with(|n| n.chars().next().map_or(false, |c| c.is_ascii_uppercase()))
{
(format!("use of undeclared type `{}`", ident), None)
} else {
(format!("use of undeclared crate or module `{}`", ident), None)
}
} else {
let mut msg =
format!("could not find `{}` in `{}`", ident, path[i - 1].ident);
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/attributes/register-attr-tool-prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#[no_implicit_prelude]
mod m {
#[attr] //~ ERROR cannot find attribute `attr` in this scope
#[tool::attr] //~ ERROR failed to resolve: use of undeclared type or module `tool`
#[tool::attr] //~ ERROR failed to resolve: use of undeclared crate or module `tool`
fn check() {}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/attributes/register-attr-tool-prelude.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0433]: failed to resolve: use of undeclared type or module `tool`
error[E0433]: failed to resolve: use of undeclared crate or module `tool`
--> $DIR/register-attr-tool-prelude.rs:10:7
|
LL | #[tool::attr]
| ^^^^ use of undeclared type or module `tool`
| ^^^^ use of undeclared crate or module `tool`

error: cannot find attribute `attr` in this scope
--> $DIR/register-attr-tool-prelude.rs:9:7
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/bad/bad-module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn main() {
let foo = thing::len(Vec::new());
//~^ ERROR failed to resolve: use of undeclared type or module `thing`
//~^ ERROR failed to resolve: use of undeclared crate or module `thing`

let foo = foo::bar::baz();
//~^ ERROR failed to resolve: use of undeclared type or module `foo`
//~^ ERROR failed to resolve: use of undeclared crate or module `foo`
}
8 changes: 4 additions & 4 deletions src/test/ui/bad/bad-module.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error[E0433]: failed to resolve: use of undeclared type or module `thing`
error[E0433]: failed to resolve: use of undeclared crate or module `thing`
--> $DIR/bad-module.rs:2:15
|
LL | let foo = thing::len(Vec::new());
| ^^^^^ use of undeclared type or module `thing`
| ^^^^^ use of undeclared crate or module `thing`

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

error: aborting due to 2 previous errors

Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/coherence/conflicting-impl-with-err.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error[E0433]: failed to resolve: use of undeclared type or module `nope`
error[E0433]: failed to resolve: use of undeclared crate or module `nope`
--> $DIR/conflicting-impl-with-err.rs:4:11
|
LL | impl From<nope::Thing> for Error {
| ^^^^ use of undeclared type or module `nope`
| ^^^^ use of undeclared crate or module `nope`

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

error: aborting due to 2 previous errors

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/derived-errors/issue-31997-1.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0433]: failed to resolve: use of undeclared type or module `HashMap`
error[E0433]: failed to resolve: use of undeclared type `HashMap`
--> $DIR/issue-31997-1.rs:20:19
|
LL | let mut map = HashMap::new();
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/dyn-trait-compatibility.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
type A0 = dyn;
//~^ ERROR cannot find type `dyn` in this scope
type A1 = dyn::dyn;
//~^ ERROR use of undeclared type or module `dyn`
//~^ ERROR use of undeclared crate or module `dyn`
type A2 = dyn<dyn, dyn>;
//~^ ERROR cannot find type `dyn` in this scope
//~| ERROR cannot find type `dyn` in this scope
//~| ERROR cannot find type `dyn` in this scope
type A3 = dyn<<dyn as dyn>::dyn>;
//~^ ERROR cannot find type `dyn` in this scope
//~| ERROR cannot find type `dyn` in this scope
//~| ERROR use of undeclared type or module `dyn`
//~| ERROR use of undeclared crate or module `dyn`

fn main() {}
8 changes: 4 additions & 4 deletions src/test/ui/dyn-trait-compatibility.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error[E0433]: failed to resolve: use of undeclared type or module `dyn`
error[E0433]: failed to resolve: use of undeclared crate or module `dyn`
--> $DIR/dyn-trait-compatibility.rs:3:11
|
LL | type A1 = dyn::dyn;
| ^^^ use of undeclared type or module `dyn`
| ^^^ use of undeclared crate or module `dyn`

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

error[E0412]: cannot find type `dyn` in this scope
--> $DIR/dyn-trait-compatibility.rs:1:11
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/error-codes/E0433.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0433]: failed to resolve: use of undeclared type or module `NonExistingMap`
error[E0433]: failed to resolve: use of undeclared type `NonExistingMap`
--> $DIR/E0433.rs:2:15
|
LL | let map = NonExistingMap::new();
| ^^^^^^^^^^^^^^ use of undeclared type or module `NonExistingMap`
| ^^^^^^^^^^^^^^ use of undeclared type `NonExistingMap`

error: aborting due to previous error

Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/export-fully-qualified.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// ignore-tidy-linelength

// In this test baz isn't resolved when called as foo.baz even though
// it's called from inside foo. This is somewhat surprising and may
// want to change eventually.

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

fn baz() { }
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/export-fully-qualified.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0433]: failed to resolve: use of undeclared type or module `foo`
--> $DIR/export-fully-qualified.rs:6:20
error[E0433]: failed to resolve: use of undeclared crate or module `foo`
--> $DIR/export-fully-qualified.rs:8:20
|
LL | pub fn bar() { foo::baz(); }
| ^^^ use of undeclared type or module `foo`
| ^^^ use of undeclared crate or module `foo`

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/export2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod foo {
pub fn x() { bar::x(); } //~ ERROR failed to resolve: use of undeclared type or module `bar`
pub fn x() { bar::x(); } //~ ERROR failed to resolve: use of undeclared crate or module `bar`
}

mod bar {
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/export2.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0433]: failed to resolve: use of undeclared type or module `bar`
error[E0433]: failed to resolve: use of undeclared crate or module `bar`
--> $DIR/export2.rs:2:18
|
LL | pub fn x() { bar::x(); }
| ^^^ use of undeclared type or module `bar`
| ^^^ use of undeclared crate or module `bar`

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/extern-flag/multiple-opts.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0433]: failed to resolve: use of undeclared type or module `somedep`
error[E0433]: failed to resolve: use of undeclared crate or module `somedep`
--> $DIR/multiple-opts.rs:19:5
|
LL | somedep::somefun();
| ^^^^^^^ use of undeclared type or module `somedep`
| ^^^^^^^ use of undeclared crate or module `somedep`

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/extern-flag/noprelude.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0433]: failed to resolve: use of undeclared type or module `somedep`
error[E0433]: failed to resolve: use of undeclared crate or module `somedep`
--> $DIR/noprelude.rs:6:5
|
LL | somedep::somefun();
| ^^^^^^^ use of undeclared type or module `somedep`
| ^^^^^^^ use of undeclared crate or module `somedep`

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/hygiene/extern-prelude-from-opaque-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ macro a() {
mod u {
// Late resolution.
fn f() { my_core::mem::drop(0); }
//~^ ERROR failed to resolve: use of undeclared type or module `my_core`
//~^ ERROR failed to resolve: use of undeclared crate or module `my_core`
}
}

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

fn main() {}
8 changes: 4 additions & 4 deletions src/test/ui/hygiene/extern-prelude-from-opaque-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ LL | a!();
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

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

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

error: aborting due to 4 previous errors

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/hygiene/no_implicit_prelude.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | assert_eq!(0, 0);
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0433]: failed to resolve: use of undeclared type or module `Vec`
error[E0433]: failed to resolve: use of undeclared type `Vec`
--> $DIR/no_implicit_prelude.rs:11:9
|
LL | fn f() { ::bar::m!(); }
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/impl-trait/issue-72911.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error[E0433]: failed to resolve: use of undeclared type or module `foo`
error[E0433]: failed to resolve: use of undeclared crate or module `foo`
--> $DIR/issue-72911.rs:12:33
|
LL | fn gather_from_file(dir_entry: &foo::MissingItem) -> impl Iterator<Item = Lint> {
| ^^^ use of undeclared type or module `foo`
| ^^^ use of undeclared crate or module `foo`

error[E0433]: failed to resolve: use of undeclared type or module `foo`
error[E0433]: failed to resolve: use of undeclared crate or module `foo`
--> $DIR/issue-72911.rs:17:41
|
LL | fn lint_files() -> impl Iterator<Item = foo::MissingItem> {
| ^^^ use of undeclared type or module `foo`
| ^^^ use of undeclared crate or module `foo`

error[E0720]: cannot resolve opaque type
--> $DIR/issue-72911.rs:7:24
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/imports/extern-prelude-extern-crate-fail.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore-tidy-linelength

// aux-build:two_macros.rs
// compile-flags:--extern non_existent

Expand All @@ -7,7 +9,7 @@ mod n {

mod m {
fn check() {
two_macros::m!(); //~ ERROR failed to resolve: use of undeclared type or module `two_macros`
two_macros::m!(); //~ ERROR failed to resolve: use of undeclared crate or module `two_macros`
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/imports/extern-prelude-extern-crate-fail.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: macro-expanded `extern crate` items cannot shadow names passed with `--extern`
--> $DIR/extern-prelude-extern-crate-fail.rs:16:9
--> $DIR/extern-prelude-extern-crate-fail.rs:18:9
|
LL | extern crate std as non_existent;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -9,11 +9,11 @@ LL | define_std_as_non_existent!();
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

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

error: aborting due to 2 previous errors

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-33293.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
match 0 {
aaa::bbb(_) => ()
//~^ ERROR failed to resolve: use of undeclared type or module `aaa`
//~^ ERROR failed to resolve: use of undeclared crate or module `aaa`
};
}
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-33293.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0433]: failed to resolve: use of undeclared type or module `aaa`
error[E0433]: failed to resolve: use of undeclared crate or module `aaa`
--> $DIR/issue-33293.rs:3:9
|
LL | aaa::bbb(_) => ()
| ^^^ use of undeclared type or module `aaa`
| ^^^ use of undeclared crate or module `aaa`

error: aborting due to previous error

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/macros/builtin-prelude-no-accidents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// because macros with the same names are in prelude.

fn main() {
env::current_dir; //~ ERROR use of undeclared type or module `env`
type A = panic::PanicInfo; //~ ERROR use of undeclared type or module `panic`
type B = vec::Vec<u8>; //~ ERROR use of undeclared type or module `vec`
env::current_dir; //~ ERROR use of undeclared crate or module `env`
type A = panic::PanicInfo; //~ ERROR use of undeclared crate or module `panic`
type B = vec::Vec<u8>; //~ ERROR use of undeclared crate or module `vec`
}
12 changes: 6 additions & 6 deletions src/test/ui/macros/builtin-prelude-no-accidents.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
error[E0433]: failed to resolve: use of undeclared type or module `env`
error[E0433]: failed to resolve: use of undeclared crate or module `env`
--> $DIR/builtin-prelude-no-accidents.rs:5:5
|
LL | env::current_dir;
| ^^^ use of undeclared type or module `env`
| ^^^ use of undeclared crate or module `env`

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

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

error: aborting due to 3 previous errors

Expand Down
Loading

0 comments on commit 5ea5551

Please sign in to comment.