c++: fix ICE with -Wduplicated-cond [PR107593]
commit623730d954a051941ae6a098f851bef308916ca0
authorMarek Polacek <polacek@redhat.com>
Thu, 26 Jan 2023 14:34:28 +0000 (26 09:34 -0500)
committerMarek Polacek <polacek@redhat.com>
Tue, 31 Jan 2023 16:35:45 +0000 (31 11:35 -0500)
tree0c319d577e791f6a825274df677c6c409f811116
parenta39c6ec97906766ad65d15d4856fd41121ee7a45
c++: fix ICE with -Wduplicated-cond [PR107593]

Here we crash because a CAST_EXPR, representing T(), doesn't have
its operand, and operand_equal_p's STRIP_ANY_LOCATION_WRAPPER doesn't
expect that.  (o_e_p is called from warn_duplicated_cond_add_or_warn.)

In the past we've adjusted o_e_p to better cope with template codes,
but in this case I think we just want to avoid attempting to warn
about inst-dependent expressions; I don't think I've ever envisioned
-Wduplicated-cond to warn about them.  Also destroy the chain when
an inst-dependent expression is encountered to not warn in
Wduplicated-cond4.C.

The ICE started with r12-6022, two-stage name lookup for overloaded
operators, which gave dependent operators a TREE_TYPE (in particular,
DEPENDENT_OPERATOR_TYPE), so we no longer bail out here in o_e_p:

  /* Similar, if either does not have a type (like a template id),
     they aren't equal.  */
  if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
    return false;

PR c++/107593
PR c++/108597

gcc/c-family/ChangeLog:

* c-common.h (instantiation_dependent_expression_p): Declare.
* c-warn.cc (warn_duplicated_cond_add_or_warn): If the condition
is dependent, invalidate the chain.

gcc/c/ChangeLog:

* c-objc-common.cc (instantiation_dependent_expression_p): New.

gcc/cp/ChangeLog:

* cp-tree.h (instantiation_dependent_expression_p): Don't
declare here.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Wduplicated-cond3.C: New test.
* g++.dg/warn/Wduplicated-cond4.C: New test.
* g++.dg/warn/Wduplicated-cond5.C: New test.
gcc/c-family/c-common.h
gcc/c-family/c-warn.cc
gcc/c/c-objc-common.cc
gcc/cp/cp-tree.h
gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wduplicated-cond4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wduplicated-cond5.C [new file with mode: 0644]