c++: Avoid calls in non-evaluated contexts affect whether function can or can't throw [PR94326]

Message ID 20200326193930.GN2156@tucnak
State New
Headers show
Series
  • c++: Avoid calls in non-evaluated contexts affect whether function can or can't throw [PR94326]
Related show

Commit Message

Christophe Lyon via Gcc-patches March 26, 2020, 7:39 p.m.
Hi!

The following testcase FAILs -fcompare-debug, because if we emit a
-Wreturn-local-addr warning, we tsubst decltype in order to print the
warning and as that function could throw, set_flags_from_callee during that
sets cp_function_chain->can_throw and later on we don't set TREE_NOTHROW
on foo.  While with -w or -Wno-return-local-addr, tsubst isn't called during
the warning_at, cp_function_chain->can_throw is kept clear and TREE_NOTHROW
is set on foo.
It isn't just a matter of the warning though, in
int foo ();
int bar () { return sizeof (foo ()); }
int baz () { return sizeof (int); }
I don't really see why we should mark only baz as TREE_NOTHROW and not bar
too, when neither can really throw.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk?

2020-03-26  Jakub Jelinek  <jakub@redhat.com>

	PR c++/94326
	* call.c (set_flags_from_callee): Don't update
	cp_function_chain->can_throw or current_function_returns_abnormally
	if cp_unevaluated_operand.

	* g++.dg/other/pr94326.C: New test.


	Jakub

Comments

Christophe Lyon via Gcc-patches March 27, 2020, 2:11 a.m. | #1
On 3/26/20 3:39 PM, Jakub Jelinek wrote:
> Hi!

> 

> The following testcase FAILs -fcompare-debug, because if we emit a

> -Wreturn-local-addr warning, we tsubst decltype in order to print the

> warning and as that function could throw, set_flags_from_callee during that

> sets cp_function_chain->can_throw and later on we don't set TREE_NOTHROW

> on foo.  While with -w or -Wno-return-local-addr, tsubst isn't called during

> the warning_at, cp_function_chain->can_throw is kept clear and TREE_NOTHROW

> is set on foo.

> It isn't just a matter of the warning though, in

> int foo ();

> int bar () { return sizeof (foo ()); }

> int baz () { return sizeof (int); }

> I don't really see why we should mark only baz as TREE_NOTHROW and not bar

> too, when neither can really throw.

> 

> Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for

> trunk?


OK.

> 2020-03-26  Jakub Jelinek  <jakub@redhat.com>

> 

> 	PR c++/94326

> 	* call.c (set_flags_from_callee): Don't update

> 	cp_function_chain->can_throw or current_function_returns_abnormally

> 	if cp_unevaluated_operand.

> 

> 	* g++.dg/other/pr94326.C: New test.

> 

> --- gcc/cp/call.c.jj	2020-03-25 08:05:07.153731580 +0100

> +++ gcc/cp/call.c	2020-03-26 15:03:42.432909693 +0100

> @@ -333,11 +333,14 @@ set_flags_from_callee (tree call)

>   	   && internal_fn_flags (CALL_EXPR_IFN (call)) & ECF_NOTHROW)

>       nothrow = true;

>   

> -  if (!nothrow && at_function_scope_p () && cfun && cp_function_chain)

> -    cp_function_chain->can_throw = 1;

> +  if (cfun && cp_function_chain && !cp_unevaluated_operand)

> +    {

> +      if (!nothrow && at_function_scope_p ())

> +	cp_function_chain->can_throw = 1;

>   

> -  if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)

> -    current_function_returns_abnormally = 1;

> +      if (decl && TREE_THIS_VOLATILE (decl))

> +	current_function_returns_abnormally = 1;

> +    }

>   

>     TREE_NOTHROW (call) = nothrow;

>   }

> --- gcc/testsuite/g++.dg/other/pr94326.C.jj	2020-03-26 15:14:23.609400216 +0100

> +++ gcc/testsuite/g++.dg/other/pr94326.C	2020-03-26 15:14:54.162947065 +0100

> @@ -0,0 +1,19 @@

> +// PR c++/94326

> +// { dg-do compile { target c++11 } }

> +// { dg-options "-fcompare-debug" }

> +

> +template <typename = int> struct A {

> +  const int &foo() { return 0; }	// { dg-warning "returning reference to temporary" }

> +  template <typename _Kt> void bar(_Kt) { foo(); }

> +};

> +struct B {

> +  A<> b;

> +  template <typename _Kt> auto baz(_Kt p1) -> decltype(b.bar(p1)) {

> +    b.bar(p1);

> +  }

> +};

> +struct C {};

> +void operator<(C, int) {

> +  B a;

> +  a.baz(C{});

> +}

> 

> 	Jakub

>

Patch

--- gcc/cp/call.c.jj	2020-03-25 08:05:07.153731580 +0100
+++ gcc/cp/call.c	2020-03-26 15:03:42.432909693 +0100
@@ -333,11 +333,14 @@  set_flags_from_callee (tree call)
 	   && internal_fn_flags (CALL_EXPR_IFN (call)) & ECF_NOTHROW)
     nothrow = true;
 
-  if (!nothrow && at_function_scope_p () && cfun && cp_function_chain)
-    cp_function_chain->can_throw = 1;
+  if (cfun && cp_function_chain && !cp_unevaluated_operand)
+    {
+      if (!nothrow && at_function_scope_p ())
+	cp_function_chain->can_throw = 1;
 
-  if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
-    current_function_returns_abnormally = 1;
+      if (decl && TREE_THIS_VOLATILE (decl))
+	current_function_returns_abnormally = 1;
+    }
 
   TREE_NOTHROW (call) = nothrow;
 }
--- gcc/testsuite/g++.dg/other/pr94326.C.jj	2020-03-26 15:14:23.609400216 +0100
+++ gcc/testsuite/g++.dg/other/pr94326.C	2020-03-26 15:14:54.162947065 +0100
@@ -0,0 +1,19 @@ 
+// PR c++/94326
+// { dg-do compile { target c++11 } }
+// { dg-options "-fcompare-debug" }
+
+template <typename = int> struct A {
+  const int &foo() { return 0; }	// { dg-warning "returning reference to temporary" }
+  template <typename _Kt> void bar(_Kt) { foo(); }
+};
+struct B {
+  A<> b;
+  template <typename _Kt> auto baz(_Kt p1) -> decltype(b.bar(p1)) {
+    b.bar(p1);
+  }
+};
+struct C {};
+void operator<(C, int) {
+  B a;
+  a.baz(C{});
+}