From 6e6bf569299318be8020e4e58e9b9028abe35091 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 26 Dec 2021 22:23:24 -0800 Subject: [PATCH] Distinguish boolean literals more precisely This avoids taking the slow path for things like `0usize` or `0x7e`. --- src/runtime.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/runtime.rs b/src/runtime.rs index 5896858..94a9f8c 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -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) }; @@ -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) }; @@ -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) {