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
This commit is contained in:
许杰友 Jieyou Xu (Joe) 2024-10-26 22:01:14 +08:00 committed by GitHub
commit 50e78b8b3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 0 deletions

View File

@ -627,6 +627,13 @@ fn print_attribute_inline(&mut self, attr: &ast::Attribute, is_inline: bool) {
fn print_attr_item(&mut self, item: &ast::AttrItem, span: Span) { fn print_attr_item(&mut self, item: &ast::AttrItem, span: Span) {
self.ibox(0); self.ibox(0);
match item.unsafety {
ast::Safety::Unsafe(_) => {
self.word("unsafe");
self.popen();
}
ast::Safety::Default | ast::Safety::Safe(_) => {}
}
match &item.args { match &item.args {
AttrArgs::Delimited(DelimArgs { dspan: _, delim, tokens }) => self.print_mac_common( AttrArgs::Delimited(DelimArgs { dspan: _, delim, tokens }) => self.print_mac_common(
Some(MacHeader::Path(&item.path)), Some(MacHeader::Path(&item.path)),
@ -655,6 +662,10 @@ fn print_attr_item(&mut self, item: &ast::AttrItem, span: Span) {
self.word(token_str); self.word(token_str);
} }
} }
match item.unsafety {
ast::Safety::Unsafe(_) => self.pclose(),
ast::Safety::Default | ast::Safety::Safe(_) => {}
}
self.end(); self.end();
} }

View File

@ -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() {}

View File

@ -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() {}