Bug 102174

Summary: Unused result of undefined behavior arithmetic is accepted during constant evaluation
Product: gcc Reporter: Johel Ernesto Guerrero Peña <johelegp>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: NEW ---    
Severity: normal CC: johelegp, webrown.cpp
Priority: P3 Keywords: accepts-invalid
Version: 12.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2021-12-04 00:00:00
Bug Depends on:    
Bug Blocks: 55004    

Description Johel Ernesto Guerrero Peña 2021-09-02 11:07:54 UTC
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
```
Comment 1 Drea Pinski 2021-12-04 13:03:40 UTC
Confirmed.