diff mbox series

c++: Fix -std=c++20 ICE on virtual method call [PR99132]

Message ID 20210218083001.GY4020736@tucnak
State New
Headers show
Series c++: Fix -std=c++20 ICE on virtual method call [PR99132] | expand

Commit Message

Jakub Jelinek Feb. 18, 2021, 8:30 a.m. UTC
Hi!

On the following testcase we ICE in C++20 mode during cp_get_callee_fndecl
-> constexpr evaluation.
It is only in C++20 mode on this testcase because virtual methods can't
be constexpr in C++17 and earlier and so potential_constant_expression_1
rejects it earlier.
And the ICE is caused by genericization changing the h PARM_DECL from having
B<C *> type to B<C *> & DECL_BY_REFERENCE and the constexpr evaluation
not being able to deal with that.
I think this just shows that we shouldn't do the constexpr evaluation during
genericization and later, and other spots e.g. during gimplification
also don't call cp_get_callee_fndecl but cp_get_callee_fndecl_nofold.
After all, cp_fold has already been run and it did the folding if there
was any opportunity to do so.  And furthermore, what that cp_genericize_r
spot does is check for any left-over immediate function calls (which can be
ATM just std::source_location::current() call) and immediate functions
outside of immediate functions can't have addresses leaked into the IL,
so it will be always a direct call anyway.  And immediate functions
themselves don't make it into genericization/gimplification.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

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

	PR c++/99132
	* cp-gimplify.c (cp_genericize_r) <case CALL_EXPR>: Use
	cp_get_callee_fndecl_nofold instead of cp_get_callee_fndecl to check
	for immediate function calls.

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


	Jakub

Comments

Nathan Sidwell Feb. 18, 2021, 1:48 p.m. UTC | #1
On 2/18/21 3:30 AM, Jakub Jelinek wrote:
> Hi!
> 
> On the following testcase we ICE in C++20 mode during cp_get_callee_fndecl
> -> constexpr evaluation.
> It is only in C++20 mode on this testcase because virtual methods can't
> be constexpr in C++17 and earlier and so potential_constant_expression_1
> rejects it earlier.
> And the ICE is caused by genericization changing the h PARM_DECL from having
> B<C *> type to B<C *> & DECL_BY_REFERENCE and the constexpr evaluation
> not being able to deal with that.
> I think this just shows that we shouldn't do the constexpr evaluation during
> genericization and later, and other spots e.g. during gimplification
> also don't call cp_get_callee_fndecl but cp_get_callee_fndecl_nofold.

Agreed, we've run into this kind of thing before.

> After all, cp_fold has already been run and it did the folding if there
> was any opportunity to do so.  And furthermore, what that cp_genericize_r
> spot does is check for any left-over immediate function calls (which can be
> ATM just std::source_location::current() call) and immediate functions
> outside of immediate functions can't have addresses leaked into the IL,
> so it will be always a direct call anyway.  And immediate functions
> themselves don't make it into genericization/gimplification.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

ok, thanks
> 
> 2021-02-18  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/99132
> 	* cp-gimplify.c (cp_genericize_r) <case CALL_EXPR>: Use
> 	cp_get_callee_fndecl_nofold instead of cp_get_callee_fndecl to check
> 	for immediate function calls.
> 
> 	* g++.dg/cpp2a/constexpr-virtual18.C: New test.
> 
> --- gcc/cp/cp-gimplify.c.jj	2021-02-17 16:14:42.621294819 +0100
> +++ gcc/cp/cp-gimplify.c	2021-02-17 16:48:30.343291226 +0100
> @@ -1386,7 +1386,7 @@ cp_genericize_r (tree *stmt_p, int *walk
>   	  break;
>   	}
>   
> -      if (tree fndecl = cp_get_callee_fndecl (stmt))
> +      if (tree fndecl = cp_get_callee_fndecl_nofold (stmt))
>   	if (DECL_IMMEDIATE_FUNCTION_P (fndecl))
>   	  {
>   	    gcc_assert (source_location_current_p (fndecl));
> --- gcc/testsuite/g++.dg/cpp2a/constexpr-virtual18.C.jj	2021-02-17 16:48:18.136421425 +0100
> +++ gcc/testsuite/g++.dg/cpp2a/constexpr-virtual18.C	2021-02-17 16:49:55.929378398 +0100
> @@ -0,0 +1,13 @@
> +// PR c++/99132
> +// { dg-do compile { target c++11 } }
> +
> +template <class T> struct A { T c; };
> +template <class T> struct B {
> +  A<T> d;
> +  constexpr T operator-> () { return d.c; }
> +  B (B &&);
> +};
> +struct C {
> +  virtual void foo ();
> +  void bar (B<C *> h) { h->foo (); }
> +};
> 
> 	Jakub
>
diff mbox series

Patch

--- gcc/cp/cp-gimplify.c.jj	2021-02-17 16:14:42.621294819 +0100
+++ gcc/cp/cp-gimplify.c	2021-02-17 16:48:30.343291226 +0100
@@ -1386,7 +1386,7 @@  cp_genericize_r (tree *stmt_p, int *walk
 	  break;
 	}
 
-      if (tree fndecl = cp_get_callee_fndecl (stmt))
+      if (tree fndecl = cp_get_callee_fndecl_nofold (stmt))
 	if (DECL_IMMEDIATE_FUNCTION_P (fndecl))
 	  {
 	    gcc_assert (source_location_current_p (fndecl));
--- gcc/testsuite/g++.dg/cpp2a/constexpr-virtual18.C.jj	2021-02-17 16:48:18.136421425 +0100
+++ gcc/testsuite/g++.dg/cpp2a/constexpr-virtual18.C	2021-02-17 16:49:55.929378398 +0100
@@ -0,0 +1,13 @@ 
+// PR c++/99132
+// { dg-do compile { target c++11 } }
+
+template <class T> struct A { T c; };
+template <class T> struct B {
+  A<T> d;
+  constexpr T operator-> () { return d.c; }
+  B (B &&);
+};
+struct C {
+  virtual void foo ();
+  void bar (B<C *> h) { h->foo (); }
+};