about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPonasKovas <mykolas.peteraitis@gmail.com>2024-11-05 18:55:34 +0200
committerPonasKovas <mykolas.peteraitis@gmail.com>2024-11-11 21:20:02 +0200
commit7c0a7f78a01657c23d23bc4c7e6d3045fd534462 (patch)
treef785acabaf6f401786147093f022fbdba7615622
parent096277e989d6de11c3077472fc05778e261e7b8e (diff)
downloadrust-7c0a7f78a01657c23d23bc4c7e6d3045fd534462.tar.gz
rust-7c0a7f78a01657c23d23bc4c7e6d3045fd534462.zip
remove attributes from generics in built-in derive macros
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 <me@fmease.dev>
-rw-r--r--compiler/rustc_builtin_macros/src/deriving/generic/mod.rs6
-rw-r--r--tests/ui/proc-macro/auxiliary/helper-attr.rs12
-rw-r--r--tests/ui/proc-macro/helper-attr-builtin-derive.rs19
3 files changed, 37 insertions, 0 deletions
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() {}