diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 87bf79d722b..2ee7f53ffa8 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -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]; diff --git a/src/test/ui/editions-crate-root-2015.rs b/src/test/ui/editions-crate-root-2015.rs index 71b7112804d..fc35b001a60 100644 --- a/src/test/ui/editions-crate-root-2015.rs +++ b/src/test/ui/editions-crate-root-2015.rs @@ -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() { diff --git a/src/test/ui/editions-crate-root-2015.stderr b/src/test/ui/editions-crate-root-2015.stderr index 94c972227d0..f8d65fec3d1 100644 --- a/src/test/ui/editions-crate-root-2015.stderr +++ b/src/test/ui/editions-crate-root-2015.stderr @@ -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`. diff --git a/src/test/ui/editions-crate-root-2018.rs b/src/test/ui/editions-crate-root-2018.rs index 7e65f00920e..7b96c1d294b 100644 --- a/src/test/ui/editions-crate-root-2018.rs +++ b/src/test/ui/editions-crate-root-2018.rs @@ -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() { diff --git a/src/test/ui/editions-crate-root-2018.stderr b/src/test/ui/editions-crate-root-2018.stderr index 95a36907448..7dfd3442260 100644 --- a/src/test/ui/editions-crate-root-2018.stderr +++ b/src/test/ui/editions-crate-root-2018.stderr @@ -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`. diff --git a/src/test/ui/resolve/raw-ident-in-path.rs b/src/test/ui/resolve/raw-ident-in-path.rs index 1bcbef59437..7f1163bebde 100644 --- a/src/test/ui/resolve/raw-ident-in-path.rs +++ b/src/test/ui/resolve/raw-ident-in-path.rs @@ -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() {} diff --git a/src/test/ui/resolve/raw-ident-in-path.stderr b/src/test/ui/resolve/raw-ident-in-path.stderr index f2efcbc8e85..771dacbbb20 100644 --- a/src/test/ui/resolve/raw-ident-in-path.stderr +++ b/src/test/ui/resolve/raw-ident-in-path.stderr @@ -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