public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Patrick Palka <ppalka@redhat.com>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] c++: 'mutable' within constexpr [PR92505]
Date: Fri, 16 Sep 2022 14:10:26 +0200	[thread overview]
Message-ID: <99dcf250-4d50-c64c-d607-d012208a8eec@redhat.com> (raw)
In-Reply-To: <20220915180312.1596193-1-ppalka@redhat.com>

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, "");


      reply	other threads:[~2022-09-16 12:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-15 18:03 Patrick Palka
2022-09-16 12:10 ` Jason Merrill [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=99dcf250-4d50-c64c-d607-d012208a8eec@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=ppalka@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).