public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-2701] c++: 'mutable' member within constexpr [PR92505]
@ 2022-09-16 15:11 Patrick Palka
  0 siblings, 0 replies; only message in thread
From: Patrick Palka @ 2022-09-16 15:11 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:7107ea6fb933f1e928593c7e92edfd64ccf0df63

commit r13-2701-g7107ea6fb933f1e928593c7e92edfd64ccf0df63
Author: Patrick Palka <ppalka@redhat.com>
Date:   Fri Sep 16 11:10:43 2022 -0400

    c++: 'mutable' member within constexpr [PR92505]
    
    This patch permits accessing 'mutable' members of local objects during
    constexpr evaluation, while continuing to reject it for global objects
    (as in the last line of cpp0x/constexpr-mutable1.C).  To distinguish
    between the two cases, it looks like it suffices to just check
    CONSTRUCTOR_MUTABLE_POSION in cxx_eval_component_reference before
    deciding to reject a DECL_MUTABLE_P member access.
    
            PR c++/92505
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (cxx_eval_component_reference): Check non_constant_p
            sooner.  In C++14 or later, reject a DECL_MUTABLE_P member access
            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.

Diff:
---
 gcc/cp/constexpr.cc                             | 11 +++++++----
 gcc/testsuite/g++.dg/cpp0x/constexpr-mutable3.C |  9 +++++++++
 gcc/testsuite/g++.dg/cpp1y/constexpr-mutable1.C | 16 ++++++++++++++++
 3 files changed, 32 insertions(+), 4 deletions(-)

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..51499fac520
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-mutable3.C
@@ -0,0 +1,9 @@
+// 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, "");

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-09-16 15:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-16 15:11 [gcc r13-2701] c++: 'mutable' member within constexpr [PR92505] Patrick Palka

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).