diagnostics: Differentiate between edition meanings of ::foo in resolve diagnostics for ::foo::Bar

This commit is contained in:
Manish Goregaokar 2021-03-07 14:19:50 -08:00
parent ac7f9ccb6f
commit 9d5d669b77
7 changed files with 59 additions and 9 deletions

View File

@ -16,6 +16,7 @@
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::PrimTy;
use rustc_session::parse::feature_err;
use rustc_span::edition::Edition;
use rustc_span::hygiene::MacroKind;
use rustc_span::lev_distance::find_best_match_for_name;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@ -133,7 +134,7 @@ pub(crate) fn smart_resolve_report_errors(
let is_enum_variant = &|res| matches!(res, Res::Def(DefKind::Variant, _));
// Make the base error.
let expected = source.descr_expected();
let mut expected = source.descr_expected();
let path_str = Segment::names_to_string(path);
let item_str = path.last().unwrap().ident;
let (base_msg, fallback_label, base_span, could_be_expr) = if let Some(res) = res {
@ -166,6 +167,15 @@ pub(crate) fn smart_resolve_report_errors(
let (mod_prefix, mod_str) = if path.len() == 1 {
(String::new(), "this scope".to_string())
} else if path.len() == 2 && path[0].ident.name == kw::PathRoot {
if self.r.session.edition() > Edition::Edition2015 {
// In edition 2018 onwards, the `::foo` syntax may only pull from the extern prelude
// which overrides all other expectations of item type
expected = "crate";
(String::new(), "the list of imported crates".to_string())
} else {
(String::new(), "the crate root".to_string())
}
} else if path.len() == 2 && path[0].ident.name == kw::Crate {
(String::new(), "the crate root".to_string())
} else {
let mod_path = &path[..path.len() - 1];

View File

@ -7,6 +7,13 @@ fn global_inner(_: ::nonexistant::Foo) {
fn crate_inner(_: crate::nonexistant::Foo) {
//~^ ERROR failed to resolve: maybe a missing crate `nonexistant`?
}
fn bare_global(_: ::nonexistant) {
//~^ ERROR cannot find type `nonexistant` in the crate root
}
fn bare_crate(_: crate::nonexistant) {
//~^ ERROR cannot find type `nonexistant` in the crate root
}
}
fn main() {

View File

@ -5,11 +5,24 @@ LL | fn global_inner(_: ::nonexistant::Foo) {
| ^^^^^^^^^^^ maybe a missing crate `nonexistant`?
error[E0433]: failed to resolve: maybe a missing crate `nonexistant`?
--> $DIR/editions-crate-root-2015.rs:8:30
--> $DIR/editions-crate-root-2015.rs:7:30
|
LL | fn crate_inner(_: crate::nonexistant::Foo) {
| ^^^^^^^^^^^ maybe a missing crate `nonexistant`?
error: aborting due to 2 previous errors
error[E0412]: cannot find type `nonexistant` in the crate root
--> $DIR/editions-crate-root-2015.rs:11:25
|
LL | fn bare_global(_: ::nonexistant) {
| ^^^^^^^^^^^ not found in the crate root
For more information about this error, try `rustc --explain E0433`.
error[E0412]: cannot find type `nonexistant` in the crate root
--> $DIR/editions-crate-root-2015.rs:14:29
|
LL | fn bare_crate(_: crate::nonexistant) {
| ^^^^^^^^^^^ not found in the crate root
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0412, E0433.
For more information about an error, try `rustc --explain E0412`.

View File

@ -7,6 +7,13 @@ fn global_inner(_: ::nonexistant::Foo) {
fn crate_inner(_: crate::nonexistant::Foo) {
//~^ ERROR failed to resolve: maybe a missing crate `nonexistant`?
}
fn bare_global(_: ::nonexistant) {
//~^ ERROR cannot find crate `nonexistant` in the list of imported crates
}
fn bare_crate(_: crate::nonexistant) {
//~^ ERROR cannot find type `nonexistant` in the crate root
}
}
fn main() {

View File

@ -10,6 +10,19 @@ error[E0433]: failed to resolve: maybe a missing crate `nonexistant`?
LL | fn crate_inner(_: crate::nonexistant::Foo) {
| ^^^^^^^^^^^ maybe a missing crate `nonexistant`?
error: aborting due to 2 previous errors
error[E0412]: cannot find crate `nonexistant` in the list of imported crates
--> $DIR/editions-crate-root-2018.rs:11:25
|
LL | fn bare_global(_: ::nonexistant) {
| ^^^^^^^^^^^ not found in the list of imported crates
For more information about this error, try `rustc --explain E0433`.
error[E0412]: cannot find type `nonexistant` in the crate root
--> $DIR/editions-crate-root-2018.rs:14:29
|
LL | fn bare_crate(_: crate::nonexistant) {
| ^^^^^^^^^^^ not found in the crate root
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0412, E0433.
For more information about an error, try `rustc --explain E0412`.

View File

@ -1,5 +1,5 @@
// Regression test for issue #63882.
type A = crate::r#break; //~ ERROR cannot find type `r#break` in module `crate`
type A = crate::r#break; //~ ERROR cannot find type `r#break` in the crate root
fn main() {}

View File

@ -1,8 +1,8 @@
error[E0412]: cannot find type `r#break` in module `crate`
error[E0412]: cannot find type `r#break` in the crate root
--> $DIR/raw-ident-in-path.rs:3:17
|
LL | type A = crate::r#break;
| ^^^^^^^ not found in `crate`
| ^^^^^^^ not found in the crate root
error: aborting due to previous error