From patchwork Wed Aug 14 19:20:02 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 267185 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]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id B86AE2C0212 for ; Thu, 15 Aug 2013 05:20:21 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:cc:subject:content-type; q=dns; s=default; b=uSpgdukuehzWlcKY8rZTDpVYbpK1tl6SFtzr1EnKpkN s97L3WksmhO6CtS1fkwRdVIJ/rFpK81k/b8Zk1o0BBDCaxr0XYHdaroZwSH1KNX5 CwNbgbM/UEpJBtH1RQdcmPWrxByetqWO+A36gqIsYUSLX0ZTJRUIy9BkQD0Y0YaU = DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:cc:subject:content-type; s=default; bh=N1dW1PrTMHaDi11crWwoala3LIE=; b=f6QGUFoS6k0LNc96T Hb+2L44oX5ciJ1TU4Xhg0/uskW7csU9daAPnEBvkF8G4BpSZntQTGa6ecmb9he2N iRKHe6FkIaDFi4h2xSUq8LcaX+eKiCZVUtSMlGigKa/i4PCsj8lEyVEUNfr31KoF hyeGo5paCnSqB+kFEboBfZhfls= Received: (qmail 18851 invoked by alias); 14 Aug 2013 19:20:14 -0000 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 Received: (qmail 18838 invoked by uid 89); 14 Aug 2013 19:20:14 -0000 X-Spam-SWARE-Status: No, score=-6.5 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_MED, RCVD_IN_HOSTKARMA_YE, RP_MATCHES_RCVD, SPF_PASS, UNPARSEABLE_RELAY autolearn=ham version=3.3.2 Received: from aserp1040.oracle.com (HELO aserp1040.oracle.com) (141.146.126.69) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Wed, 14 Aug 2013 19:20:13 +0000 Received: from ucsinet21.oracle.com (ucsinet21.oracle.com [156.151.31.93]) by aserp1040.oracle.com (Sentrion-MTA-4.3.1/Sentrion-MTA-4.3.1) with ESMTP id r7EJK8P4013205 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 14 Aug 2013 19:20:09 GMT Received: from userz7021.oracle.com (userz7021.oracle.com [156.151.31.85]) by ucsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id r7EJK5Sq027528 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 14 Aug 2013 19:20:08 GMT Received: from abhmt102.oracle.com (abhmt102.oracle.com [141.146.116.54]) by userz7021.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id r7EJK53Z027501; Wed, 14 Aug 2013 19:20:05 GMT Received: from poldo4.casa (/79.43.213.168) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 14 Aug 2013 12:20:04 -0700 Message-ID: <520BD862.1070104@oracle.com> Date: Wed, 14 Aug 2013 21:20:02 +0200 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 MIME-Version: 1.0 To: "gcc-patches@gcc.gnu.org" CC: Jason Merrill Subject: [C++ Patch /RFC] PR 51912 X-Virus-Found: No Hi all, Jason, thus, per the notes I left on the audit trail some time ago, this seems to me a possible approach to reject this kind of invalid code. What do you think? Booted & tested x86_64-linux. Thanks, Paolo. ////////////////////// Index: cp/call.c =================================================================== --- cp/call.c (revision 201731) +++ cp/call.c (working copy) @@ -1314,7 +1314,8 @@ standard_conversion (tree to, tree from, tree expr /* As an extension, allow conversion to complex type. */ else if (ARITHMETIC_TYPE_P (to)) { - if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE) + if (! (INTEGRAL_CODE_P (fcode) + || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL))) || SCOPED_ENUM_P (from)) return NULL; conv = build_conv (ck_std, to, conv); @@ -1681,7 +1682,7 @@ implicit_conversion (tree to, tree from, tree expr resolution, or after we've chosen one. */ flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_PREFER_RVALUE - |LOOKUP_NO_NARROWING|LOOKUP_PROTECT); + |LOOKUP_NO_NARROWING|LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL); /* FIXME: actually we don't want warnings either, but we can't just have 'complain &= ~(tf_warning|tf_error)' because it would cause Index: cp/cp-tree.h =================================================================== --- cp/cp-tree.h (revision 201731) +++ cp/cp-tree.h (working copy) @@ -4510,6 +4510,8 @@ enum overload_flags { NO_SPECIAL = 0, DTOR_FLAG, T #define LOOKUP_EXPLICIT_TMPL_ARGS (LOOKUP_ALREADY_DIGESTED << 1) /* Like LOOKUP_NO_TEMP_BIND, but also prevent binding to xvalues. */ #define LOOKUP_NO_RVAL_BIND (LOOKUP_EXPLICIT_TMPL_ARGS << 1) +/* Used in case_conversion. */ +#define LOOKUP_NO_NON_INTEGRAL (LOOKUP_NO_RVAL_BIND << 1) #define LOOKUP_NAMESPACES_ONLY(F) \ (((F) & LOOKUP_PREFER_NAMESPACES) && !((F) & LOOKUP_PREFER_TYPES)) Index: cp/decl.c =================================================================== --- cp/decl.c (revision 201731) +++ cp/decl.c (working copy) @@ -3103,7 +3103,9 @@ case_conversion (tree type, tree value) { if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type)) type = type_promotes_to (type); - value = perform_implicit_conversion (type, value, tf_warning_or_error); + value = (perform_implicit_conversion_flags + (type, value, tf_warning_or_error, + LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL)); } return cxx_constant_value (value); } Index: testsuite/g++.dg/cpp0x/enum15.C =================================================================== --- testsuite/g++.dg/cpp0x/enum15.C (revision 201726) +++ testsuite/g++.dg/cpp0x/enum15.C (working copy) @@ -15,6 +15,6 @@ void foo (A a, int i) { case A::Val0: break; // { dg-error "" } case 1: break; - case 2.0: break; + case 2.0: break; // { dg-error "" } } } Index: testsuite/g++.dg/cpp0x/enum28.C =================================================================== --- testsuite/g++.dg/cpp0x/enum28.C (revision 0) +++ testsuite/g++.dg/cpp0x/enum28.C (working copy) @@ -0,0 +1,17 @@ +// PR c++/51912 +// { dg-do compile { target c++11 } } + +constexpr double g() { return 2.0; } + +void f(int i) +{ + switch (i) + { + case 1.0:; // { dg-error "could not convert" } + } + + switch (i) + { + case g():; // { dg-error "could not convert" } + } +}