Skip to content

Commit b25e322

Browse files
committed
add more test add resreved word caller
Signed-off-by: Michael Pollind <[email protected]>
1 parent 439f83b commit b25e322

File tree

6 files changed

+147
-24
lines changed

6 files changed

+147
-24
lines changed

askama_derive/src/generator/node.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,11 @@ impl<'a> Generator<'a, '_> {
624624
(*def, ctx)
625625
};
626626

627-
if self.seen_callers.iter().any(|(_, s, _)| std::ptr::eq(*s, def)) {
627+
if self
628+
.seen_callers
629+
.iter()
630+
.any(|(_, s, _)| std::ptr::eq(*s, def))
631+
{
628632
let mut message = "Found recursion in macro calls:".to_owned();
629633
for (_, m, f) in &self.seen_callers {
630634
if let Some(f) = f {
@@ -1105,7 +1109,23 @@ impl<'a> Generator<'a, '_> {
11051109
..
11061110
} = &**s
11071111
{
1112+
fn check_num_args<'a>(
1113+
s: &'a WithSpan<'a, Expr<'a>>,
1114+
ctx: &Context<'a>,
1115+
expected: usize,
1116+
found: usize,
1117+
) -> Result<(), CompileError> {
1118+
if expected != found {
1119+
Err(ctx.generate_error(
1120+
format!("expected `{}` argument, found `{}`", expected, found),
1121+
s.span(),
1122+
))
1123+
} else {
1124+
Ok(())
1125+
}
1126+
}
11081127
if ***path == Expr::Var("super") {
1128+
check_num_args(s, ctx, 0, expr_args.len())?;
11091129
return self.write_block(ctx, buf, None, ws, s.span());
11101130
} else if ***path == Expr::Var("caller") {
11111131
let def = self.active_caller.ok_or_else(|| {
@@ -1118,6 +1138,7 @@ impl<'a> Generator<'a, '_> {
11181138
buf.write('{');
11191139
this.prepare_ws(def.ws1);
11201140
let mut value = Buffer::new();
1141+
check_num_args(s, ctx, def.caller_args.len(), expr_args.len())?;
11211142
for (index, arg) in def.caller_args.iter().enumerate() {
11221143
match expr_args.get(index) {
11231144
Some(expr) => {

askama_parser/src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,16 @@ const KWS_PARSER: &[&[[AsciiChar; MAX_RUST_RAW_KEYWORD_LEN]]; MAX_RUST_KEYWORD_L
12651265
result[result.len() - 1] = AsciiStr::new_sized("r#union");
12661266
result
12671267
};
1268+
const KW6: &[[AsciiChar; MAX_RUST_RAW_KEYWORD_LEN]] = &{
1269+
let mut result = [AsciiStr::new_sized("r#"); RUST_KEYWORDS[6].len() + 1];
1270+
let mut i = 0;
1271+
while i < RUST_KEYWORDS[6].len() {
1272+
result[i] = RUST_KEYWORDS[6][i];
1273+
i += 1;
1274+
}
1275+
result[result.len() - 1] = AsciiStr::new_sized("r#caller");
1276+
result
1277+
};
12681278

12691279
[
12701280
RUST_KEYWORDS[0],
@@ -1273,7 +1283,7 @@ const KWS_PARSER: &[&[[AsciiChar; MAX_RUST_RAW_KEYWORD_LEN]]; MAX_RUST_KEYWORD_L
12731283
RUST_KEYWORDS[3],
12741284
KW4,
12751285
KW5,
1276-
RUST_KEYWORDS[6],
1286+
KW6,
12771287
RUST_KEYWORDS[7],
12781288
RUST_KEYWORDS[8],
12791289
]
@@ -1493,6 +1503,7 @@ mod test {
14931503

14941504
#[test]
14951505
fn test_is_rust_keyword() {
1506+
assert!(is_rust_keyword("caller"));
14961507
assert!(is_rust_keyword("super"));
14971508
assert!(is_rust_keyword("become"));
14981509
assert!(!is_rust_keyword("supeeeer"));

testing/tests/ui/caller_arguments.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ use askama::Template;
1515
struct InvalidNumberArguments {
1616
}
1717

18+
#[derive(Template)]
19+
#[template(
20+
source = r#"
21+
{% macro test() %}
22+
{{- caller("a", "b") -}}
23+
{%- endmacro -%}
24+
{%- call(a) test() -%}
25+
{{- a -}}
26+
{%- endcall -%}
27+
"#,
28+
ext = "txt"
29+
)]
30+
struct InvalidNumberArguments1 {
31+
}
32+
1833
#[derive(Template)]
1934
#[template(
2035
source = r#"
@@ -66,5 +81,28 @@ struct CallerInCaller {
6681
struct CallerInCaller1 {
6782
}
6883

84+
#[derive(Template)]
85+
#[template(
86+
source = r#"{{caller()}}"#,
87+
ext = "txt"
88+
)]
89+
struct JustCaller{
90+
}
91+
92+
#[derive(Template)]
93+
#[template(
94+
source = r#"
95+
{% macro test() %}
96+
{{ caller("a", one = "b") }}
97+
{%- endmacro -%}
98+
{%- call(two, one) test() -%}
99+
{{- two -}} {{- one -}}
100+
{%- endcall -%}
101+
"#,
102+
ext = "txt"
103+
)]
104+
struct NamedArguments {
105+
}
106+
69107
fn main() {}
70108

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: missing `c` argument
1+
error: expected `3` argument, found `2`
22
--> InvalidNumberArguments.txt:3:18
33
"(\"a\", \"b\") -}}\n {%- endmacro -%}\n {%- call(a,b,c) test() -%}\n {{- a"...
44
--> tests/ui/caller_arguments.rs:5:14
@@ -13,47 +13,85 @@ error: missing `c` argument
1313
12 | | "#,
1414
| |______^
1515

16+
error: expected `1` argument, found `2`
17+
--> InvalidNumberArguments1.txt:3:18
18+
"(\"a\", \"b\") -}}\n {%- endmacro -%}\n {%- call(a) test() -%}\n {{- a -}}"...
19+
--> tests/ui/caller_arguments.rs:21:14
20+
|
21+
21 | source = r#"
22+
| ______________^
23+
22 | | {% macro test() %}
24+
23 | | {{- caller("a", "b") -}}
25+
24 | | {%- endmacro -%}
26+
... |
27+
27 | | {%- endcall -%}
28+
28 | | "#,
29+
| |______^
30+
1631
error: expected `)` to close call argument list
1732
--> <source attribute>:5:15
1833
"test() -%}\n {{- a -}}\n {%- endcall -%}\n "
19-
--> tests/ui/caller_arguments.rs:20:14
34+
--> tests/ui/caller_arguments.rs:36:14
2035
|
21-
20 | source = r#"
36+
36 | source = r#"
2237
| ______________^
23-
21 | | {% macro test() %}
24-
22 | | {{- caller("a") -}}
25-
23 | | {%- endmacro -%}
38+
37 | | {% macro test() %}
39+
38 | | {{- caller("a") -}}
40+
39 | | {%- endmacro -%}
2641
... |
27-
26 | | {%- endcall -%}
28-
27 | | "#,
42+
42 | | {%- endcall -%}
43+
43 | | "#,
2944
| |______^
3045

3146
error: block is not defined for caller
3247
--> CallerInCaller.txt:6:18
3348
"(a) -}}\n {%- endcall -%}\n "
34-
--> tests/ui/caller_arguments.rs:35:14
49+
--> tests/ui/caller_arguments.rs:51:14
3550
|
36-
35 | source = r#"
51+
51 | source = r#"
3752
| ______________^
38-
36 | | {% macro test() %}
39-
37 | | {{- caller("a") -}}
40-
38 | | {%- endmacro -%}
53+
52 | | {% macro test() %}
54+
53 | | {{- caller("a") -}}
55+
54 | | {%- endmacro -%}
4156
... |
42-
41 | | {%- endcall -%}
43-
42 | | "#,
57+
57 | | {%- endcall -%}
58+
58 | | "#,
4459
| |______^
4560

4661
error: block is not defined for caller
4762
--> CallerInCaller1.txt:10:21
4863
"(\"b\") }}\n {% endcall %}\n {{- a -}}\n {%- endcall -%}\n "
49-
--> tests/ui/caller_arguments.rs:50:14
64+
--> tests/ui/caller_arguments.rs:66:14
5065
|
51-
50 | source = r#"
66+
66 | source = r#"
5267
| ______________^
53-
51 | | {% macro test2() %}
54-
52 | | {{ caller("bb") }}
55-
53 | | {% endmacro %}
68+
67 | | {% macro test2() %}
69+
68 | | {{ caller("bb") }}
70+
69 | | {% endmacro %}
5671
... |
57-
62 | | {%- endcall -%}
58-
63 | | "#,
72+
78 | | {%- endcall -%}
73+
79 | | "#,
5974
| |______^
75+
76+
error: block is not defined for caller
77+
--> JustCaller.txt:1:8
78+
"()}}"
79+
--> tests/ui/caller_arguments.rs:87:14
80+
|
81+
87 | source = r#"{{caller()}}"#,
82+
| ^^^^^^^^^^^^^^^^^
83+
84+
error: failed to parse template source
85+
--> <source attribute>:3:27
86+
"= \"b\") }}\n {%- endmacro -%}\n {%- call(two, one) test() -%}\n {{- two"...
87+
--> tests/ui/caller_arguments.rs:95:14
88+
|
89+
95 | source = r#"
90+
| ______________^
91+
96 | | {% macro test() %}
92+
97 | | {{ caller("a", one = "b") }}
93+
98 | | {%- endmacro -%}
94+
... |
95+
101 | | {%- endcall -%}
96+
102 | | "#,
97+
| |______^

testing/tests/ui/macro-caller.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use askama::Template;
2+
3+
#[derive(Template)]
4+
#[template(source = "{%- macro caller() -%}{%- endmacro -%}", ext = "html")]
5+
struct MacroSuper;
6+
7+
fn main() {
8+
}

testing/tests/ui/macro-caller.stderr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
error: 'caller' is not a valid name for a macro
2+
--> <source attribute>:1:2
3+
"- macro caller() -%}{%- endmacro -%}"
4+
--> tests/ui/macro-caller.rs:4:21
5+
|
6+
4 | #[template(source = "{%- macro caller() -%}{%- endmacro -%}", ext = "html")]
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)