public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: Jason Merrill <jason@redhat.com>
Cc: Patrick Palka <ppalka@redhat.com>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] c++: some missing-SFINAE fixes
Date: Tue, 13 Sep 2022 09:46:40 -0400 (EDT)	[thread overview]
Message-ID: <4336c12a-a7e0-a2ba-c39d-55d970865649@idea> (raw)
In-Reply-To: <30bb32f6-3f08-5a45-e39a-ae8f49bc4af0@redhat.com>

On Tue, 13 Sep 2022, Jason Merrill wrote:

> On 9/13/22 07:45, Patrick Palka wrote:
> > It looks like we aren't respecting SFINAE for:
> > 
> >    * an invalid/non-constant conditional explicit-specifier
> >    * a non-constant conditional noexcept-specifier
> >    * a non-constant argument to __integer_pack
> > 
> > This patch fixes these issues in the usual way, by passing complain
> > and propagating error_mark_node appropriately.
> > 
> > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> > trunk?
> > 
> > gcc/cp/ChangeLog:
> > 
> > 	* decl.cc (build_explicit_specifier): Pass complain to
> > 	cxx_constant_value.
> > 	* except.cc (build_noexcept_spec): Likewise.
> > 	* pt.cc (expand_integer_pack): Likewise.
> > 	(tsubst_function_decl): Propagate error_mark_node returned
> > 	from build_explicit_specifier.
> > 
> > gcc/testsuite/ChangeLog:
> > 
> > 	* g++.dg/cpp1z/noexcept-type26.C: New test.
> > 	* g++.dg/cpp2a/explicit19.C: New test.
> > 	* g++.dg/ext/integer-pack6.C: New test.
> > ---
> >   gcc/cp/decl.cc                               |  2 +-
> >   gcc/cp/except.cc                             |  2 +-
> >   gcc/cp/pt.cc                                 |  6 ++++--
> >   gcc/testsuite/g++.dg/cpp1z/noexcept-type26.C | 12 ++++++++++++
> >   gcc/testsuite/g++.dg/cpp2a/explicit19.C      | 12 ++++++++++++
> >   gcc/testsuite/g++.dg/ext/integer-pack6.C     | 13 +++++++++++++
> >   6 files changed, 43 insertions(+), 4 deletions(-)
> >   create mode 100644 gcc/testsuite/g++.dg/cpp1z/noexcept-type26.C
> >   create mode 100644 gcc/testsuite/g++.dg/cpp2a/explicit19.C
> >   create mode 100644 gcc/testsuite/g++.dg/ext/integer-pack6.C
> > 
> > diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
> > index 936f1cf0197..5404d7e084c 100644
> > --- a/gcc/cp/decl.cc
> > +++ b/gcc/cp/decl.cc
> > @@ -18557,7 +18557,7 @@ build_explicit_specifier (tree expr, tsubst_flags_t
> > complain)
> >       expr = build_converted_constant_bool_expr (expr, complain);
> >     expr = instantiate_non_dependent_expr (expr, complain);
> > -  expr = cxx_constant_value (expr);
> > +  expr = cxx_constant_value (expr, NULL_TREE, complain);
> 
> The patch is OK, but perhaps we want another overload that omits the object
> parameter?

Thanks -- sounds good, like the following incremental patch?

-- >8 --

Subject: [PATCH] c++: two-parameter version of cxx_constant_value

Since some callers need the complain parameter but not the object
parameter, let's introduce and use an overload of cxx_constant_value
that omits the latter.

gcc/cp/ChangeLog:

	* cp-tree.h (cxx_constant_value): Define two-parameter version
	that omits the object parameter.
	* decl.cc (build_explicit_specifier): Omit NULL_TREE object
	argument to cxx_constant_value.
	* except.cc (build_noexcept_spec): Likewise.
	* pt.cc (expand_integer_pack): Likewise.
	(fold_targs_r): Likewise.
	* semantics.cc (finish_if_stmt_cond): Likewise.
---
 gcc/cp/cp-tree.h    | 2 ++
 gcc/cp/decl.cc      | 2 +-
 gcc/cp/except.cc    | 2 +-
 gcc/cp/pt.cc        | 4 ++--
 gcc/cp/semantics.cc | 2 +-
 5 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 2ba44e80e20..1eb176d4a50 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -8414,6 +8414,8 @@ extern bool require_rvalue_constant_expression (tree);
 extern bool require_potential_rvalue_constant_expression (tree);
 extern tree cxx_constant_value			(tree, tree = NULL_TREE,
 						 tsubst_flags_t = tf_error);
+inline tree cxx_constant_value (tree t, tsubst_flags_t complain)
+{ return cxx_constant_value (t, NULL_TREE, complain); }
 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/decl.cc b/gcc/cp/decl.cc
index 5404d7e084c..006e9affcba 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -18557,7 +18557,7 @@ build_explicit_specifier (tree expr, tsubst_flags_t complain)
 
   expr = build_converted_constant_bool_expr (expr, complain);
   expr = instantiate_non_dependent_expr (expr, complain);
-  expr = cxx_constant_value (expr, NULL_TREE, complain);
+  expr = cxx_constant_value (expr, complain);
   return expr;
 }
 
diff --git a/gcc/cp/except.cc b/gcc/cp/except.cc
index 4d7f0ce102d..048612de400 100644
--- a/gcc/cp/except.cc
+++ b/gcc/cp/except.cc
@@ -1257,7 +1257,7 @@ build_noexcept_spec (tree expr, tsubst_flags_t complain)
     {
       expr = build_converted_constant_bool_expr (expr, complain);
       expr = instantiate_non_dependent_expr (expr, complain);
-      expr = cxx_constant_value (expr, NULL_TREE, complain);
+      expr = cxx_constant_value (expr, complain);
     }
   if (TREE_CODE (expr) == INTEGER_CST)
     {
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 074179288b6..db4e808adec 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -3869,7 +3869,7 @@ expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
   else
     {
       hi = instantiate_non_dependent_expr (hi, complain);
-      hi = cxx_constant_value (hi, NULL_TREE, complain);
+      hi = cxx_constant_value (hi, complain);
       int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
 
       /* Calculate the largest value of len that won't make the size of the vec
@@ -20121,7 +20121,7 @@ fold_targs_r (tree targs, tsubst_flags_t complain)
 	       && !glvalue_p (elt)
 	       && !TREE_CONSTANT (elt))
 	{
-	  elt = cxx_constant_value (elt, NULL_TREE, complain);
+	  elt = cxx_constant_value (elt, complain);
 	  if (elt == error_mark_node)
 	    return false;
 	}
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 3edefb2fca2..4f189978484 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -1042,7 +1042,7 @@ finish_if_stmt_cond (tree orig_cond, tree if_stmt)
     {
       maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/true);
       cond = instantiate_non_dependent_expr (cond);
-      cond = cxx_constant_value (cond, NULL_TREE);
+      cond = cxx_constant_value (cond);
     }
   else
     {
-- 
2.37.3.542.gdd3f6c4cae

> 
> >     return expr;
> >   }
> >   diff --git a/gcc/cp/except.cc b/gcc/cp/except.cc
> > index 7fdbc747c22..4d7f0ce102d 100644
> > --- a/gcc/cp/except.cc
> > +++ b/gcc/cp/except.cc
> > @@ -1257,7 +1257,7 @@ build_noexcept_spec (tree expr, tsubst_flags_t
> > complain)
> >       {
> >         expr = build_converted_constant_bool_expr (expr, complain);
> >         expr = instantiate_non_dependent_expr (expr, complain);
> > -      expr = cxx_constant_value (expr);
> > +      expr = cxx_constant_value (expr, NULL_TREE, complain);
> >       }
> >     if (TREE_CODE (expr) == INTEGER_CST)
> >       {
> > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> > index 1c6f4b84612..f16f5ffca20 100644
> > --- a/gcc/cp/pt.cc
> > +++ b/gcc/cp/pt.cc
> > @@ -3869,7 +3869,7 @@ expand_integer_pack (tree call, tree args,
> > tsubst_flags_t complain,
> >     else
> >       {
> >         hi = instantiate_non_dependent_expr (hi, complain);
> > -      hi = cxx_constant_value (hi);
> > +      hi = cxx_constant_value (hi, NULL_TREE, complain);
> >         int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
> >           /* Calculate the largest value of len that won't make the size of
> > the vec
> > @@ -14312,7 +14312,9 @@ tsubst_function_decl (tree t, tree args,
> > tsubst_flags_t complain,
> >   				    /*function_p=*/false,
> >   				    /*i_c_e_p=*/true);
> >         spec = build_explicit_specifier (spec, complain);
> > -      if (instantiation_dependent_expression_p (spec))
> > +      if (spec == error_mark_node)
> > +	return error_mark_node;
> > +      else if (instantiation_dependent_expression_p (spec))
> >   	store_explicit_specifier (r, spec);
> >         else
> >   	{
> > diff --git a/gcc/testsuite/g++.dg/cpp1z/noexcept-type26.C
> > b/gcc/testsuite/g++.dg/cpp1z/noexcept-type26.C
> > new file mode 100644
> > index 00000000000..73058395f0a
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/cpp1z/noexcept-type26.C
> > @@ -0,0 +1,12 @@
> > +// Verify a non-constant conditional noexcept-specifier of a function type
> > +// is a SFINAE error.
> > +// { dg-do compile { target c++17 } }
> > +
> > +template<class T> void f(void() noexcept(T::value)) = delete;
> > +template<class T> void f(...);
> > +
> > +struct B { static bool value; };
> > +
> > +int main() {
> > +  f<B>(nullptr);
> > +}
> > diff --git a/gcc/testsuite/g++.dg/cpp2a/explicit19.C
> > b/gcc/testsuite/g++.dg/cpp2a/explicit19.C
> > new file mode 100644
> > index 00000000000..47903813680
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/cpp2a/explicit19.C
> > @@ -0,0 +1,12 @@
> > +// Verify a conditional explicit-specifier is a SFINAE context.
> > +// { dg-do compile { target c++20 } }
> > +
> > +struct A {
> > +  template<class T> explicit(T::value) A(T) = delete;
> > +  A(...);
> > +};
> > +
> > +struct B { static bool value; };
> > +
> > +A x(0);
> > +A y(B{});
> > diff --git a/gcc/testsuite/g++.dg/ext/integer-pack6.C
> > b/gcc/testsuite/g++.dg/ext/integer-pack6.C
> > new file mode 100644
> > index 00000000000..7e16bc44b40
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/ext/integer-pack6.C
> > @@ -0,0 +1,13 @@
> > +// Verify a non-constant argument to __integer_pack is a SFINAE error.
> > +// { dg-do compile { target c++11 } }
> > +
> > +template<int...> struct A { };
> > +
> > +template<class T> auto f(int) -> A<__integer_pack(T::value)...> = delete;
> > +template<class T> void f(...);
> > +
> > +struct B { static int value; };
> > +
> > +int main() {
> > +  f<B>(0);
> > +}
> 
> 


  reply	other threads:[~2022-09-13 13:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-13 11:45 Patrick Palka
2022-09-13 12:53 ` Jason Merrill
2022-09-13 13:46   ` Patrick Palka [this message]
2022-09-13 13:49     ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4336c12a-a7e0-a2ba-c39d-55d970865649@idea \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).