Skip to content

[clang] constant evaluation of for loop does not run destructor for condition variable #139818

@zygoloid

Description

@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:

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, when terminating a loop iteration because the condition evaluated to false.

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions