[clang] [clang][ExprConst] Don't create useless temporary variable (PR #67716)

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 28 11:20:22 PDT 2023


zygoloid wrote:

The semantics of `ArrayInitLoopExpr` are to first evaluate (once, up-front) the common expression, and then evaluate the subexpression once for each array element, where the subexpression can make repeated reference to the value of the common expression. With this change, we will instead evaluate the common expression once for each iteration of the loop. I would expect this change will cause us to miscompile [https://godbolt.org/z/d4so4rfjh](this example):

```c++
struct X {
    int arr[3];
};
constexpr X f(int &r) {
    return {++r, ++r, ++r};
}
constexpr int g() {
    int n = 0;
    auto [a, b, c] = f(n).arr;
    return a + b + c;
}
static_assert(g() == 6)
```

... because we'll interpret the initializer of `[a, b, c]` as `{f(n).arr[0], f(n).arr[1], f(n).arr[2]}` instead of as `<common> = f(n).arr; {<common>[0], <common>[1], <common>[2]}`.

https://github.com/llvm/llvm-project/pull/67716


More information about the cfe-commits mailing list