llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.83k stars 11.91k forks source link

Unsequenced modifications of variable within a constexpr function used in constant expression context not considered ill-formed #37768

Open llvmbot opened 6 years ago

llvmbot commented 6 years ago
Bugzilla Link 38420
Version trunk
OS All
Reporter LLVM Bugzilla Contributor
CC @dwblaikie,@DougGregor

Extended Description

Given

constexpr int f(int x) {return x++ + x++;}

int main() {
   constexpr int x = 2;
   constexpr int y = f(x); 
}

clang does not consider the declaration of y ill-formed but I believe it should.

x++ + x++;

is undefined behavior since we have unsequenced modifications to x. See [intro.execution]p10 http://eel.is/c++draft/intro.execution#10 and this should be ill-formed in a context requiring a constant expression since undefined behavior is ill-formed in a constant expression see [expr.cons]p2.6 http://eel.is/c++draft/expr.const#2.6

wheatman commented 9 months ago

This code is accepted by all of clang post 18 trunk(d9a9872ec4760762fdc467ef283cea302a3742e5), gcc13.2, msvc 19.38, and EDG 6.6 https://godbolt.org/z/qP4T96hef code

constexpr int f(int x) {return x++ + x++;}

int main() {
   constexpr int x = 2;
   constexpr int y = f(x); 
   return y;
}

clang has a warning (without any extra flags specified) the rest have no warnings (without specifying any flags)

clang warning

<source>:1:33: warning: multiple unsequenced modifications to 'x' [-Wunsequenced]
    1 | constexpr int f(int x) {return x++ + x++;}
      |                                 ^     ~~
1 warning generated.
Compiler returned: 0
llvmbot commented 9 months ago

@llvm/issue-subscribers-clang-frontend

Author: None (llvmbot)

| | | | --- | --- | | Bugzilla Link | [38420](https://llvm.org/bz38420) | | Version | trunk | | OS | All | | Reporter | LLVM Bugzilla Contributor | | CC | @dwblaikie,@DougGregor | ## Extended Description Given ```cpp constexpr int f(int x) {return x++ + x++;} int main() { constexpr int x = 2; constexpr int y = f(x); } ``` clang does not consider the declaration of y ill-formed but I believe it should. ```cpp x++ + x++; ``` is undefined behavior since we have unsequenced modifications to x. See [intro.execution]p10 http://eel.is/c++draft/intro.execution#10 and this should be ill-formed in a context requiring a constant expression since undefined behavior is ill-formed in a constant expression see [expr.cons]p2.6 http://eel.is/c++draft/expr.const#2.6
zygoloid commented 1 month ago

This is CWG2192.