Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diagnostics: Be clear about "crate root" and ::foo paths in resolve diagnostics #82881

Merged
merged 3 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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];
Expand Down
18 changes: 13 additions & 5 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -2450,10 +2452,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);
Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/editions-crate-root-2015.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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 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() {

}
28 changes: 28 additions & 0 deletions src/test/ui/editions-crate-root-2015.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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:7:30
|
LL | fn crate_inner(_: crate::nonexistant::Foo) {
| ^^^^^^^^^^^ maybe a missing crate `nonexistant`?

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

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`.
21 changes: 21 additions & 0 deletions src/test/ui/editions-crate-root-2018.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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: could not find `nonexistant` in the crate root
}

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() {

}
28 changes: 28 additions & 0 deletions src/test/ui/editions-crate-root-2018.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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: could not find `nonexistant` in the crate root
--> $DIR/editions-crate-root-2018.rs:7:30
|
LL | fn crate_inner(_: crate::nonexistant::Foo) {
| ^^^^^^^^^^^ 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
|
LL | fn bare_global(_: ::nonexistant) {
| ^^^^^^^^^^^ not found in the list of imported crates

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`.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/resolve/raw-ident-in-path.rs
Original file line number Diff line number Diff line change
@@ -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() {}
4 changes: 2 additions & 2 deletions src/test/ui/resolve/raw-ident-in-path.stderr
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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

Expand Down