about summary refs log tree commit diff
path: root/tests/ui/macros/macro-rules-attr-error.rs
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-10-02 10:27:49 +0200
committerGitHub <noreply@github.com>2025-10-02 10:27:49 +0200
commitac7beab527861103dd1e1515d534f87956786554 (patch)
tree52d7ecb9c0fd9d077a7a7a897d052734e2629cb7 /tests/ui/macros/macro-rules-attr-error.rs
parent92aac1bdf61642cfc99498ca86e663cf53d2039e (diff)
parent4fc0a0d42a66e9b248fa527f7f063ec7226803dd (diff)
downloadrust-ac7beab527861103dd1e1515d534f87956786554.tar.gz
rust-ac7beab527861103dd1e1515d534f87956786554.zip
Rollup merge of #146535 - joshtriplett:mbe-unsafe-attr, r=petrochenkov
mbe: Implement `unsafe` attribute rules

This implements `unsafe attr` rules for declarative `macro_rules!` attributes, as specified in [RFC 3697](https://github.com/rust-lang/rfcs/pull/3697).

An invocation of an attribute that uses an `unsafe attr` rule requires the `unsafe(attr(...))` syntax.

An invocation of an attribute that uses an ordinary `attr` rule must *not* use the `unsafe(attr(...))` syntax.

`unsafe` is only supported on an `attr` rule, not any other kind of `macro_rules!` rule.

Tracking issue for `macro_rules!` attributes: https://github.com/rust-lang/rust/issues/143547
Diffstat (limited to 'tests/ui/macros/macro-rules-attr-error.rs')
-rw-r--r--tests/ui/macros/macro-rules-attr-error.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/ui/macros/macro-rules-attr-error.rs b/tests/ui/macros/macro-rules-attr-error.rs
index 81eadb6692f..60290b883cb 100644
--- a/tests/ui/macros/macro-rules-attr-error.rs
+++ b/tests/ui/macros/macro-rules-attr-error.rs
@@ -50,3 +50,22 @@ macro_rules! forward_referenced_attr {
 macro_rules! cyclic_attr {
     attr() {} => {}
 }
+
+macro_rules! attr_with_safety {
+    unsafe attr() { struct RequiresUnsafe; } => {};
+    attr() { struct SafeInvocation; } => {};
+}
+
+#[attr_with_safety]
+struct SafeInvocation;
+
+//~v ERROR: unnecessary `unsafe` on safe attribute invocation
+#[unsafe(attr_with_safety)]
+struct SafeInvocation;
+
+//~v ERROR: unsafe attribute invocation requires `unsafe`
+#[attr_with_safety]
+struct RequiresUnsafe;
+
+#[unsafe(attr_with_safety)]
+struct RequiresUnsafe;