-
Notifications
You must be signed in to change notification settings - Fork 24
Description
Proposal
Problem statement
Context: rust-lang/rust#54722 (comment)
Our version of quote! currently relies on From<TokenStream>, which has some limitations:
- Individual tokens such as
Ident,Literalcannot be quoted directly; they need to be converted to aTokenStreamoutside of thequote!invocation first - Literals such as
u32,&str, etc cannot be quoted directly - References such as
&TokenStreamcannot be quoted; they need to be cloned outside ofpm::quote!
These are pretty notable limitations compared to quote::quote!, for which all of the above work.
Motivating examples or use cases
The following does not work:
let x = pm::Ident::new("foo", pm::Span::call_site());
let _ = pm::quote! {
// ERROR: `From<proc_macro::Ident>` is not implemented for `proc_macro::TokenStream`
let $x = 199;
};Reusing the same item is inconvenient:
let x1: pm::TokenStream = pm::Ident::new("foo", pm::Span::call_site()).into();
let x2 = x1.clone();
let _ = pm::quote! {
let mut $x1 = 199;
$x2 += 10;
};Solution sketch
-
Add
proc_macro::ToTokensthat is identical toquote::ToTokens:pub trait ToTokens { fn to_tokens(&self, tokens: &mut TokenStream); fn to_token_stream(&self) -> TokenStream { ... } fn into_token_stream(self) -> TokenStream where Self: Sized { ... } }
-
Implement this for most types that have
quote::ToTokens, including:- Aggregate token types
TokenTree,TokenStream - Single token types
Literal,Ident,Punct,Group - Indirect types where
T: ToTokens:&T&mut TBox<T>Rc<T>Option<T>Cow<T> - Types that can create
Literals: integer and float primitives,bool,char,str,String,CStr,CString - Ecosystem:
proc_macro2will be able to implementpm::ToTokenson its token types so they are accepted bypm::quote!
- Aggregate token types
-
Change
pm::quoteto make use ofToTokenswhen it expands, rather thanFrom -
Reimplement some
TokenStreamconversion traits in terms ofToTokensif this can be done without breakage// currently `Extend<TokenStream>` and `Extend<TokenTree>` impl Extend<T: ToTokens> for TokenStream { /* ... */ } // currently `FromIterator<TokenStream>` and `FromIterator<TokenTree>` impl FromIterator<T: ToTokens> for TokenStream { /* ... */ }
This does some of the job
quote::TokenStreamExt.
Alternatives
- Continue to rely on
From<X> for TokenStreambut add this impl more of the types listed above. Having a dedicated trait seems less fragile, especially for literal types. - Omitting
to_token_streamandinto_token_streamfromToTokensor gating them separately since they are just for convenience, rather than helping to solve a - A different name;
ToTokensdoesn't seem quite accurate, but I don't know what would be better (ToTokenStream?ExtendTokenStream? Those seem a bit clunky). - Considering
impl<T: ToTokens> ToTokens for Tis provided, shouldto_tokenstakeselfby value rather than by reference so cloning isn't always necessary? (fn to_tokens(self, tokens: &mut TokenStream))
Links and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.