c++: ICE with attribute on enumerator [PR104667]
authorMarek Polacek <polacek@redhat.com>
Thu, 24 Feb 2022 21:41:53 +0000 (16:41 -0500)
committerMarek Polacek <polacek@redhat.com>
Mon, 28 Feb 2022 16:29:25 +0000 (11:29 -0500)
When processing a template, the enumerators we build don't have a type
yet.  But is_late_template_attribute is not prepared to see a _DECL
without a type, so we crash on

  enum tree_code code = TREE_CODE (type);

(I found that we don't give the "is deprecated" warning for the enumerator
'f' in the test.  Reported as PR104682.)

PR c++/104667

gcc/cp/ChangeLog:

* decl2.cc (is_late_template_attribute): Cope with a decl without
a type.

gcc/testsuite/ChangeLog:

* g++.dg/ext/attrib64.C: New test.

gcc/cp/decl2.cc
gcc/testsuite/g++.dg/ext/attrib64.C [new file with mode: 0644]

index 2e58419..22edc2b 100644 (file)
@@ -1298,6 +1298,9 @@ is_late_template_attribute (tree attr, tree decl)
     {
       tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
 
+      if (!type)
+       return true;
+
       /* We can't apply any attributes to a completely unknown type until
         instantiation time.  */
       enum tree_code code = TREE_CODE (type);
diff --git a/gcc/testsuite/g++.dg/ext/attrib64.C b/gcc/testsuite/g++.dg/ext/attrib64.C
new file mode 100644 (file)
index 0000000..4a4505f
--- /dev/null
@@ -0,0 +1,11 @@
+// PR c++/104667
+// { dg-do compile }
+
+template<typename> struct A {
+  enum E { // { dg-warning "only applies to function types" }
+    e __attribute__ ((access(read_only))),
+    f __attribute__ ((deprecated))
+  };
+};
+
+A<int> a;