From patchwork Thu Jan 25 19:18:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1890948 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=jUkpdra4; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TLVxN57J3z1yS7 for ; Fri, 26 Jan 2024 06:19:04 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8B6973858C62 for ; Thu, 25 Jan 2024 19:19:02 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 18F5E3858C53 for ; Thu, 25 Jan 2024 19:18:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 18F5E3858C53 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 18F5E3858C53 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706210291; cv=none; b=Rb7YHF59uKTaC3xUldyp7HL6oq7rN8fVmUKlUZRVsZ8Zuiv0Mn0CUZ35agRede4aEZWbxYZp8zqQWN3O5KE9mf+kqOxFQB4yRkhImbclh5biv0qYIvCz69yGOj28O6d6JXWVz1cNZ3y4JRjZX6oEbtDTxW4j/FbUResffjkdcTg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706210291; c=relaxed/simple; bh=KnhBb7wq84WDGvglzoZZa4fQajUSMwSBzjzGsukW++4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=P38HhkFsGpPyIAjCI+lboQNTPKiWEVd1bTtSmdvAcGxnUcBU9nN18VXlKgQmwjElB91v8yiKO3MPV7flvADtigZWjw+Jbug0RkAG/Gn18XTpfBHG5IyOKV1mVQpqPyIQP3ITbstHz0gJpGzotCtFdh3TTAkh6utvXzi4M2YQ9n0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706210289; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=qg7hL1NMMkWrPsCLsYY523K5346Lg74ou8oksOs59yo=; b=jUkpdra487kPSlflq04CFHKS12myQs2ipFSXcAizAhKd7pvFHoDqFWn/QNsyUdIvJvwXlZ p0kyVPrCqG8Rztpw/VT0hTpLA1bx5ZUr8WsUScAAcvNVomQN080do/QUSvO/qHVMd5PVDy 74xqi7wpGsm68ZfbClaqoUZckX+DFnc= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-611-6pPkfSKnNSaOL51b1AxuLw-1; Thu, 25 Jan 2024 14:18:08 -0500 X-MC-Unique: 6pPkfSKnNSaOL51b1AxuLw-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-42a56cd0837so40756941cf.2 for ; Thu, 25 Jan 2024 11:18:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706210287; x=1706815087; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qg7hL1NMMkWrPsCLsYY523K5346Lg74ou8oksOs59yo=; b=EIq0tKczUA4+gsTYcO9tdAdEqadoOl9Q0U2Q8JrGM8gYE37BMfxrI3ytTN///TqtUT rxKBVsslDrrOusSk6ix7v1ICwrUoeL8YdUY8fGNnFHg2EQMDntHIGXqNWoKWHF+eTQba vAW+SY/pIbInye7TT5z9z3EHoAvAJ1WRac92fuiBcvryAETxjFDC9QYCXq4h9eeB73Fg KjxPm+eiDwCCMyp4J/4S/puKytBPOZ8kKe0nUIZhPh+24KMlrW2+6zq3Y/qxnCIaI43Q bdYVv8gPjJKoszsQOkCqkluds+9i2UVXCfnGFNIUp2DUywAB1OnbOxjh368WbqfSWmmm /4gQ== X-Gm-Message-State: AOJu0YwzlaE+ZTh27nWG2nhURwVL4xcI3cipEqGLYD7mNdILqSk7mEN0 cBB9ypXSbmNml/tCvpTJ3ebUHQvc0Z2MYy/iGDpS5PCs42biMFBsH8eplveZsEZ1kluQF+Ni84r nW/T3DhMCMsaeZmYx5pbHTYArxwmuj5ME9zeL/0DcKWOyh8PM9vedvZ9A8cPLEvH8ZoPYjy6zoY 51M8CcDlqniMGnsZNVFGV478PYGnHtrLl0iFRi X-Received: by 2002:a05:622a:198a:b0:42a:6cf8:c5d7 with SMTP id u10-20020a05622a198a00b0042a6cf8c5d7mr168647qtc.54.1706210286800; Thu, 25 Jan 2024 11:18:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IGrGzPHw/l1N1S6cXVhZAR4tPU4I/RyyaY2bFGNUFRcZeyCes6qtmX5/7WHvfO8qS9V9lf65Q== X-Received: by 2002:a05:622a:198a:b0:42a:6cf8:c5d7 with SMTP id u10-20020a05622a198a00b0042a6cf8c5d7mr168637qtc.54.1706210286496; Thu, 25 Jan 2024 11:18:06 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id c17-20020ac81111000000b00429b98d43c3sm5580081qtj.67.2024.01.25.11.18.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 11:18:06 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: problematic assert in reference_binding [PR113141] Date: Thu, 25 Jan 2024 14:18:04 -0500 Message-ID: <20240125191804.2574786-1-ppalka@redhat.com> X-Mailer: git-send-email 2.43.0.386.ge02ecfcc53 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk/13? This isn't a very satisfactory fix, but at least it safely fixes these testcases I guess. Note that there's implementation disagreement about the second testcase, GCC always accepted it but Clang/MSVC/icc reject it. -- >8 -- In the bad reference binding shortcutting commit r13-1755-g68f37670eff0b872 I made us check the badness criteria in reference_binding earlier in the function so that we can fail fast and avoid unnecessary template instantiation during the first pass of overload resolution. This was for the most part obviously safe, except now the badness criteria are also checked before the recursive case[1] in reference_binding, whereas before the criteria didn't apply in that case. So in order to justify this hoisting I added a sanity check that the badness criteria are still sound in the recursive case. Unfortunately the below testcases triggers the sanity check. I'm not sure if this means the bad conversion shortcutting is unsound since I wasn't able to construct a testcase that affects overload resolution. And if it is unsound, I'm not sure how we can make it sound in light of this recursive logic for non-direct user-defined conversions. But we can at least restore the pre-r13-1755 behavior for the below two testcases by simply getting rid of this sanity check. [1]: Added in https://gcc.gnu.org/pipermail/gcc-patches/2014-April/386365.html PR c++/113141 gcc/cp/ChangeLog: * call.cc (reference_binding): Remove badness criteria sanity check in the recursive case. gcc/testsuite/ChangeLog: * g++.dg/conversion/ref10.C: New test. * g++.dg/conversion/ref11.C: New test. --- gcc/cp/call.cc | 1 - gcc/testsuite/g++.dg/conversion/ref10.C | 13 +++++++++++++ gcc/testsuite/g++.dg/conversion/ref11.C | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/conversion/ref10.C create mode 100644 gcc/testsuite/g++.dg/conversion/ref11.C diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 9de0d77c423..2dce52bc7b8 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -2034,7 +2034,6 @@ reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags, if (!new_second) return bad_direct_conv ? bad_direct_conv : nullptr; conv = merge_conversion_sequences (t, new_second); - gcc_assert (maybe_valid_p || conv->bad_p); return conv; } } diff --git a/gcc/testsuite/g++.dg/conversion/ref10.C b/gcc/testsuite/g++.dg/conversion/ref10.C new file mode 100644 index 00000000000..633b7e48e47 --- /dev/null +++ b/gcc/testsuite/g++.dg/conversion/ref10.C @@ -0,0 +1,13 @@ +// PR c++/113141 + +struct Matrix { }; + +struct TPoint3 { operator const Matrix(); }; + +void f(Matrix&); + +int main() { + TPoint3 X; + Matrix& m = (Matrix &)X; + f((Matrix &)X); +} diff --git a/gcc/testsuite/g++.dg/conversion/ref11.C b/gcc/testsuite/g++.dg/conversion/ref11.C new file mode 100644 index 00000000000..f893f12dafa --- /dev/null +++ b/gcc/testsuite/g++.dg/conversion/ref11.C @@ -0,0 +1,16 @@ +// PR c++/113141 +// { dg-do compile { target c++11 } } + +struct ConvToRef { + operator int&(); +}; + +struct A { int& r; }; + +void f(A); + +int main() { + ConvToRef c; + A a{{c}}; + f({{c}}); +}