c++: Fix ICE with -Wmismatched-tags [PR105725]

Here we ICE with -Wmismatched-tags on something like

  template <class T>
  bool B<T, enable_if_t<is_class_v<class T::foo>>>;

Specifically, the "class T::foo" bit.  There, class_decl_loc_t::add gets
a TYPENAME_TYPE as TYPE, rather than a class/union type, so checking
TYPE_BEING_DEFINED will crash.  I think it's OK to allow a TYPENAME_TYPE to
slip into that function; we just shouldn't consider the 'class' tag redundant
(which works as a 'typename').  In fact, every other compiler *requires* it.

	PR c++/105725

gcc/cp/ChangeLog:

	* parser.cc (class_decl_loc_t::add): Check CLASS_TYPE_P.

gcc/testsuite/ChangeLog:

	* g++.dg/warn/Wmismatched-tags-10.C: New test.

(cherry picked from commit d822f4bbd7)
This commit is contained in:
Marek Polacek 2022-05-27 10:51:30 -04:00
parent 57fdcaf17d
commit 2c11a9a380
2 changed files with 13 additions and 2 deletions

View File

@ -33508,7 +33508,8 @@ class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
bool key_redundant = (!def_p && !decl_p
&& (decl == type_decl
|| TREE_CODE (decl) == TEMPLATE_DECL
|| TYPE_BEING_DEFINED (type)));
|| (CLASS_TYPE_P (type)
&& TYPE_BEING_DEFINED (type))));
if (key_redundant
&& class_key != class_type
@ -33546,7 +33547,7 @@ class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
}
else
{
/* TYPE was previously defined in some unknown precompiled hdeader.
/* TYPE was previously defined in some unknown precompiled header.
Simply add a record of its definition at an unknown location and
proceed below to add a reference to it at the current location.
(Declarations in precompiled headers that are not definitions

View File

@ -0,0 +1,10 @@
// PR c++/105725
// { dg-do compile { target c++14 } }
// { dg-options "-Wall -Wmismatched-tags" }
template <bool> struct enable_if;
template <bool Cond> using enable_if_t = typename enable_if<Cond>::type;
template <typename> bool is_class_v;
template <class, class> bool B;
template <class T>
bool B<T, enable_if_t<is_class_v<class T::foo>>>;