Skip to content

[clang] Break statements with unknown named loops should interrupt constant evaluation #156801

@tbaederr

Description

@tbaederr

For this code:

constexpr int f1() {
  a: for (;;) {
    break azzz;
  }
  return 1;
}
static_assert(f1() == 1);

we currently emit:

array.cpp:102:11: error: 'break' label does not name an enclosing loop or 'switch'
  102 |     break azzz;
      |           ^
array.cpp:106:15: error: static assertion expression is not an integral constant expression
  106 | static_assert(f1() == 1);
      |               ^~~~~~~~~
array.cpp:101:15: note: constexpr evaluation hit maximum step limit; possible infinite loop?
  101 |   a: for (;;) {
      |               ^
array.cpp:106:15: note: in call to 'f1()'
  106 | static_assert(f1() == 1);
      |               ^~~~

(No godbolt link as it doesn't work there yet)

We emit an error for the break statement, but when evaluating the function call at compile time, we completely ignore the break statement altogether. We could handle it like a regular break; which would work in this case, but not for nested loops. We should simply interrupt constant evaluation of this function completely since we have already emitted an error for it.

CC @Sirraide

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constexprAnything related to constant evaluation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions