public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Don't reject calls through PMF during constant evaluation [PR102786]
@ 2021-10-18 12:14 Jakub Jelinek
  2021-10-18 16:57 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2021-10-18 12:14 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

Hi!

The following testcase incorrectly rejects the c initializer,
while in the s.*a case cxx_eval_* sees .__pfn reads etc.,
in the s.*&S::foo case get_member_function_from_ptrfunc creates
expressions which use INTEGER_CSTs with type of pointer to METHOD_TYPE.
And cxx_eval_constant_expression rejects any INTEGER_CSTs with pointer
type if they aren't 0.
Either we'd need to make sure we defer such folding till cp_fold but the
function and pfn_from_ptrmemfunc is used from lots of places, or
the following patch just tries to reject only non-zero INTEGER_CSTs
with pointer types if they don't point to METHOD_TYPE in the hope that
all such INTEGER_CSTs with POINTER_TYPE to METHOD_TYPE are result of
folding valid pointer-to-member function expressions.
I don't immediately see how one could create such INTEGER_CSTs otherwise,
cast of integers to PMF is rejected and would have the PMF RECORD_TYPE
anyway, etc.

Regtested on x86_64-linux
(with GXX_TESTSUITE_STDS=98,11,14,17,20,2b make check-g++)
ok for trunk if it passes full bootstrap/regtest?

2021-10-18  Jakub Jelinek  <jakub@redhat.com>

	PR c++/102786
	* constexpr.c (cxx_eval_constant_expression): Don't reject
	INTEGER_CSTs with type POINTER_TYPE to METHOD_TYPE.

	* g++.dg/cpp2a/constexpr-virtual19.C: New test.

--- gcc/cp/constexpr.c.jj	2021-10-15 11:59:15.917687093 +0200
+++ gcc/cp/constexpr.c	2021-10-18 13:26:49.458610657 +0200
@@ -6191,6 +6191,7 @@ cxx_eval_constant_expression (const cons
 
       if (TREE_CODE (t) == INTEGER_CST
 	  && TYPE_PTR_P (TREE_TYPE (t))
+	  && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
 	  && !integer_zerop (t))
 	{
 	  if (!ctx->quiet)
--- gcc/testsuite/g++.dg/cpp2a/constexpr-virtual19.C.jj	2021-10-18 13:35:00.229693908 +0200
+++ gcc/testsuite/g++.dg/cpp2a/constexpr-virtual19.C	2021-10-18 12:31:05.265747723 +0200
@@ -0,0 +1,11 @@
+// PR c++/102786
+// { dg-do compile { target c++20 } }
+
+struct S {
+  virtual constexpr int foo () const { return 42; }
+};
+
+constexpr S s;
+constexpr auto a = &S::foo;
+constexpr auto b = (s.*a) ();
+constexpr auto c = (s.*&S::foo) ();

	Jakub


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] c++: Don't reject calls through PMF during constant evaluation [PR102786]
  2021-10-18 12:14 [PATCH] c++: Don't reject calls through PMF during constant evaluation [PR102786] Jakub Jelinek
@ 2021-10-18 16:57 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2021-10-18 16:57 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On 10/18/21 08:14, Jakub Jelinek wrote:
> Hi!
> 
> The following testcase incorrectly rejects the c initializer,
> while in the s.*a case cxx_eval_* sees .__pfn reads etc.,
> in the s.*&S::foo case get_member_function_from_ptrfunc creates
> expressions which use INTEGER_CSTs with type of pointer to METHOD_TYPE.
> And cxx_eval_constant_expression rejects any INTEGER_CSTs with pointer
> type if they aren't 0.
> Either we'd need to make sure we defer such folding till cp_fold but the
> function and pfn_from_ptrmemfunc is used from lots of places, or
> the following patch just tries to reject only non-zero INTEGER_CSTs
> with pointer types if they don't point to METHOD_TYPE in the hope that
> all such INTEGER_CSTs with POINTER_TYPE to METHOD_TYPE are result of
> folding valid pointer-to-member function expressions.
> I don't immediately see how one could create such INTEGER_CSTs otherwise,
> cast of integers to PMF is rejected and would have the PMF RECORD_TYPE
> anyway, etc.
> 
> Regtested on x86_64-linux
> (with GXX_TESTSUITE_STDS=98,11,14,17,20,2b make check-g++)
> ok for trunk if it passes full bootstrap/regtest?
> 
> 2021-10-18  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/102786
> 	* constexpr.c (cxx_eval_constant_expression): Don't reject
> 	INTEGER_CSTs with type POINTER_TYPE to METHOD_TYPE.
> 
> 	* g++.dg/cpp2a/constexpr-virtual19.C: New test.
> 
> --- gcc/cp/constexpr.c.jj	2021-10-15 11:59:15.917687093 +0200
> +++ gcc/cp/constexpr.c	2021-10-18 13:26:49.458610657 +0200
> @@ -6191,6 +6191,7 @@ cxx_eval_constant_expression (const cons
>   
>         if (TREE_CODE (t) == INTEGER_CST
>   	  && TYPE_PTR_P (TREE_TYPE (t))
> +	  && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE

This should have a comment that, as you say, an INTEGER_CST with 
pointer-to-method type is only used for a virtual method in a pointer to 
member function.  OK with that change.

>   	  && !integer_zerop (t))
>   	{
>   	  if (!ctx->quiet)
> --- gcc/testsuite/g++.dg/cpp2a/constexpr-virtual19.C.jj	2021-10-18 13:35:00.229693908 +0200
> +++ gcc/testsuite/g++.dg/cpp2a/constexpr-virtual19.C	2021-10-18 12:31:05.265747723 +0200
> @@ -0,0 +1,11 @@
> +// PR c++/102786
> +// { dg-do compile { target c++20 } }
> +
> +struct S {
> +  virtual constexpr int foo () const { return 42; }
> +};
> +
> +constexpr S s;
> +constexpr auto a = &S::foo;
> +constexpr auto b = (s.*a) ();
> +constexpr auto c = (s.*&S::foo) ();
> 
> 	Jakub
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-10-18 16:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-18 12:14 [PATCH] c++: Don't reject calls through PMF during constant evaluation [PR102786] Jakub Jelinek
2021-10-18 16:57 ` Jason Merrill

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).