Skip to content

Commit

Permalink
Rollup merge of #132180 - Urgau:ast_pretty-unsafe-attr, r=compiler-er…
Browse files Browse the repository at this point in the history
…rors

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)
  • Loading branch information
jieyouxu authored Oct 26, 2024
2 parents 656a2ec + f5b6f93 commit 50e78b8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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();
}

Expand Down
11 changes: 11 additions & 0 deletions tests/ui/unpretty/unsafe-attr.rs
Original file line number Diff line number Diff line change
@@ -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() {}
11 changes: 11 additions & 0 deletions tests/ui/unpretty/unsafe-attr.stdout
Original file line number Diff line number Diff line change
@@ -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() {}

0 comments on commit 50e78b8

Please sign in to comment.