jinja : fix loop body variable scoping (#30533)

Assisted-by: Codex
This commit is contained in:
William Tan authored and GitHub committed 2026-10-15 09:14:01 +02:00
1 parent 23f83e056d
commit 2130ca8476
2 files changed
+36 -3

No files matched your search

+4 -3
View File
@@ -600,6 +600,7 @@ value for_statement::execute_impl(context & ctx) const {
bool noIteration = true;
for (size_t i = 0; i < filtered_items.size(); i++) {
JJ_DEBUG("For loop iteration %zu/%zu", i + 1, filtered_items.size());
context loop_scope(scope);
value_object loop_obj = mk_val<value_object>();
loop_obj->has_builtins = false; // loop object has no builtins
loop_obj->insert("index", mk_val<value_int>(i + 1));
@@ -611,11 +612,11 @@ value for_statement::execute_impl(context & ctx) const {
loop_obj->insert("length", mk_val<value_int>(filtered_items.size()));
loop_obj->insert("previtem", i > 0 ? filtered_items[i - 1] : mk_val<value_undefined>("previtem"));
loop_obj->insert("nextitem", i < filtered_items.size() - 1 ? filtered_items[i + 1] : mk_val<value_undefined>("nextitem"));
scope.set_val("loop", loop_obj);
scope_update_fns[i](scope);
loop_scope.set_val("loop", loop_obj);
scope_update_fns[i](loop_scope);
try {
for (auto & stmt : body) {
value val = stmt->execute(scope);
value val = stmt->execute(loop_scope);
result->push_back(val);
}
} catch (const continue_statement::signal &) {
+32
View File
@@ -360,6 +360,38 @@ static void test_loops(testing & t) {
"1x1y2x2y"
);
test_template(t, "loop assignments do not persist between iterations",
"{% for i in [1, 2] %}{% if i == 1 %}{% set x = 7 %}{% endif %}{{ x is defined }};{% endfor %}",
json::object(),
"True;False;"
);
test_template(t, "loop assignments shadow outer variables",
"{% set x = 9 %}{% for i in [1, 2] %}{% if i == 1 %}{% set x = 7 %}{% endif %}{{ x }};{% endfor %}{{ x }}",
json::object(),
"7;9;9"
);
test_template(t, "nested loop assignments do not persist between iterations",
"{% for i in [1, 2] %}{% for j in [1, 2] %}{% if j == 1 %}{% set x = i %}{% endif %}{{ x is defined }};{% endfor %}{% endfor %}",
json::object(),
"True;False;True;False;"
);
test_template(t, "reasoning is local to each message",
"{% for m in messages %}{% if m.reasoning_content is string %}{% set reasoning_content = m.reasoning_content %}{% elif '</think>' in m.content %}{% set reasoning_content = m.content.split('</think>')[0].split('<think>')[-1] %}{% endif %}[{{ reasoning_content is defined }}:{{ reasoning_content if reasoning_content is defined else '' }}]{% endfor %}",
{{"messages", json::array({
{{"reasoning_content", "first"}, {"content", "answer"}},
{{"content", "answer"}},
{{"reasoning_content", nullptr}, {"content", "answer"}},
{{"reasoning_content", 1}, {"content", "answer"}},
{{"reasoning_content", ""}, {"content", "answer"}},
{{"content", "<think>embedded</think>answer"}},
{{"content", "answer"}}
})}},
"[True:first][False:][False:][False:][True:][True:embedded][False:]"
);
test_template(t, "for with range",
"{% for i in range(3) %}{{ i }}{% endfor %}",
json::object(),