Merge pull request #196 from dtolnay/boolliteral

Fix boolean as $literal
This commit is contained in:
David Tolnay 2021-12-26 20:31:41 -08:00 committed by GitHub
commit 68bf5916a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 5 deletions

View File

@ -279,14 +279,24 @@ pub fn push_lifetime_spanned(tokens: &mut TokenStream, span: Span, lifetime: &st
}
pub fn push_literal(tokens: &mut TokenStream, repr: &str) {
let literal: Literal = repr.parse().expect("invalid literal");
tokens.extend(iter::once(TokenTree::Literal(literal)));
// 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') {
parse(tokens, repr);
} else {
let literal: Literal = repr.parse().expect("invalid literal");
tokens.extend(iter::once(TokenTree::Literal(literal)));
}
}
pub fn push_literal_spanned(tokens: &mut TokenStream, span: Span, repr: &str) {
let mut literal: Literal = repr.parse().expect("invalid literal");
literal.set_span(span);
tokens.extend(iter::once(TokenTree::Literal(literal)));
if repr.ends_with('e') {
parse_spanned(tokens, span, repr);
} else {
let mut literal: Literal = repr.parse().expect("invalid literal");
literal.set_span(span);
tokens.extend(iter::once(TokenTree::Literal(literal)));
}
}
macro_rules! push_punct {

View File

@ -229,6 +229,31 @@ fn test_string() {
assert_eq!(expected, tokens.to_string());
}
#[test]
fn test_interpolated_literal() {
macro_rules! m {
($literal:literal) => {
quote!($literal)
};
}
let tokens = m!(1);
let expected = "1";
assert_eq!(expected, tokens.to_string());
let tokens = m!(-1);
let expected = "- 1";
assert_eq!(expected, tokens.to_string());
let tokens = m!(true);
let expected = "true";
assert_eq!(expected, tokens.to_string());
let tokens = m!(-true);
let expected = "- true";
assert_eq!(expected, tokens.to_string());
}
#[test]
fn test_ident() {
let foo = Ident::new("Foo", Span::call_site());