Skip to content

Instantly share code, notes, and snippets.

@raphlinus
Last active September 25, 2020 16:38
Show Gist options
  • Save raphlinus/6127e08691cb1147d42efac6ac898b06 to your computer and use it in GitHub Desktop.
Save raphlinus/6127e08691cb1147d42efac6ac898b06 to your computer and use it in GitHub Desktop.
Test of whether attribute macros can emit attributes
extern crate proc_macro;
use proc_macro::TokenStream;
#[proc_macro_attribute]
pub fn widget(attr: TokenStream, item: TokenStream) -> TokenStream {
r#"
#[track_caller]
fn answer() -> u32 {
println!("{:?}", std::panic::Location::caller());
42
}"#.parse().unwrap()
}
extern crate attrib_derive;
use attrib_derive::widget;
#[widget]
fn answer() {
// Body isn't actually parsed.
}
fn main() {
println!("{}", answer());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment