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

liu zhenyu <lzy20000419 at outlook dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lzy20000419 at outlook dot com

--- Comment #18 from liu zhenyu <lzy20000419 at outlook dot com> ---
On the ice-on-invalid-fuzzy keyword: the comment 0 testcase is indeed invalid,
but the ICE does not need invalid code. Here is a reduction with no errors and
no call, so it also avoids the by-value passing of a variable-size union that
comment 7 questions:

    int foo (int n, int x)
    {
      return ((union { int a; char b[n]; }) x).b[0];
    }

    $ gcc -O0 -c t.c
    t.c: In function 'foo':
    t.c:3:11: internal compiler error: in force_constant_size, at
gimplify.cc:813

The only diagnostics GCC gives for this code are -Wpedantic warnings ("a member
of a structure or union cannot have a variably modified type" and "ISO C
forbids casts to union type"). Without -Wpedantic it is accepted silently up to
the ICE, at -O0 and -O2. The same ICE (in force_constant_size) occurs on 4.4.7,
4.9.4, 5.4, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.4, 14.4, 15.3, 16.2 and
trunk (17.0.0 20261119). 4.0.4 and 4.1.2 ICE in create_tmp_var, and 3.4.6 ICEs
in assign_stack_temp_for_type. So this form has never worked.

The comment 4 testcase still ICEs on trunk with -std=gnu17. With the default
-std=gnu23 it now gets "too many arguments to function 'bar'", because void bar
(); is a prototype in C23, so it no longer reaches the ICE with default
options. That may be why it looks like invalid code today. 3.4.6 compiles it
and 4.1.2 ICEs (both with their default -std), and 14.4, 16.2 and trunk ICE
with -std=gnu17.

Assigning the cast to a variable of the same union type compiles fine on 3.4.6,
4.1.2, 7.5, 14.4, 16.2 and trunk:

    int foo (int n, int x)
    {
      union U { int a; char b[n]; } u;
      u = (union U) x;
      return u.b[0];
    }

So the ICE only happens when the gimplifier has to create a temporary of the
variable-size union type for the cast (gimple_add_tmp_var ->
force_constant_size). The compound literal spelling ((union { int a; char b[n];
}) { x }).b[0] is rejected on trunk with "variable-sized object may not be
initialized except with an empty initializer". The cast-to-union path in
build_c_cast (gcc/c/c-typeck.cc) has no such check. Either a similar error for
a cast to a variable-size union, or the VLA temporary suggested in comment 6,
would avoid the ICE.

Godbolt (trunk and 4.1.2): https://godbolt.org/z/94dhvcGz9

Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.

Reply via email to