about summary refs log tree commit diff
diff options
context:
space:
mode:
author许杰友 Jieyou Xu (Joe) <39484203+jieyouxu@users.noreply.github.com>2024-10-26 22:01:14 +0800
committerGitHub <noreply@github.com>2024-10-26 22:01:14 +0800
commit50e78b8b3c4de72cd2a156be114bcf73dfde6709 (patch)
treed45f5b02c18994d011efa2dc63cf80c575755289
parent656a2ec0bd1a56fbd93b1c88c4a6e48a8ce9fb0a (diff)
parentf5b6f938cec70d7b6ef3681217ad4c1d84e597e0 (diff)
downloadrust-50e78b8b3c4de72cd2a156be114bcf73dfde6709.tar.gz
rust-50e78b8b3c4de72cd2a156be114bcf73dfde6709.zip
Rollup merge of #132180 - Urgau:ast_pretty-unsafe-attr, r=compiler-errors
Print unsafety of attribute in AST pretty print

This PR fixes the AST pretty print, which was missing the unsafety for unsafe attributes.

Related to https://github.com/rust-lang/rust/pull/131558#discussion_r1807736204
-rw-r--r--compiler/rustc_ast_pretty/src/pprust/state.rs11
-rw-r--r--tests/ui/unpretty/unsafe-attr.rs11
-rw-r--r--tests/ui/unpretty/unsafe-attr.stdout11
3 files changed, 33 insertions, 0 deletions
diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs
index 2cdec2138ad..1e18f0779f0 100644
--- a/compiler/rustc_ast_pretty/src/pprust/state.rs
+++ b/compiler/rustc_ast_pretty/src/pprust/state.rs
@@ -627,6 +627,13 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
 
     fn print_attr_item(&mut self, item: &ast::AttrItem, span: Span) {
         self.ibox(0);
+        match item.unsafety {
+            ast::Safety::Unsafe(_) => {
+                self.word("unsafe");
+                self.popen();
+            }
+            ast::Safety::Default | ast::Safety::Safe(_) => {}
+        }
         match &item.args {
             AttrArgs::Delimited(DelimArgs { dspan: _, delim, tokens }) => self.print_mac_common(
                 Some(MacHeader::Path(&item.path)),
@@ -655,6 +662,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
                 self.word(token_str);
             }
         }
+        match item.unsafety {
+            ast::Safety::Unsafe(_) => self.pclose(),
+            ast::Safety::Default | ast::Safety::Safe(_) => {}
+        }
         self.end();
     }
 
diff --git a/tests/ui/unpretty/unsafe-attr.rs b/tests/ui/unpretty/unsafe-attr.rs
new file mode 100644
index 00000000000..8734ea86b6d
--- /dev/null
+++ b/tests/ui/unpretty/unsafe-attr.rs
@@ -0,0 +1,11 @@
+//@ compile-flags: -Zunpretty=normal
+//@ check-pass
+
+#[no_mangle]
+extern "C" fn foo() {}
+
+#[unsafe(no_mangle)]
+extern "C" fn bar() {}
+
+#[cfg_attr(FALSE, unsafe(no_mangle))]
+extern "C" fn zoo() {}
diff --git a/tests/ui/unpretty/unsafe-attr.stdout b/tests/ui/unpretty/unsafe-attr.stdout
new file mode 100644
index 00000000000..8734ea86b6d
--- /dev/null
+++ b/tests/ui/unpretty/unsafe-attr.stdout
@@ -0,0 +1,11 @@
+//@ compile-flags: -Zunpretty=normal
+//@ check-pass
+
+#[no_mangle]
+extern "C" fn foo() {}
+
+#[unsafe(no_mangle)]
+extern "C" fn bar() {}
+
+#[cfg_attr(FALSE, unsafe(no_mangle))]
+extern "C" fn zoo() {}