bug #3344 Fix the compilation of Twig comparison operators (stof)

This PR was merged into the 3.x branch.

Discussion
----------

Fix the compilation of Twig comparison operators

The new compilation implemented in Twig 3 for PHP 7 (to match the comparison semantic of PHP 8) was missing the surrounding braces (which are part of the AsbtractBinary compilation). This caused issues when the full expression would involve other operators (like the not operator), as things would then rely on having the correct precedence in PHP (which would not be the case).

Closes #3318

Commits
-------

bf24f136 Fix the compilation of Twig comparison operators
This commit is contained in:
Fabien Potencier
2020-06-05 15:59:44 +02:00
7 changed files with 26 additions and 12 deletions
+2 -2
View File
@@ -24,11 +24,11 @@ class EqualBinary extends AbstractBinary
}
$compiler
->raw('0 === twig_compare(')
->raw('(0 === twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
->raw('))')
;
}
+2 -2
View File
@@ -24,11 +24,11 @@ class GreaterBinary extends AbstractBinary
}
$compiler
->raw('1 === twig_compare(')
->raw('(1 === twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
->raw('))')
;
}
@@ -24,11 +24,11 @@ class GreaterEqualBinary extends AbstractBinary
}
$compiler
->raw('0 <= twig_compare(')
->raw('(0 <= twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
->raw('))')
;
}
+2 -2
View File
@@ -24,11 +24,11 @@ class LessBinary extends AbstractBinary
}
$compiler
->raw('-1 === twig_compare(')
->raw('(-1 === twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
->raw('))')
;
}
@@ -24,11 +24,11 @@ class LessEqualBinary extends AbstractBinary
}
$compiler
->raw('0 >= twig_compare(')
->raw('(0 >= twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
->raw('))')
;
}
@@ -24,11 +24,11 @@ class NotEqualBinary extends AbstractBinary
}
$compiler
->raw('0 !== twig_compare(')
->raw('(0 !== twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
->raw('))')
;
}
@@ -0,0 +1,14 @@
--TEST--
Twig comparison operators precendence
--TEMPLATE--
{{ not(1 > 2) }}/{{ not(1 > 1) }}/{{ not(1 >= 2) }}/{{ not(1 >= 1) }}
{{ not(1 < 2) }}/{{ not(1 < 1) }}/{{ not(1 <= 2) }}/{{ not(1 <= 1) }}
{{ not(1 == 1) }}/{{ not(1 == 2) }}
{{ not(1 != 1) }}/{{ not(1 != 2) }}
--DATA--
return []
--EXPECT--
1/1/1/
/1//
/1
1/