Skip to content

Commit 2852c95

Browse files
authored
Unrolled build for rust-lang#132949
Rollup merge of rust-lang#132949 - clubby789:macro-rules-attr-derive, r=fmease Add specific diagnostic for using macro_rules macro as attribute/derive Fixes rust-lang#132928
2 parents ff1737b + 4627db2 commit 2852c95

7 files changed

+91
-6
lines changed

compiler/rustc_resolve/messages.ftl

+7-1
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,14 @@ 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 =
261+
`{$ident}` exists, but a declarative macro cannot be used as an attribute macro
262+
263+
resolve_macro_cannot_use_as_derive =
264+
`{$ident}` exists, but a declarative macro cannot be used as a derive macro
265+
260266
resolve_macro_defined_later =
261-
a macro with the same name exists, but it appears later at here
267+
a macro with the same name exists, but it appears later
262268
263269
resolve_macro_expanded_extern_crate_cannot_shadow_extern_arguments =
264270
macro-expanded `extern crate` items cannot shadow names passed with `--extern`

compiler/rustc_resolve/src/diagnostics.rs

+15-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};
@@ -1473,8 +1474,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14731474
let scope = self.local_macro_def_scopes[&def_id];
14741475
let parent_nearest = parent_scope.module.nearest_parent_mod();
14751476
if Some(parent_nearest) == scope.opt_def_id() {
1476-
err.subdiagnostic(MacroDefinedLater { span: unused_ident.span });
1477-
err.subdiagnostic(MacroSuggMovePosition { span: ident.span, ident });
1477+
match macro_kind {
1478+
MacroKind::Bang => {
1479+
err.subdiagnostic(MacroDefinedLater { span: unused_ident.span });
1480+
err.subdiagnostic(MacroSuggMovePosition { span: ident.span, ident });
1481+
}
1482+
MacroKind::Attr => {
1483+
err.subdiagnostic(MacroRulesNot::Attr { span: unused_ident.span, ident });
1484+
}
1485+
MacroKind::Derive => {
1486+
err.subdiagnostic(MacroRulesNot::Derive { span: unused_ident.span, ident });
1487+
}
1488+
}
1489+
14781490
return;
14791491
}
14801492
}

compiler/rustc_resolve/src/errors.rs

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

668+
#[derive(Subdiagnostic)]
669+
pub(crate) enum MacroRulesNot {
670+
#[label(resolve_macro_cannot_use_as_attr)]
671+
Attr {
672+
#[primary_span]
673+
span: Span,
674+
ident: Ident,
675+
},
676+
#[label(resolve_macro_cannot_use_as_derive)]
677+
Derive {
678+
#[primary_span]
679+
span: Span,
680+
ident: Ident,
681+
},
682+
}
683+
668684
#[derive(Subdiagnostic)]
669685
#[note(resolve_missing_macro_rules_name)]
670686
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 {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![crate_type = "lib"]
2+
3+
macro_rules! sample { () => {} }
4+
5+
#[sample] //~ ERROR cannot find attribute `sample` in this scope
6+
#[derive(sample)] //~ ERROR cannot find derive macro `sample` in this scope
7+
//~| ERROR cannot find derive macro `sample` in this scope
8+
//~| ERROR cannot find derive macro `sample` in this scope
9+
pub struct S {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
error: cannot find derive macro `sample` in this scope
2+
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:6:10
3+
|
4+
LL | macro_rules! sample { () => {} }
5+
| ------ `sample` exists, but a declarative macro cannot be used as a derive macro
6+
...
7+
LL | #[derive(sample)]
8+
| ^^^^^^
9+
10+
error: cannot find attribute `sample` in this scope
11+
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:5:3
12+
|
13+
LL | macro_rules! sample { () => {} }
14+
| ------ `sample` exists, but a declarative macro cannot be used as an attribute macro
15+
LL |
16+
LL | #[sample]
17+
| ^^^^^^
18+
19+
error: cannot find derive macro `sample` in this scope
20+
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:6:10
21+
|
22+
LL | macro_rules! sample { () => {} }
23+
| ------ `sample` exists, but a declarative macro cannot be used as a derive macro
24+
...
25+
LL | #[derive(sample)]
26+
| ^^^^^^
27+
|
28+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
29+
30+
error: cannot find derive macro `sample` in this scope
31+
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:6:10
32+
|
33+
LL | macro_rules! sample { () => {} }
34+
| ------ `sample` exists, but a declarative macro cannot be used as a derive macro
35+
...
36+
LL | #[derive(sample)]
37+
| ^^^^^^
38+
|
39+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
40+
41+
error: aborting due to 4 previous errors
42+

0 commit comments

Comments
 (0)