diff --git a/src/Node/CaptureNode.php b/src/Node/CaptureNode.php index 561e1ea53c..7c187727ad 100644 --- a/src/Node/CaptureNode.php +++ b/src/Node/CaptureNode.php @@ -44,9 +44,11 @@ public function compile(Compiler $compiler): void ->indent() ->subcompile($this->getNode('body')) ->outdent() - ->write("})() ?? new \EmptyIterator())") + ->write("})() ?? new \EmptyIterator()") ; if ($useYield) { + $compiler->raw(', false))'); + } else { $compiler->raw(')'); } if (!$this->getAttribute('raw')) { diff --git a/tests/Fixtures/regression/4029-iterator_to_array.test b/tests/Fixtures/regression/4029-iterator_to_array.test new file mode 100644 index 0000000000..99afd892f5 --- /dev/null +++ b/tests/Fixtures/regression/4029-iterator_to_array.test @@ -0,0 +1,14 @@ +--TEST-- +#4029 When use_yield is true, CaptureNode fall in iterator_to_array pitfall regarding index overwrite +--TEMPLATE-- +{%- set tmp -%} + {%- block foo 'foo' -%} + {%- block bar 'bar' -%} +{%- endset -%} +{{ tmp }} +--DATA-- +return [] +--CONFIG-- +return ['use_yield' => true] +--EXPECT-- +foobar \ No newline at end of file diff --git a/tests/Node/MacroTest.php b/tests/Node/MacroTest.php index 09d7ee6ca7..88ce9b299e 100644 --- a/tests/Node/MacroTest.php +++ b/tests/Node/MacroTest.php @@ -60,7 +60,7 @@ public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo", ...\$__varargs__) return new Markup(implode('', iterator_to_array((function () use (\$context, \$macros, \$blocks) { yield "foo"; - })() ?? new \EmptyIterator())), \$this->env->getCharset()); + })() ?? new \EmptyIterator(), false)), \$this->env->getCharset()); } EOF , new Environment(new ArrayLoader()), diff --git a/tests/Node/SetTest.php b/tests/Node/SetTest.php index 57f66cb9d6..ed6ee9ebe1 100644 --- a/tests/Node/SetTest.php +++ b/tests/Node/SetTest.php @@ -57,7 +57,7 @@ public function getTests() // line 1 \$context["foo"] = ('' === \$tmp = implode('', iterator_to_array((function () use (&\$context, \$macros, \$blocks) { yield "foo"; -})() ?? new \EmptyIterator()))) ? '' : new Markup(\$tmp, \$this->env->getCharset()); +})() ?? new \EmptyIterator(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset()); EOF , new Environment(new ArrayLoader()), ];