public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-7802] c++: missing SFINAE for non-constant consteval calls [PR104620]
@ 2022-03-24 15:42 Patrick Palka
  0 siblings, 0 replies; only message in thread
From: Patrick Palka @ 2022-03-24 15:42 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:647537adefb34041cc2d44585252fd765cc0daae

commit r12-7802-g647537adefb34041cc2d44585252fd765cc0daae
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu Mar 24 11:42:31 2022 -0400

    c++: missing SFINAE for non-constant consteval calls [PR104620]
    
    Here we weren't respecting SFINAE when evaluating a call to a consteval
    function, which caused us to reject the new testcase below.  This patch
    fixes this by making build_over_call use the SFINAE-friendly version of
    cxx_constant_value.
    
    This change causes us to no longer diagnose ahead of time a couple of
    non-constant non-dependent consteval calls in consteval-if2.C with
    -fchecking=2.  These errors were apparently coming from the call to
    fold_non_dependent_expr in build_non_dependent_expr (for the RHS of the +=)
    despite complain=tf_none being passed.  Now that build_over_call respects
    the value of complain during constant evaluation of a consteval call,
    the errors are gone.
    
    That the errors are also gone without -fchecking=2 is a regression caused
    by r12-7264-gc19f317a78c0e4 and is the subject of PR104620.  As described
    in comment #5, I think it's basically an accident that we were diagnosing
    these two calls correctly before r12-7264, so perhaps we can live without
    these errors for GCC 12.  Thus this patch just XFAILs the two tests.
    
            PR c++/104620
    
    gcc/cp/ChangeLog:
    
            * call.cc (build_over_call): Use cxx_constant_value_sfinae
            instead of cxx_constant_value to evaluate a consteval call.
            * constexpr.cc (cxx_constant_value_sfinae): Add decl parameter
            and pass it to cxx_eval_outermost_constant_expr.
            * cp-tree.h (cxx_constant_value_sfinae): Add decl parameter.
            * pt.cc (fold_targs_r): Pass NULL_TREE as decl parameter to
            cxx_constant_value_sfinae.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp23/consteval-if2.C: XFAIL two dg-error tests where
            the argument to the non-constant non-dependent consteval call is
            wrapped by NON_DEPENDENT_EXPR.
            * g++.dg/cpp2a/consteval30.C: New test.

Diff:
---
 gcc/cp/call.cc                             |  2 +-
 gcc/cp/constexpr.cc                        |  4 ++--
 gcc/cp/cp-tree.h                           |  2 +-
 gcc/cp/pt.cc                               |  2 +-
 gcc/testsuite/g++.dg/cpp23/consteval-if2.C |  4 ++--
 gcc/testsuite/g++.dg/cpp2a/consteval30.C   | 12 ++++++++++++
 6 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 23d3fc496b8..ec6c5d5baa2 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -9939,7 +9939,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
 		    obj_arg = TREE_OPERAND (addr, 0);
 		}
 	    }
-	  call = cxx_constant_value (call, obj_arg);
+	  call = cxx_constant_value_sfinae (call, obj_arg, complain);
 	  if (obj_arg && !error_operand_p (call))
 	    call = build2 (INIT_EXPR, void_type_node, obj_arg, call);
 	  call = convert_from_reference (call);
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index ce4ef8edfac..778680b8270 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -7960,10 +7960,10 @@ cxx_constant_value (tree t, tree decl)
 /* As above, but respect SFINAE.  */
 
 tree
-cxx_constant_value_sfinae (tree t, tsubst_flags_t complain)
+cxx_constant_value_sfinae (tree t, tree decl, tsubst_flags_t complain)
 {
   bool sfinae = !(complain & tf_error);
-  tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, true);
+  tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, true, false, decl);
   if (sfinae && !TREE_CONSTANT (r))
     r = error_mark_node;
   return r;
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 1bd7bc6fca2..2f718852ac1 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -8414,7 +8414,7 @@ extern bool require_constant_expression (tree);
 extern bool require_rvalue_constant_expression (tree);
 extern bool require_potential_rvalue_constant_expression (tree);
 extern tree cxx_constant_value			(tree, tree = NULL_TREE);
-extern tree cxx_constant_value_sfinae		(tree, tsubst_flags_t);
+extern tree cxx_constant_value_sfinae		(tree, tree, tsubst_flags_t);
 extern void cxx_constant_dtor			(tree, tree);
 extern tree cxx_constant_init			(tree, tree = NULL_TREE);
 extern tree maybe_constant_value		(tree, tree = NULL_TREE, bool = false);
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 715eea27577..173bc3a8c7f 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -19811,7 +19811,7 @@ fold_targs_r (tree targs, tsubst_flags_t complain)
 	       && !glvalue_p (elt)
 	       && !TREE_CONSTANT (elt))
 	{
-	  elt = cxx_constant_value_sfinae (elt, complain);
+	  elt = cxx_constant_value_sfinae (elt, NULL_TREE, complain);
 	  if (elt == error_mark_node)
 	    return false;
 	}
diff --git a/gcc/testsuite/g++.dg/cpp23/consteval-if2.C b/gcc/testsuite/g++.dg/cpp23/consteval-if2.C
index f7053b91c3c..d1845da9e58 100644
--- a/gcc/testsuite/g++.dg/cpp23/consteval-if2.C
+++ b/gcc/testsuite/g++.dg/cpp23/consteval-if2.C
@@ -77,11 +77,11 @@ qux (int x)
     }
   else
     {
-      r += foo (8 * x);	// { dg-error "is not a constant expression" }
+      r += foo (8 * x);	// { dg-error "is not a constant expression" "" { xfail *-*-* } }
     }
   if ! consteval	// { dg-warning "'if consteval' only available with" "" { target c++20_only } }
     {
-      r += foo (32 * x);// { dg-error "is not a constant expression" }
+      r += foo (32 * x);// { dg-error "is not a constant expression" "" { xfail *-*-* } }
     }
   if consteval		// { dg-warning "'if consteval' only available with" "" { target c++20_only } }
     {
diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval30.C b/gcc/testsuite/g++.dg/cpp2a/consteval30.C
new file mode 100644
index 00000000000..035265e019c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/consteval30.C
@@ -0,0 +1,12 @@
+// Test SFINAE for a non-constant consteval call.
+// { dg-do compile { target c++20 } }
+
+consteval int deref(const int* x) { return *x; }
+
+template<const int* P, int = deref(P)> // { dg-bogus "null pointer" }
+constexpr int f(int) { return 0; }
+
+template<const int* P>
+constexpr int f(...) { return 1; }
+
+static_assert(f<nullptr>(0) == 1);


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

only message in thread, other threads:[~2022-03-24 15:42 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-24 15:42 [gcc r12-7802] c++: missing SFINAE for non-constant consteval calls [PR104620] 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).