Issue 139818
Summary [clang] constant evaluation of `for` loop does not run destructor for condition variable
Labels good first issue, clang:frontend, accepts-invalid, c++23
Assignees
Reporter zygoloid
    If a condition variable is declared in the second clause of a three-clause `for` loop, clang doesn't destroy it when constant-evaluating the loop. For example:
```c++
struct X {
  constexpr ~X() noexcept(false) { throw "oops"; }
  constexpr operator bool() {
    return b;
  }
  bool b;
};
constexpr bool f() {
  for (bool b = false; X x = {b}; b = true) {}
  return true;
}
static_assert(f());
```
Clang treats this as constant, but the destructor for `x` throws an exception.

It looks like the problem is that we fail to call `IterScope.destroy()` [here](https://github.com/llvm/llvm-project/blob/5f41928689125667cf645835fa9fefee97511d8a/clang/lib/AST/ExprConstant.cpp#L5742), when terminating a loop iteration because the condition evaluated to `false`.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to