Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-disallow integer literals #465

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions maud/tests/warnings/non-string-literal.stderr
Original file line number Diff line number Diff line change
@@ -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"`
--> tests/warnings/non-string-literal.rs:7:9
|
Expand Down
60 changes: 37 additions & 23 deletions maud_macros/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,21 +597,24 @@ impl Display for HtmlName {
#[derive(Debug, Clone)]
pub enum HtmlNameFragment {
Ident(Ident),
Lit(HtmlLit),
LitInt(LitInt),
LitStr(LitStr),
Empty,
}

impl DiagnosticParse for HtmlNameFragment {
fn diagnostic_parse(
input: ParseStream,
diagnostics: &mut Vec<Diagnostic>,
_diagnostics: &mut Vec<Diagnostic>,
) -> syn::Result<Self> {
let lookahead = input.lookahead1();

if lookahead.peek(Ident::peek_any) {
input.call(Ident::parse_any).map(Self::Ident)
} else if lookahead.peek(Lit) {
input.diagnostic_parse(diagnostics).map(Self::Lit)
} else if lookahead.peek(LitInt) {
input.parse().map(Self::LitInt)
} else if lookahead.peek(LitStr) {
input.parse().map(Self::LitStr)
} else if lookahead.peek(Minus) || lookahead.peek(Colon) {
Ok(Self::Empty)
} else {
Expand All @@ -624,7 +627,8 @@ impl ToTokens for HtmlNameFragment {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Self::Ident(ident) => ident.to_tokens(tokens),
Self::Lit(lit) => lit.to_tokens(tokens),
Self::LitInt(lit) => lit.to_tokens(tokens),
Self::LitStr(lit) => lit.to_tokens(tokens),
Self::Empty => {}
}
}
Expand All @@ -634,16 +638,16 @@ impl Display for HtmlNameFragment {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Ident(ident) => ident.fmt(f),
Self::Lit(lit) => lit.fmt(f),
Self::LitInt(lit) => lit.fmt(f),
Self::LitStr(lit) => lit.value().fmt(f),
Self::Empty => Ok(()),
}
}
}

#[derive(Debug, Clone)]
pub enum HtmlLit {
Str(LitStr),
Int(LitInt),
pub struct HtmlLit {
pub lit: LitStr,
}

impl DiagnosticParse for HtmlLit {
Expand All @@ -656,29 +660,45 @@ impl DiagnosticParse for HtmlLit {
if lookahead.peek(Lit) {
let lit = input.parse()?;
match lit {
Lit::Str(lit) => Ok(Self::Str(lit)),
Lit::Int(lit) => Ok(Self::Int(lit)),
Lit::Str(lit) => Ok(Self { lit }),
Lit::Int(lit) => {
diagnostics.push(
lit.span()
.error(format!(r#"literal must be double-quoted: `"{lit}"`"#)),
);
Ok(Self {
lit: LitStr::new("", lit.span()),
})
}
Lit::Float(lit) => {
diagnostics.push(
lit.span()
.error(format!(r#"literal must be double-quoted: `"{lit}"`"#)),
);
Ok(Self::Str(LitStr::new("", lit.span())))
Ok(Self {
lit: LitStr::new("", lit.span()),
})
}
Lit::Char(lit) => {
diagnostics.push(lit.span().error(format!(
r#"literal must be double-quoted: `"{}"`"#,
lit.value()
)));
Ok(Self::Str(LitStr::new("", lit.span())))
Ok(Self {
lit: LitStr::new("", lit.span()),
})
}
Lit::Bool(_) => {
// diagnostic handled earlier with more information
Ok(Self::Str(LitStr::new("", lit.span())))
Ok(Self {
lit: LitStr::new("", lit.span()),
})
}
_ => {
diagnostics.push(lit.span().error("expected string"));
Ok(Self::Str(LitStr::new("", lit.span())))
Ok(Self {
lit: LitStr::new("", lit.span()),
})
}
}
} else {
Expand All @@ -689,19 +709,13 @@ impl DiagnosticParse for HtmlLit {

impl ToTokens for HtmlLit {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Self::Str(lit) => lit.to_tokens(tokens),
Self::Int(lit) => lit.to_tokens(tokens),
}
self.lit.to_tokens(tokens);
}
}

impl Display for HtmlLit {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Str(lit) => lit.value().fmt(f),
Self::Int(lit) => lit.fmt(f),
}
self.lit.value().fmt(f)
}
}

Expand Down
Loading