From patchwork Mon Sep 17 16:03:54 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 184484 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 195802C0078 for ; Tue, 18 Sep 2012 02:04:42 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1348502683; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Message-ID:Date:From:User-Agent:MIME-Version:To:Subject: Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=sJUeoGt uudlt+GNDFXqGYOZTykA=; b=ThbR1gIZ4c+mM0gUas5/w4IVwprdSc03unoy87t uFqGUCJvANNSVGbeZK2WqXn0d9RjioVSISEChNBSItC+/kZzbODnH9U0a//qoMzA +O9KyaGYO35CgJYi28fUJxzOCZFZMRgOdYKCLQv7ohG5+6QlMeZjsEu9xohbRTFP NPxk= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Message-ID:Date:From:User-Agent:MIME-Version:To:Subject:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=AlljaJX/3J409hi6mkiapWad/bgDq6LgD/62oHGr66NV8aI1UCHnwyIRyBeU4n O8wLH/nFZPx12iQmcg/4ZWX0j9RoiK3totXoNz3Dg0hceFpWwbUIwgfGrnnWogpy TksInc6dIaGECqdjNN6TvwWDaXwuU1Z1Sj/upkiIYaMl0=; Received: (qmail 2505 invoked by alias); 17 Sep 2012 16:04:13 -0000 Received: (qmail 2483 invoked by uid 22791); 17 Sep 2012 16:04:11 -0000 X-SWARE-Spam-Status: No, hits=-6.6 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, RCVD_IN_DNSWL_HI, RCVD_IN_HOSTKARMA_W, 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, 17 Sep 2012 16:03:56 +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 q8HG3tqf028960 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 17 Sep 2012 12:03:55 -0400 Received: from [10.3.113.156] (ovpn-113-156.phx2.redhat.com [10.3.113.156]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q8HG3s3n004906 for ; Mon, 17 Sep 2012 12:03:55 -0400 Message-ID: <505749EA.7080807@redhat.com> Date: Mon, 17 Sep 2012 12:03:54 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120828 Thunderbird/15.0 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/53661 (wrong narrowing error with enum) 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 When determining whether a conversion from an unscoped enum is a narrowing conversion, we check whether the values of the enum fit into the target type. [dcl.enum] defines the values of an enum with unspecified underlying type to be the values of the smallest bit-field that could hold it, not all the values representable in the size of the type. This patch fixes that check. Tested x86_64-pc-linux-gnu, applying to trunk and 4.7. commit 373e79aaea9c5b73cebeb1e8fd4fedbdfd553a35 Author: Jason Merrill Date: Fri Sep 14 09:48:48 2012 -0400 PR c++/53661 * typeck2.c (check_narrowing): Avoid false positives on conversion from enumeral type. diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index 6faebb5..58b2db6 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -787,6 +787,9 @@ check_narrowing (tree type, tree init) else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype) && CP_INTEGRAL_TYPE_P (type)) { + if (TREE_CODE (ftype) == ENUMERAL_TYPE) + /* Check for narrowing based on the values of the enumeration. */ + ftype = ENUM_UNDERLYING_TYPE (ftype); if ((tree_int_cst_lt (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (ftype)) || tree_int_cst_lt (TYPE_MIN_VALUE (ftype), diff --git a/gcc/testsuite/g++.dg/init/aggr9.C b/gcc/testsuite/g++.dg/init/aggr9.C new file mode 100644 index 0000000..67d8299 --- /dev/null +++ b/gcc/testsuite/g++.dg/init/aggr9.C @@ -0,0 +1,9 @@ +// PR c++/53661 + +enum Code { + SUCCESS = 0 +}; + +Code a; + +int r[] = {a};