Bug 115290 - [12 Regression] tree check fail in c_tree_printer, at c/c-objc-common.cc:330
Summary: [12 Regression] tree check fail in c_tree_printer, at c/c-objc-common.cc:330
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 15.0
: P2 normal
Target Milestone: 12.5
Assignee: Jakub Jelinek
URL:
Keywords: diagnostic, ice-checking, ice-on-valid-code
Depends on:
Blocks:
 
Reported: 2024-05-30 07:50 UTC by David Binderman
Modified: 2024-06-20 09:32 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2024-05-30 00:00:00


Attachments
gcc15-pr115290.patch (788 bytes, patch)
2024-06-17 13:20 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description David Binderman 2024-05-30 07:50:38 UTC
For this C code:

$ more bug1029.c
typedef enum { SERVER_HELLO_DONE } message_type_t;
message_type_t handshakes[256][32], tls13_handshakes[256][32];
void main() { (0 ? tls13_handshakes : handshakes) == tls13_handshakes; }
$ 

compiled as follows:

$ ~/gcc/results/bin/gcc -c -Wall bug1029.c 

does this:

bug1029.c:3:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
    3 | void main() { (0 ? tls13_handshakes : handshakes) == tls13_handshakes; }
      |      ^~~~
bug1029.c: In function ‘main’:
bug1029.c:3:51: warning: comparison between two arrays [-Warray-compare]
    3 | void main() { (0 ? tls13_handshakes : handshakes) == tls13_handshakes; }
      |                                                   ^~

tree check: expected tree that contains ‘decl minimal’ structure, have ‘cond_exp
r’ in c_tree_printer, at c/c-objc-common.cc:330
    3 | void main() { (0 ? tls13_handshakes : handshakes) == tls13_handshakes; }
      | ^~~~
0x81bb1e tree_contains_struct_check_failed(tree_node const*, tree_node_structure
_enum, char const*, int, char const*)
	../../trunk.20210101/gcc/tree.cc:9169
0x6e18a8 contains_struct_check(tree_node*, tree_node_structure_enum, char const*
, int, char const*)
	../../trunk.20210101/gcc/tree.h:3770
0x6e18a8 c_tree_printer
	../../trunk.20210101/gcc/c/c-objc-common.cc:330

The bug first appeared sometime before git hash 28b508233a12c132.
Comment 1 Andrew Pinski 2024-05-30 14:50:16 UTC
<source>:4:50: note: use '&\204[0] == &tls13_handshakes[0]' to compare the addresses

I think it was introduced with r12-4148-g2dda00b734888d which also introduced the warning. Since the diagnostic looks broken with release checking.


+       inform (location, "use %<&%D[0] %s &%D[0]%> to compare the addresses",
+               op0, op_symbol_code (code), op1);


%D here is expecting a decl but we have an expression.
Comment 2 Jakub Jelinek 2024-06-17 13:20:38 UTC
Created attachment 58449 [details]
gcc15-pr115290.patch

Untested fix.
Comment 3 GCC Commits 2024-06-17 17:24:57 UTC
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:b63c7d92012f92e0517190cf263d29bbef8a06bf

commit r15-1381-gb63c7d92012f92e0517190cf263d29bbef8a06bf
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Jun 17 19:24:05 2024 +0200

    c-family: Fix -Warray-compare warning ICE [PR115290]
    
    The warning code uses %D to print the ARRAY_REF first operands.
    That works in the most common case where those operands are decls, but
    as can be seen on the following testcase, they can be other expressions
    with array type.
    Just changing %D to %E isn't enough, because then the diagnostics can
    suggest something like
    note: use '&(x) != 0 ? (int (*)[32])&a : (int (*)[32])&b[0] == &(y) != 0 ? (int (*)[32])&a : (int (*)[32])&b[0]' to compare the addresses
    which is a bad suggestion, the %E printing doesn't know that the
    warning code will want to add & before it and [0] after it.
    So, the following patch adds ()s around the operand as well, but does
    that only for non-decls, for decls keeps it as &arr[0] like before.
    
    2024-06-17  Jakub Jelinek  <jakub@redhat.com>
    
            PR c/115290
            * c-warn.cc (do_warn_array_compare): Use %E rather than %D for
            printing op0 and op1; if those operands aren't decls, also print
            parens around them.
    
            * c-c++-common/Warray-compare-3.c: New test.
Comment 4 GCC Commits 2024-06-17 17:27:07 UTC
The releases/gcc-14 branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:922648759b034c356e7d5c1ae530bdb6f3d00c62

commit r14-10322-g922648759b034c356e7d5c1ae530bdb6f3d00c62
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Jun 17 19:24:05 2024 +0200

    c-family: Fix -Warray-compare warning ICE [PR115290]
    
    The warning code uses %D to print the ARRAY_REF first operands.
    That works in the most common case where those operands are decls, but
    as can be seen on the following testcase, they can be other expressions
    with array type.
    Just changing %D to %E isn't enough, because then the diagnostics can
    suggest something like
    note: use '&(x) != 0 ? (int (*)[32])&a : (int (*)[32])&b[0] == &(y) != 0 ? (int (*)[32])&a : (int (*)[32])&b[0]' to compare the addresses
    which is a bad suggestion, the %E printing doesn't know that the
    warning code will want to add & before it and [0] after it.
    So, the following patch adds ()s around the operand as well, but does
    that only for non-decls, for decls keeps it as &arr[0] like before.
    
    2024-06-17  Jakub Jelinek  <jakub@redhat.com>
    
            PR c/115290
            * c-warn.cc (do_warn_array_compare): Use %E rather than %D for
            printing op0 and op1; if those operands aren't decls, also print
            parens around them.
    
            * c-c++-common/Warray-compare-3.c: New test.
    
    (cherry picked from commit b63c7d92012f92e0517190cf263d29bbef8a06bf)
Comment 5 GCC Commits 2024-06-17 17:28:55 UTC
The releases/gcc-13 branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:be14e6cf7f2dc23012dfced0a4aff0894fd6ff57

commit r13-8854-gbe14e6cf7f2dc23012dfced0a4aff0894fd6ff57
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Jun 17 19:24:05 2024 +0200

    c-family: Fix -Warray-compare warning ICE [PR115290]
    
    The warning code uses %D to print the ARRAY_REF first operands.
    That works in the most common case where those operands are decls, but
    as can be seen on the following testcase, they can be other expressions
    with array type.
    Just changing %D to %E isn't enough, because then the diagnostics can
    suggest something like
    note: use '&(x) != 0 ? (int (*)[32])&a : (int (*)[32])&b[0] == &(y) != 0 ? (int (*)[32])&a : (int (*)[32])&b[0]' to compare the addresses
    which is a bad suggestion, the %E printing doesn't know that the
    warning code will want to add & before it and [0] after it.
    So, the following patch adds ()s around the operand as well, but does
    that only for non-decls, for decls keeps it as &arr[0] like before.
    
    2024-06-17  Jakub Jelinek  <jakub@redhat.com>
    
            PR c/115290
            * c-warn.cc (do_warn_array_compare): Use %E rather than %D for
            printing op0 and op1; if those operands aren't decls, also print
            parens around them.
    
            * c-c++-common/Warray-compare-3.c: New test.
    
    (cherry picked from commit b63c7d92012f92e0517190cf263d29bbef8a06bf)
Comment 6 Jakub Jelinek 2024-06-17 17:30:35 UTC
Should be fixed for 13.4+/14.2+/15.1+ for now, 12 branch is frozen right now, so it will need to wait for 12.5.
Comment 7 GCC Commits 2024-06-20 08:54:48 UTC
The releases/gcc-12 branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:8f612e6559b39569747894ec0f8b4694b96492a7

commit r12-10568-g8f612e6559b39569747894ec0f8b4694b96492a7
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Jun 17 19:24:05 2024 +0200

    c-family: Fix -Warray-compare warning ICE [PR115290]
    
    The warning code uses %D to print the ARRAY_REF first operands.
    That works in the most common case where those operands are decls, but
    as can be seen on the following testcase, they can be other expressions
    with array type.
    Just changing %D to %E isn't enough, because then the diagnostics can
    suggest something like
    note: use '&(x) != 0 ? (int (*)[32])&a : (int (*)[32])&b[0] == &(y) != 0 ? (int (*)[32])&a : (int (*)[32])&b[0]' to compare the addresses
    which is a bad suggestion, the %E printing doesn't know that the
    warning code will want to add & before it and [0] after it.
    So, the following patch adds ()s around the operand as well, but does
    that only for non-decls, for decls keeps it as &arr[0] like before.
    
    2024-06-17  Jakub Jelinek  <jakub@redhat.com>
    
            PR c/115290
            * c-warn.cc (do_warn_array_compare): Use %E rather than %D for
            printing op0 and op1; if those operands aren't decls, also print
            parens around them.
    
            * c-c++-common/Warray-compare-3.c: New test.
    
    (cherry picked from commit b63c7d92012f92e0517190cf263d29bbef8a06bf)
Comment 8 Richard Biener 2024-06-20 09:15:43 UTC
GCC 12.4 is being released, retargeting bugs to GCC 12.5.
Comment 9 Jakub Jelinek 2024-06-20 09:32:55 UTC
Fixed for 12.5 as well.