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

            Bug ID: 124573
           Summary: internal compiler error in finish_enum_value_list with
                    templated scoped enum redeclaration
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Keywords: error-recovery, ice-checking, ice-on-invalid-code
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: attackerj1113 at gmail dot com
  Target Milestone: ---

The following code causes ICE out on x86-64 gcc since version 15.1(assertions)
and reproducible on trunk:

Maybe related: Bug 124562 Bug 124561

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <typename T>
struct A {
    enum E : T;
};

using X = A<int>;

template <typename T>
enum X::E : T { e1 };

template <>
enum A<int>::E : int {};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
https://godbolt.org/z/Yoao4M8nK

The code is invalid: it forward-declares a dependent scoped enum E : T inside a
class template, then tries to define X::E as a templated enum and finally
redeclares A<int>::E with a fixed underlying type. Instead of just rejecting
this bad code with normal diagnostics, GCC crashes with an internal compiler
error in its enum handling (finish_enum_value_list).

Compiler Output:

<source>:9:13: error: different underlying type in enum 'enum A<int>::E'
[-Wtemplate-body]
    9 | enum X::E : T { e1 };
      |             ^
<source>:3:14: note: previous definition here
    3 |     enum E : T;
      |              ^
<source>:9:6: error: cannot add an enumerator list to a template instantiation
[-Wtemplate-body]
    9 | enum X::E : T { e1 };
      |      ^
<source>:12:23: internal compiler error: Segmentation fault
   12 | enum A<int>::E : int {};
      |                       ^
0x2967708 diagnostics::context::diagnostic_impl(rich_location*,
diagnostics::metadata const*, diagnostics::option_id, char const*,
__va_list_tag (*) [1], diagnostics::kind)
        ???:0
0x295c34b internal_error(char const*, ...)
        ???:0
0xc12f83 finish_enum_value_list(tree_node*)
        ???:0
0xd6ef63 c_parse_file()
        ???:0
0xef94f9 c_common_parse_file()
        ???:0
/cefs/66/66ce76984aaae5f97de46d73_gcc-trunk-20260319/bin/../libexec/gcc/x86_64-linux-gnu/16.0.1/cc1plus
-quiet -imultiarch x86_64-linux-gnu -iprefix
/cefs/66/66ce76984aaae5f97de46d73_gcc-trunk-20260319/bin/../lib/gcc/x86_64-linux-gnu/16.0.1/
-D_GNU_SOURCE <source> -quiet -dumpdir /app/ -dumpbase output.cpp -dumpbase-ext
.cpp -masm=intel -mtune=generic -march=x86-64 -g -fdiagnostics-color=always
-fno-verbose-asm -o /app/output.s
Please submit a full bug report, with preprocessed source (by using
-freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
Compiler returned: 1

Reply via email to