Skip to content

Instantly share code, notes, and snippets.

@jcaesar
Last active October 6, 2022 13:35
Show Gist options
  • Save jcaesar/1bfd102d5328cb87ac60b2eaed8e1922 to your computer and use it in GitHub Desktop.
Save jcaesar/1bfd102d5328cb87ac60b2eaed8e1922 to your computer and use it in GitHub Desktop.
By the grace of Lord dtolnay
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "macros"
version = "0.1.0"
dependencies = [
"proc-macro2",
"quote",
"syn",
"venial",
]
[[package]]
name = "proc-macro2"
version = "1.0.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94e2ef8dbfc347b10c094890f778ee2e36ca9bb4262e86dc99cd217e35f3470b"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "1.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e90cde112c4b9690b8cbe810cba9ddd8bc1d7472e2cae317b69e9438c1cba7d2"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd"
[[package]]
name = "venial"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3d723d4b0ebf523971bc63776095798272df2873910f16d53bb680aa2f4d609"
dependencies = [
"proc-macro2",
"quote",
]
[package]
name = "macros"
version = "0.1.0"
edition = "2021"
[lib]
proc-macro = true
path = "macros.rs"
[[bin]]
name = "repro"
path = "repro.rs"
[dependencies]
proc-macro2 = "1.0.46"
quote = "1.0.21"
syn = "1.0.101"
venial = "0.4.0"
use proc_macro::TokenStream;
#[proc_macro]
pub fn repro(item: TokenStream) -> TokenStream {
eprintln!("{item:#?}");
venial::parse_declaration(item.into()).expect("Won't even get here");
Default::default()
}
#[proc_macro]
pub fn non_repro_syn(input: TokenStream) -> TokenStream {
syn::parse_macro_input!(input as syn::DeriveInput);
Default::default()
}
#[proc_macro]
pub fn non_repro_normalize(item: TokenStream) -> TokenStream {
let item = normalize(item.into());
eprintln!("{item:#?}");
venial::parse_declaration(item).expect("Won't even get here");
Default::default()
}
fn normalize(ts: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
ts.into_iter()
.flat_map(|tt| match tt {
proc_macro2::TokenTree::Group(g) if g.delimiter() == proc_macro2::Delimiter::None => {
normalize(g.stream())
}
proc_macro2::TokenTree::Group(group) => {
let inner = normalize(group.stream());
let mut ngroup = proc_macro2::Group::new(group.delimiter(), inner);
ngroup.set_span(group.span());
[proc_macro2::TokenTree::Group(ngroup)]
.into_iter()
.collect()
}
x => [x].into_iter().collect(),
})
.collect()
}
macro_rules! repro {
($visibility:vis) => {
macros::repro! {
$visibility struct Struct {
$visibility member: Type,
}
}
};
}
repro!(pub);
fn main() {
println!("Hello, world!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment