From ac7f9ccb6f84922d316a3efa2f50061d4af22801 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sun, 7 Mar 2021 13:53:45 -0800 Subject: [PATCH 1/3] diagnostics: Differentiate between edition meanings of ::foo in resolve diagnostics (for bare `::foo`) --- compiler/rustc_resolve/src/lib.rs | 14 ++++++++++---- src/test/ui/editions-crate-root-2015.rs | 14 ++++++++++++++ src/test/ui/editions-crate-root-2015.stderr | 15 +++++++++++++++ src/test/ui/editions-crate-root-2018.rs | 14 ++++++++++++++ src/test/ui/editions-crate-root-2018.stderr | 15 +++++++++++++++ .../edition-imports-virtual-2015-gated.stderr | 2 +- .../non-existent-2.rs | 2 +- .../non-existent-2.stderr | 4 ++-- 8 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 src/test/ui/editions-crate-root-2015.rs create mode 100644 src/test/ui/editions-crate-root-2015.stderr create mode 100644 src/test/ui/editions-crate-root-2018.rs create mode 100644 src/test/ui/editions-crate-root-2018.stderr diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 7aee718bfa9..11b0ade1e83 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -2450,10 +2450,16 @@ impl<'a> Resolver<'a> { } } else { let parent = path[i - 1].ident.name; - let parent = if parent == kw::PathRoot { - "crate root".to_owned() - } else { - format!("`{}`", parent) + let parent = match parent { + // ::foo is mounted at the crate root for 2015, and is the extern + // prelude for 2018+ + kw::PathRoot if self.session.edition() > Edition::Edition2015 => { + "the list of imported crates".to_owned() + } + kw::PathRoot | kw::Crate => "the crate root".to_owned(), + _ => { + format!("`{}`", parent) + } }; let mut msg = format!("could not find `{}` in {}", ident, parent); diff --git a/src/test/ui/editions-crate-root-2015.rs b/src/test/ui/editions-crate-root-2015.rs new file mode 100644 index 00000000000..71b7112804d --- /dev/null +++ b/src/test/ui/editions-crate-root-2015.rs @@ -0,0 +1,14 @@ +// edition:2015 + +mod inner { + fn global_inner(_: ::nonexistant::Foo) { + //~^ ERROR failed to resolve: maybe a missing crate `nonexistant`? + } + fn crate_inner(_: crate::nonexistant::Foo) { + //~^ ERROR failed to resolve: maybe a missing crate `nonexistant`? + } +} + +fn main() { + +} \ No newline at end of file diff --git a/src/test/ui/editions-crate-root-2015.stderr b/src/test/ui/editions-crate-root-2015.stderr new file mode 100644 index 00000000000..94c972227d0 --- /dev/null +++ b/src/test/ui/editions-crate-root-2015.stderr @@ -0,0 +1,15 @@ +error[E0433]: failed to resolve: maybe a missing crate `nonexistant`? + --> $DIR/editions-crate-root-2015.rs:4:26 + | +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 + | +LL | fn crate_inner(_: crate::nonexistant::Foo) { + | ^^^^^^^^^^^ maybe a missing crate `nonexistant`? + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0433`. diff --git a/src/test/ui/editions-crate-root-2018.rs b/src/test/ui/editions-crate-root-2018.rs new file mode 100644 index 00000000000..7e65f00920e --- /dev/null +++ b/src/test/ui/editions-crate-root-2018.rs @@ -0,0 +1,14 @@ +// edition:2018 + +mod inner { + fn global_inner(_: ::nonexistant::Foo) { + //~^ ERROR failed to resolve: could not find `nonexistant` in the list of imported crates + } + fn crate_inner(_: crate::nonexistant::Foo) { + //~^ ERROR failed to resolve: maybe a missing crate `nonexistant`? + } +} + +fn main() { + +} \ No newline at end of file diff --git a/src/test/ui/editions-crate-root-2018.stderr b/src/test/ui/editions-crate-root-2018.stderr new file mode 100644 index 00000000000..95a36907448 --- /dev/null +++ b/src/test/ui/editions-crate-root-2018.stderr @@ -0,0 +1,15 @@ +error[E0433]: failed to resolve: could not find `nonexistant` in the list of imported crates + --> $DIR/editions-crate-root-2018.rs:4:26 + | +LL | fn global_inner(_: ::nonexistant::Foo) { + | ^^^^^^^^^^^ could not find `nonexistant` in the list of imported crates + +error[E0433]: failed to resolve: maybe a missing crate `nonexistant`? + --> $DIR/editions-crate-root-2018.rs:7:30 + | +LL | fn crate_inner(_: crate::nonexistant::Foo) { + | ^^^^^^^^^^^ maybe a missing crate `nonexistant`? + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0433`. diff --git a/src/test/ui/editions/edition-imports-virtual-2015-gated.stderr b/src/test/ui/editions/edition-imports-virtual-2015-gated.stderr index 06605c6f208..1cde0f72140 100644 --- a/src/test/ui/editions/edition-imports-virtual-2015-gated.stderr +++ b/src/test/ui/editions/edition-imports-virtual-2015-gated.stderr @@ -2,7 +2,7 @@ error[E0432]: unresolved import `E` --> $DIR/edition-imports-virtual-2015-gated.rs:8:5 | LL | gen_gated!(); - | ^^^^^^^^^^^^^ could not find `E` in crate root + | ^^^^^^^^^^^^^ could not find `E` in the list of imported crates | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/src/test/ui/rfc-2126-extern-absolute-paths/non-existent-2.rs b/src/test/ui/rfc-2126-extern-absolute-paths/non-existent-2.rs index 61212f299be..def60feb5a6 100644 --- a/src/test/ui/rfc-2126-extern-absolute-paths/non-existent-2.rs +++ b/src/test/ui/rfc-2126-extern-absolute-paths/non-existent-2.rs @@ -2,5 +2,5 @@ fn main() { let s = ::xcrate::S; - //~^ ERROR failed to resolve: could not find `xcrate` in crate root + //~^ ERROR failed to resolve: could not find `xcrate` in the list of imported crates } diff --git a/src/test/ui/rfc-2126-extern-absolute-paths/non-existent-2.stderr b/src/test/ui/rfc-2126-extern-absolute-paths/non-existent-2.stderr index 8b2a6933f37..7df4f06d1c7 100644 --- a/src/test/ui/rfc-2126-extern-absolute-paths/non-existent-2.stderr +++ b/src/test/ui/rfc-2126-extern-absolute-paths/non-existent-2.stderr @@ -1,8 +1,8 @@ -error[E0433]: failed to resolve: could not find `xcrate` in crate root +error[E0433]: failed to resolve: could not find `xcrate` in the list of imported crates --> $DIR/non-existent-2.rs:4:15 | LL | let s = ::xcrate::S; - | ^^^^^^ could not find `xcrate` in crate root + | ^^^^^^ could not find `xcrate` in the list of imported crates error: aborting due to previous error From 9d5d669b77f8c100f3e01a7bd5924d30c8bad152 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sun, 7 Mar 2021 14:19:50 -0800 Subject: [PATCH 2/3] diagnostics: Differentiate between edition meanings of ::foo in resolve diagnostics for ::foo::Bar --- .../rustc_resolve/src/late/diagnostics.rs | 12 +++++++++++- src/test/ui/editions-crate-root-2015.rs | 7 +++++++ src/test/ui/editions-crate-root-2015.stderr | 19 ++++++++++++++++--- src/test/ui/editions-crate-root-2018.rs | 7 +++++++ src/test/ui/editions-crate-root-2018.stderr | 17 +++++++++++++++-- src/test/ui/resolve/raw-ident-in-path.rs | 2 +- src/test/ui/resolve/raw-ident-in-path.stderr | 4 ++-- 7 files changed, 59 insertions(+), 9 deletions(-) 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::{self, CtorKind, CtorOf, DefKind}; 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 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { 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 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { 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 @@ mod inner { 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 @@ mod inner { 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 From 0eeae1abfcd8e588b41b01100db4dbc9384f6144 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sun, 7 Mar 2021 14:51:35 -0800 Subject: [PATCH 3/3] diagnostics: Don't mention external crates when hitting import errors on crate imports in 2018 --- compiler/rustc_resolve/src/lib.rs | 4 +++- src/test/ui/editions-crate-root-2015.rs | 2 +- src/test/ui/editions-crate-root-2018.rs | 4 ++-- src/test/ui/editions-crate-root-2018.stderr | 4 ++-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 11b0ade1e83..2ce54658c0b 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -2433,8 +2433,10 @@ impl<'a> Resolver<'a> { Applicability::MaybeIncorrect, )), ) - } else { + } else if self.session.edition() == Edition::Edition2015 { (format!("maybe a missing crate `{}`?", ident), None) + } else { + (format!("could not find `{}` in the crate root", ident), None) } } else if i == 0 { if ident diff --git a/src/test/ui/editions-crate-root-2015.rs b/src/test/ui/editions-crate-root-2015.rs index fc35b001a60..4c890e3ae69 100644 --- a/src/test/ui/editions-crate-root-2015.rs +++ b/src/test/ui/editions-crate-root-2015.rs @@ -18,4 +18,4 @@ mod inner { fn main() { -} \ No newline at end of file +} diff --git a/src/test/ui/editions-crate-root-2018.rs b/src/test/ui/editions-crate-root-2018.rs index 7b96c1d294b..61e4329bbb3 100644 --- a/src/test/ui/editions-crate-root-2018.rs +++ b/src/test/ui/editions-crate-root-2018.rs @@ -5,7 +5,7 @@ mod inner { //~^ ERROR failed to resolve: could not find `nonexistant` in the list of imported crates } fn crate_inner(_: crate::nonexistant::Foo) { - //~^ ERROR failed to resolve: maybe a missing crate `nonexistant`? + //~^ ERROR failed to resolve: could not find `nonexistant` in the crate root } fn bare_global(_: ::nonexistant) { @@ -18,4 +18,4 @@ mod inner { fn main() { -} \ No newline at end of file +} diff --git a/src/test/ui/editions-crate-root-2018.stderr b/src/test/ui/editions-crate-root-2018.stderr index 7dfd3442260..967a5a2fca1 100644 --- a/src/test/ui/editions-crate-root-2018.stderr +++ b/src/test/ui/editions-crate-root-2018.stderr @@ -4,11 +4,11 @@ error[E0433]: failed to resolve: could not find `nonexistant` in the list of imp LL | fn global_inner(_: ::nonexistant::Foo) { | ^^^^^^^^^^^ could not find `nonexistant` in the list of imported crates -error[E0433]: failed to resolve: maybe a missing crate `nonexistant`? +error[E0433]: failed to resolve: could not find `nonexistant` in the crate root --> $DIR/editions-crate-root-2018.rs:7:30 | LL | fn crate_inner(_: crate::nonexistant::Foo) { - | ^^^^^^^^^^^ maybe a missing crate `nonexistant`? + | ^^^^^^^^^^^ could not find `nonexistant` in the crate root error[E0412]: cannot find crate `nonexistant` in the list of imported crates --> $DIR/editions-crate-root-2018.rs:11:25