From patchwork Tue Nov 18 13:35:45 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 412040 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 35217140179 for ; Wed, 19 Nov 2014 00:36:00 +1100 (AEDT) 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=NvL5HzhmCAWSCq5xx+GLsPUPp6maIk/MUEzsJKQlVfXZ2r H8rh9bSlKbj1s5Nzr+x0QvXApMhQ1y+c3r5GiWGYnGmCoX47gap8z8W7twfU0cEs LNUF0vzWvSPwW4g4No5F9WDHCpUgFvBgk607867z+sOJJXxtiz5hf5YqzpqbM= 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=+pbm+6gQK3RtZvlEC6YhpZFZyRs=; b=PbsHfIboWe4Prz9T1Kmo udPgzXAZ67zBZkkDla8XZNzyJz+VEZRHE++jmtpCb/3qJfx5I9d/9Lg+9q2XR7+9 L6kNx0CozNnr9IidUz/MQbvLX4ztdLuXhPrIY1J7/jykiuNEcNt6LJnpY54oZmi3 3pXhW+6tD+Wmh+hzH5o0yBs= Received: (qmail 23614 invoked by alias); 18 Nov 2014 13:35:53 -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 23604 invoked by uid 89); 18 Nov 2014 13:35:52 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.7 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 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; Tue, 18 Nov 2014 13:35:51 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id sAIDZn8I028235 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Tue, 18 Nov 2014 08:35:50 -0500 Received: from [10.10.116.43] ([10.10.116.43]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id sAIDZn5O001905 for ; Tue, 18 Nov 2014 08:35:49 -0500 Message-ID: <546B4B31.6040906@redhat.com> Date: Tue, 18 Nov 2014 08:35:45 -0500 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/58102 (constexpr and mutable) We need to allow copy-list-initialization of constexpr variables with mutable members, too. The thing we need to avoid is not so much an full-expression with mutable-containing type as assuming that a mutable member of a variable hasn't changed since the variable was initialized. Tested x86_64-pc-linux-gnu, applied to trunk. commit 58e6e22ca70f9efe534f9d038f0cf9a2fe1a7504 Author: Jason Merrill Date: Mon Nov 17 23:44:41 2014 -0500 PR c++/58102 * typeck2.c (store_init_value): Set it. * cp-tree.h (CONSTRUCTOR_MUTABLE_POISON): New. * constexpr.c (cxx_eval_outermost_constant_expr): Check it. diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 5b25654..2f0708b 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -3315,15 +3315,15 @@ cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p); - if (TREE_CODE (t) != CONSTRUCTOR - && cp_has_mutable_p (TREE_TYPE (t))) + /* Mutable logic is a bit tricky: we want to allow initialization of + constexpr variables with mutable members, but we can't copy those + members to another constexpr variable. */ + if (TREE_CODE (r) == CONSTRUCTOR + && CONSTRUCTOR_MUTABLE_POISON (r)) { - /* We allow a mutable type if the original expression was a - CONSTRUCTOR so that we can do aggregate initialization of - constexpr variables. */ if (!allow_non_constant) - error ("%qT cannot be the type of a complete constant expression " - "because it has mutable sub-objects", type); + error ("%qE is not a constant expression because it refers to " + "mutable subobjects of %qT", t, type); non_constant_p = true; } diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index d3722d7..3542344 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -109,6 +109,7 @@ c-common.h, not after. DECLTYPE_FOR_LAMBDA_PROXY (in DECLTYPE_TYPE) REF_PARENTHESIZED_P (in COMPONENT_REF, INDIRECT_REF) AGGR_INIT_ZERO_FIRST (in AGGR_INIT_EXPR) + CONSTRUCTOR_MUTABLE_POISON (in CONSTRUCTOR) 3: (TREE_REFERENCE_EXPR) (in NON_LVALUE_EXPR) (commented-out). ICS_BAD_FLAG (in _CONV) FN_TRY_BLOCK_P (in TRY_BLOCK) @@ -3497,6 +3498,11 @@ more_aggr_init_expr_args_p (const aggr_init_expr_arg_iterator *iter) #define CONSTRUCTOR_NO_IMPLICIT_ZERO(NODE) \ (TREE_LANG_FLAG_1 (CONSTRUCTOR_CHECK (NODE))) +/* True if this CONSTRUCTOR should not be used as a variable initializer + because it was loaded from a constexpr variable with mutable fields. */ +#define CONSTRUCTOR_MUTABLE_POISON(NODE) \ + (TREE_LANG_FLAG_2 (CONSTRUCTOR_CHECK (NODE))) + #define DIRECT_LIST_INIT_P(NODE) \ (BRACE_ENCLOSED_INITIALIZER_P (NODE) && CONSTRUCTOR_IS_DIRECT_INIT (NODE)) diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index 01a0671..5748650 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -809,6 +809,10 @@ store_init_value (tree decl, tree init, vec** cleanups, int flags) value = cxx_constant_value (value, decl); } value = maybe_constant_init (value, decl); + if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type)) + /* Poison this CONSTRUCTOR so it can't be copied to another + constexpr variable. */ + CONSTRUCTOR_MUTABLE_POISON (value) = true; const_init = (reduced_constant_expression_p (value) || error_operand_p (value)); DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init; diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-mutable2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-mutable2.C new file mode 100644 index 0000000..c449c3a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-mutable2.C @@ -0,0 +1,10 @@ +// PR c++/58102 +// { dg-do compile { target c++11 } } + +struct S { + mutable int n; + constexpr S() : n() {} +}; + +constexpr S s = {}; +constexpr S s2 = s; // { dg-error "mutable" }