From 8522140caafdec5c63308bbe9aa9f5ce5e6b1863 Mon Sep 17 00:00:00 2001 From: clubby789 Date: Tue, 12 Nov 2024 14:05:42 +0000 Subject: [PATCH 1/2] Add test for using macro_rules macro as attribute/derive --- ...ro-rules-as-derive-or-attr-issue-132928.rs | 9 ++++ ...ules-as-derive-or-attr-issue-132928.stderr | 52 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 tests/ui/macros/macro-rules-as-derive-or-attr-issue-132928.rs create mode 100644 tests/ui/macros/macro-rules-as-derive-or-attr-issue-132928.stderr diff --git a/tests/ui/macros/macro-rules-as-derive-or-attr-issue-132928.rs b/tests/ui/macros/macro-rules-as-derive-or-attr-issue-132928.rs new file mode 100644 index 0000000000000..a2e1398c61e66 --- /dev/null +++ b/tests/ui/macros/macro-rules-as-derive-or-attr-issue-132928.rs @@ -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 {} diff --git a/tests/ui/macros/macro-rules-as-derive-or-attr-issue-132928.stderr b/tests/ui/macros/macro-rules-as-derive-or-attr-issue-132928.stderr new file mode 100644 index 0000000000000..fb7b649d32fa5 --- /dev/null +++ b/tests/ui/macros/macro-rules-as-derive-or-attr-issue-132928.stderr @@ -0,0 +1,52 @@ +error: cannot find derive macro `sample` in this scope + --> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:6:10 + | +LL | #[derive(sample)] + | ^^^^^^ consider moving the definition of `sample` before this call + | +note: a macro with the same name exists, but it appears later at here + --> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:3:14 + | +LL | macro_rules! sample { () => {} } + | ^^^^^^ + +error: cannot find attribute `sample` in this scope + --> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:5:3 + | +LL | #[sample] + | ^^^^^^ consider moving the definition of `sample` before this call + | +note: a macro with the same name exists, but it appears later at here + --> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:3:14 + | +LL | macro_rules! sample { () => {} } + | ^^^^^^ + +error: cannot find derive macro `sample` in this scope + --> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:6:10 + | +LL | #[derive(sample)] + | ^^^^^^ consider moving the definition of `sample` before this call + | +note: a macro with the same name exists, but it appears later at here + --> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:3:14 + | +LL | macro_rules! 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 | #[derive(sample)] + | ^^^^^^ consider moving the definition of `sample` before this call + | +note: a macro with the same name exists, but it appears later at here + --> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:3:14 + | +LL | macro_rules! sample { () => {} } + | ^^^^^^ + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + +error: aborting due to 4 previous errors + From 4627db2a107891da1999af06729e3422af773b56 Mon Sep 17 00:00:00 2001 From: clubby789 Date: Tue, 12 Nov 2024 14:13:58 +0000 Subject: [PATCH 2/2] Diagnostic for using macro_rules macro as attr/derive --- compiler/rustc_resolve/messages.ftl | 8 +++- compiler/rustc_resolve/src/diagnostics.rs | 18 ++++++-- compiler/rustc_resolve/src/errors.rs | 16 +++++++ .../defined-later-issue-121061-2.stderr | 2 +- .../macros/defined-later-issue-121061.stderr | 2 +- ...ules-as-derive-or-attr-issue-132928.stderr | 42 +++++++------------ 6 files changed, 56 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_resolve/messages.ftl b/compiler/rustc_resolve/messages.ftl index 6602c7889691f..b13de2875bc20 100644 --- a/compiler/rustc_resolve/messages.ftl +++ b/compiler/rustc_resolve/messages.ftl @@ -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` diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 5437ca65935f8..7bcab3f1c43d6 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -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}; @@ -1475,8 +1476,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; } } diff --git a/compiler/rustc_resolve/src/errors.rs b/compiler/rustc_resolve/src/errors.rs index f605b7096f0c2..24f5a812a82cd 100644 --- a/compiler/rustc_resolve/src/errors.rs +++ b/compiler/rustc_resolve/src/errors.rs @@ -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 { diff --git a/tests/ui/macros/defined-later-issue-121061-2.stderr b/tests/ui/macros/defined-later-issue-121061-2.stderr index aa6ef33853193..2ec590d46ed2c 100644 --- a/tests/ui/macros/defined-later-issue-121061-2.stderr +++ b/tests/ui/macros/defined-later-issue-121061-2.stderr @@ -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 { diff --git a/tests/ui/macros/defined-later-issue-121061.stderr b/tests/ui/macros/defined-later-issue-121061.stderr index 65cb53432a932..7b3496991afec 100644 --- a/tests/ui/macros/defined-later-issue-121061.stderr +++ b/tests/ui/macros/defined-later-issue-121061.stderr @@ -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 { diff --git a/tests/ui/macros/macro-rules-as-derive-or-attr-issue-132928.stderr b/tests/ui/macros/macro-rules-as-derive-or-attr-issue-132928.stderr index fb7b649d32fa5..e5b913b208dca 100644 --- a/tests/ui/macros/macro-rules-as-derive-or-attr-issue-132928.stderr +++ b/tests/ui/macros/macro-rules-as-derive-or-attr-issue-132928.stderr @@ -1,51 +1,41 @@ error: cannot find derive macro `sample` in this scope --> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:6:10 | -LL | #[derive(sample)] - | ^^^^^^ consider moving the definition of `sample` before this call - | -note: a macro with the same name exists, but it appears later at here - --> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:3:14 - | 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 | #[sample] - | ^^^^^^ consider moving the definition of `sample` before this call - | -note: a macro with the same name exists, but it appears later at here - --> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:3:14 - | 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)] - | ^^^^^^ consider moving the definition of `sample` before this call + | ^^^^^^ | -note: a macro with the same name exists, but it appears later at here - --> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:3:14 - | -LL | macro_rules! 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)] - | ^^^^^^ consider moving the definition of `sample` before this call - | -note: a macro with the same name exists, but it appears later at here - --> $DIR/macro-rules-as-derive-or-attr-issue-132928.rs:3:14 + | ^^^^^^ | -LL | macro_rules! sample { () => {} } - | ^^^^^^ = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` error: aborting due to 4 previous errors