From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 8EE733943404 for ; Fri, 16 Sep 2022 12:10:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8EE733943404 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663330239; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8ILp57pIg1yHjvHntwDG5Yc9v3Y5r6oPUBWbrFXkx1A=; b=P4ftE5SJVoRXXQ+9pTzRdM7mkN1cVqD7APs1AhE8LEYxbg1ehjAeJTYZbQHDUJCaxJTV8o xX4X2jzISP88zdpwx4KPASKUf8Now83uCWvByWGxf/mrZh5f5VEGScnuB5Azg2lDFwfSx9 vjXolXZrxKnnYOHh/jPERQwVk5XWCaw= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-288-bxEsqnylOQ-dXUobiev5Iw-1; Fri, 16 Sep 2022 08:10:29 -0400 X-MC-Unique: bxEsqnylOQ-dXUobiev5Iw-1 Received: by mail-ed1-f69.google.com with SMTP id w17-20020a056402269100b0045249bc17a9so8372872edd.9 for ; Fri, 16 Sep 2022 05:10:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date; bh=8ILp57pIg1yHjvHntwDG5Yc9v3Y5r6oPUBWbrFXkx1A=; b=ViU/enAgE6vKNCY4VZH2AjJi/8Y+GqAas+KaemEejtn8aLOBDGKeXtHEUtwIVHIlua drg5wJ72RsKk5nupilkf0dpQrTWBCLyKi1YUS81ik8YnkXjZdiAoGlLe3K3OCHEF7daN mYuWOB6ny8sDv9wIdzNGG3cZiGl08PNqLJobE4bWLzMb0BD3eAwykizqHsFv9hEJqA7+ bJk9MmByEqOxjBCo57+UnXJ+LrVsNnN13tb5mrXDoo0hXyaDjuUL8VBJXhngbu4kXjgc 9Obms5Jb0HsvJfCdBC/YinLOYGFnhamqbY9U6/kr9uHn1J4SQ9JUpt2+t/adxiAK6PSC d4mQ== X-Gm-Message-State: ACrzQf3hTOPhkEJWKu/eUpoQebxqowsPIahPNfykzPAKSMD2NQrgiWM/ nBPwqY9DDx4LytWYNgwGl7NTabh8adNtMmP55P6R92UlEhWos6k/4i8pIFpo74pnXkycB2AV59t XOgW+xqdnCkAfepNPuw== X-Received: by 2002:a17:906:8a41:b0:780:a367:db50 with SMTP id gx1-20020a1709068a4100b00780a367db50mr1984094ejc.118.1663330227628; Fri, 16 Sep 2022 05:10:27 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7PSvfEgBZXczVEax69/aTcuro3Tt6x/mJ3CbuS/5kwN7roygNbUoqXsc8UiXKu+116dEfyXQ== X-Received: by 2002:a17:906:8a41:b0:780:a367:db50 with SMTP id gx1-20020a1709068a4100b00780a367db50mr1984081ejc.118.1663330227372; Fri, 16 Sep 2022 05:10:27 -0700 (PDT) Received: from [10.9.7.96] (vpn-konference.ms.mff.cuni.cz. [195.113.20.101]) by smtp.gmail.com with ESMTPSA id ky17-20020a170907779100b0073bf84be798sm10368647ejc.142.2022.09.16.05.10.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 16 Sep 2022 05:10:26 -0700 (PDT) Message-ID: <99dcf250-4d50-c64c-d607-d012208a8eec@redhat.com> Date: Fri, 16 Sep 2022 14:10:26 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.13.0 Subject: Re: [PATCH] c++: 'mutable' within constexpr [PR92505] To: Patrick Palka , gcc-patches@gcc.gnu.org References: <20220915180312.1596193-1-ppalka@redhat.com> From: Jason Merrill In-Reply-To: <20220915180312.1596193-1-ppalka@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 9/15/22 14:03, Patrick Palka wrote: > This patch permits accessing 'mutable' members of local objects during > constexpr evaluation (which other compilers seem to accept in C++14 > mode, while we reject), while continuing to reject it for global objects > (as in the last line of cpp0x/constexpr-mutable1.C, which other > compilers also reject). To distinguish between the two cases, it looks > like we just need to additionally check CONSTRUCTOR_MUTABLE_POISION > alongside DECL_MUTABLE_P in cxx_eval_component_reference before > rejecting the access. > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk? OK. > PR c++/92505 > > gcc/cp/ChangeLog: > > * constexpr.cc (cxx_eval_component_reference): Test non_constant_p > earlier. In C++14 or later, reject DECL_MUTABLE_P member > accesses only if CONSTRUCTOR_MUTABLE_POISION is also set. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp0x/constexpr-mutable3.C: New test. > * g++.dg/cpp1y/constexpr-mutable1.C: New test. > --- > gcc/cp/constexpr.cc | 11 +++++++---- > gcc/testsuite/g++.dg/cpp0x/constexpr-mutable3.C | 7 +++++++ > gcc/testsuite/g++.dg/cpp1y/constexpr-mutable1.C | 16 ++++++++++++++++ > 3 files changed, 30 insertions(+), 4 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-mutable3.C > create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-mutable1.C > > diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc > index 57283eabf3c..10639876d9c 100644 > --- a/gcc/cp/constexpr.cc > +++ b/gcc/cp/constexpr.cc > @@ -4088,6 +4088,8 @@ cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, > tree whole = cxx_eval_constant_expression (ctx, orig_whole, > lval, > non_constant_p, overflow_p); > + if (*non_constant_p) > + return t; > if (INDIRECT_REF_P (whole) > && integer_zerop (TREE_OPERAND (whole, 0))) > { > @@ -4108,20 +4110,21 @@ cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, > whole, part, NULL_TREE); > /* Don't VERIFY_CONSTANT here; we only want to check that we got a > CONSTRUCTOR. */ > - if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR) > + if (TREE_CODE (whole) != CONSTRUCTOR) > { > if (!ctx->quiet) > error ("%qE is not a constant expression", orig_whole); > *non_constant_p = true; > + return t; > } > - if (DECL_MUTABLE_P (part)) > + if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole)) > + && DECL_MUTABLE_P (part)) > { > if (!ctx->quiet) > error ("mutable %qD is not usable in a constant expression", part); > *non_constant_p = true; > + return t; > } > - if (*non_constant_p) > - return t; > bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole)); > FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) > { > diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-mutable3.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-mutable3.C > new file mode 100644 > index 00000000000..46c9d8437be > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-mutable3.C > @@ -0,0 +1,7 @@ > +// PR c++/92505 > +// { dg-do compile { target c++11 } } > + > +struct A { mutable int m; }; > +constexpr int f(A a) { return a.m; } > +static_assert(f({42}) == 42, ""); > +// { dg-error "non-constant|mutable" "" { target c++11_only } .-1 } > diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-mutable1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-mutable1.C > new file mode 100644 > index 00000000000..6c47988c01a > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-mutable1.C > @@ -0,0 +1,16 @@ > +// PR c++/92505 > +// { dg-do compile { target c++14 } } > + > +struct S { mutable int m; }; > + > +static_assert(S{42}.m == 42, ""); > + > +constexpr int f() { > + S s = {40}; > + s.m++; > + const auto& cs = s; > + ++cs.m; > + return cs.m; > +} > + > +static_assert(f() == 42, "");