Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve unknown external crate error #81046

Merged
merged 2 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ impl<'a> PathSource<'a> {
// "function" here means "anything callable" rather than `DefKind::Fn`,
// this is not precise but usually more helpful than just "value".
Some(ExprKind::Call(call_expr, _)) => match &call_expr.kind {
// the case of `::some_crate()`
ExprKind::Path(_, path)
if path.segments.len() == 2
&& path.segments[0].ident.name == kw::PathRoot =>
{
"external crate"
}
rylev marked this conversation as resolved.
Show resolved Hide resolved
ExprKind::Path(_, path) => {
let mut msg = "function";
if let Some(segment) = path.segments.iter().last() {
Expand Down
14 changes: 10 additions & 4 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2458,20 +2458,26 @@ impl<'a> Resolver<'a> {
(format!("use of undeclared crate or module `{}`", ident), None)
}
} else {
let mut msg =
format!("could not find `{}` in `{}`", ident, path[i - 1].ident);
let parent = path[i - 1].ident.name;
let parent = if parent == kw::PathRoot {
"crate root".to_owned()
Copy link
Contributor

@estebank estebank Jan 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be root crate instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, the path root is normally referred to as the crate root as you can see here. root crate makes it sound like this refers to a crate itself but it does not, it's the root of the namespace hierarchy.

} else {
format!("`{}`", parent)
estebank marked this conversation as resolved.
Show resolved Hide resolved
rylev marked this conversation as resolved.
Show resolved Hide resolved
};
Comment on lines +2462 to +2466
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than special casing this here, does it make sense to change in impl Display for Symbol instead? Then you don't have to worry about missing it somewhere (and everyone else using this benefits as well).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is certainly a possibility. I didn't do this because I was unsure of the ramifications of renaming the symbol's string representation (particularly around json serialization/deserialization). The change I made seems to be the central place for diagnostics around path resolution (i.e., there are not UI tests that still reference {{root}}).


let mut msg = format!("could not find `{}` in {}", ident, parent);
if ns == TypeNS || ns == ValueNS {
let ns_to_try = if ns == TypeNS { ValueNS } else { TypeNS };
if let FindBindingResult::Binding(Ok(binding)) =
find_binding_in_ns(self, ns_to_try)
{
let mut found = |what| {
msg = format!(
"expected {}, found {} `{}` in `{}`",
"expected {}, found {} `{}` in {}",
ns.descr(),
what,
ident,
path[i - 1].ident
parent
)
};
if binding.module().is_some() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0432]: unresolved import `E`
--> $DIR/edition-imports-virtual-2015-gated.rs:8:5
|
LL | gen_gated!();
| ^^^^^^^^^^^^^ could not find `E` in `{{root}}`
| ^^^^^^^^^^^^^ could not find `E` in crate root
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/resolve/crate-called-as-function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
::foo() //~ cannot find external crate `foo` in the crate root
}
9 changes: 9 additions & 0 deletions src/test/ui/resolve/crate-called-as-function.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0425]: cannot find external crate `foo` in the crate root
--> $DIR/crate-called-as-function.rs:2:7
|
LL | ::foo()
| ^^^ not found in the crate root

error: aborting due to previous error

For more information about this error, try `rustc --explain E0425`.
4 changes: 4 additions & 0 deletions src/test/ui/resolve/missing-in-namespace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let _map = std::hahmap::HashMap::new();
//~^ ERROR failed to resolve: could not find `hahmap` in `std
}
14 changes: 14 additions & 0 deletions src/test/ui/resolve/missing-in-namespace.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0433]: failed to resolve: could not find `hahmap` in `std`
--> $DIR/missing-in-namespace.rs:2:29
|
LL | let _map = std::hahmap::HashMap::new();
| ^^^^^^^ not found in `std::hahmap`
|
help: consider importing this struct
|
LL | use std::collections::HashMap;
|

error: aborting due to previous error

For more information about this error, try `rustc --explain E0433`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

fn main() {
let s = ::xcrate::S;
//~^ ERROR failed to resolve: could not find `xcrate` in `{{root}}`
//~^ ERROR failed to resolve: could not find `xcrate` in crate root
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0433]: failed to resolve: could not find `xcrate` in `{{root}}`
error[E0433]: failed to resolve: could not find `xcrate` in crate root
--> $DIR/non-existent-2.rs:4:15
|
LL | let s = ::xcrate::S;
| ^^^^^^ could not find `xcrate` in `{{root}}`
| ^^^^^^ could not find `xcrate` in crate root

error: aborting due to previous error

Expand Down