https://gcc.gnu.org/g:6f5ada57162c10d5ff4b04de275aadf9c81a3da5

commit r14-11295-g6f5ada57162c10d5ff4b04de275aadf9c81a3da5
Author: Jakub Jelinek <ja...@redhat.com>
Date:   Fri Feb 7 14:27:18 2025 +0100

    c++: Allow constexpr reads from volatile std::nullptr_t objects [PR118661]
    
    As mentioned in the PR, https://eel.is/c++draft/conv.lval#note-1
    says that even volatile reads from std::nullptr_t typed objects actually
    don't read anything and https://eel.is/c++draft/expr.const#10.9
    says that even those are ok in constant expressions.
    
    So, the following patch adjusts the r9-4793 changes to have an exception
    for NULLPTR_TYPE.
    As [conv.lval]/3 also talks about accessing to inactive member, I've added
    testcase to cover that as well.
    
    2025-02-07  Jakub Jelinek  <ja...@redhat.com>
    
            PR c++/118661
            * constexpr.cc (potential_constant_expression_1): Don't diagnose
            lvalue-to-rvalue conversion of volatile lvalue if it has 
NULLPTR_TYPE.
            * decl2.cc (decl_maybe_constant_var_p): Return true for constexpr
            decls with NULLPTR_TYPE even if they are volatile.
    
            * g++.dg/cpp0x/constexpr-volatile4.C: New test.
            * g++.dg/cpp0x/constexpr-union9.C: New test.
    
    (cherry picked from commit 6c8e6d6febaed3c167ca9534935c2cb18045528e)

Diff:
---
 gcc/cp/constexpr.cc                              |  3 ++-
 gcc/cp/decl2.cc                                  |  3 ++-
 gcc/testsuite/g++.dg/cpp0x/constexpr-union9.C    | 16 ++++++++++++++++
 gcc/testsuite/g++.dg/cpp0x/constexpr-volatile4.C | 20 ++++++++++++++++++++
 4 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 79dd6a7423c0..83a9175b265d 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -9597,7 +9597,8 @@ potential_constant_expression_1 (tree t, bool want_rval, 
bool strict, bool now,
     return true;
 
   if (TREE_THIS_VOLATILE (t) && want_rval
-      && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t)))
+      && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))
+      && !NULLPTR_TYPE_P (TREE_TYPE (t)))
     {
       if (flags & tf_error)
        constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index 139b5c15b3e8..5cbe4c0ceda7 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -4650,7 +4650,8 @@ decl_maybe_constant_var_p (tree decl)
   tree type = TREE_TYPE (decl);
   if (!VAR_P (decl))
     return false;
-  if (DECL_DECLARED_CONSTEXPR_P (decl) && !TREE_THIS_VOLATILE (decl))
+  if (DECL_DECLARED_CONSTEXPR_P (decl)
+      && (!TREE_THIS_VOLATILE (decl) || NULLPTR_TYPE_P (type)))
     return true;
   if (DECL_HAS_VALUE_EXPR_P (decl))
     /* A proxy isn't constant.  */
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-union9.C 
b/gcc/testsuite/g++.dg/cpp0x/constexpr-union9.C
new file mode 100644
index 000000000000..5d365f9cca62
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-union9.C
@@ -0,0 +1,16 @@
+// PR c++/118661
+// { dg-do compile { target c++11 } }
+
+using nullptr_t = decltype (nullptr);
+union U { int i; nullptr_t n; };
+constexpr U u = { 42 };
+static_assert (u.n == nullptr, "");
+
+#if __cplusplus >= 201402L
+constexpr nullptr_t
+foo ()
+{
+  union U { int i; nullptr_t n; } u = { 42 };
+  return u.n;
+}
+#endif
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-volatile4.C 
b/gcc/testsuite/g++.dg/cpp0x/constexpr-volatile4.C
new file mode 100644
index 000000000000..5ef024009b50
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-volatile4.C
@@ -0,0 +1,20 @@
+// PR c++/118661
+// { dg-do compile { target c++11 } }
+
+using nullptr_t = decltype (nullptr);
+constexpr volatile nullptr_t a = {};
+constexpr nullptr_t b = a;
+
+constexpr nullptr_t
+foo ()
+{
+#if __cplusplus >= 201402L
+  volatile nullptr_t c = {};
+  return c;
+#else
+  return nullptr;
+#endif
+}
+
+static_assert (b == nullptr, "");
+static_assert (foo () == nullptr, "");

Reply via email to