https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118968

            Bug ID: 118968
           Summary: GCC  compiles illegal label in constexpr
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: qurong at ios dot ac.cn
  Target Milestone: ---

The compiler gcc10.1 accepts this program. The other versions of GCC and all
versions of clang will reject it.

Test Program:

template <int> void f1() {}

constexpr int foo() {
    lab: return 1; // Illegal label, `goto` statements or labels are not
allowed in `constexpr` functions
}

int main() {
    constexpr int a = foo(); 
    return 0;
}

Problem Description:

Illegal Label: The label lab: is used within the constexpr function foo.
According to the C++ standard, constexpr functions do not allow goto statements
or any form of labels, as these operations may introduce runtime behavior,
which contradicts the compile-time evaluation requirement of constexpr
functions. However, the GCC 10.1 compiler does not diagnose this issue.

Expected Behavior:

The GCC compiler should diagnose the illegal label within the constexpr
function foo and refuse to compile the program. If the compiler allows the
program to compile successfully, the behavior of the program should be
undefined, but in this case, the compiler should provide clear warnings or
error messages pointing out the issues in the code.

Reply via email to