Skip to content

Commit

Permalink
parser: reject closing delims that look like an op
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Aug 13, 2024
1 parent d5710c9 commit a95ffd5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
20 changes: 20 additions & 0 deletions rinja_derive/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,26 @@ impl<'a> RawSyntax<'a> {
}
}

for end in [syntax.block_end, syntax.expr_end, syntax.comment_end] {
for prefix in ["<<", ">>", "&&", "..", "||"] {
if end.starts_with(prefix) {
let msg = if end == prefix {
format!("a closing delimiter must not start with an operator: {end:?}")
} else {
format!(
"a closing delimiter must not start an with operator: \
{end:?} vs {prefix:?}",
)
};
return Err(CompileError::new_with_span(
msg,
file_info.copied(),
config_span,
));
}
}
}

Ok(syntax)
}
}
Expand Down
4 changes: 4 additions & 0 deletions testing/issue-128-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[[syntax]]
name = "mwe"
expr_start = "<<<"
expr_end = ">>>"
4 changes: 4 additions & 0 deletions testing/issue-128.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[[syntax]]
name = "mwe"
expr_start = "<<"
expr_end = ">>"
17 changes: 17 additions & 0 deletions testing/tests/ui/terminator-operator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use rinja::Template;

#[derive(Template)]
#[template(source = "<<a>> and <<b>>", config = "issue-128.toml", syntax = "mwe", ext="")]
struct HelloTemplate {
a: u32,
b: u32,
}

#[derive(Template)]
#[template(source = "<<a>> and <<b>>", config = "issue-128-2.toml", syntax = "mwe", ext="")]
struct HelloTemplate2 {
a: u32,
b: u32,
}

fn main() {}
13 changes: 13 additions & 0 deletions testing/tests/ui/terminator-operator.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error: a closing delimiter must not start with an operator: ">>"
--> testing/issue-128.toml
--> tests/ui/terminator-operator.rs:4:49
|
4 | #[template(source = "<<a>> and <<b>>", config = "issue-128.toml", syntax = "mwe", ext="")]
| ^^^^^^^^^^^^^^^^

error: a closing delimiter must not start an with operator: ">>>" vs ">>"
--> testing/issue-128-2.toml
--> tests/ui/terminator-operator.rs:11:49
|
11 | #[template(source = "<<a>> and <<b>>", config = "issue-128-2.toml", syntax = "mwe", ext="")]
| ^^^^^^^^^^^^^^^^^^

0 comments on commit a95ffd5

Please sign in to comment.