Disallow int literals in attribute values ()

This commit is contained in:
Chris Wong 2024-01-15 00:45:49 +11:00 committed by GitHub
parent 320add87a1
commit 26a9d7484d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 10 deletions
maud/tests/warnings
maud_macros/src

View file

@ -1,3 +1,15 @@
error: literal must be double-quoted: `"42"`
--> tests/warnings/non-string-literal.rs:5:9
|
5 | 42
| ^^
error: literal must be double-quoted: `"42usize"`
--> tests/warnings/non-string-literal.rs:6:9
|
6 | 42usize
| ^^^^^^^
error: literal must be double-quoted: `"42.0"` error: literal must be double-quoted: `"42.0"`
--> tests/warnings/non-string-literal.rs:7:9 --> tests/warnings/non-string-literal.rs:7:9
| |

View file

@ -223,11 +223,6 @@ pub fn name_to_string(name: TokenStream) -> String {
if let TokenTree::Literal(literal) = token { if let TokenTree::Literal(literal) = token {
match Lit::new(literal.clone()) { match Lit::new(literal.clone()) {
Lit::Str(str) => str.value(), Lit::Str(str) => str.value(),
Lit::Char(char) => char.value().to_string(),
Lit::ByteStr(byte) => {
String::from_utf8(byte.value()).expect("Invalid utf8 byte")
}
Lit::Byte(byte) => (byte.value() as char).to_string(),
_ => literal.to_string(), _ => literal.to_string(),
} }
} else { } else {

View file

@ -94,7 +94,7 @@ impl Parser {
// Literal // Literal
TokenTree::Literal(literal) => { TokenTree::Literal(literal) => {
self.advance(); self.advance();
self.literal(literal) self.literal(literal, false)
} }
// Special form // Special form
TokenTree::Punct(ref punct) if punct.as_char() == '@' => { TokenTree::Punct(ref punct) if punct.as_char() == '@' => {
@ -194,7 +194,10 @@ impl Parser {
} }
/// Parses a literal string. /// Parses a literal string.
fn literal(&mut self, literal: Literal) -> ast::Markup { ///
/// If `allow_int_literal` is `true`, then integer literals (like `123`) will be accepted and
/// returned.
fn literal(&mut self, literal: Literal, allow_int_literal: bool) -> ast::Markup {
match Lit::new(literal.clone()) { match Lit::new(literal.clone()) {
Lit::Str(lit_str) => { Lit::Str(lit_str) => {
return ast::Markup::Literal { return ast::Markup::Literal {
@ -204,13 +207,13 @@ impl Parser {
} }
// Boolean literals are idents, so `Lit::Bool` is handled in // Boolean literals are idents, so `Lit::Bool` is handled in
// `markup`, not here. // `markup`, not here.
Lit::Int(lit_int) => { Lit::Int(lit_int) if allow_int_literal => {
return ast::Markup::Literal { return ast::Markup::Literal {
content: lit_int.to_string(), content: lit_int.to_string(),
span: SpanRange::single_span(literal.span()), span: SpanRange::single_span(literal.span()),
}; };
} }
Lit::Float(..) => { Lit::Int(..) | Lit::Float(..) => {
emit_error!(literal, r#"literal must be double-quoted: `"{}"`"#, literal); emit_error!(literal, r#"literal must be double-quoted: `"{}"`"#, literal);
} }
Lit::Char(lit_char) => { Lit::Char(lit_char) => {
@ -722,7 +725,7 @@ impl Parser {
false false
} }
Some(TokenTree::Literal(ref literal)) if expect_ident_or_literal => { Some(TokenTree::Literal(ref literal)) if expect_ident_or_literal => {
self.literal(literal.clone()); self.literal(literal.clone(), true);
self.advance(); self.advance();
result.push(TokenTree::Literal(literal.clone())); result.push(TokenTree::Literal(literal.clone()));
false false