Skip to content

Commit

Permalink
Unrolled build for rust-lang#132949
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rust-timer authored Nov 23, 2024
2 parents ff1737b + 4627db2 commit 2852c95
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 6 deletions.
8 changes: 7 additions & 1 deletion compiler/rustc_resolve/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,14 @@ resolve_lowercase_self =
attempt to use a non-constant value in a constant
.suggestion = try using `Self`
resolve_macro_cannot_use_as_attr =
`{$ident}` exists, but a declarative macro cannot be used as an attribute macro
resolve_macro_cannot_use_as_derive =
`{$ident}` exists, but a declarative macro cannot be used as a derive macro
resolve_macro_defined_later =
a macro with the same name exists, but it appears later at here
a macro with the same name exists, but it appears later
resolve_macro_expanded_extern_crate_cannot_shadow_extern_arguments =
macro-expanded `extern crate` items cannot shadow names passed with `--extern`
Expand Down
18 changes: 15 additions & 3 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ use tracing::debug;

use crate::errors::{
self, AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion, ConsiderAddingADerive,
ExplicitUnsafeTraits, MacroDefinedLater, MacroSuggMovePosition, MaybeMissingMacroRulesName,
ExplicitUnsafeTraits, MacroDefinedLater, MacroRulesNot, MacroSuggMovePosition,
MaybeMissingMacroRulesName,
};
use crate::imports::{Import, ImportKind};
use crate::late::{PatternSource, Rib};
Expand Down Expand Up @@ -1473,8 +1474,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let scope = self.local_macro_def_scopes[&def_id];
let parent_nearest = parent_scope.module.nearest_parent_mod();
if Some(parent_nearest) == scope.opt_def_id() {
err.subdiagnostic(MacroDefinedLater { span: unused_ident.span });
err.subdiagnostic(MacroSuggMovePosition { span: ident.span, ident });
match macro_kind {
MacroKind::Bang => {
err.subdiagnostic(MacroDefinedLater { span: unused_ident.span });
err.subdiagnostic(MacroSuggMovePosition { span: ident.span, ident });
}
MacroKind::Attr => {
err.subdiagnostic(MacroRulesNot::Attr { span: unused_ident.span, ident });
}
MacroKind::Derive => {
err.subdiagnostic(MacroRulesNot::Derive { span: unused_ident.span, ident });
}
}

return;
}
}
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,22 @@ pub(crate) struct MacroSuggMovePosition {
pub(crate) ident: Ident,
}

#[derive(Subdiagnostic)]
pub(crate) enum MacroRulesNot {
#[label(resolve_macro_cannot_use_as_attr)]
Attr {
#[primary_span]
span: Span,
ident: Ident,
},
#[label(resolve_macro_cannot_use_as_derive)]
Derive {
#[primary_span]
span: Span,
ident: Ident,
},
}

#[derive(Subdiagnostic)]
#[note(resolve_missing_macro_rules_name)]
pub(crate) struct MaybeMissingMacroRulesName {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/defined-later-issue-121061-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: cannot find macro `something_later` in this scope
LL | something_later!();
| ^^^^^^^^^^^^^^^ consider moving the definition of `something_later` before this call
|
note: a macro with the same name exists, but it appears later at here
note: a macro with the same name exists, but it appears later
--> $DIR/defined-later-issue-121061-2.rs:6:18
|
LL | macro_rules! something_later {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/defined-later-issue-121061.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: cannot find macro `something_later` in this scope
LL | something_later!();
| ^^^^^^^^^^^^^^^ consider moving the definition of `something_later` before this call
|
note: a macro with the same name exists, but it appears later at here
note: a macro with the same name exists, but it appears later
--> $DIR/defined-later-issue-121061.rs:5:14
|
LL | macro_rules! something_later {
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/macros/macro-rules-as-derive-or-attr-issue-132928.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![crate_type = "lib"]

macro_rules! sample { () => {} }

#[sample] //~ ERROR cannot find attribute `sample` in this scope
#[derive(sample)] //~ ERROR cannot find derive macro `sample` in this scope
//~| ERROR cannot find derive macro `sample` in this scope
//~| ERROR cannot find derive macro `sample` in this scope
pub struct S {}
42 changes: 42 additions & 0 deletions tests/ui/macros/macro-rules-as-derive-or-attr-issue-132928.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
error: cannot find derive macro `sample` in this scope
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:6:10
|
LL | macro_rules! sample { () => {} }
| ------ `sample` exists, but a declarative macro cannot be used as a derive macro
...
LL | #[derive(sample)]
| ^^^^^^

error: cannot find attribute `sample` in this scope
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:5:3
|
LL | macro_rules! sample { () => {} }
| ------ `sample` exists, but a declarative macro cannot be used as an attribute macro
LL |
LL | #[sample]
| ^^^^^^

error: cannot find derive macro `sample` in this scope
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:6:10
|
LL | macro_rules! sample { () => {} }
| ------ `sample` exists, but a declarative macro cannot be used as a derive macro
...
LL | #[derive(sample)]
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: cannot find derive macro `sample` in this scope
--> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:6:10
|
LL | macro_rules! sample { () => {} }
| ------ `sample` exists, but a declarative macro cannot be used as a derive macro
...
LL | #[derive(sample)]
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: aborting due to 4 previous errors

0 comments on commit 2852c95

Please sign in to comment.