Patchwork C++ PATCH for c++/49085 (ICE with offsetof in template)

login
register
mail settings
Submitter Jason Merrill
Date July 1, 2011, 1:35 p.m.
Message ID <4E0DCD37.7050605@redhat.com>
Download mbox | patch
Permalink /patch/102906/
State New
Headers show

Comments

Jason Merrill - July 1, 2011, 1:35 p.m.
We can't take the offsetof a field until after we've laid out the class.

Tested x86_64-pc-linux-gnu, applying to trunk.

Patch

commit 92f52fccaaf25b5791c0f8bff930fe75d25bd90b
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Jun 30 23:05:49 2011 -0400

    	PR c++/49085
    	* semantics.c (finish_offsetof): Complain about incomplete type.

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index ad68a01..a704aa3 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -3422,6 +3422,12 @@  finish_offsetof (tree expr)
     }
   if (REFERENCE_REF_P (expr))
     expr = TREE_OPERAND (expr, 0);
+  if (TREE_CODE (expr) == COMPONENT_REF)
+    {
+      tree object = TREE_OPERAND (expr, 0);
+      if (!complete_type_or_else (TREE_TYPE (object), object))
+	return error_mark_node;
+    }
   return fold_offsetof (expr, NULL_TREE);
 }
 
diff --git a/gcc/testsuite/g++.dg/template/offsetof2.C b/gcc/testsuite/g++.dg/template/offsetof2.C
new file mode 100644
index 0000000..da888f7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/offsetof2.C
@@ -0,0 +1,10 @@ 
+// PR c++/49085
+
+template <class T>
+struct A			// { dg-error "declaration" }
+{
+  int i, j;
+  int ar[__builtin_offsetof(A,j)]; // { dg-error "incomplete type" }
+};
+
+A<int> a;