This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
C++ PATCH for PR/39415 (type of static_cast<cv Derived*>(Base*))
- From: James Dennett <jdennett at google dot com>
- To: gcc-patches at gcc dot gnu dot org
- Cc: Jason Merrill <jason at redhat dot com>
- Date: Fri, 12 Nov 2010 10:04:17 -0600
- Subject: C++ PATCH for PR/39415 (type of static_cast<cv Derived*>(Base*))
This is a (morally 2-line) fix for PR/39415 (duplicated as PR/44916),
where static_cast<cv Derived *> could give the wrong cv-qualification,
with a short regression test.
I've tested this on Linux/x86-64 (only).
My thanks to Jason Merrill for supplying me with clues about how to
prepare and submit this .
Additional clues from others are very welcome.
-- James
Index: ChangeLog
===================================================================
--- ChangeLog (revision 166644)
+++ ChangeLog (working copy)
@@ -1,3 +1,8 @@
+2010-11-12 James Dennett <jdennett@google.com>
+ PR/39415
+ * cp/typeck.c (build_static_cast_1): Convert to the target type
+ when doing static_cast<cv Derived*>(Base*).
+
2010-11-11 Nathan Froyd <froydnj@codesourcery.com>
PR c/44782
Index: testsuite/g++.dg/expr/static_cast7.C
===================================================================
--- testsuite/g++.dg/expr/static_cast7.C (revision 0)
+++ testsuite/g++.dg/expr/static_cast7.C (revision 0)
@@ -0,0 +1,10 @@
+// Regression test for bug 39415 (and its duplicate 44916).
+struct S {};
+struct T : S {};
+int f(const T*) {}
+void f(T*);
+int main() {
+ S* s(0);
+ int a = f(static_cast<const T*>(s));
+ int b = f(static_cast<const T*>(0));
+}
Index: cp/typeck.c
===================================================================
--- cp/typeck.c (revision 166644)
+++ cp/typeck.c (working copy)
@@ -5884,7 +5884,8 @@ build_static_cast_1 (tree type, tree exp
base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
c_cast_p ? ba_unique : ba_check,
NULL);
- return build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false);
+ expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false);
+ return cp_fold_convert(type, expr);
}
if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))