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: 192261 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:e09d:b0:103:945f:af90 with SMTP id gm29csp201413dyb; Thu, 25 Jan 2024 11:19:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IFlBZL1i2v9G85+XFej5sYHAgSJdLeamF8MC8kMD1hvjlPqGphWsmf6vL0rT98ceosH79IH X-Received: by 2002:a05:6830:22ef:b0:6e0:e52b:cca9 with SMTP id t15-20020a05683022ef00b006e0e52bcca9mr333399otc.26.1706210363512; Thu, 25 Jan 2024 11:19:23 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706210363; cv=pass; d=google.com; s=arc-20160816; b=IiduRVlmofYdUMDfe2CQBXeQKlg9dt0O7kGNV84rpKCAdtGj1tXaf7jmTE/z9XXURY PSb0lU6LvHa/0MgqXgLL9vbHI5zQjaeAmczti9pQ5Pm3Lb6N7EraX9StiydSVX0q9N2S xDuCxG4drj3QHpIB8kSvtX0GLHkJQifKTGplov6QP6oro6UnSHfZHxaaKIb0XTB5VDhm sTSB2e0SmRVRkpyKQFlAslhox/b4YFnvjG5JYEZxczFPdq2H6cw51umXHra3kv9rzU6h 7fZOKVXCTF+oENzzS9/eCQ5f0DiUiugQyfjZGtDfqcodfWo2JiPvgB8tbKse5Kt2epim iDOQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=qg7hL1NMMkWrPsCLsYY523K5346Lg74ou8oksOs59yo=; fh=1Hi9m88IqcWZGtfSxUrMGG+GBgL26N02X1SANXM6iTs=; b=zN1Gu5IhFdpB3st+SL5+7i+j1xEweHIyrBQsPve8L23juLpb7mCg5aiTSKX3JuCrXL 6gK/Rb8EtH0zlxqvAW9VWAFwFiVsYddnI6aoQLNG0NY+VkiAqximxCJt9tuUR4+nEv5M LBTVukRwSyMBwtH0P0Z0BJJzhBQrgryra2nW7I6PysEMd16/FNxTKS1JNmiCDFiA97Lm tbQOfhp5tc4FEER7h9D7tBL44OtQdOjsCNUR0M7VksFellryewllraHKwlDhoAhFV10F y+FpFwi3cGXlZ47zxZjIZ+S6toxlKnXWeHQSlkeA5FkgwRmxHO6B47Sap4pThsFNA1lc vHDQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=jUkpdra4; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id h3-20020ac85843000000b0042a450e0fa9si8732088qth.332.2024.01.25.11.19.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 11:19:23 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=jUkpdra4; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 348883858C35 for ; Thu, 25 Jan 2024 19:19:23 +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-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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789091238500615843 X-GMAIL-MSGID: 1789091238500615843 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}}); +}