Jason,
this fixes 65936, where we have two identical types with mismatching TYPE_CANONICAL. The problem comes from template decl creation vs template instantiation.

At the point we create 'const C<int>' in the operator function we have not applied the 'may_alias' attribute to 'const C<int>'. This means that the following snippet in tree.c:7729 build_pointer_type_for_mode

  if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
    can_alias_all = true;

doesn't set 'can_alias_all'.

Later we complete C<int> and parse the static_cast in the operator function. Completing C<int> will have applied the may_alias attribute. So this time can_alias_all gets set on pointers to C<int> variants. This leads to two different pointer types differing by TYPE_REF_CAN_ALIAS_ALL, each having different CANONICAL_TYPEs. However as TYPE_STRUCTURAL_EQUALITY_P is false, we have a problem. We blow up in comptypes verifying the canonical type machinery.

This patch applies the may_alias attribute to the instantiated decl.

built & tested on x86_64-linux, ok?

nathan
2015-05-21  Nathan Sidwell  <nat...@acm.org>

	cp/
	PR c++/65936
	pt.c (lookup_template_class_1): Apply may_alias attribute here.

	testsuite/
	* g++.dg/template/pr65936.C: New.

Index: cp/pt.c
===================================================================
--- cp/pt.c	(revision 223503)
+++ cp/pt.c	(working copy)
@@ -7856,6 +7856,10 @@ lookup_template_class_1 (tree d1, tree a
 	    = CLASSTYPE_DECLARED_CLASS (template_type);
 	  SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
 	  TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
+	  if (lookup_attribute ("may_alias",
+				TYPE_ATTRIBUTES (template_type)))
+	    TYPE_ATTRIBUTES (t) = tree_cons (get_identifier ("may_alias"),
+					     NULL_TREE, NULL_TREE);
 
 	  /* A local class.  Make sure the decl gets registered properly.  */
 	  if (context == current_function_decl)
Index: testsuite/g++.dg/template/pr65936.C
===================================================================
--- testsuite/g++.dg/template/pr65936.C	(revision 0)
+++ testsuite/g++.dg/template/pr65936.C	(working copy)
@@ -0,0 +1,21 @@
+// checking ICE in canonical typing
+
+class A;
+
+template <typename> struct B
+{
+  typedef A type;
+};
+
+template <class T> class C
+  : public B<T>::type
+{
+} __attribute__ ((__may_alias__));
+
+class A
+{
+  operator const C<int> &()
+  {
+    return *static_cast<const C<int> *> (this);
+  }
+};

Reply via email to