On 3/1/22 00:10, Patrick Palka wrote:
On Tue, 19 Jan 2021, Jason Merrill wrote:

On 1/13/21 12:05 PM, Patrick Palka wrote:
In the below testcase, the expression of the atomic constraint after
substitution is (int *) NON_LVALUE_EXPR <1> != 0B which is not a C++
constant expression, but its TREE_CONSTANT flag is set (from build2),
so satisfy_atom fails to notice that it's non-constant (and we end
up tripping over the assert in satisfaction_value).

Since TREE_CONSTANT doesn't necessarily correspond to C++ constantness,
this patch makes satisfy_atom instead check is_rvalue_constant_expression.

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk/10?

gcc/cp/ChangeLog:

        PR c++/98644
        * constraint.cc (satisfy_atom): Check is_rvalue_constant_expression
        instead of TREE_CONSTANT.

gcc/testsuite/ChangeLog:

        PR c++/98644
        * g++.dg/cpp2a/concepts-pr98644.C: New test.
---
   gcc/cp/constraint.cc                          | 2 +-
   gcc/testsuite/g++.dg/cpp2a/concepts-pr98644.C | 7 +++++++
   2 files changed, 8 insertions(+), 1 deletion(-)
   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-pr98644.C

diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 9049d087859..f99a25dc8a4 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -2969,7 +2969,7 @@ satisfy_atom (tree t, tree args, sat_info info)
       {
         result = maybe_constant_value (result, NULL_TREE,
                                     /*manifestly_const_eval=*/true);
-      if (!TREE_CONSTANT (result))

This should be sufficient.  If the result isn't constant, maybe_constant_value
shouldn't return it with TREE_CONSTANT set.  See

       /* This isn't actually constant, so unset TREE_CONSTANT.

in cxx_eval_outermost_constant_expr.

I see, so the problem seems to be that the fail-fast path of
maybe_constant_value isn't clearing TREE_CONSTANT sufficiently.  Would
it make sense to fix this like so?

-- >8 --

Subject: [PATCH] c++: ICE with non-constant satisfaction value [PR98644]

Here during satisfaction the expression of the atomic constraint after
substitution is (int *) NON_LVALUE_EXPR <1> != 0B, which is not a C++
constant expression due to the reinterpret_cast, but TREE_CONSTANT is
set since its value is otherwise effectively constant.  We then call
maybe_constant_value on it, which proceeds via its fail-fast path to
exit early without clearing TREE_CONSTANT.  But satisfy_atom relies
on checking TREE_CONSTANT of the result of maybe_constant_value in order
to detect non-constant satisfaction.

This patch fixes this by making the fail-fast path of maybe_constant_value
clear TREE_CONSTANT in this case, like cxx_eval_outermost_constant_expr
in the normal path would have done.

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?

        PR c++/98644

gcc/cp/ChangeLog:

        * constexpr.cc (maybe_constant_value): In the fail-fast path,
        clear TREE_CONSTANT on the result if it's set on the input.

gcc/testsuite/ChangeLog:

        * g++.dg/cpp2a/concepts-pr98644.C: New test.
        * g++.dg/parse/array-size2.C: Remove expected diagnostic about a
        narrowing conversion.
---
  gcc/cp/constexpr.cc                           | 4 +++-
  gcc/testsuite/g++.dg/cpp2a/concepts-pr98644.C | 7 +++++++
  gcc/testsuite/g++.dg/parse/array-size2.C      | 2 --
  3 files changed, 10 insertions(+), 3 deletions(-)
  create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-pr98644.C

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 4716694cb71..234cf0acc26 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -7965,8 +7965,10 @@ maybe_constant_value (tree t, tree decl, bool 
manifestly_const_eval)
if (!is_nondependent_constant_expression (t))
      {
-      if (TREE_OVERFLOW_P (t))
+      if (TREE_OVERFLOW_P (t)
+         || (!processing_template_decl && TREE_CONSTANT (t)))
        {
+         /* This isn't actually constant, so unset TREE_CONSTANT.  */
          t = build_nop (TREE_TYPE (t), t);

build_nop isn't appropriate for arbitrary expressions (classes, in particular). We probably want to factor out the code in cxx_eval_outermost_constant_expr under the "this isn't actually constant" comment.

          TREE_CONSTANT (t) = false;
        }
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-pr98644.C 
b/gcc/testsuite/g++.dg/cpp2a/concepts-pr98644.C
new file mode 100644
index 00000000000..6772f72a3ce
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-pr98644.C
@@ -0,0 +1,7 @@
+// PR c++/98644
+// { dg-do compile { target c++20 } }
+
+template<class T> concept Signed = bool(T(1)); // { dg-error 
"reinterpret_cast" }
+static_assert(Signed<int*>); // { dg-error "non-constant" }
+
+constexpr bool B = requires { requires bool((char *)1); }; // { dg-error 
"reinterpret_cast" }
diff --git a/gcc/testsuite/g++.dg/parse/array-size2.C 
b/gcc/testsuite/g++.dg/parse/array-size2.C
index c4a69df3b01..e58fe266e77 100644
--- a/gcc/testsuite/g++.dg/parse/array-size2.C
+++ b/gcc/testsuite/g++.dg/parse/array-size2.C
@@ -15,8 +15,6 @@ void
  foo (void)
  {
    char g[(char *) &((struct S *) 0)->b - (char *) 0]; // { dg-error "40:size of 
array .g. is not an integral constant-expression" }
-                                                     // { dg-error "narrowing 
conversion" "" { target c++11 } .-1 }
-                                                     // { dg-message "expression has a 
constant value but is not a C.. constant-expression" "" { target c++11 } .-2 }
    char h[(__SIZE_TYPE__) &((struct S *) 8)->b];              // { dg-error 
"10:size of array .h. is not an integral constant-expression" }
    bar (g, h);
  }

Reply via email to