diff options
| author | Jana Dönszelmann <jana@donsz.nl> | 2025-02-09 22:50:03 +0100 |
|---|---|---|
| committer | Jana Dönszelmann <jana@donsz.nl> | 2025-02-24 14:31:19 +0100 |
| commit | 95b52d51eac77be1fcc624c2278dcaafa6989b0b (patch) | |
| tree | a952e5797f050dc9a4cfd4cae3e4483e9e83fe55 /compiler/rustc_macros | |
| parent | 309b46ad68d15377edd95836796bceb196b2d1b8 (diff) | |
| download | rust-95b52d51eac77be1fcc624c2278dcaafa6989b0b.tar.gz rust-95b52d51eac77be1fcc624c2278dcaafa6989b0b.zip | |
pretty print hir attributes
Diffstat (limited to 'compiler/rustc_macros')
| -rw-r--r-- | compiler/rustc_macros/src/lib.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_macros/src/print_attribute.rs | 145 |
2 files changed, 154 insertions, 0 deletions
diff --git a/compiler/rustc_macros/src/lib.rs b/compiler/rustc_macros/src/lib.rs index 0df674eb4c9..34fc0f00320 100644 --- a/compiler/rustc_macros/src/lib.rs +++ b/compiler/rustc_macros/src/lib.rs @@ -17,6 +17,7 @@ mod diagnostics; mod extension; mod hash_stable; mod lift; +mod print_attribute; mod query; mod serialize; mod symbols; @@ -175,3 +176,11 @@ decl_derive! { /// The error type is `u32`. try_from::try_from_u32 } +decl_derive! { + [PrintAttribute] => + /// Derives `PrintAttribute` for `AttributeKind`. + /// This macro is pretty specific to `rustc_attr_data_structures` and likely not that useful in + /// other places. It's deriving something close to `Debug` without printing some extraenous + /// things like spans. + print_attribute::print_attribute +} diff --git a/compiler/rustc_macros/src/print_attribute.rs b/compiler/rustc_macros/src/print_attribute.rs new file mode 100644 index 00000000000..3c6e30b851b --- /dev/null +++ b/compiler/rustc_macros/src/print_attribute.rs @@ -0,0 +1,145 @@ +use proc_macro2::TokenStream; +use quote::{format_ident, quote, quote_spanned}; +use syn::spanned::Spanned; +use syn::{Data, Fields, Ident}; +use synstructure::Structure; + +fn print_fields(name: &Ident, fields: &Fields) -> (TokenStream, TokenStream, TokenStream) { + let string_name = name.to_string(); + let mut disps = vec![quote! {let mut __printed_anything = false;}]; + + match fields { + Fields::Named(fields_named) => { + let mut field_names = Vec::new(); + + for field in &fields_named.named { + let name = field.ident.as_ref().unwrap(); + let string_name = name.to_string(); + disps.push(quote! { + if __printed_anything && #name.print_something() { + __p.word_space(","); + __printed_anything = true; + } + __p.word(#string_name); + __p.word_space(":"); + #name.print_attribute(__p); + }); + field_names.push(name); + } + + ( + quote! { {#(#field_names),*} }, + quote! { + __p.word(#string_name); + if true #(&& !#field_names.print_something())* { + return; + } + + __p.word("{"); + #(#disps)* + __p.word("}"); + }, + quote! { true }, + ) + } + Fields::Unnamed(fields_unnamed) => { + let mut field_names = Vec::new(); + + for idx in 0..fields_unnamed.unnamed.len() { + let name = format_ident!("f{idx}"); + disps.push(quote! { + if __printed_anything && #name.print_something() { + __p.word_space(","); + __printed_anything = true; + } + #name.print_attribute(__p); + }); + field_names.push(name); + } + + ( + quote! { (#(#field_names),*) }, + quote! { + __p.word(#string_name); + + if true #(&& !#field_names.print_something())* { + return; + } + + __p.word("("); + #(#disps)* + __p.word(")"); + }, + quote! { true }, + ) + } + Fields::Unit => (quote! {}, quote! { __p.word(#string_name) }, quote! { true }), + } +} + +pub(crate) fn print_attribute(input: Structure<'_>) -> TokenStream { + let span_error = |span, message: &str| { + quote_spanned! { span => const _: () = ::core::compile_error!(#message); } + }; + + // Must be applied to an enum type. + let (code, printed) = match &input.ast().data { + Data::Enum(e) => { + let (arms, printed) = e + .variants + .iter() + .map(|x| { + let ident = &x.ident; + let (pat, code, printed) = print_fields(ident, &x.fields); + + ( + quote! { + Self::#ident #pat => {#code} + }, + quote! { + Self::#ident #pat => {#printed} + }, + ) + }) + .unzip::<_, _, Vec<_>, Vec<_>>(); + + ( + quote! { + match self { + #(#arms)* + } + }, + quote! { + match self { + #(#printed)* + } + }, + ) + } + Data::Struct(s) => { + let (pat, code, printed) = print_fields(&input.ast().ident, &s.fields); + ( + quote! { + let Self #pat = self; + #code + }, + quote! { + let Self #pat = self; + #printed + }, + ) + } + Data::Union(u) => { + return span_error(u.union_token.span(), "can't derive PrintAttribute on unions"); + } + }; + + #[allow(keyword_idents_2024)] + input.gen_impl(quote! { + #[allow(unused)] + gen impl PrintAttribute for @Self { + fn print_something(&self) -> bool { #printed } + fn print_attribute(&self, __p: &mut rustc_ast_pretty::pp::Printer) { #code } + } + }) +} |
