Skip to content

Commit 9d5d669

Browse files
committed
diagnostics: Differentiate between edition meanings of ::foo in resolve diagnostics for ::foo::Bar
1 parent ac7f9cc commit 9d5d669

7 files changed

+59
-9
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];

src/test/ui/editions-crate-root-2015.rs

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ mod inner {
77
fn crate_inner(_: crate::nonexistant::Foo) {
88
//~^ ERROR failed to resolve: maybe a missing crate `nonexistant`?
99
}
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+
}
1017
}
1118

1219
fn main() {

src/test/ui/editions-crate-root-2015.stderr

+16-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,24 @@ LL | fn global_inner(_: ::nonexistant::Foo) {
55
| ^^^^^^^^^^^ maybe a missing crate `nonexistant`?
66

77
error[E0433]: failed to resolve: maybe a missing crate `nonexistant`?
8-
--> $DIR/editions-crate-root-2015.rs:8:30
8+
--> $DIR/editions-crate-root-2015.rs:7:30
99
|
1010
LL | fn crate_inner(_: crate::nonexistant::Foo) {
1111
| ^^^^^^^^^^^ maybe a missing crate `nonexistant`?
1212

13-
error: aborting due to 2 previous errors
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
1426

15-
For more information about this error, try `rustc --explain E0433`.
27+
Some errors have detailed explanations: E0412, E0433.
28+
For more information about an error, try `rustc --explain E0412`.

src/test/ui/editions-crate-root-2018.rs

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ mod inner {
77
fn crate_inner(_: crate::nonexistant::Foo) {
88
//~^ ERROR failed to resolve: maybe a missing crate `nonexistant`?
99
}
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+
}
1017
}
1118

1219
fn main() {

src/test/ui/editions-crate-root-2018.stderr

+15-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ error[E0433]: failed to resolve: maybe a missing crate `nonexistant`?
1010
LL | fn crate_inner(_: crate::nonexistant::Foo) {
1111
| ^^^^^^^^^^^ maybe a missing crate `nonexistant`?
1212

13-
error: aborting due to 2 previous errors
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
1426

15-
For more information about this error, try `rustc --explain E0433`.
27+
Some errors have detailed explanations: E0412, E0433.
28+
For more information about an error, try `rustc --explain E0412`.
+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

0 commit comments

Comments
 (0)