From patchwork Thu Mar 28 18:09:33 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 232157 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 D3D4A2C0097 for ; Fri, 29 Mar 2013 05:09:58 +1100 (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:subject:content-type; q= dns; s=default; b=T2OcCMUtavTzAJxoS9mfC+d2TcN2JxGlF0hGUI7UbcHX5R ko4vvoj2W0eCppkd56VhWLc51X2yfiyCMJlXZnMoMEIRl9yoPrj/wnx9lkmOqqbX 4LLftKcMcWzyQ/mQMlPJjXhRKTRP28r9Yv+/Bi23oHpZpMbOEm4iZh1b59wJE= 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:subject:content-type; s= default; bh=zMGG9dKtUxVXYAOEHnG7z1m4cUM=; b=RnJy+8W7fgbXQ5OzhRtm GtivUUkAmyZV5RvF/EjfPMaJMPvnHIh9SBEr5/7J32iMe0RQjmNrMYko7KqeDatD O5M0LuFvu+zVE+yuiqySLKPH8LCxh3Zstdv6T/K8NPnYEoMuagAKXqgC4cbBSvX/ E+E0tNKICULxrUvXqr8hLq8= Received: (qmail 17007 invoked by alias); 28 Mar 2013 18:09:47 -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 16979 invoked by uid 89); 28 Mar 2013 18:09:38 -0000 X-Spam-SWARE-Status: No, score=-7.1 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, RCVD_IN_DNSWL_HI, RCVD_IN_HOSTKARMA_W, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.1 Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Thu, 28 Mar 2013 18:09:36 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r2SI9ZMb021050 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 28 Mar 2013 14:09:35 -0400 Received: from [10.16.196.202] (wlan-196-202.bos.redhat.com [10.16.196.202]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r2SI9YjH029954 for ; Thu, 28 Mar 2013 14:09:34 -0400 Message-ID: <5154875D.4030003@redhat.com> Date: Thu, 28 Mar 2013 14:09:33 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:21.0) Gecko/20100101 Thunderbird/21.0a2 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/56728 (ICE with bogus constexpr function) X-Virus-Found: No In this testcase we were crashing because after function argument substitution in the initialization for crashnkill we couldn't figure out how to simplify *(Inner*)4, so we went back to bridge2. It certainly doesn't make sense for the initializer to refer to a parameter for a function called in the initializer, so the back end correctly blew up. The cxx_eval_indirect_ref hunk addresses this by returning the expanded version of the operand rather than the original one. The other two hunks address the issue that the getInner function is not a valid constexpr function, because it involves a reinterpret_cast from integer to pointer. Tested x86_64-pc-linux-gnu, applying to trunk. commit 44cdacef5b56c91e2787f737f0ea90aa66790436 Author: Jason Merrill Date: Wed Mar 27 14:16:14 2013 -0400 PR c++/56728 * semantics.c (potential_constant_expression_1) [NOP_EXPR]: Reject conversion from integer to pointer. (cxx_eval_constant_expression): Likewise. (cxx_eval_indirect_ref): Use the folded operand if we still think this might be constant. diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 72b884e..0b8e2f7 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -7661,6 +7661,8 @@ cxx_eval_indirect_ref (const constexpr_call *call, tree t, if (r == NULL_TREE) { + if (addr && op0 != orig_op0) + return build1 (INDIRECT_REF, TREE_TYPE (t), op0); if (!addr) VERIFY_CONSTANT (t); return t; @@ -8056,6 +8058,16 @@ cxx_eval_constant_expression (const constexpr_call *call, tree t, non_constant_p, overflow_p); if (*non_constant_p) return t; + if (POINTER_TYPE_P (TREE_TYPE (t)) + && TREE_CODE (op) == INTEGER_CST + && !integer_zerop (op)) + { + if (!allow_non_constant) + error_at (EXPR_LOC_OR_HERE (t), + "reinterpret_cast from integer to pointer"); + *non_constant_p = true; + return t; + } if (op == oldop) /* We didn't fold at the top so we could check for ptr-int conversion. */ @@ -8452,6 +8464,15 @@ potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags) may change to something more specific to type-punning (DR 1312). */ { tree from = TREE_OPERAND (t, 0); + if (POINTER_TYPE_P (TREE_TYPE (t)) + && TREE_CODE (from) == INTEGER_CST + && !integer_zerop (from)) + { + if (flags & tf_error) + error_at (EXPR_LOC_OR_HERE (t), + "reinterpret_cast from integer to pointer"); + return false; + } return (potential_constant_expression_1 (from, TREE_CODE (t) != VIEW_CONVERT_EXPR, flags)); } diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-reinterpret1.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-reinterpret1.C new file mode 100644 index 0000000..69db98b --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-reinterpret1.C @@ -0,0 +1,37 @@ +// PR c++/56728 +// { dg-require-effective-target c++11 } + +class B { +public: + static B instance; + class Inner + { + public: + class Wuzi + { + unsigned int m; + } m_Class[3]; + unsigned m_Int[4]; + }; + + constexpr static Inner & getInner() + { + /* I am surprised this is considered a constexpr */ + return *((Inner *)4); + } // { dg-error "reinterpret_cast" } +}; + +B B::instance; + +class A +{ +public: + constexpr A(B &bridge, B::Inner &bridge2, unsigned char index) + : m_Bridge(bridge), m_Wuz(bridge2.m_Class[index]) + {} + + B &m_Bridge; + B::Inner::Wuzi &m_Wuz; +}; +A works{B::instance, B::getInner(), 3}; +A crashnkill[1]{{B::instance, B::getInner(), 3}};