Fix pretty printing of parsed attrs in hir_pretty

This commit is contained in:
Michael Goulet 2025-03-05 15:20:16 +00:00
parent 6a38322d26
commit 279377f87a
8 changed files with 34 additions and 32 deletions

View file

@ -125,7 +125,7 @@ macro_rules! print_tup {
let ($t, $($ts),*) = self; let ($t, $($ts),*) = self;
let parens = print_tup!(num_should_render $t $($ts)*) > 1; let parens = print_tup!(num_should_render $t $($ts)*) > 1;
if parens { if parens {
p.word("("); p.popen();
} }
let mut printed_anything = $t.should_render(); let mut printed_anything = $t.should_render();
@ -134,14 +134,16 @@ macro_rules! print_tup {
$( $(
if $ts.should_render() { if $ts.should_render() {
p.word_space(","); if printed_anything {
p.word_space(",");
}
printed_anything = true; printed_anything = true;
} }
$ts.print_attribute(p); $ts.print_attribute(p);
)* )*
if parens { if parens {
p.word(")"); p.pclose();
} }
} }
} }
@ -152,8 +154,8 @@ macro_rules! print_tup {
print_tup!(A B C D E F G H); print_tup!(A B C D E F G H);
print_skip!(Span, ()); print_skip!(Span, ());
print_disp!(Symbol, u16, bool, NonZero<u32>); print_disp!(u16, bool, NonZero<u32>);
print_debug!(UintTy, IntTy, Align, AttrStyle, CommentKind, Transparency); print_debug!(Symbol, UintTy, IntTy, Align, AttrStyle, CommentKind, Transparency);
/// Finds attributes in sequences of attributes by pattern matching. /// Finds attributes in sequences of attributes by pattern matching.
/// ///

View file

@ -117,9 +117,9 @@ impl<'a> State<'a> {
self.hardbreak() self.hardbreak()
} }
hir::Attribute::Parsed(pa) => { hir::Attribute::Parsed(pa) => {
self.word("#[attr=\""); self.word("#[attr = ");
pa.print_attribute(self); pa.print_attribute(self);
self.word("\")]"); self.word("]");
self.hardbreak() self.hardbreak()
} }
} }

View file

@ -16,12 +16,14 @@ fn print_fields(name: &Ident, fields: &Fields) -> (TokenStream, TokenStream, Tok
let name = field.ident.as_ref().unwrap(); let name = field.ident.as_ref().unwrap();
let string_name = name.to_string(); let string_name = name.to_string();
disps.push(quote! { disps.push(quote! {
if __printed_anything && #name.should_render() { if #name.should_render() {
__p.word_space(","); if __printed_anything {
__p.word_space(",");
}
__p.word(#string_name);
__p.word_space(":");
__printed_anything = true; __printed_anything = true;
} }
__p.word(#string_name);
__p.word_space(":");
#name.print_attribute(__p); #name.print_attribute(__p);
}); });
field_names.push(name); field_names.push(name);
@ -35,6 +37,7 @@ fn print_fields(name: &Ident, fields: &Fields) -> (TokenStream, TokenStream, Tok
return; return;
} }
__p.nbsp();
__p.word("{"); __p.word("{");
#(#disps)* #(#disps)*
__p.word("}"); __p.word("}");
@ -48,8 +51,10 @@ fn print_fields(name: &Ident, fields: &Fields) -> (TokenStream, TokenStream, Tok
for idx in 0..fields_unnamed.unnamed.len() { for idx in 0..fields_unnamed.unnamed.len() {
let name = format_ident!("f{idx}"); let name = format_ident!("f{idx}");
disps.push(quote! { disps.push(quote! {
if __printed_anything && #name.should_render() { if #name.should_render() {
__p.word_space(","); if __printed_anything {
__p.word_space(",");
}
__printed_anything = true; __printed_anything = true;
} }
#name.print_attribute(__p); #name.print_attribute(__p);
@ -66,9 +71,9 @@ fn print_fields(name: &Ident, fields: &Fields) -> (TokenStream, TokenStream, Tok
return; return;
} }
__p.word("("); __p.popen();
#(#disps)* #(#disps)*
__p.word(")"); __p.pclose();
}, },
quote! { true }, quote! { true },
) )

View file

@ -6,6 +6,6 @@ extern crate std;
//@ pretty-mode:hir //@ pretty-mode:hir
//@ pp-exact:hir-pretty-attr.pp //@ pp-exact:hir-pretty-attr.pp
#[attr="Repr([ReprC, ReprPacked(Align(4 bytes)), ReprTransparent])")] #[attr = Repr([ReprC, ReprPacked(Align(4 bytes)), ReprTransparent])]
struct Example { struct Example {
} }

View file

@ -1,5 +1,5 @@
#[repr(i32)] #[repr(i32)]
//@ is "$.index[*][?(@.name=='Foo')].attrs" '["#[attr=\"Repr([ReprInt(SignedInt(I32))])\")]\n"]' //@ is "$.index[*][?(@.name=='Foo')].attrs" '["#[attr = Repr([ReprInt(SignedInt(I32))])]\n"]'
pub enum Foo { pub enum Foo {
//@ is "$.index[*][?(@.name=='Struct')].inner.variant.discriminant" null //@ is "$.index[*][?(@.name=='Struct')].inner.variant.discriminant" null
//@ count "$.index[*][?(@.name=='Struct')].inner.variant.kind.struct.fields[*]" 0 //@ count "$.index[*][?(@.name=='Struct')].inner.variant.kind.struct.fields[*]" 0

View file

@ -1,5 +1,5 @@
#[repr(u32)] #[repr(u32)]
//@ is "$.index[*][?(@.name=='Foo')].attrs" '["#[attr=\"Repr([ReprInt(UnsignedInt(U32))])\")]\n"]' //@ is "$.index[*][?(@.name=='Foo')].attrs" '["#[attr = Repr([ReprInt(UnsignedInt(U32))])]\n"]'
pub enum Foo { pub enum Foo {
//@ is "$.index[*][?(@.name=='Tuple')].inner.variant.discriminant" null //@ is "$.index[*][?(@.name=='Tuple')].inner.variant.discriminant" null
//@ count "$.index[*][?(@.name=='Tuple')].inner.variant.kind.tuple[*]" 0 //@ count "$.index[*][?(@.name=='Tuple')].inner.variant.kind.tuple[*]" 0

View file

@ -1,8 +1,6 @@
//@ compile-flags: -Zunpretty=hir //@ compile-flags: -Zunpretty=hir
//@ check-pass //@ check-pass
// FIXME(jdonszelmann): the pretty printing output for deprecated (and possibly more attrs) is
// slightly broken.
#[deprecated] #[deprecated]
pub struct PlainDeprecated; pub struct PlainDeprecated;

View file

@ -5,24 +5,21 @@ extern crate std;
//@ compile-flags: -Zunpretty=hir //@ compile-flags: -Zunpretty=hir
//@ check-pass //@ check-pass
// FIXME(jdonszelmann): the pretty printing output for deprecated (and possibly more attrs) is #[attr = Deprecation {deprecation: Deprecation {since: Unspecified}}]
// slightly broken.
#[attr="Deprecation{deprecation: Deprecation{since: Unspecifiednote:
suggestion: }span: }")]
struct PlainDeprecated; struct PlainDeprecated;
#[attr="Deprecation{deprecation: Deprecation{since: Unspecifiednote: #[attr = Deprecation {deprecation: Deprecation {since: Unspecified, note:
here's why this is deprecatedsuggestion: }span: }")] "here's why this is deprecated"}}]
struct DirectNote; struct DirectNote;
#[attr="Deprecation{deprecation: Deprecation{since: Unspecifiednote: #[attr = Deprecation {deprecation: Deprecation {since: Unspecified, note:
here's why this is deprecatedsuggestion: }span: }")] "here's why this is deprecated"}}]
struct ExplicitNote; struct ExplicitNote;
#[attr="Deprecation{deprecation: Deprecation{since: NonStandard(1.2.3)note: #[attr = Deprecation {deprecation: Deprecation {since: NonStandard("1.2.3"),
here's why this is deprecatedsuggestion: }span: }")] note: "here's why this is deprecated"}}]
struct SinceAndNote; struct SinceAndNote;
#[attr="Deprecation{deprecation: Deprecation{since: NonStandard(1.2.3)note: #[attr = Deprecation {deprecation: Deprecation {since: NonStandard("1.2.3"),
here's why this is deprecatedsuggestion: }span: }")] note: "here's why this is deprecated"}}]
struct FlippedOrder; struct FlippedOrder;