This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

C++ PATCH: PR 27177

This patch fixes PR c++/27177, a tricky ICE-on-valid regression.  The
cast, performed within a sizeof in the initializer for a static data
member, would be a static_cast -- except that the derived class is not
yet complete.  Therefore, it's a reinterpret_cast.  We were trying to
do the derived-to-base conversion, and getting an ICE since the base
class offset isn't yet known.

(This is another example of the weird problems that are caused by the
C++ standard permitting arbitrary expressions inside sizeof; the stuff
inside sizeof is generally not valid in an
integral-constant-expression, but sizeof is.)

Tested on x86_64-unknown-linux-gnu, applied on the mainline and on the
4.1 branch.

Mark Mitchell
(650) 331-3385 x713

2006-06-06  Mark Mitchell  <>

	PR c++/27177
	* call.c (standard_conversion): Require that the derived type be
	complete when performing a derived-to-base conversion.

2006-06-06  Mark Mitchell  <>

	PR c++/27177
	* g++.dg/expr/cast7.C: New test.

Index: gcc/cp/call.c
--- gcc/cp/call.c	(revision 114381)
+++ gcc/cp/call.c	(working copy)
@@ -727,7 +727,19 @@ standard_conversion (tree to, tree from,
 		  that necessitates this conversion is ill-formed.
 		  Therefore, we use DERIVED_FROM_P, and do not check
 		  access or uniqueness.  */
-	       && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
+	       && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from))
+	       /* If FROM is not yet complete, then we must be parsing
+		  the body of a class.  We know what's derived from
+		  what, but we can't actually perform a
+		  derived-to-base conversion.  For example, in:
+		     struct D : public B { 
+                       static const int i = sizeof((B*)(D*)0);
+                     };
+                  the D*-to-B* conversion is a reinterpret_cast, not a
+		  static_cast.  */
+	       && COMPLETE_TYPE_P (TREE_TYPE (from)))
 	  from =
 	    cp_build_qualified_type (TREE_TYPE (to),
Index: gcc/testsuite/g++.dg/expr/cast7.C
--- gcc/testsuite/g++.dg/expr/cast7.C	(revision 0)
+++ gcc/testsuite/g++.dg/expr/cast7.C	(revision 0)
@@ -0,0 +1,13 @@
+// PR c++/27177
+struct X {};
+struct Y : virtual X {};
+struct Z : virtual X {};
+struct A : Y, Z {};
+struct B : A
+  static const int i = sizeof((Z*)(B*)0);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]