diff --git a/CHANGELOG b/CHANGELOG index 671f2abe4..e85961b0a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -9,6 +9,7 @@ * Add a fourth `array $tests` argument to `Twig\Sandbox\SecurityPolicyInterface::checkSecurity()` * Throw a `SyntaxError` when an `extends`, `use`, or `macro` tag is not at the root of a template * Throw a `SyntaxError` when a macro is defined more than once in a template + * Throw a `SyntaxError` when calling a macro without parentheses # 4.0.0 alpha 1 (2026-05-17) diff --git a/src/Node/Expression/MacroReferenceExpression.php b/src/Node/Expression/MacroReferenceExpression.php index 546b0fb87..108c19325 100644 --- a/src/Node/Expression/MacroReferenceExpression.php +++ b/src/Node/Expression/MacroReferenceExpression.php @@ -12,6 +12,7 @@ namespace Twig\Node\Expression; use Twig\Compiler; +use Twig\Error\SyntaxError; use Twig\Node\CoercesChildrenToStringInterface; use Twig\Node\Expression\Variable\MacroVariable; @@ -73,7 +74,7 @@ class MacroReferenceExpression extends AbstractExpression implements SupportDefi public function compile(Compiler $compiler): void { if (!$this->hasParentheses && !$this->definedTest) { - trigger_deprecation('twig/twig', '3.29', 'Omitting parentheses when calling a macro is deprecated and will throw a SyntaxError in Twig 4.0; add parentheses after the macro name in "%s" at line %d.', $this->getTemplateName(), $this->getTemplateLine()); + throw new SyntaxError('Omitting parentheses when calling a macro is not allowed; add parentheses after the macro name.', $this->getTemplateLine(), $this->getSourceContext()); } if ($this->hasNode('name')) { diff --git a/tests/Fixtures/macros/call_without_parentheses.legacy.test b/tests/Fixtures/macros/call_without_parentheses.legacy.test deleted file mode 100644 index bdb93d656..000000000 --- a/tests/Fixtures/macros/call_without_parentheses.legacy.test +++ /dev/null @@ -1,16 +0,0 @@ ---TEST-- -Omitting parentheses when calling macros is deprecated ---DEPRECATION-- -Since twig/twig 3.29: Omitting parentheses when calling a macro is deprecated and will throw a SyntaxError in Twig 4.0; add parentheses after the macro name in "index.twig" at line 4. -Since twig/twig 3.29: Omitting parentheses when calling a macro is deprecated and will throw a SyntaxError in Twig 4.0; add parentheses after the macro name in "index.twig" at line 5. ---TEMPLATE-- -{% import _self as macros %} -{% set name = 'hello' %} -{{ macros.hello }} -{{ _self.(name) }} -{% macro hello() %}Hello{% endmacro %} ---DATA-- -return [] ---EXPECT-- -Hello -Hello diff --git a/tests/Fixtures/macros/call_without_parentheses.test b/tests/Fixtures/macros/call_without_parentheses.test new file mode 100644 index 000000000..72e180437 --- /dev/null +++ b/tests/Fixtures/macros/call_without_parentheses.test @@ -0,0 +1,10 @@ +--TEST-- +Calling a macro without parentheses is forbidden +--TEMPLATE-- +{% import _self as macros %} +{{ macros.hello }} +{% macro hello() %}Hello{% endmacro %} +--DATA-- +return [] +--EXCEPTION-- +Twig\Error\SyntaxError: Omitting parentheses when calling a macro is not allowed; add parentheses after the macro name in "index.twig" at line 3. diff --git a/tests/Fixtures/macros/dynamic_call_without_parentheses.test b/tests/Fixtures/macros/dynamic_call_without_parentheses.test new file mode 100644 index 000000000..3c08a1b6b --- /dev/null +++ b/tests/Fixtures/macros/dynamic_call_without_parentheses.test @@ -0,0 +1,11 @@ +--TEST-- +Calling a dynamic macro without parentheses is forbidden +--TEMPLATE-- +{% import _self as macros %} +{% set name = 'hello' %} +{{ macros.(name) }} +{% macro hello() %}Hello{% endmacro %} +--DATA-- +return [] +--EXCEPTION-- +Twig\Error\SyntaxError: Omitting parentheses when calling a macro is not allowed; add parentheses after the macro name in "index.twig" at line 4.