Skip to content

Commit d829e40

Browse files
committed
Improve unknown external crate error
1 parent 4e208f6 commit d829e40

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

compiler/rustc_resolve/src/late.rs

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

compiler/rustc_resolve/src/lib.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -2458,20 +2458,26 @@ impl<'a> Resolver<'a> {
24582458
(format!("use of undeclared crate or module `{}`", ident), None)
24592459
}
24602460
} else {
2461-
let mut msg =
2462-
format!("could not find `{}` in `{}`", ident, path[i - 1].ident);
2461+
let parent = path[i - 1].ident.name;
2462+
let parent = if parent == kw::PathRoot {
2463+
"crate root".to_owned()
2464+
} else {
2465+
format!("`{}`", parent)
2466+
};
2467+
2468+
let mut msg = format!("could not find `{}` in {}", ident, parent);
24632469
if ns == TypeNS || ns == ValueNS {
24642470
let ns_to_try = if ns == TypeNS { ValueNS } else { TypeNS };
24652471
if let FindBindingResult::Binding(Ok(binding)) =
24662472
find_binding_in_ns(self, ns_to_try)
24672473
{
24682474
let mut found = |what| {
24692475
msg = format!(
2470-
"expected {}, found {} `{}` in `{}`",
2476+
"expected {}, found {} `{}` in {}",
24712477
ns.descr(),
24722478
what,
24732479
ident,
2474-
path[i - 1].ident
2480+
parent
24752481
)
24762482
};
24772483
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

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)