This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH][C++] Fix PR67333


Hi.
This patch fixes a rejects-valid bug related to volatile-qualified arguments of
constexpr functions. This is essentially a one-line change in constexpr.c.
Bootstrapped and regtested on x86_64-pc-linux-gnu. OK for trunk?

gcc/cp/ChangeLog:

2015-08-25  Mikhail Maltsev  <maltsevm@gmail.com>

        PR c++/67333
        * constexpr.c (potential_constant_expression_1): Do not reject
        volatile-qualified values, if there is no lvalue-to-rvalue conversion.

gcc/testsuite/ChangeLog:

2015-08-25  Mikhail Maltsev  <maltsevm@gmail.com>

        PR c++/67333
        * g++.dg/cpp0x/constexpr-67333.C: New test.
        * g++.dg/cpp1y/constexpr-67333.C: New test.

-- 
Regards,
    Mikhail Maltsev
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 1eacb8b..f4ee727 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -4006,7 +4006,7 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict,
     return false;
   if (t == NULL_TREE)
     return true;
-  if (TREE_THIS_VOLATILE (t))
+  if (TREE_THIS_VOLATILE (t) && (!DECL_P (t) || want_rval))
     {
       if (flags & tf_error)
         error ("expression %qE has side-effects", t);
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-67333.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-67333.C
new file mode 100644
index 0000000..885ece6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-67333.C
@@ -0,0 +1,29 @@
+// PR c++/67333
+// { dg-do compile { target c++11 } }
+
+template <int N>
+struct integral_constant
+{
+  static constexpr int value = N;
+};
+
+template <typename T, int S>
+constexpr int lengthof (const volatile T (&)[S])
+{
+  return S;
+}
+
+template <typename T, int S>
+constexpr int valueof (const volatile T (&s)[S])  // { dg-error "has side-effects" }
+{
+  return s[0];
+}
+
+int main ()
+{
+  volatile int meow[4] {};
+  integral_constant<lengthof (meow)>::value;  // OK
+  integral_constant<valueof (meow)>::value;   // { dg-error "in a constant expression" }
+  return 0;
+}
+
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-67333.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-67333.C
new file mode 100644
index 0000000..7e3ef21
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-67333.C
@@ -0,0 +1,26 @@
+// PR c++/67333
+// { dg-do compile { target c++14 } }
+
+template <int N>
+struct integral_constant
+{
+  static constexpr int value = N;
+};
+
+constexpr int decl (int x)
+{
+  volatile int v = x;
+  return x;
+}
+
+constexpr int use (int x)
+{
+  volatile int v = x;
+  return v;
+} // { dg-error "has side-effects" }
+
+int main()
+{
+  integral_constant<decl (2)>::value; // OK
+  integral_constant<use (2)>::value;  // { dg-error "in a constant expression" }
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]