Skip to content

Commit e645cca

Browse files
committed
Add note and suggestion when crate location fails due to the identifier meta being used
1 parent ec9d524 commit e645cca

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

src/librustc_metadata/locator.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
219219
use rustc_data_structures::owning_ref::OwningRef;
220220
use rustc_data_structures::svh::Svh;
221221
use rustc_data_structures::sync::MetadataRef;
222-
use rustc_errors::struct_span_err;
222+
use rustc_errors::{struct_span_err, Applicability};
223223
use rustc_middle::middle::cstore::{CrateSource, MetadataLoader};
224224
use rustc_session::config::{self, CrateType};
225225
use rustc_session::filesearch::{FileDoesntMatch, FileMatches, FileSearch};
@@ -1068,6 +1068,14 @@ impl CrateError {
10681068
err.note(&format!("the `{}` target may not be installed", locator.triple));
10691069
} else if crate_name == sym::profiler_builtins {
10701070
err.note(&"the compiler may have been built without the profiler runtime");
1071+
} else if crate_name == sym::meta {
1072+
err.note(&"meta is a reserved crate name");
1073+
err.span_suggestion(
1074+
span,
1075+
"you can use `crate::` or `self::` if you intended to refer to a local module and not a crate",
1076+
"crate::meta".to_string(),
1077+
Applicability::MaybeIncorrect,
1078+
);
10711079
}
10721080
err.span_label(span, "can't find crate");
10731081
err

src/test/ui/rfc-2126-extern-absolute-paths/meta.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0463]: can't find crate for `meta`
33
|
44
LL | use meta;
55
| ^^^^ can't find crate
6+
|
7+
= note: meta is a reserved crate name
8+
help: you can use `crate::` or `self::` if you intended to refer to a local module and not a crate
9+
|
10+
LL | use crate::meta;
11+
| ^^^^^^^^^^^
612

713
error: aborting due to previous error
814

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// check-only
2+
// run-rustfix
3+
// edition:2018
4+
5+
// https://github.com/rust-lang/rust/issues/73948
6+
// Tests that when `meta` is used as a module name and imported uniformly a
7+
// suggestion is made to make the import unambiguous with `crate::meta`
8+
9+
mod meta {
10+
pub const FOO: bool = true;
11+
}
12+
13+
use crate::meta::FOO; //~ ERROR can't find crate for `meta`
14+
15+
fn main() {
16+
assert!(FOO);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// check-only
2+
// run-rustfix
3+
// edition:2018
4+
5+
// https://github.com/rust-lang/rust/issues/73948
6+
// Tests that when `meta` is used as a module name and imported uniformly a
7+
// suggestion is made to make the import unambiguous with `crate::meta`
8+
9+
mod meta {
10+
pub const FOO: bool = true;
11+
}
12+
13+
use meta::FOO; //~ ERROR can't find crate for `meta`
14+
15+
fn main() {
16+
assert!(FOO);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0463]: can't find crate for `meta`
2+
--> $DIR/issue-73948-meta-as-module-name.rs:13:5
3+
|
4+
LL | use meta::FOO;
5+
| ^^^^ can't find crate
6+
|
7+
= note: meta is a reserved crate name
8+
help: you can use `crate::` or `self::` if you intended to refer to a local module and not a crate
9+
|
10+
LL | use crate::meta::FOO;
11+
| ^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0463`.

0 commit comments

Comments
 (0)