Twig icon indicating copy to clipboard operation
Twig copied to clipboard

Documented precedence for binary operators does not match actual behavior

Open nox-4O4 opened this issue 3 years ago • 2 comments

The documentation states the following:

The operator precedence is as follows, with the lowest-precedence operators listed first: ?: (ternary operator), b-and, b-xor, b-or, or, and, ==, !=, <=>, <, >, >=, <=, in, matches, starts with, ends with, .., +, -, ~, *, /, //, %, is (tests), **, ??, | (filters), [], and .

When compiling following template:

{{ 6 b-and 2 or 6 b-and 16 }}

This PHP code gets generated:

echo twig_escape_filter($this->env, ((6 & 2) || (6 & 16)), "html", null, true);

If b-and had indeed had lower precedence than or, the result would have been ((6 & (2 || 6)) & 16).

The documentation should be adjusted to reflect current behavior.

nox-4O4 avatar Jun 12 '22 22:06 nox-4O4

@nox-4O4 I proposed a PR to answer it, put it in the right order and added some additional information

lacatoire avatar Feb 29 '24 18:02 lacatoire

Thanks for clarifying the current behavior in the documentation! I added some review comments to your PR.

nox-4O4 avatar Mar 01 '24 00:03 nox-4O4