From patchwork Mon Aug 8 14:37:23 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 108955 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 5D5DFB6EE8 for ; Tue, 9 Aug 2011 00:37:49 +1000 (EST) Received: (qmail 24979 invoked by alias); 8 Aug 2011 14:37:46 -0000 Received: (qmail 24909 invoked by uid 22791); 8 Aug 2011 14:37:45 -0000 X-SWARE-Spam-Status: No, hits=-6.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, SPF_HELO_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 08 Aug 2011 14:37:25 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p78EbPQr006437 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 8 Aug 2011 10:37:25 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p78EbPnd014239 for ; Mon, 8 Aug 2011 10:37:25 -0400 Received: from [0.0.0.0] (ovpn-113-87.phx2.redhat.com [10.3.113.87]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id p78EbOrq009047 for ; Mon, 8 Aug 2011 10:37:24 -0400 Message-ID: <4E3FF4A3.4010704@redhat.com> Date: Mon, 08 Aug 2011 10:37:23 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:5.0) Gecko/20110719 Thunderbird/5.0 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/50011 (wrong narrowing error) 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 Oops, my last fix was still wrong. This patch just compares min/max values, which should be safe. :) Tested x86_64-pc-linux-gnu, applied to trunk. commit 5efa9cf21a329bba89748ddbc8e3bf3c8ea4329e Author: Jason Merrill Date: Sun Aug 7 17:10:26 2011 -0400 PR c++/50011 * typeck2.c (check_narrowing): Fix integer logic. diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index c6b8c44..0788138 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -740,8 +740,10 @@ check_narrowing (tree type, tree init) else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype) && CP_INTEGRAL_TYPE_P (type)) { - if ((TYPE_PRECISION (type) < TYPE_PRECISION (ftype) - || TYPE_UNSIGNED (type) != TYPE_UNSIGNED (ftype)) + if ((tree_int_cst_lt (TYPE_MAX_VALUE (type), + TYPE_MAX_VALUE (ftype)) + || tree_int_cst_lt (TYPE_MIN_VALUE (ftype), + TYPE_MIN_VALUE (type))) && (TREE_CODE (init) != INTEGER_CST || !int_fits_type_p (init, type))) ok = false; diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist5.C b/gcc/testsuite/g++.dg/cpp0x/initlist5.C index c5ba87d..51345c7 100644 --- a/gcc/testsuite/g++.dg/cpp0x/initlist5.C +++ b/gcc/testsuite/g++.dg/cpp0x/initlist5.C @@ -29,3 +29,7 @@ float fa2[] = { d2, 1.1 }; // PR c++/49577 unsigned u{ -1 }; // { dg-error "narrowing" } char c = char{ u }; // { dg-error "narrowing" } + +// PR c++/50011 +short unsigned su; +int i { su };