Skip to content

Commit 50e78b8

Browse files
authored
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 #131558 (comment)
2 parents 656a2ec + f5b6f93 commit 50e78b8

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+11
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,13 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
627627

628628
fn print_attr_item(&mut self, item: &ast::AttrItem, span: Span) {
629629
self.ibox(0);
630+
match item.unsafety {
631+
ast::Safety::Unsafe(_) => {
632+
self.word("unsafe");
633+
self.popen();
634+
}
635+
ast::Safety::Default | ast::Safety::Safe(_) => {}
636+
}
630637
match &item.args {
631638
AttrArgs::Delimited(DelimArgs { dspan: _, delim, tokens }) => self.print_mac_common(
632639
Some(MacHeader::Path(&item.path)),
@@ -655,6 +662,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
655662
self.word(token_str);
656663
}
657664
}
665+
match item.unsafety {
666+
ast::Safety::Unsafe(_) => self.pclose(),
667+
ast::Safety::Default | ast::Safety::Safe(_) => {}
668+
}
658669
self.end();
659670
}
660671

tests/ui/unpretty/unsafe-attr.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ compile-flags: -Zunpretty=normal
2+
//@ check-pass
3+
4+
#[no_mangle]
5+
extern "C" fn foo() {}
6+
7+
#[unsafe(no_mangle)]
8+
extern "C" fn bar() {}
9+
10+
#[cfg_attr(FALSE, unsafe(no_mangle))]
11+
extern "C" fn zoo() {}

tests/ui/unpretty/unsafe-attr.stdout

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ compile-flags: -Zunpretty=normal
2+
//@ check-pass
3+
4+
#[no_mangle]
5+
extern "C" fn foo() {}
6+
7+
#[unsafe(no_mangle)]
8+
extern "C" fn bar() {}
9+
10+
#[cfg_attr(FALSE, unsafe(no_mangle))]
11+
extern "C" fn zoo() {}

0 commit comments

Comments
 (0)