Skip to content

Commit 3908eec

Browse files
authored
Rollup merge of #82881 - Manishearth:crate-root, r=estebank
diagnostics: Be clear about "crate root" and `::foo` paths in resolve diagnostics Various changes to make sure the diagnostics are clear about the differences in `::foo` paths across editions: - `::foo` will say "crate root" in 2015 and "list of imported crates" in 2018 - `crate::` will never reference imported crates in 2018 Fixes #82876
2 parents a5035c9 + 0eeae1a commit 3908eec

11 files changed

+129
-13
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_hir::def::{self, CtorKind, CtorOf, DefKind};
1616
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
1717
use rustc_hir::PrimTy;
1818
use rustc_session::parse::feature_err;
19+
use rustc_span::edition::Edition;
1920
use rustc_span::hygiene::MacroKind;
2021
use rustc_span::lev_distance::find_best_match_for_name;
2122
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -133,7 +134,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
133134
let is_enum_variant = &|res| matches!(res, Res::Def(DefKind::Variant, _));
134135

135136
// Make the base error.
136-
let expected = source.descr_expected();
137+
let mut expected = source.descr_expected();
137138
let path_str = Segment::names_to_string(path);
138139
let item_str = path.last().unwrap().ident;
139140
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> {
166167
let (mod_prefix, mod_str) = if path.len() == 1 {
167168
(String::new(), "this scope".to_string())
168169
} else if path.len() == 2 && path[0].ident.name == kw::PathRoot {
170+
if self.r.session.edition() > Edition::Edition2015 {
171+
// In edition 2018 onwards, the `::foo` syntax may only pull from the extern prelude
172+
// which overrides all other expectations of item type
173+
expected = "crate";
174+
(String::new(), "the list of imported crates".to_string())
175+
} else {
176+
(String::new(), "the crate root".to_string())
177+
}
178+
} else if path.len() == 2 && path[0].ident.name == kw::Crate {
169179
(String::new(), "the crate root".to_string())
170180
} else {
171181
let mod_path = &path[..path.len() - 1];

compiler/rustc_resolve/src/lib.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -2433,8 +2433,10 @@ impl<'a> Resolver<'a> {
24332433
Applicability::MaybeIncorrect,
24342434
)),
24352435
)
2436-
} else {
2436+
} else if self.session.edition() == Edition::Edition2015 {
24372437
(format!("maybe a missing crate `{}`?", ident), None)
2438+
} else {
2439+
(format!("could not find `{}` in the crate root", ident), None)
24382440
}
24392441
} else if i == 0 {
24402442
if ident
@@ -2450,10 +2452,16 @@ impl<'a> Resolver<'a> {
24502452
}
24512453
} else {
24522454
let parent = path[i - 1].ident.name;
2453-
let parent = if parent == kw::PathRoot {
2454-
"crate root".to_owned()
2455-
} else {
2456-
format!("`{}`", parent)
2455+
let parent = match parent {
2456+
// ::foo is mounted at the crate root for 2015, and is the extern
2457+
// prelude for 2018+
2458+
kw::PathRoot if self.session.edition() > Edition::Edition2015 => {
2459+
"the list of imported crates".to_owned()
2460+
}
2461+
kw::PathRoot | kw::Crate => "the crate root".to_owned(),
2462+
_ => {
2463+
format!("`{}`", parent)
2464+
}
24572465
};
24582466

24592467
let mut msg = format!("could not find `{}` in {}", ident, parent);
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// edition:2015
2+
3+
mod inner {
4+
fn global_inner(_: ::nonexistant::Foo) {
5+
//~^ ERROR failed to resolve: maybe a missing crate `nonexistant`?
6+
}
7+
fn crate_inner(_: crate::nonexistant::Foo) {
8+
//~^ ERROR failed to resolve: maybe a missing crate `nonexistant`?
9+
}
10+
11+
fn bare_global(_: ::nonexistant) {
12+
//~^ ERROR cannot find type `nonexistant` in the crate root
13+
}
14+
fn bare_crate(_: crate::nonexistant) {
15+
//~^ ERROR cannot find type `nonexistant` in the crate root
16+
}
17+
}
18+
19+
fn main() {
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0433]: failed to resolve: maybe a missing crate `nonexistant`?
2+
--> $DIR/editions-crate-root-2015.rs:4:26
3+
|
4+
LL | fn global_inner(_: ::nonexistant::Foo) {
5+
| ^^^^^^^^^^^ maybe a missing crate `nonexistant`?
6+
7+
error[E0433]: failed to resolve: maybe a missing crate `nonexistant`?
8+
--> $DIR/editions-crate-root-2015.rs:7:30
9+
|
10+
LL | fn crate_inner(_: crate::nonexistant::Foo) {
11+
| ^^^^^^^^^^^ maybe a missing crate `nonexistant`?
12+
13+
error[E0412]: cannot find type `nonexistant` in the crate root
14+
--> $DIR/editions-crate-root-2015.rs:11:25
15+
|
16+
LL | fn bare_global(_: ::nonexistant) {
17+
| ^^^^^^^^^^^ not found in the crate root
18+
19+
error[E0412]: cannot find type `nonexistant` in the crate root
20+
--> $DIR/editions-crate-root-2015.rs:14:29
21+
|
22+
LL | fn bare_crate(_: crate::nonexistant) {
23+
| ^^^^^^^^^^^ not found in the crate root
24+
25+
error: aborting due to 4 previous errors
26+
27+
Some errors have detailed explanations: E0412, E0433.
28+
For more information about an error, try `rustc --explain E0412`.
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// edition:2018
2+
3+
mod inner {
4+
fn global_inner(_: ::nonexistant::Foo) {
5+
//~^ ERROR failed to resolve: could not find `nonexistant` in the list of imported crates
6+
}
7+
fn crate_inner(_: crate::nonexistant::Foo) {
8+
//~^ ERROR failed to resolve: could not find `nonexistant` in the crate root
9+
}
10+
11+
fn bare_global(_: ::nonexistant) {
12+
//~^ ERROR cannot find crate `nonexistant` in the list of imported crates
13+
}
14+
fn bare_crate(_: crate::nonexistant) {
15+
//~^ ERROR cannot find type `nonexistant` in the crate root
16+
}
17+
}
18+
19+
fn main() {
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0433]: failed to resolve: could not find `nonexistant` in the list of imported crates
2+
--> $DIR/editions-crate-root-2018.rs:4:26
3+
|
4+
LL | fn global_inner(_: ::nonexistant::Foo) {
5+
| ^^^^^^^^^^^ could not find `nonexistant` in the list of imported crates
6+
7+
error[E0433]: failed to resolve: could not find `nonexistant` in the crate root
8+
--> $DIR/editions-crate-root-2018.rs:7:30
9+
|
10+
LL | fn crate_inner(_: crate::nonexistant::Foo) {
11+
| ^^^^^^^^^^^ could not find `nonexistant` in the crate root
12+
13+
error[E0412]: cannot find crate `nonexistant` in the list of imported crates
14+
--> $DIR/editions-crate-root-2018.rs:11:25
15+
|
16+
LL | fn bare_global(_: ::nonexistant) {
17+
| ^^^^^^^^^^^ not found in the list of imported crates
18+
19+
error[E0412]: cannot find type `nonexistant` in the crate root
20+
--> $DIR/editions-crate-root-2018.rs:14:29
21+
|
22+
LL | fn bare_crate(_: crate::nonexistant) {
23+
| ^^^^^^^^^^^ not found in the crate root
24+
25+
error: aborting due to 4 previous errors
26+
27+
Some errors have detailed explanations: E0412, E0433.
28+
For more information about an error, try `rustc --explain E0412`.

src/test/ui/editions/edition-imports-virtual-2015-gated.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0432]: unresolved import `E`
22
--> $DIR/edition-imports-virtual-2015-gated.rs:8:5
33
|
44
LL | gen_gated!();
5-
| ^^^^^^^^^^^^^ could not find `E` in crate root
5+
| ^^^^^^^^^^^^^ could not find `E` in the list of imported crates
66
|
77
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
88

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Regression test for issue #63882.
22

3-
type A = crate::r#break; //~ ERROR cannot find type `r#break` in module `crate`
3+
type A = crate::r#break; //~ ERROR cannot find type `r#break` in the crate root
44

55
fn main() {}

src/test/ui/resolve/raw-ident-in-path.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0412]: cannot find type `r#break` in module `crate`
1+
error[E0412]: cannot find type `r#break` in the crate root
22
--> $DIR/raw-ident-in-path.rs:3:17
33
|
44
LL | type A = crate::r#break;
5-
| ^^^^^^^ not found in `crate`
5+
| ^^^^^^^ not found in the crate root
66

77
error: aborting due to previous error
88

src/test/ui/rfc-2126-extern-absolute-paths/non-existent-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
fn main() {
44
let s = ::xcrate::S;
5-
//~^ ERROR failed to resolve: could not find `xcrate` in crate root
5+
//~^ ERROR failed to resolve: could not find `xcrate` in the list of imported crates
66
}

src/test/ui/rfc-2126-extern-absolute-paths/non-existent-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0433]: failed to resolve: could not find `xcrate` in crate root
1+
error[E0433]: failed to resolve: could not find `xcrate` in the list of imported crates
22
--> $DIR/non-existent-2.rs:4:15
33
|
44
LL | let s = ::xcrate::S;
5-
| ^^^^^^ could not find `xcrate` in crate root
5+
| ^^^^^^ could not find `xcrate` in the list of imported crates
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)