From patchwork Mon Nov 14 22:52:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1703767 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=UHGYuzgR; dkim-atps=neutral Received: from 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 (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NB4Md75hLz23mH for ; Tue, 15 Nov 2022 09:52:44 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D7B703858C52 for ; Mon, 14 Nov 2022 22:52:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D7B703858C52 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668466361; bh=/qfGzAK4HrrfJU1Ji9gh2I8HWVZ+puKbxsNGaTMmAqQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=UHGYuzgRID7uO+ybRlPWx0cE37e+em7TkQ6S7VmNNX/SwPXdwKFGragOjEpdGDk8S 8cbwNHXnYULTgMxsFvLGTWYW6Fj5qC9y+huGdkJ6qw8nkXKSv037Jh75Of1o9xph08 BlPustpLqlYbYe8H3nIQE7Nn9Odt3ZlwxFcf0iHo= 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 331143858D32 for ; Mon, 14 Nov 2022 22:52:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 331143858D32 Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-348-pwWIOAsoMjm7-C8uytjEDg-1; Mon, 14 Nov 2022 17:52:17 -0500 X-MC-Unique: pwWIOAsoMjm7-C8uytjEDg-1 Received: by mail-qk1-f198.google.com with SMTP id ay43-20020a05620a17ab00b006fa30ed61fdso12120951qkb.5 for ; Mon, 14 Nov 2022 14:52:17 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/qfGzAK4HrrfJU1Ji9gh2I8HWVZ+puKbxsNGaTMmAqQ=; b=hg3D1LUP4s9NltmcM9rp3BZ6OfCw/dnbaEgAiQWGoXE1GVkRiQ7kVuh0lHAge1xCp/ XiLx/ijlpqvOzF2e0AqdQAIooVHox/ARsB3F/LAnBQOT3ujxXL+4zSgLR7Oy79HTgZ/7 JMgagIiTWhWqHbpexJtvpG/s7xvoeZomGylIH9JOiBJJSYYFNr9gB3pWFdNoRQElYQ4W TpNqShkT/QltN5BFcZQ+gB/Li1dwpOqr7OhSwsO7HVXsj4bAVy94r2apxoZeg+oE6jfL wY6ISzt2XP2oX0/39ECwSopuZrDwRSsVb0Z+MbDSECdfmD4xEulS9+vzW6C50hVwTyLj 3nZg== X-Gm-Message-State: ANoB5pmDwN2gTWmqdqM0FlmvG6wB2AkQpi6NfGCpJ6O9eSj0T5dp7u35 qFFZvZ1vDnqV5llyr3uJbNp091YWAA77PwqyI+3BJGDtEDvoR1TRjwEp+IJRTjqcWgYEDY2R11+ 2jh2FaGVwaR8STxG/VhRkXbDbqK4a1q//hW/yZ/A2J9tQHRG2vrvKFP7NMvUsVoioMQ== X-Received: by 2002:a05:620a:1a23:b0:6fa:b73:812e with SMTP id bk35-20020a05620a1a2300b006fa0b73812emr13258680qkb.433.1668466336696; Mon, 14 Nov 2022 14:52:16 -0800 (PST) X-Google-Smtp-Source: AA0mqf6KodJ0BvHFy1+tOqXs1ZfIQVrgIXFjSzVfrpELz1n9g3IpauC6Oxzp/HAHB5fo1wq3IUuY+w== X-Received: by 2002:a05:620a:1a23:b0:6fa:b73:812e with SMTP id bk35-20020a05620a1a2300b006fa0b73812emr13258651qkb.433.1668466336192; Mon, 14 Nov 2022 14:52:16 -0800 (PST) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id z2-20020ae9c102000000b006eeae49537bsm7064748qki.98.2022.11.14.14.52.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Nov 2022 14:52:15 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: only declare satisfied friends Date: Mon, 14 Nov 2022 17:52:13 -0500 Message-Id: <20221114225213.3714883-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.7 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_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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-pc-linux-gnu, applying to trunk. -- >8 -- A friend declaration can only have constraints if it is defined. If multiple instantiations of a class template define the same friend function signature, it's an error, but that shouldn't happen if it's constrained to only be declared in one instantiation. Currently we don't mangle requirements, so the foos all mangle the same and actually instantiating #1 will break, but for now we can test that they're considered distinct. gcc/cp/ChangeLog: * pt.cc (tsubst_friend_function): Check satisfaction. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-friend11.C: New test. --- gcc/cp/pt.cc | 3 +++ .../g++.dg/cpp2a/concepts-friend11.C | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-friend11.C base-commit: e7c12a921525b2aa27ca4814c42c63d61a6d954e diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 57917de321f..af96c5ca25f 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -11284,6 +11284,9 @@ tsubst_friend_function (tree decl, tree args) not_tmpl = DECL_TEMPLATE_RESULT (new_friend); new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl); } + else if (!constraints_satisfied_p (new_friend)) + /* Only define a constrained hidden friend when satisfied. */ + return error_mark_node; /* Inside pushdecl_namespace_level, we will push into the current namespace. However, the friend function should go diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-friend11.C b/gcc/testsuite/g++.dg/cpp2a/concepts-friend11.C new file mode 100644 index 00000000000..0350ac3553e --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-friend11.C @@ -0,0 +1,21 @@ +// CWG2596 +// { dg-do compile { target c++20 } } + +struct Base {}; + +int foo(Base&) { return 0; } // #0 + +template +struct S : Base { + friend int foo(Base&) requires (N == 1) { return 1; } // #1 + // friend int foo(Base&) requires (N == 2) { return 3; } // #2 +}; + +S<1> s1; +S<2> s2; // OK, no conflict between #1 and #0 +int x = foo(s1); // { dg-error "ambiguous" } +int y = foo(s2); // OK, selects #0 + +// ??? currently the foos all mangle the same, so comment out #2 +// and only test that #1 isn't multiply defined and overloads with #0. +// The 2596 example does not include #0 and expects both calls to work.