[C++ PATCH] Fix dynamic_cast<XXX &> in template (PR c++/34364)
Jakub Jelinek
jakub@redhat.com
Mon Dec 10 22:42:00 GMT 2007
Hi!
The following valid testcase is rejected, because unlike
build_static_cast/build_const_cast/build_reinterpret_cast
build_dynamic_cast doesn't convert_from_reference when
processing_template_decl, so a REFERENCE_TYPE object leaks all the
way through into finish_class_member_access_expr, where it is rejected
as non-class type. When not processing_template_decl,
convert_from_reference is called even in build_dynamic_cast.
Regtested on x86_64-linux, ok for trunk?
2007-12-10 Jakub Jelinek <jakub@redhat.com>
PR c++/34364
* rtti.c (build_dynamic_cast): Call convert_from_reference even for
dynamic_cast in a template.
* g++.dg/rtti/dyncast2.C: New test.
--- gcc/cp/rtti.c.jj 2007-11-01 20:59:36.000000000 +0100
+++ gcc/cp/rtti.c 2007-12-10 23:18:58.000000000 +0100
@@ -728,8 +728,7 @@ build_dynamic_cast (tree type, tree expr
{
expr = build_min (DYNAMIC_CAST_EXPR, type, expr);
TREE_SIDE_EFFECTS (expr) = 1;
-
- return expr;
+ return convert_from_reference (expr);
}
return convert_from_reference (build_dynamic_cast_1 (type, expr));
--- gcc/testsuite/g++.dg/rtti/dyncast2.C.jj 2007-12-10 23:21:13.000000000 +0100
+++ gcc/testsuite/g++.dg/rtti/dyncast2.C 2007-12-10 23:20:09.000000000 +0100
@@ -0,0 +1,31 @@
+// PR c++/34364
+// { dg-do run }
+
+struct A
+{
+ virtual ~A () {}
+};
+
+struct B : public A
+{
+ template <typename T> struct C
+ {
+ static void f (A &a)
+ {
+ dynamic_cast <B &>(a).g ();
+ }
+ };
+
+ B () : c (6) {}
+ void g () { c++; }
+ int c;
+};
+
+B b;
+
+int
+main (void)
+{
+ B::C<int>::f (b);
+ return b.c != 7;
+}
Jakub
More information about the Gcc-patches
mailing list