dtolnay / quote

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

Punct spacing is not preserved #253

Closed vldm closed 1 year ago

vldm commented 1 year ago

During testing custom punctuation in syn, i have found that it is impossible to write tests using quote. Debugging lead me to the minimal test:

#[cfg(test)]
mod test {
    use quote::quote;
    use proc_macro2::{TokenTree, Spacing};

    #[test]
    fn it_works() {
        let mut tts = quote!(<>).into_iter();
        let TokenTree::Punct(c) = tts.next().unwrap() else { panic!() };
        assert_eq!(c.spacing(), Spacing::Joint)
    }
}

Output:

thread 'test::it_works' panicked at 'assertion failed: `(left == right)`
  left: `Alone`,
 right: `Joint`', src/lib.rs:10:9

If i understand correctly, Rustc will produce Spacing::Joint for similar token stream.

dtolnay commented 1 year ago

I don't know any other way to implement this, but I would be prepared to consider a PR if someone else has a way.

vldm commented 1 year ago

Thanks, for fast response. Maybe implementation on procedural macro can solve this? Is there any reason to keep quote declarative?

I have found few issues https://github.com/dtolnay/quote/issues/82 https://github.com/dtolnay/quote/issues/113 that mention, that the main purpose is the speed.

Do you have some repo for benchmarks(or guide how one can measure perfomance of its own macro)? (very interested to apply it for my project)