Skip to content

Commit 26a9d74

Browse files
authored
Disallow int literals in attribute values (#415)
1 parent 320add8 commit 26a9d74

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

maud/tests/warnings/non-string-literal.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
error: literal must be double-quoted: `"42"`
2+
--> tests/warnings/non-string-literal.rs:5:9
3+
|
4+
5 | 42
5+
| ^^
6+
7+
error: literal must be double-quoted: `"42usize"`
8+
--> tests/warnings/non-string-literal.rs:6:9
9+
|
10+
6 | 42usize
11+
| ^^^^^^^
12+
113
error: literal must be double-quoted: `"42.0"`
214
--> tests/warnings/non-string-literal.rs:7:9
315
|

maud_macros/src/ast.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,6 @@ pub fn name_to_string(name: TokenStream) -> String {
223223
if let TokenTree::Literal(literal) = token {
224224
match Lit::new(literal.clone()) {
225225
Lit::Str(str) => str.value(),
226-
Lit::Char(char) => char.value().to_string(),
227-
Lit::ByteStr(byte) => {
228-
String::from_utf8(byte.value()).expect("Invalid utf8 byte")
229-
}
230-
Lit::Byte(byte) => (byte.value() as char).to_string(),
231226
_ => literal.to_string(),
232227
}
233228
} else {

maud_macros/src/parse.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl Parser {
9494
// Literal
9595
TokenTree::Literal(literal) => {
9696
self.advance();
97-
self.literal(literal)
97+
self.literal(literal, false)
9898
}
9999
// Special form
100100
TokenTree::Punct(ref punct) if punct.as_char() == '@' => {
@@ -194,7 +194,10 @@ impl Parser {
194194
}
195195

196196
/// Parses a literal string.
197-
fn literal(&mut self, literal: Literal) -> ast::Markup {
197+
///
198+
/// If `allow_int_literal` is `true`, then integer literals (like `123`) will be accepted and
199+
/// returned.
200+
fn literal(&mut self, literal: Literal, allow_int_literal: bool) -> ast::Markup {
198201
match Lit::new(literal.clone()) {
199202
Lit::Str(lit_str) => {
200203
return ast::Markup::Literal {
@@ -204,13 +207,13 @@ impl Parser {
204207
}
205208
// Boolean literals are idents, so `Lit::Bool` is handled in
206209
// `markup`, not here.
207-
Lit::Int(lit_int) => {
210+
Lit::Int(lit_int) if allow_int_literal => {
208211
return ast::Markup::Literal {
209212
content: lit_int.to_string(),
210213
span: SpanRange::single_span(literal.span()),
211214
};
212215
}
213-
Lit::Float(..) => {
216+
Lit::Int(..) | Lit::Float(..) => {
214217
emit_error!(literal, r#"literal must be double-quoted: `"{}"`"#, literal);
215218
}
216219
Lit::Char(lit_char) => {
@@ -722,7 +725,7 @@ impl Parser {
722725
false
723726
}
724727
Some(TokenTree::Literal(ref literal)) if expect_ident_or_literal => {
725-
self.literal(literal.clone());
728+
self.literal(literal.clone(), true);
726729
self.advance();
727730
result.push(TokenTree::Literal(literal.clone()));
728731
false

0 commit comments

Comments
 (0)