dtolnay / quote

Rust quasi-quoting
Apache License 2.0
1.32k stars 90 forks source link

misplaced \n #269

Closed doubleailes closed 9 months ago

doubleailes commented 9 months ago

I think quote macro quote! misplace the \n

I am rebuilding a Vec<syn::Stmt>

When i'am printing the content of the vector i have this result

let s : String = serde_json :: to_value(data [0usize].clone()) ? ;
let output = format! ("Hello, {}!", s) ;
return Ok(serde_json :: to_value(output) ?) ;

But when i use print quote! for the whole ItemFn this is the result

"fn hello(data : Vec < & Value >) -> girolle :: Result < Value >\n{\n let s : String = serde_json :: to_value(data [0usize].clone()) ? ; let\n output = format! (\"Hello, {}!\", s) ; return\n Ok(serde_json :: to_value(output) ?) ;\n}"

As you can for example the \n is misplaced between the let and output

Here is my implementation of the macro

pub(crate) fn main(input: TokenStream) -> TokenStream {
    // proc_marco2 version of "parse_macro_input!(input as ItemFn)"
    let old_item_fn = match parse2::<ItemFn>(input) {
        Ok(syntax_tree) => syntax_tree,
        Err(error) => return error.to_compile_error(),
    };
    let mut task = Task::new();
    let mut new_item_fn = task.fold_item_fn(old_item_fn);
    task.add_input_serialize();
    task.add_output_serialize();
    task.add_output_final();
    new_item_fn.block.stmts = task.inner_statements.clone();
    //println!("numbers of args {}", task.args.len());
    println!("{:#?}", quote!(#new_item_fn).to_string());
    TokenStream::from(quote!(#new_item_fn))
}

You can find the whole code https://github.com/doubleailes/girolle/blob/7e9261d62834aba32159c51a01529fede371f136/girolle_macro/src/entry.rs

dtolnay commented 9 months ago

I think this is behaving correctly. Quote produces a TokenStream. TokenStream does not hold information about whitespace, only the tokens.