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

Use Ident::new_raw when it's available #223

Closed
mystor opened this issue Jun 19, 2022 · 0 comments · Fixed by dtolnay/proc-macro2#331 or #225
Closed

Use Ident::new_raw when it's available #223

mystor opened this issue Jun 19, 2022 · 0 comments · Fixed by dtolnay/proc-macro2#331 or #225

Comments

@mystor
Copy link
Collaborator

mystor commented Jun 19, 2022

The Ident::new_raw method has been stable since rustc 1.47.0 (https://doc.rust-lang.org/nightly/proc_macro/struct.Ident.html#method.new_raw). The logic for pushing raw identifiers into a TokenStream still uses TokenStream::from_str however, and could be updated to call this new_raw on versions where it's available.

quote/src/runtime.rs

Lines 192 to 202 in 7d5d360

pub fn push_ident(tokens: &mut TokenStream, s: &str) {
// Optimization over `mk_ident`, as `s` is guaranteed to be a valid ident.
//
// FIXME: When `Ident::new_raw` becomes stable, this method should be
// updated to call it when available.
if s.starts_with("r#") {
parse(tokens, s);
} else {
tokens.append(Ident::new(s, Span::call_site()));
}
}

quote/src/runtime.rs

Lines 204 to 214 in 7d5d360

pub fn push_ident_spanned(tokens: &mut TokenStream, span: Span, s: &str) {
// Optimization over `mk_ident`, as `s` is guaranteed to be a valid ident.
//
// FIXME: When `Ident::new_raw` becomes stable, this method should be
// updated to call it when available.
if s.starts_with("r#") {
parse_spanned(tokens, span, s);
} else {
tokens.append(Ident::new(s, span));
}
}

quote/src/runtime.rs

Lines 322 to 335 in 7d5d360

// At this point, the identifier is raw, and the unraw-ed version of it was
// successfully converted into an identifier. Try to produce a valid raw
// identifier by running the `TokenStream` parser, and unwrapping the first
// token as an `Ident`.
//
// FIXME: When `Ident::new_raw` becomes stable, this method should be
// updated to call it when available.
if let Ok(ts) = id.parse::<TokenStream>() {
let mut iter = ts.into_iter();
if let (Some(TokenTree::Ident(mut id)), None) = (iter.next(), iter.next()) {
id.set_span(span);
return id;
}
}

mystor added a commit to mystor/quote that referenced this issue Jun 20, 2022
This requires the changes in
dtolnay/proc-macro2#331 which expose
Ident::new_raw from proc-macro2, along with providing a fallback for
earlier versions of Rust.

Fixes dtolnay#223
@dtolnay dtolnay reopened this Jun 20, 2022
dtolnay pushed a commit to mystor/quote that referenced this issue Jun 20, 2022
This requires the changes in
dtolnay/proc-macro2#331 which expose
Ident::new_raw from proc-macro2, along with providing a fallback for
earlier versions of Rust.

Fixes dtolnay#223
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants