From patchwork Thu Mar 17 22:02:25 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 87440 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 18B44B6FD3 for ; Fri, 18 Mar 2011 09:02:41 +1100 (EST) Received: (qmail 877 invoked by alias); 17 Mar 2011 22:02:38 -0000 Received: (qmail 852 invoked by uid 22791); 17 Mar 2011 22:02:37 -0000 X-SWARE-Spam-Status: No, hits=-6.3 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, T_RP_MATCHES_RCVD 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; Thu, 17 Mar 2011 22:02:31 +0000 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p2HM2Up5003044 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 17 Mar 2011 18:02:30 -0400 Received: from [127.0.0.1] (ovpn-113-145.phx2.redhat.com [10.3.113.145]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p2HM2TIr018349 for ; Thu, 17 Mar 2011 18:02:29 -0400 Message-ID: <4D8284F1.3040408@redhat.com> Date: Thu, 17 Mar 2011 18:02:25 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.15) Gecko/20110307 Fedora/3.1.9-0.39.b3pre.fc14 Lightning/1.0b2 Thunderbird/3.1.9 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/47504 (bogus overflow error with constexpr) 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 In this testcase, the fix for PR 25125 causes us to rewrite what starts as char(int(-1) - int(1)) to char((unsigned char)(-1) - (unsigned char)1) and thus char((unsigned char)254). 254 doesn't fit in char, so the result has TREE_OVERFLOW set even though the original expression was all signed and therefore shouldn't. One issue here is that the rewriting creates this situation where it didn't exist before, so I think the rewriting is wrong. But it's also the case that both C and C++ distinguish between arithmetic overflow (i.e. INT_MAX+1) which has undefined behavior, and conversion of a value that doesn't fit in the target type (i.e. char(254)), which has implementation-defined behavior. So we should allow the latter in constant expressions, even if it was wrong of the compiler to introduce it. Tested x86_64-pc-linux-gnu, applied to trunk. commit de85bc3224ebed6104076ea0e6218e202da1a64e Author: Jason Merrill Date: Thu Mar 17 15:33:40 2011 -0400 PR c++/47504 * semantics.c (cxx_eval_constant_expression) [NOP_EXPR]: Don't let the conversion set TREE_OVERFLOW. diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index cafca56..b6d1008 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -6991,6 +6991,11 @@ cxx_eval_constant_expression (const constexpr_call *call, tree t, conversion. */ return fold (t); r = fold_build1 (TREE_CODE (t), to, op); + /* Conversion of an out-of-range value has implementation-defined + behavior; the language considers it different from arithmetic + overflow, which is undefined. */ + if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op)) + TREE_OVERFLOW (r) = false; } break; diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-data2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-data2.C index 598cae6..2d614ec 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-data2.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-data2.C @@ -44,5 +44,4 @@ extern template struct A3; // Use. A3 a31; -// FIXME should this be an error? A3 a32; // { dg-warning "overflow" } diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-overflow2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-overflow2.C new file mode 100644 index 0000000..5d5749c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-overflow2.C @@ -0,0 +1,8 @@ +// PR c++/47504 +// { dg-options -std=c++0x } + +char constexpr sub(char arg) +{ return char(arg - char(1)); } + +int main() +{ static char constexpr m = sub(-1); }