|
| 1 | +// force-host |
| 2 | +// no-prefer-dynamic |
| 3 | +#![crate_type = "proc-macro"] |
| 4 | + |
| 5 | +extern crate proc_macro; |
| 6 | + |
| 7 | +use proc_macro::{Group, TokenStream, TokenTree}; |
| 8 | + |
| 9 | +// This macro exists as part of a reproduction of #61963 but without using quote/syn/proc_macro2. |
| 10 | + |
| 11 | +#[proc_macro_derive(DomObject)] |
| 12 | +pub fn expand_token_stream(input: TokenStream) -> TokenStream { |
| 13 | + // Construct a dummy span - `#0 bytes(0..0)` - which is present in the input because |
| 14 | + // of the specially crafted generated tokens in the `attribute-crate` proc-macro. |
| 15 | + let dummy_span = input.clone().into_iter().nth(0).unwrap().span(); |
| 16 | + |
| 17 | + // Define what the macro would output if constructed properly from the source using syn/quote. |
| 18 | + let output: TokenStream = "impl Bar for ((), Qux<Qux<Baz> >) { } |
| 19 | + impl Bar for ((), Box<Bar>) { }".parse().unwrap(); |
| 20 | + |
| 21 | + let mut tokens: Vec<_> = output.into_iter().collect(); |
| 22 | + // Adjust token spans to match the original crate (which would use `quote`). Some of the |
| 23 | + // generated tokens point to the dummy span. |
| 24 | + for token in tokens.iter_mut() { |
| 25 | + if let TokenTree::Group(group) = token { |
| 26 | + let mut tokens: Vec<_> = group.stream().into_iter().collect(); |
| 27 | + for token in tokens.iter_mut().skip(2) { |
| 28 | + token.set_span(dummy_span); |
| 29 | + } |
| 30 | + |
| 31 | + let mut stream = TokenStream::new(); |
| 32 | + stream.extend(tokens); |
| 33 | + *group = Group::new(group.delimiter(), stream); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + let mut output = TokenStream::new(); |
| 38 | + output.extend(tokens); |
| 39 | + output |
| 40 | +} |
0 commit comments