From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 650AC3886C47; Thu, 27 Oct 2022 08:27:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 650AC3886C47 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666859251; bh=+zfzSYZkt01OWKLcXQwv3yb/kjRnzgvcAn5kHzRjcgk=; h=From:To:Subject:Date:From; b=QQzqF4G477kJbeSbKnffSXl41YJ/7IF/niOrnlCAaB6WsGlPUVQ15NFD7AwP5KxWH aqD52KWZVyyTuULaL/5pjYq5oKgs6AV1QWa3oM/qUR6lna6e3AM3M0YzvzWWB1WIeX x28Gk6FKCwxoR+9P6lqSf5nt1w2keCjbP02ErvMI= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-3522] c++: Fix excess precision related ICE on invalid binop [PR107382, PR107383] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: 4e1d704243a4f3c4ded47cd0d02427bb7efef069 X-Git-Newrev: bfb7994a9fb0b10767d12b8d670c081014ad8b01 Message-Id: <20221027082731.650AC3886C47@sourceware.org> Date: Thu, 27 Oct 2022 08:27:31 +0000 (GMT) List-Id: https://gcc.gnu.org/g:bfb7994a9fb0b10767d12b8d670c081014ad8b01 commit r13-3522-gbfb7994a9fb0b10767d12b8d670c081014ad8b01 Author: Jakub Jelinek Date: Thu Oct 27 10:24:45 2022 +0200 c++: Fix excess precision related ICE on invalid binop [PR107382, PR107383] 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. 2022-10-27 Jakub Jelinek 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. Diff: --- gcc/cp/typeck.cc | 3 ++- gcc/testsuite/g++.dg/diagnostic/bad-binary-ops2.C | 26 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 4605f734e2d..2e0fd8fbf17 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -6179,7 +6179,8 @@ cp_build_binary_op (const op_location_t &location, } 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); diff --git a/gcc/testsuite/g++.dg/diagnostic/bad-binary-ops2.C b/gcc/testsuite/g++.dg/diagnostic/bad-binary-ops2.C new file mode 100644 index 00000000000..627e8a573f1 --- /dev/null +++ b/gcc/testsuite/g++.dg/diagnostic/bad-binary-ops2.C @@ -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-'" } +}