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: 60613 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1764F3858414 for ; Mon, 14 Nov 2022 22:52:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1764F3858414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668466371; bh=/qfGzAK4HrrfJU1Ji9gh2I8HWVZ+puKbxsNGaTMmAqQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=TTX0EZj5OLOxZV5J/xgQXgWJ1iA0WikzCXNttjJc/3i2yS8P+DqD4rkxDVXXaKNaB 7qeWIzHBTe/h3/R7GrQ6myJf0O/pB2LvTIzjMGqLQdZHduZT/8NWEVHuhmtBbFXaIu K7Hb5H/YKd4RUrfObxR5lw2wTYM7ngijPrxf6b4s= 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+patchwork=sourceware.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.