Issue 184100
Summary Clang rejects valid chained relational _expression_ without -Werror
Labels clang
Assignees
Reporter vrukesh
    **What code / commands /steps will reproduce the problem?**
Compile the below sample code:

`
#include <stdio.h>
int main(void) {
    int i;
    i = 3 < 6 < 0;
    printf("Result = %d\n", i);
    return 0;
}
`

Godbolt compiler explorer link: https://godbolt.org/z/cacbWeh71


**What is the expected result?**
As per ISO-IEC 9899-2018:  Section 6.5.8 "Relational operators", para 6:
Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or
equal to) shall yield 1 if the specified relation is true and 0 if it is false.111) The result has type int.


**What happens instead?**
Compilation in GCC passes.
Compilation in Clang fails with an error.
`
<source>:4:15: error: chained comparison 'X < Y < Z' does not behave the same as a mathematical _expression_ [-Wparentheses]
    4 |     i = 3 < 6 < 0;
      |               ^
1 error generated.
Compiler returned: 1
`

Relational operators are left-associative.
The problematic line is the chained comparison `i = 3 < 6 < 0;`. In C99 this is parsed left-to-right as `(3 < 6) < 0`; `3 < 6` yields `1`, so `1 < 0` yields `0`. The _expression_ is well-defined in C; Clang is rejecting it.
The _expression_ is valid per C99 (relational operators are left-associative and yield `int` 0/1), so it should compile; at most a warning is permissible, not a hard error unless `-Werror` is in effect.

Fix the behavior in Clang.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to