c++: Fix ICE with -Wmismatched-tags [PR93869]
authorMarek Polacek <polacek@redhat.com>
Fri, 21 Feb 2020 17:58:04 +0000 (12:58 -0500)
committerMarek Polacek <polacek@redhat.com>
Mon, 24 Feb 2020 15:48:31 +0000 (10:48 -0500)
This is a crash in cp_parser_check_class_key:
  tree type_decl = TYPE_MAIN_DECL (type);
  tree name = DECL_NAME (type_decl); // HERE
because TYPE_MAIN_DECL of type was null as it's not a class type.
Instead of checking CLASS_TYPE_P we should simply check class_key
a bit earlier (in this case it was typename_type).

2020-02-24  Marek Polacek  <polacek@redhat.com>

PR c++/93869 - ICE with -Wmismatched-tags.
* parser.c (cp_parser_check_class_key): Check class_key earlier.

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

gcc/cp/ChangeLog
gcc/cp/parser.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wmismatched-tags-2.C [new file with mode: 0644]

index d441add49e161d9e75396025d71bfa58b1a66633..cc0929aaefa2e3993ca7ac6592086f0090625a83 100644 (file)
@@ -1,3 +1,8 @@
+2020-02-24  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/93869 - ICE with -Wmismatched-tags.
+       * parser.c (cp_parser_check_class_key): Check class_key earlier.
+
 2020-02-24  Marek Polacek  <polacek@redhat.com>
 
        PR c++/93712 - ICE with ill-formed array list-initialization.
index 87ed2a3a6482ba3c28de077c67598d800a286bad..01936e8f39a19eee07109a0fb4d0370f8b8a74ad 100644 (file)
@@ -30998,6 +30998,13 @@ cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
   if (!warn_mismatched_tags && !warn_redundant_tags)
     return;
 
+  /* Only consider the true class-keys below and ignore typename_type,
+     etc. that are not C++ class-keys.  */
+  if (class_key != class_type
+      && class_key != record_type
+      && class_key != union_type)
+    return;
+
   tree type_decl = TYPE_MAIN_DECL (type);
   tree name = DECL_NAME (type_decl);
   /* Look up the NAME to see if it unambiguously refers to the TYPE
@@ -31006,13 +31013,6 @@ cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
   tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
   pop_deferring_access_checks ();
 
-  /* Only consider the true class-keys below and ignore typename_type,
-     etc. that are not C++ class-keys.  */
-  if (class_key != class_type
-      && class_key != record_type
-      && class_key != union_type)
-    return;
-
   /* The class-key is redundant for uses of the CLASS_TYPE that are
      neither definitions of it nor declarations, and for which name
      lookup returns just the type itself.  */
index 8c7ae6f3685365db5020f3b774d53084cf05b879..59cedcf7c9eb783b7315a7f8effc2d936aeec309 100644 (file)
@@ -1,3 +1,8 @@
+2020-02-24  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/93869 - ICE with -Wmismatched-tags.
+       * g++.dg/warn/Wmismatched-tags-2.C: New test.
+
 2020-02-20  Mark Eggleston  <mark.eggleston@codethink.com>
 
        PR fortran/93835
diff --git a/gcc/testsuite/g++.dg/warn/Wmismatched-tags-2.C b/gcc/testsuite/g++.dg/warn/Wmismatched-tags-2.C
new file mode 100644 (file)
index 0000000..00193f0
--- /dev/null
@@ -0,0 +1,6 @@
+// PR c++/93869 - ICE with -Wmismatched-tags.
+// { dg-do compile }
+// { dg-options "-Wmismatched-tags" }
+
+namespace N { typedef int T; }
+typename N::T x;