From 7c0a7f78a01657c23d23bc4c7e6d3045fd534462 Mon Sep 17 00:00:00 2001 From: PonasKovas Date: Tue, 5 Nov 2024 18:55:34 +0200 Subject: [PATCH] remove attributes from generics in built-in derive macros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add a test add github issue link to description of the test replace new ThinVec with clear() Co-authored-by: León Orell Valerian Liehr --- .../src/deriving/generic/mod.rs | 6 ++++++ tests/ui/proc-macro/auxiliary/helper-attr.rs | 12 ++++++++++++ .../proc-macro/helper-attr-builtin-derive.rs | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 tests/ui/proc-macro/auxiliary/helper-attr.rs create mode 100644 tests/ui/proc-macro/helper-attr-builtin-derive.rs diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index 82baaca9a46..79c198ed2d0 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -680,6 +680,12 @@ impl<'a> TraitDef<'a> { param_clone } }) + .map(|mut param| { + // Remove all attributes, because there might be helper attributes + // from other macros that will not be valid in the expanded implementation. + param.attrs.clear(); + param + }) .collect(); // and similarly for where clauses diff --git a/tests/ui/proc-macro/auxiliary/helper-attr.rs b/tests/ui/proc-macro/auxiliary/helper-attr.rs new file mode 100644 index 00000000000..79ccefd9844 --- /dev/null +++ b/tests/ui/proc-macro/auxiliary/helper-attr.rs @@ -0,0 +1,12 @@ +//@ force-host +//@ no-prefer-dynamic + +#![crate_type = "proc-macro"] + +extern crate proc_macro; + +// Doesn't do anything, but has a helper attribute. +#[proc_macro_derive(WithHelperAttr, attributes(x))] +pub fn derive(_input: proc_macro::TokenStream) -> proc_macro::TokenStream { + proc_macro::TokenStream::new() +} diff --git a/tests/ui/proc-macro/helper-attr-builtin-derive.rs b/tests/ui/proc-macro/helper-attr-builtin-derive.rs new file mode 100644 index 00000000000..eb7292e093c --- /dev/null +++ b/tests/ui/proc-macro/helper-attr-builtin-derive.rs @@ -0,0 +1,19 @@ +// This test checks that helper attributes of a derive proc macro can be used together with +// other built-in derive macros. +// issue: rust-lang/rust#132561 +//@ check-pass +//@ aux-build:helper-attr.rs +//@ edition:2021 + +#[macro_use] +extern crate helper_attr; + +use helper_attr::WithHelperAttr; + +#[derive(WithHelperAttr, Debug, Clone, PartialEq)] +struct MyStruct<#[x] 'a, #[x] const A: usize, #[x] B> { + #[x] + field: &'a [B; A], +} + +fn main() {}