Skip to content

Commit 41875c7

Browse files
committed
Diagnostic for using macro_rules macro as attr/derive
1 parent 8522140 commit 41875c7

6 files changed

+66
-14
lines changed

compiler/rustc_resolve/messages.ftl

+10-1
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,11 @@ resolve_lowercase_self =
257257
attempt to use a non-constant value in a constant
258258
.suggestion = try using `Self`
259259
260+
resolve_macro_cannot_use_as_attr = `macro_rules!` macros cannot be used as attribute macros
261+
resolve_macro_cannot_use_as_derive = `macro_rules!` macros cannot be used as derive macros
262+
260263
resolve_macro_defined_later =
261-
a macro with the same name exists, but it appears later at here
264+
a macro with the same name exists, but it appears later
262265
263266
resolve_macro_expanded_extern_crate_cannot_shadow_extern_arguments =
264267
macro-expanded `extern crate` items cannot shadow names passed with `--extern`
@@ -271,6 +274,12 @@ resolve_macro_extern_deprecated =
271274
`#[macro_escape]` is a deprecated synonym for `#[macro_use]`
272275
.help = try an outer attribute: `#[macro_use]`
273276
277+
resolve_macro_not_attr =
278+
`{$ident}` exists, but is not an attribute macro
279+
280+
resolve_macro_not_derive =
281+
`{$ident}` exists, but is not an derive macro
282+
274283
resolve_macro_use_extern_crate_self = `#[macro_use]` is not supported on `extern crate self`
275284
276285
resolve_macro_use_name_already_in_use =

compiler/rustc_resolve/src/diagnostics.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ use tracing::debug;
3535

3636
use crate::errors::{
3737
self, AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion, ConsiderAddingADerive,
38-
ExplicitUnsafeTraits, MacroDefinedLater, MacroSuggMovePosition, MaybeMissingMacroRulesName,
38+
ExplicitUnsafeTraits, MacroDefinedLater, MacroRulesNot, MacroSuggMovePosition,
39+
MaybeMissingMacroRulesName,
3940
};
4041
use crate::imports::{Import, ImportKind};
4142
use crate::late::{PatternSource, Rib};
@@ -1475,8 +1476,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14751476
let scope = self.local_macro_def_scopes[&def_id];
14761477
let parent_nearest = parent_scope.module.nearest_parent_mod();
14771478
if Some(parent_nearest) == scope.opt_def_id() {
1478-
err.subdiagnostic(MacroDefinedLater { span: unused_ident.span });
1479-
err.subdiagnostic(MacroSuggMovePosition { span: ident.span, ident });
1479+
match macro_kind {
1480+
MacroKind::Bang => {
1481+
err.subdiagnostic(MacroDefinedLater { span: unused_ident.span });
1482+
err.subdiagnostic(MacroSuggMovePosition { span: ident.span, ident });
1483+
}
1484+
MacroKind::Attr => {
1485+
err.subdiagnostic(MacroRulesNot::Attr { span: unused_ident.span, ident });
1486+
}
1487+
MacroKind::Derive => {
1488+
err.subdiagnostic(MacroRulesNot::Derive { span: unused_ident.span, ident });
1489+
}
1490+
}
1491+
1492+
if matches!(macro_kind, MacroKind::Derive | MacroKind::Attr) {}
14801493
return;
14811494
}
14821495
}

compiler/rustc_resolve/src/errors.rs

+18
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,24 @@ pub(crate) struct MacroSuggMovePosition {
665665
pub(crate) ident: Ident,
666666
}
667667

668+
#[derive(Subdiagnostic)]
669+
pub(crate) enum MacroRulesNot {
670+
#[label(resolve_macro_not_attr)]
671+
#[help(resolve_macro_cannot_use_as_attr)]
672+
Attr {
673+
#[primary_span]
674+
span: Span,
675+
ident: Ident,
676+
},
677+
#[label(resolve_macro_not_derive)]
678+
#[help(resolve_macro_cannot_use_as_derive)]
679+
Derive {
680+
#[primary_span]
681+
span: Span,
682+
ident: Ident,
683+
},
684+
}
685+
668686
#[derive(Subdiagnostic)]
669687
#[note(resolve_missing_macro_rules_name)]
670688
pub(crate) struct MaybeMissingMacroRulesName {

tests/ui/macros/defined-later-issue-121061-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: cannot find macro `something_later` in this scope
44
LL | something_later!();
55
| ^^^^^^^^^^^^^^^ consider moving the definition of `something_later` before this call
66
|
7-
note: a macro with the same name exists, but it appears later at here
7+
note: a macro with the same name exists, but it appears later
88
--> $DIR/defined-later-issue-121061-2.rs:6:18
99
|
1010
LL | macro_rules! something_later {

tests/ui/macros/defined-later-issue-121061.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: cannot find macro `something_later` in this scope
44
LL | something_later!();
55
| ^^^^^^^^^^^^^^^ consider moving the definition of `something_later` before this call
66
|
7-
note: a macro with the same name exists, but it appears later at here
7+
note: a macro with the same name exists, but it appears later
88
--> $DIR/defined-later-issue-121061.rs:5:14
99
|
1010
LL | macro_rules! something_later {

tests/ui/macros/macro-rules-as-derive-or-attr-issue-132928.stderr

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
error: cannot find derive macro `sample` in this scope
22
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:6:10
33
|
4+
LL | macro_rules! sample { () => {} }
5+
| ------ `sample` exists, but is not an derive macro
6+
...
47
LL | #[derive(sample)]
5-
| ^^^^^^ consider moving the definition of `sample` before this call
8+
| ^^^^^^
69
|
7-
note: a macro with the same name exists, but it appears later at here
10+
help: `macro_rules!` macros cannot be used as derive macros
811
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:3:14
912
|
1013
LL | macro_rules! sample { () => {} }
@@ -13,10 +16,13 @@ LL | macro_rules! sample { () => {} }
1316
error: cannot find attribute `sample` in this scope
1417
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:5:3
1518
|
19+
LL | macro_rules! sample { () => {} }
20+
| ------ `sample` exists, but is not an attribute macro
21+
LL |
1622
LL | #[sample]
17-
| ^^^^^^ consider moving the definition of `sample` before this call
23+
| ^^^^^^
1824
|
19-
note: a macro with the same name exists, but it appears later at here
25+
help: `macro_rules!` macros cannot be used as attribute macros
2026
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:3:14
2127
|
2228
LL | macro_rules! sample { () => {} }
@@ -25,10 +31,13 @@ LL | macro_rules! sample { () => {} }
2531
error: cannot find derive macro `sample` in this scope
2632
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:6:10
2733
|
34+
LL | macro_rules! sample { () => {} }
35+
| ------ `sample` exists, but is not an derive macro
36+
...
2837
LL | #[derive(sample)]
29-
| ^^^^^^ consider moving the definition of `sample` before this call
38+
| ^^^^^^
3039
|
31-
note: a macro with the same name exists, but it appears later at here
40+
help: `macro_rules!` macros cannot be used as derive macros
3241
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:3:14
3342
|
3443
LL | macro_rules! sample { () => {} }
@@ -38,10 +47,13 @@ LL | macro_rules! sample { () => {} }
3847
error: cannot find derive macro `sample` in this scope
3948
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:6:10
4049
|
50+
LL | macro_rules! sample { () => {} }
51+
| ------ `sample` exists, but is not an derive macro
52+
...
4153
LL | #[derive(sample)]
42-
| ^^^^^^ consider moving the definition of `sample` before this call
54+
| ^^^^^^
4355
|
44-
note: a macro with the same name exists, but it appears later at here
56+
help: `macro_rules!` macros cannot be used as derive macros
4557
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:3:14
4658
|
4759
LL | macro_rules! sample { () => {} }

0 commit comments

Comments
 (0)