File pr31499.patch of Package gcc41
2007-04-09 Mark Mitchell <mark@codesourcery.com>
PR c++/31449
* class.c (build_base_path): Ensure that the converted pointer has
the same cv-qualification as the input.
* g++.dg/init/const5.C: New test.
Index: gcc/testsuite/g++.dg/init/const5.C
===================================================================
--- gcc/testsuite/g++.dg/init/const5.C (revision 0)
+++ gcc/testsuite/g++.dg/init/const5.C (revision 123691)
@@ -0,0 +1,11 @@
+// PR c++/31449
+
+class Foo {};
+class Bar : public Foo {};
+static const Foo *foo = 0;
+
+static Bar *bar = static_cast<const Bar*>(foo); // { dg-error "conversion" }
+
+void func(const Foo *foo) {
+ Bar *bar = static_cast<const Bar*>(foo); // { dg-error "conversion" }
+}
Index: gcc/cp/class.c
===================================================================
--- gcc/cp/class.c (revision 123690)
+++ gcc/cp/class.c (revision 123691)
@@ -303,7 +303,18 @@ build_base_path (enum tree_code code,
field, because other parts of the compiler know that such
expressions are always non-NULL. */
if (!virtual_access && integer_zerop (offset))
- return build_nop (build_pointer_type (target_type), expr);
+ {
+ tree class_type;
+ /* TARGET_TYPE has been extracted from BINFO, and, is
+ therefore always cv-unqualified. Extract the
+ cv-qualifiers from EXPR so that the expression returned
+ matches the input. */
+ class_type = TREE_TYPE (TREE_TYPE (expr));
+ target_type
+ = cp_build_qualified_type (target_type,
+ cp_type_quals (class_type));
+ return build_nop (build_pointer_type (target_type), expr);
+ }
null_test = error_mark_node;
}