From patchwork Fri Apr 12 19:27:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1923245 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=HynNmqL1; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4VGRSv5NxJz1yYL for ; Sat, 13 Apr 2024 05:29:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EF415385842A for ; Fri, 12 Apr 2024 19:29:01 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id C4AE93857C4F for ; Fri, 12 Apr 2024 19:28:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C4AE93857C4F 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 C4AE93857C4F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712950123; cv=none; b=VFNZRwt3SDai1Fa46Xp3FBnXvwrgyuGo8iFuGa/cE9J5mJYJIJedkaMKBpjw4KgwqHUlHuHnzBffdOfQ4IwFJsFi0wOyZKzcpVBV0hDRb0Lb7foAU3aNsMvabi4Zzd6p7iwz+choLBDpcp9xMAj/ZdjxIfzVT60kbf2kaO0z/fY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712950123; c=relaxed/simple; bh=p5TEg2ZjvnhzdsSXwHTIS1IRUYWZ1/nhb4s8BoWuY20=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=gfROsgD9RZFP2xczBV1Beo13Je5g435ijQOXjampyXBtJdpqAby7Q0p6KWbMJIOVB64kD1xf/N2u9RiipGkcKp7KEL1dE29d13xAknWtr4A/hmwfyhqt22tfrOuCsMNgRvVPVd1QSxwvKpTOJm23tP1NBJxwmGtaSHUlhr2Sea0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712950120; 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=zdh/fPKM6gQwEt7rfVBV5ojP0nLeaU1uPEXFCtlOk10=; b=HynNmqL1imFK95jrAUWjCQ41ys7FcsvYyklo6Sgm3vgvt5S9mkkfLNw49CQoII0zF7qQX/ aguOmHVFGkWF553SC/x3s7ch3a81b4gbXjtH+3jADfYua94jdmRYomzDej/lAdpRSMcqT/ qt4z3GOPGkU4hdsvfwtz9UdbpwJs5GE= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-668-66PRk7XBN-W6Be34HhoGIg-1; Fri, 12 Apr 2024 15:28:39 -0400 X-MC-Unique: 66PRk7XBN-W6Be34HhoGIg-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-78ecf184346so87350185a.2 for ; Fri, 12 Apr 2024 12:28:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712950118; x=1713554918; 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=zdh/fPKM6gQwEt7rfVBV5ojP0nLeaU1uPEXFCtlOk10=; b=xDfm0YC4z4od3zLevus7eJhJJ+guGIt/U40r/uBjJzGad2PRhl+JMPWeYhCeNplJEz IfnpPHiS8dAtXHim73qBCAJEYL1mJRytwclrYOjTpcy+KZ7cnWADbaZ+VxdMlSfnntcR CK+sioKRE0dvZAZN+59K9EWds/PpfMiYd5q1zG9W8crJ6HvmWZ1TOQn1OTPdzxK3Roez 42m0Lm4vRozTVfFqVbuiLTY4vum8KZNIiGyeBDHYP7PyshAQ6KVFypeKOr9Jccnhf8M4 uKXLJ/e9OYeLtZD+58V8TDrEYHNGupg5+cbKJBL3xQ/awFktcDkSa5y7d63M1tMxEE9d qdXQ== X-Gm-Message-State: AOJu0YwBZhKCRKMt1OtzX7fDWEQbQMFmtOojk3ECaOuZTYywBe2SL0CI OeKP2k37S8lSgYg4jBFsBUb5fy6mrNG3mE+9W4IUGuTpVY95LRzhuTHZhHbqEojQyg7o57d27dV +TjF0kqKHpNbSWKXnZMHGBIDqLCIyyNE5HHSsAPIDnNjMvgzqvLeLLcRZdst3oDxOLdKyHNi187 rHjwrfyo9hdq4DqKoVRSA8jRHuTYiYsw2IG1E= X-Received: by 2002:a05:620a:890:b0:78d:5a03:2595 with SMTP id b16-20020a05620a089000b0078d5a032595mr4027510qka.24.1712950118261; Fri, 12 Apr 2024 12:28:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHb+yGLCMbNMZaQTm1WMxS4hGR/6aRZ4bcGpSzey/f19VmusCh118/Lghidv9nyxzC3h3F5eg== X-Received: by 2002:a05:620a:890:b0:78d:5a03:2595 with SMTP id b16-20020a05620a089000b0078d5a032595mr4027491qka.24.1712950117745; Fri, 12 Apr 2024 12:28:37 -0700 (PDT) Received: from jason-thinkpadp1gen4i.rmtusma.csb (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id z5-20020ae9c105000000b0078ed33d55f2sm524306qki.121.2024.04.12.12.28.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 12:28:37 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Cc: Patrick Palka Subject: [pushed] c++: reference list-init, conversion fn [PR113141] Date: Fri, 12 Apr 2024 15:27:36 -0400 Message-ID: <20240412192835.2466989-1-jason@redhat.com> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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 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 Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- The original testcase in PR113141 is an instance of CWG1996; the standard fails to consider conversion functions when initializing a reference directly from an initializer-list of one element, but then does consider them when initializing a temporary. I have a proposed fix for this defect, which is implemented here. DR 1996 PR c++/113141 gcc/cp/ChangeLog: * call.cc (reference_binding): Check direct binding from a single-element list. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/initlist-ref1.C: New test. * g++.dg/cpp0x/initlist-ref2.C: New test. * g++.dg/cpp0x/initlist-ref3.C: New test. Co-authored-by: Patrick Palka --- gcc/cp/call.cc | 21 +++++++++++++++++---- gcc/testsuite/g++.dg/cpp0x/initlist-ref1.C | 16 ++++++++++++++++ gcc/testsuite/g++.dg/cpp0x/initlist-ref2.C | 10 ++++++++++ gcc/testsuite/g++.dg/cpp0x/initlist-ref3.C | 13 +++++++++++++ 4 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist-ref1.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist-ref2.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist-ref3.C base-commit: 0fd824d717ca901319864a5eeba4e62b278f8329 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 9568b5eb2c4..15b5647298e 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -1596,7 +1596,9 @@ standard_conversion (tree to, tree from, tree expr, bool c_cast_p, return conv; } -/* Returns nonzero if T1 is reference-related to T2. */ +/* Returns nonzero if T1 is reference-related to T2. + + This is considered when a reference to T1 is initialized by a T2. */ bool reference_related_p (tree t1, tree t2) @@ -1757,6 +1759,7 @@ reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags, } bool copy_list_init = false; + bool single_list_conv = false; if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr)) { maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); @@ -1783,6 +1786,11 @@ reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags, from = etype; goto skip; } + else if (CLASS_TYPE_P (etype) && TYPE_HAS_CONVERSION (etype)) + /* CWG1996: jason's proposed drafting adds "or initializing T from E + would bind directly". We check that in the direct binding with + conversion code below. */ + single_list_conv = true; } /* Otherwise, if T is a reference type, a prvalue temporary of the type referenced by T is copy-list-initialized, and the reference is bound @@ -1907,9 +1915,14 @@ reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags, (possibly cv-qualified) object to the (possibly cv-qualified) same object type (or a reference to it), to a (possibly cv-qualified) base class of that type (or a reference to it).... */ - else if (CLASS_TYPE_P (from) && !related_p - && !(flags & LOOKUP_NO_CONVERSION)) + else if (!related_p + && !(flags & LOOKUP_NO_CONVERSION) + && (CLASS_TYPE_P (from) || single_list_conv)) { + tree rexpr = expr; + if (single_list_conv) + rexpr = CONSTRUCTOR_ELT (expr, 0)->value; + /* [dcl.init.ref] If the initializer expression @@ -1923,7 +1936,7 @@ reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags, the reference is bound to the lvalue result of the conversion in the second case. */ - z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags, + z_candidate *cand = build_user_type_conversion_1 (rto, rexpr, flags, complain); if (cand) { diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-ref1.C b/gcc/testsuite/g++.dg/cpp0x/initlist-ref1.C new file mode 100644 index 00000000000..f893f12dafa --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-ref1.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}}); +} diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-ref2.C b/gcc/testsuite/g++.dg/cpp0x/initlist-ref2.C new file mode 100644 index 00000000000..401d868d820 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-ref2.C @@ -0,0 +1,10 @@ +// CWG1996 +// { dg-do compile { target c++11 } } + +struct S { operator struct D &(); } s; +D &d{s}; // OK, direct binding + +namespace N1 { + struct S { operator volatile struct D &(); } s; + const D &dr{s}; // { dg-error "invalid user-defined|discards qualifiers" } +} diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-ref3.C b/gcc/testsuite/g++.dg/cpp0x/initlist-ref3.C new file mode 100644 index 00000000000..e2cc1deace5 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-ref3.C @@ -0,0 +1,13 @@ +// CWG1996 +// { dg-do compile { target c++11 } } + +struct D { constexpr D() {} } d; +struct S { + template + constexpr operator T& () const { return d; } +}; +constexpr S s; +constexpr const D &dr1(s); +static_assert (&dr1 == &d, ""); +constexpr const D &dr2{s}; +static_assert (&dr2 == &d, "");