From patchwork Fri Nov 12 16:04:17 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: C++ PATCH for PR/39415 (type of static_cast(Base*)) Date: Fri, 12 Nov 2010 06:04:17 -0000 From: James Dennett X-Patchwork-Id: 70983 Message-Id: To: gcc-patches@gcc.gnu.org Cc: Jason Merrill This is a (morally 2-line) fix for PR/39415 (duplicated as PR/44916), where static_cast 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 + PR/39415 + * cp/typeck.c (build_static_cast_1): Convert to the target type + when doing static_cast(Base*). + 2010-11-11 Nathan Froyd 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(s)); + int b = f(static_cast(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))