Skip to content

Commit ce2578f

Browse files
authored
Emit error UnnamedCrateRootImport also for DollarCrate
1 parent ce6daf3 commit ce2578f

File tree

9 files changed

+60
-7
lines changed

9 files changed

+60
-7
lines changed

compiler/rustc_resolve/messages.ftl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,10 @@ resolve_unexpected_res_use_at_op_in_slice_pat_with_range_sugg =
461461
if you meant to collect the rest of the slice in `{$ident}`, use the at operator
462462
463463
resolve_unnamed_crate_root_import =
464-
crate root imports need to be explicitly named: `use crate as name;`
464+
{$dollar ->
465+
[true] crate root imports need to be explicitly named: `use $crate as name;`
466+
*[false] crate root imports need to be explicitly named: `use crate as name;`
467+
}
465468
466469
resolve_unreachable_label =
467470
use of unreachable label `{$name}`

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
651651
}
652652
}
653653

654-
if ident.name == kw::Crate {
655-
self.r.dcx().emit_err(errors::UnnamedCrateRootImport { span: ident.span });
654+
if ident.name == kw::Crate || ident.name == kw::DollarCrate {
655+
self.r.dcx().emit_err(errors::UnnamedCrateRootImport {
656+
span: ident.span,
657+
dollar: ident.name == kw::DollarCrate,
658+
});
656659
}
657660

658661
let kind = ImportKind::Single {

compiler/rustc_resolve/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ pub(crate) struct ArgumentsMacroUseNotAllowed {
896896
pub(crate) struct UnnamedCrateRootImport {
897897
#[primary_span]
898898
pub(crate) span: Span,
899+
pub(crate) dollar: bool,
899900
}
900901

901902
#[derive(Diagnostic)]

tests/ui/dollar-crate/dollar-crate-is-keyword-2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod a {}
33
macro_rules! m {
44
() => {
55
use a::$crate; //~ ERROR unresolved import `a::$crate`
6+
//~^ ERROR crate root imports need to be explicitly named: `use $crate as name;`
67
use a::$crate::b; //~ ERROR `$crate` in paths can only be used in start position
78
type A = a::$crate; //~ ERROR `$crate` in paths can only be used in start position
89
}

tests/ui/dollar-crate/dollar-crate-is-keyword-2.stderr

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1+
error: crate root imports need to be explicitly named: `use $crate as name;`
2+
--> $DIR/dollar-crate-is-keyword-2.rs:5:16
3+
|
4+
LL | use a::$crate;
5+
| ^^^^^^
6+
...
7+
LL | m!();
8+
| ---- in this macro invocation
9+
|
10+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
112
error[E0433]: failed to resolve: `$crate` in paths can only be used in start position
2-
--> $DIR/dollar-crate-is-keyword-2.rs:6:16
13+
--> $DIR/dollar-crate-is-keyword-2.rs:7:16
314
|
415
LL | use a::$crate::b;
516
| ^^^^^^ `$crate` in paths can only be used in start position
@@ -21,7 +32,7 @@ LL | m!();
2132
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
2233

2334
error[E0433]: failed to resolve: `$crate` in paths can only be used in start position
24-
--> $DIR/dollar-crate-is-keyword-2.rs:7:21
35+
--> $DIR/dollar-crate-is-keyword-2.rs:8:21
2536
|
2637
LL | type A = a::$crate;
2738
| ^^^^^^ `$crate` in paths can only be used in start position
@@ -31,7 +42,7 @@ LL | m!();
3142
|
3243
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
3344

34-
error: aborting due to 3 previous errors
45+
error: aborting due to 4 previous errors
3546

3647
Some errors have detailed explanations: E0432, E0433.
3748
For more information about an error, try `rustc --explain E0432`.

tests/ui/dollar-crate/dollar-crate-is-keyword.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ macro_rules! m {
99
use $crate; //~ ERROR `$crate` may not be imported
1010
use $crate as $crate; //~ ERROR expected identifier, found reserved identifier `$crate`
1111
//~^ ERROR `$crate` may not be imported
12+
//~^^ ERROR crate root imports need to be explicitly named: `use $crate as name;`
1213
}
1314
}
1415

tests/ui/dollar-crate/dollar-crate-is-keyword.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,16 @@ LL | m!();
4242
|
4343
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
4444

45-
error: aborting due to 4 previous errors
45+
error: crate root imports need to be explicitly named: `use $crate as name;`
46+
--> $DIR/dollar-crate-is-keyword.rs:10:23
47+
|
48+
LL | use $crate as $crate;
49+
| ^^^^^^
50+
...
51+
LL | m!();
52+
| ---- in this macro invocation
53+
|
54+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
55+
56+
error: aborting due to 5 previous errors
4657

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
macro_rules! foo {
2+
() => {
3+
use $crate::{self}; //~ ERROR crate root imports need to be explicitly named: `use $crate as name;`
4+
};
5+
}
6+
7+
foo!();
8+
9+
fn main() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: crate root imports need to be explicitly named: `use $crate as name;`
2+
--> $DIR/use-dollar-crate.rs:3:22
3+
|
4+
LL | use $crate::{self};
5+
| ^^^^
6+
...
7+
LL | foo!();
8+
| ------ in this macro invocation
9+
|
10+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error: aborting due to 1 previous error
13+

0 commit comments

Comments
 (0)