c++: Fix excess precision related ICE on invalid binop [PR107382, PR107383]

Message ID Y1juZu+TsIub4jZj@tucnak
State Unresolved
Headers
Series c++: Fix excess precision related ICE on invalid binop [PR107382, PR107383] |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Jakub Jelinek Oct. 26, 2022, 8:23 a.m. UTC
  Hi!

The following tests ICE in the gcc_assert (common); in cp_build_binary_op.
I've missed that while for * common is set always, while for +, - and /
it is in some cases not.
If it is not, then
  if (!result_type
      && arithmetic_types_p
      && (shorten || common || short_compare))
condition is false, then the following
  if (may_need_excess_precision
      && (orig_type0 != type0 || orig_type1 != type1)
      && build_type == NULL_TREE)
would fail the assertion there and if there wouldn't be excess precision,
  if (code == SPACESHIP_EXPR)
would be false (for SPACESHIP_EXPR we always have build_type set like for
other comparisons) and then trigger
  if (!result_type)
    {
      if (complain & tf_error)
        {
          binary_op_rich_location richloc (location,
                                           orig_op0, orig_op1, true);
          error_at (&richloc,
                    "invalid operands of types %qT and %qT to binary %qO",
                    TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
        }
      return error_mark_node;
    }
So, if result_type is NULL, we don't really need to compute
semantic_result_type because nothing will use it anyway and can get
fall through into the error/return error_mark_node; case.

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

2022-10-26  Jakub Jelinek  <jakub@redhat.com>

	PR c++/107382
	PR c++/107383
	* typeck.cc (cp_build_binary_op): Don't compute semantic_result_type
	if result_type is NULL.

	* g++.dg/diagnostic/bad-binary-ops2.C: New test.


	Jakub
  

Comments

Jason Merrill Oct. 26, 2022, 4:56 p.m. UTC | #1
On 10/26/22 04:23, Jakub Jelinek wrote:
> Hi!
> 
> The following tests ICE in the gcc_assert (common); in cp_build_binary_op.
> I've missed that while for * common is set always, while for +, - and /
> it is in some cases not.
> If it is not, then
>    if (!result_type
>        && arithmetic_types_p
>        && (shorten || common || short_compare))
> condition is false, then the following
>    if (may_need_excess_precision
>        && (orig_type0 != type0 || orig_type1 != type1)
>        && build_type == NULL_TREE)
> would fail the assertion there and if there wouldn't be excess precision,
>    if (code == SPACESHIP_EXPR)
> would be false (for SPACESHIP_EXPR we always have build_type set like for
> other comparisons) and then trigger
>    if (!result_type)
>      {
>        if (complain & tf_error)
>          {
>            binary_op_rich_location richloc (location,
>                                             orig_op0, orig_op1, true);
>            error_at (&richloc,
>                      "invalid operands of types %qT and %qT to binary %qO",
>                      TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
>          }
>        return error_mark_node;
>      }
> So, if result_type is NULL, we don't really need to compute
> semantic_result_type because nothing will use it anyway and can get
> fall through into the error/return error_mark_node; case.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

> 2022-10-26  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/107382
> 	PR c++/107383
> 	* typeck.cc (cp_build_binary_op): Don't compute semantic_result_type
> 	if result_type is NULL.
> 
> 	* g++.dg/diagnostic/bad-binary-ops2.C: New test.
> 
> --- gcc/cp/typeck.cc.jj	2022-10-25 10:37:28.008533040 +0200
> +++ gcc/cp/typeck.cc	2022-10-25 19:18:20.951846806 +0200
> @@ -6179,7 +6179,8 @@ cp_build_binary_op (const op_location_t
>       }
>     if (may_need_excess_precision
>         && (orig_type0 != type0 || orig_type1 != type1)
> -      && build_type == NULL_TREE)
> +      && build_type == NULL_TREE
> +      && result_type)
>       {
>         gcc_assert (common);
>         semantic_result_type = cp_common_type (orig_type0, orig_type1);
> --- gcc/testsuite/g++.dg/diagnostic/bad-binary-ops2.C.jj	2022-10-25 19:49:49.190198057 +0200
> +++ gcc/testsuite/g++.dg/diagnostic/bad-binary-ops2.C	2022-10-25 20:10:35.266264891 +0200
> @@ -0,0 +1,26 @@
> +// PR c++/107382
> +// PR c++/107383
> +// { dg-do compile }
> +// { dg-options "-O2 -fexcess-precision=standard" }
> +
> +void
> +foo ()
> +{
> +  float t[2] = { 1, 2 };
> +  int const *s = 0;
> +  t[1] / s;	// { dg-error "invalid operands of types 'float' and 'const int\\\*' to binary 'operator/'" }
> +}
> +
> +void
> +bar ()
> +{
> +  float t[2] = { 1, 2 };
> +  int const *s[2] = { 0, 0 };
> +  t[1] / s[0];	// { dg-error "invalid operands of types 'float' and 'const int\\\*' to binary 'operator/'" }
> +}
> +
> +void
> +baz (float a, int* b)
> +{
> +  a -= b;	// { dg-error "invalid operands of types 'float' and 'int\\\*' to binary 'operator-'" }
> +}
> 
> 	Jakub
>
  

Patch

--- gcc/cp/typeck.cc.jj	2022-10-25 10:37:28.008533040 +0200
+++ gcc/cp/typeck.cc	2022-10-25 19:18:20.951846806 +0200
@@ -6179,7 +6179,8 @@  cp_build_binary_op (const op_location_t
     }
   if (may_need_excess_precision
       && (orig_type0 != type0 || orig_type1 != type1)
-      && build_type == NULL_TREE)
+      && build_type == NULL_TREE
+      && result_type)
     {
       gcc_assert (common);
       semantic_result_type = cp_common_type (orig_type0, orig_type1);
--- gcc/testsuite/g++.dg/diagnostic/bad-binary-ops2.C.jj	2022-10-25 19:49:49.190198057 +0200
+++ gcc/testsuite/g++.dg/diagnostic/bad-binary-ops2.C	2022-10-25 20:10:35.266264891 +0200
@@ -0,0 +1,26 @@ 
+// PR c++/107382
+// PR c++/107383
+// { dg-do compile }
+// { dg-options "-O2 -fexcess-precision=standard" }
+
+void
+foo ()
+{
+  float t[2] = { 1, 2 };
+  int const *s = 0;
+  t[1] / s;	// { dg-error "invalid operands of types 'float' and 'const int\\\*' to binary 'operator/'" }
+}
+
+void
+bar ()
+{
+  float t[2] = { 1, 2 };
+  int const *s[2] = { 0, 0 };
+  t[1] / s[0];	// { dg-error "invalid operands of types 'float' and 'const int\\\*' to binary 'operator/'" }
+}
+
+void
+baz (float a, int* b)
+{
+  a -= b;	// { dg-error "invalid operands of types 'float' and 'int\\\*' to binary 'operator-'" }
+}