From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 7BD4838983BC for ; Wed, 26 Oct 2022 08:23:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7BD4838983BC Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666772588; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=uScUFzvFoJp63QLSJmlbySJ9O2fJX6EtC5teVdi9Y2I=; b=TPv88nLX/6AOOO2XcsJKZo8/GlMr9J0jH7OPuamYx84BWrxUwxiqSilNg572C13KzhvgV4 dqbD3s5PjoKYRcpIiwXcoIbjokpJd/5wcr0aMqLNQEMrApAL6Z/jJVq1OBTi6xzZ4VtNSv /Aj5uTfu+i7E0lKIE/MXg6DnnD1p12Q= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-263-0qQenB3jOriLwZuO3ZVEkg-1; Wed, 26 Oct 2022 04:23:06 -0400 X-MC-Unique: 0qQenB3jOriLwZuO3ZVEkg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7812F29AB448 for ; Wed, 26 Oct 2022 08:23:05 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.193.252]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3971D2027064; Wed, 26 Oct 2022 08:23:05 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 29Q8N24U2560643 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 26 Oct 2022 10:23:03 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 29Q8N2eM2560642; Wed, 26 Oct 2022 10:23:02 +0200 Date: Wed, 26 Oct 2022 10:23:02 +0200 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Fix excess precision related ICE on invalid binop [PR107382, PR107383] Message-ID: Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 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