rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.07k stars 12.41k forks source link

Suggestion inconsistency when reporting unresolved import from crates.io #47647

Closed BurntPizza closed 5 months ago

BurntPizza commented 6 years ago
$ cat src/lib.rs 
extern crate ordermap;
use ordermap::Orderset;

$ cat Cargo.toml 
[package]
name = "err"
version = "0.1.0"

[dependencies]
ordermap = { version = "*", path = "ordermap" }

$ cat ordermap/src/lib.rs 
pub struct OrderSet;

$ cargo build 2>&1 | grep -B1 'Did'
5 | use ordermap::Orderset;
  |     ^^^^^^^^^^^^^^^^^^ no `Orderset` in the root. Did you mean to use `OrderSet`?

Everything looks good so far. But if we change the dependency to point to crates.io: ordermap = "*" then the error message gets weird:

$ cargo build 2>&1 | grep -B1 'Did'
5 | use ordermap::Orderset;
  |     ^^^^^^^^^^^^^^^^^^ no `Orderset` in the root. Did you mean to use `orderset`?

Note the capitalization (or lack thereof). There is no item or module called 'orderset' in ordermap's root. When I came across this I was importing Ordermap and it did the same thing which was very confusing as the lowercase is also the crate name. I thought at first that coincidence was somehow responsible, but you can see the counter-example above with OrderSet.
What's even more strange is that the inline error reporting in emacs via flycheck shows it correctly! This is magical as the bug is also present in the json output, which is what flycheck consumes:

cargo build --message-format json 2>&1 | sed 's/\"/&\n/g' | grep 'Did'
no `Orderset` in the root. Did you mean to use `orderset`?"

unresolved import ordermap::Orderset (no Orderset in the root. Did you mean to use OrderSet?) [E0432]

Some other cases: If the import is all lowercase, it works properly:

$ cat src/lib.rs 
extern crate ordermap;
use ordermap::orderset;

$ cargo build 2>&1 | grep -B1 'Did'
2 | use ordermap::orderset;
  |     ^^^^^^^^^^^^^^^^^^ no `orderset` in the root. Did you mean to use `OrderSet`?

But if the import is all lowercase but missing the first letter, it doesn't:

$ cat src/lib.rs                   
extern crate ordermap;
use ordermap::rderset;

 $ cargo build 2>&1 | grep -B1 'Did'
2 | use ordermap::rderset;
  |     ^^^^^^^^^^^^^^^^^ no `rderset` in the root. Did you mean to use `orderset`?

Flycheck doesn't magically correct this one, but the local path import does.

Info:

$ rustup -V
rustup 1.9.0 (57fc3c087 2018-01-04)

$ rustc -V
rustc 1.23.0 (766bd11c8 2018-01-01)

$ cargo -V
cargo 0.24.0 (45043115c 2017-12-05)

M-x emacs-version GNU Emacs 25.3.1 (x86_64-pc-linux-gnu, GTK+ Version 3.22.26) of 2017-12-04

M-x flycheck-version Flycheck version: 32snapshot (package: 20171214.1215)

estebank commented 5 months ago

Current output:

error[E0432]: unresolved import `ordermap::Orderset`
 --> src/main.rs:2:5
  |
2 | use ordermap::Orderset;
  |     ^^^^^^^^^^--------
  |     |         |
  |     |         help: a similar name exists in the module (notice the capitalization): `orderset`
  |     no `Orderset` in the root