c: C2x changes to function type compatibility
authorJoseph Myers <joseph@codesourcery.com>
Thu, 29 Apr 2021 00:50:35 +0000 (00:50 +0000)
committerJoseph Myers <joseph@codesourcery.com>
Thu, 29 Apr 2021 00:50:35 +0000 (00:50 +0000)
commitcc806126215c3f4dc187eff3bf923458d8cc6b4f
tree385f40e18a505a514b5cb9d3b074c6afc6e67d42
parente4ff4ffb43d3d8520f1c106e04421f2e6a021c39
c: C2x changes to function type compatibility

WG14 N2432, the C2x removal of old-style function definitions, also
changed the function type compatibility rules so that an unprototyped
declaration can be compatible with a non-variadic prototyped
declaration even if some function arguments are changed by the default
argument promotions.  I missed that change in the initial
implementation for GCC of the rest of the N2432 changes, but
discussion on the WG14 reflector in February suggests that this is
indeed an intended change.  Implement this in the C front end.

Note that while this may be of use in some cases for use of pointers
to unprototyped function types as a kind of generic function pointer,
it's *not* possible to call such a function without a prototype
visible, without getting runtime undefined behavior from the
(promoted) type used in the call being incompatible with the
(unpromoted) type in the prototype.

Note also that GCC has a longstanding extension to allow compatibility
of such a prototype with an old-style definition specifying the same
type as in the prototype (which is not valid in ISO C, before
old-style definitions were removed in C2x).

Bootstrapped with no regressions for x86_64-pc-linux-gnu.

gcc/c/
* c-typeck.c (function_types_compatible_p): For C2X, treat
unprototyped function as compatible with non-variadic prototyped
function even if some argument types are changed by the default
argument promotions.

gcc/testsuite/
* gcc.dg/c11-unproto-1.c, gcc.dg/c11-unproto-2.c,
gcc.dg/c2x-unproto-1.c, gcc.dg/c2x-unproto-2.c: New tests.
gcc/c/c-typeck.c
gcc/testsuite/gcc.dg/c11-unproto-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c11-unproto-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2x-unproto-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2x-unproto-2.c [new file with mode: 0644]