From patchwork Fri Nov 12 16:04:17 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Dennett X-Patchwork-Id: 70983 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 9F0D7B7139 for ; Sat, 13 Nov 2010 03:05:15 +1100 (EST) Received: (qmail 29949 invoked by alias); 12 Nov 2010 16:05:12 -0000 Received: (qmail 29933 invoked by uid 22791); 12 Nov 2010 16:05:11 -0000 X-SWARE-Spam-Status: No, hits=-7.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from smtp-out.google.com (HELO smtp-out.google.com) (74.125.121.35) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 12 Nov 2010 16:04:22 +0000 Received: from kpbe17.cbf.corp.google.com (kpbe17.cbf.corp.google.com [172.25.105.81]) by smtp-out.google.com with ESMTP id oACG4IPS015271 for ; Fri, 12 Nov 2010 08:04:19 -0800 Received: from qwf7 (qwf7.prod.google.com [10.241.194.71]) by kpbe17.cbf.corp.google.com with ESMTP id oACG4HrD013480 for ; Fri, 12 Nov 2010 08:04:17 -0800 Received: by qwf7 with SMTP id 7so1353886qwf.7 for ; Fri, 12 Nov 2010 08:04:17 -0800 (PST) MIME-Version: 1.0 Received: by 10.224.2.194 with SMTP id 2mr2464982qak.56.1289577857071; Fri, 12 Nov 2010 08:04:17 -0800 (PST) Received: by 10.220.23.5 with HTTP; Fri, 12 Nov 2010 08:04:17 -0800 (PST) Date: Fri, 12 Nov 2010 10:04:17 -0600 Message-ID: Subject: C++ PATCH for PR/39415 (type of static_cast(Base*)) From: James Dennett To: gcc-patches@gcc.gnu.org Cc: Jason Merrill X-System-Of-Record: true Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org 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))