Compile the generator guard as an unreachable yield instead of a yield from

This commit is contained in:
Fabien Potencier
2026-09-22 21:30:14 +01:00
parent c33dd1c473
commit 4d5d233776
9 changed files with 11 additions and 10 deletions
+1
View File
@@ -2,6 +2,7 @@
* Fix the escaping safe analysis retaining every compiled template node for the lifetime of the environment
* Speed up loading a template that the environment has already loaded
* Speed up rendering by compiling a cheaper generator guard into templates
* Fix `IntlExtension` letting the pattern derived from a date formatter prototype override an explicit locale
* Fix `IntlExtension` not honoring the locale of a date formatter prototype configured with no date and time styles
* Speed up macro calls
+1 -1
View File
@@ -42,7 +42,7 @@ class BlockNode extends Node
$compiler
->subcompile($this->getNode('body'))
->write("yield from [];\n")
->write("return; yield;\n")
->outdent()
->write("}\n\n")
;
+1 -1
View File
@@ -39,7 +39,7 @@ class CaptureNode extends Node
->raw("(function () use (&\$context, \$macros, \$blocks) {\n")
->indent()
->subcompile($this->getNode('body'))
->write("yield from [];\n")
->write("return; yield;\n")
->outdent()
->write('})()')
;
+1 -1
View File
@@ -379,7 +379,7 @@ final class ModuleNode extends Node implements CoercesChildrenToStringInterface
$compiler->subcompile($this->getNode('display_end'));
if (!$this->hasNode('parent')) {
$compiler->write("yield from [];\n");
$compiler->write("return; yield;\n");
}
$compiler
+1 -1
View File
@@ -49,7 +49,7 @@ public function block_foo(array \$context, array \$blocks = []): iterable
{
\$macros = \$this->macros;
yield "foo";
yield from [];
return; yield;
}
EOF, new Environment(new ArrayLoader()),
];
+2 -2
View File
@@ -77,7 +77,7 @@ new \\Twig\\TwigMacro("foo", function (\$foo = null, \$bar = "Foo", \$_underscor
return ('' === \$tmp = implode('', iterator_to_array((function () use (&\$context, \$macros, \$blocks) {
yield "foo";
yield from [];
return; yield;
})(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset());
}, ["foo" => true, "bar" => true, "_underscore" => true], false)
EOF, new Environment(new ArrayLoader(), ['use_yield' => true]),
@@ -98,7 +98,7 @@ new \\Twig\\TwigMacro("foo", function (\$foo = null, \$bar = "Foo", \$_underscor
return ('' === \$tmp = \\Twig\\Extension\\CoreExtension::captureOutput((function () use (&\$context, \$macros, \$blocks) {
yield "foo";
yield from [];
return; yield;
})())) ? '' : new Markup(\$tmp, \$this->env->getCharset());
}, ["foo" => true, "bar" => true, "_underscore" => true], false)
EOF, new Environment(new ArrayLoader(), ['use_yield' => false]),
+1 -1
View File
@@ -74,7 +74,7 @@ protected function loadDeclaredMacros(): array
return ('' === \$tmp = implode('', iterator_to_array((function () use (&\$context, \$macros, \$blocks) {
yield "foo";
yield from [];
return; yield;
})(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset());
}, ["foo" => true], false),
];
+1 -1
View File
@@ -159,7 +159,7 @@ class __TwigTemplate_%x extends Template
\$macros = \$this->macros;
// line 1
yield "foo";
yield from [];
return; yield;
}
/**
+2 -2
View File
@@ -65,7 +65,7 @@ EOF
// line 1
\$context["foo"] = ('' === \$tmp = implode('', iterator_to_array((function () use (&\$context, \$macros, \$blocks) {
yield "foo";
yield from [];
return; yield;
})(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset());
EOF, new Environment(new ArrayLoader(), ['use_yield' => true]),
];
@@ -74,7 +74,7 @@ EOF, new Environment(new ArrayLoader(), ['use_yield' => true]),
// line 1
$context["foo"] = ('' === $tmp = \Twig\Extension\CoreExtension::captureOutput((function () use (&$context, $macros, $blocks) {
yield "foo";
yield from [];
return; yield;
})())) ? '' : new Markup($tmp, $this->env->getCharset());
EOF, new Environment(new ArrayLoader(), ['use_yield' => false]),
];