Skip to content

Commit

Permalink
Distinguish boolean literals more precisely
Browse files Browse the repository at this point in the history
This avoids taking the slow path for things like `0usize` or `0x7e`.
  • Loading branch information
dtolnay committed Dec 27, 2021
1 parent f7768f4 commit 6e6bf56
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/runtime.rs
Expand Up @@ -281,7 +281,7 @@ pub fn push_lifetime_spanned(tokens: &mut TokenStream, span: Span, lifetime: &st
pub fn push_literal(tokens: &mut TokenStream, repr: &str) {
// Macro_rules's $literal matcher also matches `true`, `-true`, `false`,
// `-false` which are not considered valid values for a proc_macro::Literal.
if repr.ends_with('e') {
if is_boolean_literal(repr) {
parse(tokens, repr);
} else {
let literal = unsafe { Literal::from_str_unchecked(repr) };
Expand All @@ -290,7 +290,7 @@ pub fn push_literal(tokens: &mut TokenStream, repr: &str) {
}

pub fn push_literal_spanned(tokens: &mut TokenStream, span: Span, repr: &str) {
if repr.ends_with('e') {
if is_boolean_literal(repr) {
parse_spanned(tokens, span, repr);
} else {
let mut literal = unsafe { Literal::from_str_unchecked(repr) };
Expand All @@ -299,6 +299,13 @@ pub fn push_literal_spanned(tokens: &mut TokenStream, span: Span, repr: &str) {
}
}

fn is_boolean_literal(mut repr: &str) -> bool {
if repr.starts_with('-') {
repr = &repr[1..];
}
repr.starts_with(&['t', 'f'])
}

macro_rules! push_punct {
($name:ident $spanned:ident $char1:tt) => {
pub fn $name(tokens: &mut TokenStream) {
Expand Down

0 comments on commit 6e6bf56

Please sign in to comment.