Skip to content

Commit

Permalink
Add note and suggestion when crate location fails due to the identifi…
Browse files Browse the repository at this point in the history
…er `meta` being used
  • Loading branch information
fmckeogh committed Aug 5, 2020
1 parent ec9d524 commit e645cca
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/librustc_metadata/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::owning_ref::OwningRef;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::MetadataRef;
use rustc_errors::struct_span_err;
use rustc_errors::{struct_span_err, Applicability};
use rustc_middle::middle::cstore::{CrateSource, MetadataLoader};
use rustc_session::config::{self, CrateType};
use rustc_session::filesearch::{FileDoesntMatch, FileMatches, FileSearch};
Expand Down Expand Up @@ -1068,6 +1068,14 @@ impl CrateError {
err.note(&format!("the `{}` target may not be installed", locator.triple));
} else if crate_name == sym::profiler_builtins {
err.note(&"the compiler may have been built without the profiler runtime");
} else if crate_name == sym::meta {
err.note(&"meta is a reserved crate name");
err.span_suggestion(
span,
"you can use `crate::` or `self::` if you intended to refer to a local module and not a crate",
"crate::meta".to_string(),
Applicability::MaybeIncorrect,
);
}
err.span_label(span, "can't find crate");
err
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/rfc-2126-extern-absolute-paths/meta.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0463]: can't find crate for `meta`
|
LL | use meta;
| ^^^^ can't find crate
|
= note: meta is a reserved crate name
help: you can use `crate::` or `self::` if you intended to refer to a local module and not a crate
|
LL | use crate::meta;
| ^^^^^^^^^^^

error: aborting due to previous error

Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/suggestions/issue-73948-meta-as-module-name.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// check-only
// run-rustfix
// edition:2018

// https://github.com/rust-lang/rust/issues/73948
// Tests that when `meta` is used as a module name and imported uniformly a
// suggestion is made to make the import unambiguous with `crate::meta`

mod meta {
pub const FOO: bool = true;
}

use crate::meta::FOO; //~ ERROR can't find crate for `meta`

fn main() {
assert!(FOO);
}
17 changes: 17 additions & 0 deletions src/test/ui/suggestions/issue-73948-meta-as-module-name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// check-only
// run-rustfix
// edition:2018

// https://github.com/rust-lang/rust/issues/73948
// Tests that when `meta` is used as a module name and imported uniformly a
// suggestion is made to make the import unambiguous with `crate::meta`

mod meta {
pub const FOO: bool = true;
}

use meta::FOO; //~ ERROR can't find crate for `meta`

fn main() {
assert!(FOO);
}
15 changes: 15 additions & 0 deletions src/test/ui/suggestions/issue-73948-meta-as-module-name.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0463]: can't find crate for `meta`
--> $DIR/issue-73948-meta-as-module-name.rs:13:5
|
LL | use meta::FOO;
| ^^^^ can't find crate
|
= note: meta is a reserved crate name
help: you can use `crate::` or `self::` if you intended to refer to a local module and not a crate
|
LL | use crate::meta::FOO;
| ^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.

0 comments on commit e645cca

Please sign in to comment.