Skip to content

Commit a77c1d8

Browse files
authored
Rollup merge of #81046 - rylev:unknown-external-crate, r=estebank
Improve unknown external crate error This improves error messages when unknown items in the crate root are encountered. Fixes #63799 r? ```@estebank```
2 parents 8be36b1 + 38b7742 commit a77c1d8

File tree

9 files changed

+51
-8
lines changed

9 files changed

+51
-8
lines changed

compiler/rustc_resolve/src/late.rs

+7
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,13 @@ impl<'a> PathSource<'a> {
243243
// "function" here means "anything callable" rather than `DefKind::Fn`,
244244
// this is not precise but usually more helpful than just "value".
245245
Some(ExprKind::Call(call_expr, _)) => match &call_expr.kind {
246+
// the case of `::some_crate()`
247+
ExprKind::Path(_, path)
248+
if path.segments.len() == 2
249+
&& path.segments[0].ident.name == kw::PathRoot =>
250+
{
251+
"external crate"
252+
}
246253
ExprKind::Path(_, path) => {
247254
let mut msg = "function";
248255
if let Some(segment) = path.segments.iter().last() {

compiler/rustc_resolve/src/lib.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -2485,20 +2485,26 @@ impl<'a> Resolver<'a> {
24852485
(format!("use of undeclared crate or module `{}`", ident), None)
24862486
}
24872487
} else {
2488-
let mut msg =
2489-
format!("could not find `{}` in `{}`", ident, path[i - 1].ident);
2488+
let parent = path[i - 1].ident.name;
2489+
let parent = if parent == kw::PathRoot {
2490+
"crate root".to_owned()
2491+
} else {
2492+
format!("`{}`", parent)
2493+
};
2494+
2495+
let mut msg = format!("could not find `{}` in {}", ident, parent);
24902496
if ns == TypeNS || ns == ValueNS {
24912497
let ns_to_try = if ns == TypeNS { ValueNS } else { TypeNS };
24922498
if let FindBindingResult::Binding(Ok(binding)) =
24932499
find_binding_in_ns(self, ns_to_try)
24942500
{
24952501
let mut found = |what| {
24962502
msg = format!(
2497-
"expected {}, found {} `{}` in `{}`",
2503+
"expected {}, found {} `{}` in {}",
24982504
ns.descr(),
24992505
what,
25002506
ident,
2501-
path[i - 1].ident
2507+
parent
25022508
)
25032509
};
25042510
if binding.module().is_some() {

src/test/ui/editions/edition-imports-virtual-2015-gated.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0432]: unresolved import `E`
22
--> $DIR/edition-imports-virtual-2015-gated.rs:8:5
33
|
44
LL | gen_gated!();
5-
| ^^^^^^^^^^^^^ could not find `E` in `{{root}}`
5+
| ^^^^^^^^^^^^^ could not find `E` in crate root
66
|
77
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
88

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
::foo() //~ cannot find external crate `foo` in the crate root
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0425]: cannot find external crate `foo` in the crate root
2+
--> $DIR/crate-called-as-function.rs:2:7
3+
|
4+
LL | ::foo()
5+
| ^^^ not found in the crate root
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0425`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let _map = std::hahmap::HashMap::new();
3+
//~^ ERROR failed to resolve: could not find `hahmap` in `std
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0433]: failed to resolve: could not find `hahmap` in `std`
2+
--> $DIR/missing-in-namespace.rs:2:29
3+
|
4+
LL | let _map = std::hahmap::HashMap::new();
5+
| ^^^^^^^ not found in `std::hahmap`
6+
|
7+
help: consider importing this struct
8+
|
9+
LL | use std::collections::HashMap;
10+
|
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0433`.

src/test/ui/rfc-2126-extern-absolute-paths/non-existent-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
fn main() {
44
let s = ::xcrate::S;
5-
//~^ ERROR failed to resolve: could not find `xcrate` in `{{root}}`
5+
//~^ ERROR failed to resolve: could not find `xcrate` in crate root
66
}

src/test/ui/rfc-2126-extern-absolute-paths/non-existent-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0433]: failed to resolve: could not find `xcrate` in `{{root}}`
1+
error[E0433]: failed to resolve: could not find `xcrate` in crate root
22
--> $DIR/non-existent-2.rs:4:15
33
|
44
LL | let s = ::xcrate::S;
5-
| ^^^^^^ could not find `xcrate` in `{{root}}`
5+
| ^^^^^^ could not find `xcrate` in crate root
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)