https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102174

            Bug ID: 102174
           Summary: Unused result of undefined behavior arithmetic is
                    accepted during constant evaluation
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: accepts-invalid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: johelegp at gmail dot com
                CC: johelegp at gmail dot com
  Target Milestone: ---

See https://godbolt.org/z/d38ozc91x.
```C++
#include <cstdint>
constexpr bool f() {
  1 / 0;
  1.0 / 0;
  1 % 0;
  INT32_MIN / INT32_C(-1);
  INT32_MIN * INT32_C(-1);
  INT32_MAX + INT32_C(1);
  INT16_C(INT32_MAX + INT32_C(1));
  return true;
}
constexpr bool b{f()};
```
```
<source>: In function 'constexpr bool f()':
<source>:3:5: warning: division by zero [-Wdiv-by-zero]
    3 |   1 / 0;
      |   ~~^~~
<source>:3:5: warning: statement has no effect [-Wunused-value]
<source>:4:7: warning: division by zero [-Wdiv-by-zero]
    4 |   1.0 / 0;
      |   ~~~~^~~
<source>:4:7: warning: statement has no effect [-Wunused-value]
<source>:5:5: warning: division by zero [-Wdiv-by-zero]
    5 |   1 % 0;
      |   ~~^~~
<source>:5:5: warning: statement has no effect [-Wunused-value]
<source>:6:13: warning: integer overflow in expression of type 'int' results in
'-2147483648' [-Woverflow]
    6 |   INT32_MIN / INT32_C(-1);
      |             ^
<source>:6:13: warning: statement has no effect [-Wunused-value]
<source>:7:13: warning: integer overflow in expression of type 'int' results in
'-2147483648' [-Woverflow]
    7 |   INT32_MIN * INT32_C(-1);
      |             ^
<source>:7:13: warning: statement has no effect [-Wunused-value]
<source>:8:13: warning: integer overflow in expression of type 'int' results in
'-2147483648' [-Woverflow]
    8 |   INT32_MAX + INT32_C(1);
      |             ^
<source>:8:13: warning: statement has no effect [-Wunused-value]
In file included from
/opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/include/stdint.h:9,
                 from
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/cstdint:41,
                 from <source>:1:
<source>:9:21: warning: integer overflow in expression of type 'int' results in
'-2147483648' [-Woverflow]
    9 |   INT16_C(INT32_MAX + INT32_C(1));
      |                     ^
<source>:9:21: warning: statement has no effect [-Wunused-value]
Compiler returned: 0
```

Reply via email to