C: PR c/79412: Poison decls with error_mark_node after type mismatch
authorRoger Sayle <roger@nextmovesoftware.com>
Wed, 1 Sep 2021 07:38:39 +0000 (08:38 +0100)
committerRoger Sayle <roger@nextmovesoftware.com>
Wed, 1 Sep 2021 07:38:39 +0000 (08:38 +0100)
commit823685221de986afb729910a6f2237f07a377f17
tree3397097f226bb1f8809c72f58a346beb41ffa10b
parentd2874d905647a1d146dafa60199d440e837adc4d
C: PR c/79412: Poison decls with error_mark_node after type mismatch

This patch fixes an ICE during error-recovery regression in the C front-end.
The symptom is that the middle-end's sanity checking assertions fail during
gimplification when being asked to increment an array, which is non-sense.
The issue is that the C-front end has detected the type mismatch and
reported an error to the user, but hasn't provided any indication of this
to the middle-end, simply passing bogus trees that the optimizers recognize
as invalid.

This appears to be a frequently reported ICE with 94730, 94731, 101036
and 101365 all marked as duplicates.

I believe the correct (polite) fix is to mark the mismatched types as
problematic/dubious in the front-end, when the error is spotted, so that
the middle-end has a heads-up and can be a little more forgiving.  This
patch to c-decl.c's duplicate_decls sets (both) mismatched types to
error_mark_node if they are significantly different, and we've issued
an error message.  Alas, this is too punitive for FUNCTION_DECLs where
we store return types, parameter lists, parameter types and attributes
in the type, but fortunately the middle-end is already more cautious
about trusting possibly suspect function types.

This fix required one minor change to the testsuite, typedef-var-2.c
where after conflicting type definitions, we now no longer assume that
the (first or) second definition is the correct one.  This change only
affects the behaviour after seen_error(), so should be relatively safe.

2021-09-01  Roger Sayle  <roger@nextmovesoftware.com>
    Joseph Myers  <joseph@codesourcery.com>

gcc/c/ChangeLog
PR c/79412
* c-decl.c (duplicate_decls): On significant mismatches, mark the
types of both (non-function) decls as error_mark_node, so that the
middle-end can see the code is malformed.
(free_attr_access_data): Don't process if the type has been set to
error_mark_node.

gcc/testsuite/ChangeLog
PR c/79412
* gcc.dg/pr79412.c: New test case.
* gcc.dg/typedef-var-2.c: Update expeted errors.
gcc/c/c-decl.c
gcc/testsuite/gcc.dg/pr79412.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/typedef-var-2.c