Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add escaping of pound(#) with double-pound(##) for nested quote! #99

Closed

Conversation

siavash-hamedani
Copy link

@siavash-hamedani siavash-hamedani commented Mar 20, 2019

let trait_name = ...
quote! {
    #[proc_macro_derive(#trait_name)]
    ...
    let derivee = ...
    ...
    quote!{
        impl #trait_name for ## derivee {
            ...
        }
    }
}

Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I would prefer not to accept this because it complicates how users need to think about # inside of quote. Notice that currently the # that appear in ordinary Rust syntax do not require escaping in quote -- for example notice the first # in your PR description: #[proc_macro_derive(...)]. As soon as we provide an explicit escape sequence, too-savvy users would start writing this as ##[proc_macro_derive(...)] and damage readability.

Using quote to generate quote invocations is an uncommon case that I am not interested in catering to. It is already supported in a way that is maybe even more readable, by writing:

struct Interp(&'static str);

impl ToTokens for Interp {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        tokens.append(Punct::new('#', Spacing::Alone));
        tokens.append(Ident::new(self.0, Span::call_site()));
    }
}

let a = Interp("a");
let q = quote! { quote!(#a) };
assert_eq!("quote ! ( # a )", q.to_string());

As a secondary concern, this change would break the meaning of existing working code.

let v = quote!(X);
let q = quote!(# #v);
assert_eq!("# X", q.to_string()); // before

// after this PR it would be "# v"

@dtolnay dtolnay closed this Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants