From patchwork Sun Mar 6 06:46:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 592519 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.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 30234140B9A for ; Sun, 6 Mar 2016 17:47:00 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=t2Gikpxi; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=XPNuB56th3921n3/d3mGUDTeoJ5DZ6By3lExRibNcimZpgeolg 7zgxKTCc3gCDcNM6EgGPYqNJqUnLyWYJQVYdn9n2ZrJdl4tbSoXcvD6ozpJSmc7e WGw/jWm0i0hHIIiayOoGtDqNMM8G5AzvRxjezoSequ9OnWtolVVXWKiz8= 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:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=tXyp4yTLu7JZwivQ1YI1Gq9VdGM=; b=t2Gikpxi1uxiYiKCECdO dV8ajBLjoWrM+yWrdrmP9CZ9bOyA0fY8txN+F5Hphz4pYunMAkKNhOmTHKYrwxIG tiwWeQHUfJzF466ByfRUxTMuWa2LjzkTvxdUaMfHSVWQHuLhchHNWqcTbnIlA82G vQ5LyQ3/ZsGOKShpsv9N29w= Received: (qmail 76471 invoked by alias); 6 Mar 2016 06:46:52 -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 76428 invoked by uid 89); 6 Mar 2016 06:46:49 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=lewis, 1, 30 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Sun, 06 Mar 2016 06:46:48 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id CC9C23B758; Sun, 6 Mar 2016 06:46:46 +0000 (UTC) Received: from [10.10.116.24] (ovpn-116-24.rdu2.redhat.com [10.10.116.24]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u266kk7U024640; Sun, 6 Mar 2016 01:46:46 -0500 To: gcc-patches List , Louis Dionne From: Jason Merrill Subject: C++ PATCH for another c++/67364 testcase (constexpr wrong evaluation) Message-ID: <56DBD255.60404@redhat.com> Date: Sun, 6 Mar 2016 01:46:45 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 Lewis reported another testcase which hits the "accessing uninitialized member" error, and can lead to wrong values. Tested x86_64-pc-linux-gnu, applying to trunk. commit 1bfb2997968d18b337cbd144a698b7be3bf6b28e Author: Jason Merrill Date: Sat Mar 5 14:44:31 2016 -0500 PR c++/67364 * constexpr.c (cxx_eval_store_expression): Replace CONSTRUCTOR_ELTS in nested CONSTRUCTORs, too. diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index c9f9c47..f23e7c9 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -2939,39 +2939,34 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, /* Don't share a CONSTRUCTOR that might be changed later. */ init = unshare_expr (init); if (target == object) + /* The hash table might have moved since the get earlier. */ + valp = ctx->values->get (object); + + if (TREE_CODE (init) == CONSTRUCTOR) { - /* The hash table might have moved since the get earlier. */ - valp = ctx->values->get (object); - if (TREE_CODE (init) == CONSTRUCTOR) - { - /* An outer ctx->ctor might be pointing to *valp, so replace - its contents. */ - CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); - TREE_CONSTANT (*valp) = TREE_CONSTANT (init); - TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init); - } - else - *valp = init; + /* An outer ctx->ctor might be pointing to *valp, so replace + its contents. */ + CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); + TREE_CONSTANT (*valp) = TREE_CONSTANT (init); + TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init); } else - { - *valp = init; + *valp = init; - /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing - CONSTRUCTORs. */ - tree elt; - unsigned i; - bool c = TREE_CONSTANT (init); - bool s = TREE_SIDE_EFFECTS (init); - if (!c || s) - FOR_EACH_VEC_SAFE_ELT (ctors, i, elt) - { - if (!c) - TREE_CONSTANT (elt) = false; - if (s) - TREE_SIDE_EFFECTS (elt) = true; - } - } + /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing + CONSTRUCTORs, if any. */ + tree elt; + unsigned i; + bool c = TREE_CONSTANT (init); + bool s = TREE_SIDE_EFFECTS (init); + if (!c || s) + FOR_EACH_VEC_SAFE_ELT (ctors, i, elt) + { + if (!c) + TREE_CONSTANT (elt) = false; + if (s) + TREE_SIDE_EFFECTS (elt) = true; + } release_tree_vector (ctors); if (*non_constant_p) diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-aggr3.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-aggr3.C new file mode 100644 index 0000000..547dec4 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-aggr3.C @@ -0,0 +1,30 @@ +// PR c++/67364 +// { dg-do compile { target c++11 } } + +template +struct tuple { + Xn storage_; + + constexpr tuple(Xn const& xn) + : storage_(xn) + { } + + template + constexpr tuple(tuple const& other) + : storage_(other.storage_) + { } + + template + constexpr tuple(tuple& other) + : tuple(const_cast(other)) + { } +}; + +template +struct wrapper { T value; }; + +template +constexpr wrapper wrap(T t) { return {t}; } + +constexpr wrapper> t = wrap(tuple{2}); +static_assert(t.value.storage_ == 2, "");