Repository: twigphp/Twig (php)

Issue:
Why twig negative (not) conditional works like this?
I don't understand why Twig `not` is working like this : 

```
{{ dump( false ) }}
{{ dump( not(false) ) }}

{{ dump ( date1.timestamp > date2.timestamp ) }}
{{ dump ( not(date1.timestamp > date2.timestamp) ) }}

{% set comp = date1.timestamp > date2.timestamp %}
{{ dump ( comp ) }}
{{ dump ( not(comp) ) }}
```

Result : 

```
false
true

false
false

false
true
```

https://twigfiddle.com/udjjti/2

Thank's for your help :)

Pull request:
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 

Patch:
diff --git a/src/Node/Expression/Binary/EqualBinary.php b/src/Node/Expression/Binary/EqualBinary.php
index 80ca2644..6b48549e 100644
--- a/src/Node/Expression/Binary/EqualBinary.php
+++ b/src/Node/Expression/Binary/EqualBinary.php
@@ -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('))')
         ;
     }
 
diff --git a/src/Node/Expression/Binary/GreaterBinary.php b/src/Node/Expression/Binary/GreaterBinary.php
index f1af1275..e1dd0678 100644
--- a/src/Node/Expression/Binary/GreaterBinary.php
+++ b/src/Node/Expression/Binary/GreaterBinary.php
@@ -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('))')
         ;
     }
 
diff --git a/src/Node/Expression/Binary/GreaterEqualBinary.php b/src/Node/Expression/Binary/GreaterEqualBinary.php
index 7d206f7c..df9bfcfb 100644
--- a/src/Node/Expression/Binary/GreaterEqualBinary.php
+++ b/src/Node/Expression/Binary/GreaterEqualBinary.php
@@ -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('))')
         ;
     }
 
diff --git a/src/Node/Expression/Binary/LessBinary.php b/src/Node/Expression/Binary/LessBinary.php
index e8519332..598e6291 100644
--- a/src/Node/Expression/Binary/LessBinary.php
+++ b/src/Node/Expression/Binary/LessBinary.php
@@ -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('))')
         ;
     }
 
diff --git a/src/Node/Expression/Binary/LessEqualBinary.php b/src/Node/Expression/Binary/LessEqualBinary.php
index dccaf31a..e3c4af58 100644
--- a/src/Node/Expression/Binary/LessEqualBinary.php
+++ b/src/Node/Expression/Binary/LessEqualBinary.php
@@ -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('))')
         ;
     }
 
diff --git a/src/Node/Expression/Binary/NotEqualBinary.php b/src/Node/Expression/Binary/NotEqualBinary.php
index 04eb5460..db47a289 100644
--- a/src/Node/Expression/Binary/NotEqualBinary.php
+++ b/src/Node/Expression/Binary/NotEqualBinary.php
@@ -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('))')
         ;
     }
 


Test patch:
diff --git a/tests/Fixtures/expressions/comparison_precedence.test b/tests/Fixtures/expressions/comparison_precedence.test
new file mode 100644
index 00000000..db20a6ec
--- /dev/null
+++ b/tests/Fixtures/expressions/comparison_precedence.test
@@ -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/