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: 20057 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2403481wru; Mon, 14 Nov 2022 14:53:07 -0800 (PST) X-Google-Smtp-Source: AA0mqf50ZzzU9zojSGUI/wKJZILsjYGgE8XGPIjU1Rn8QLpw5jEFDRaebdEe32V55SUeDpgj41Vw X-Received: by 2002:a17:906:b10d:b0:7a3:fbfa:32e5 with SMTP id u13-20020a170906b10d00b007a3fbfa32e5mr12098713ejy.7.1668466387301; Mon, 14 Nov 2022 14:53:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668466387; cv=none; d=google.com; s=arc-20160816; b=DMGYarMB9ozBw867WCDunfWQMBFHquQKVvObOayGJhCxrNT4heGKdV1W4ITaCTZiir +EtavDDPAKsH/NvjP/txFNFiKSA1a9Li/Heehphtia7imweYSHkotVEdmdJz0wVRc35O 4pEImBYiaatHc0J7ZUBKY8jmDEIHKNqUuLpLmN4GdFRlKa1+GyDC8CJTzgcJkr+FJ1YT UABc1ies4ZO98+f0bpBzU+JYq3FAAdZGfWyGi6508zBFZIBB6fXRoDsHVrkpa6ID/vhx ahUXp4jIQVn4LLMz246IOib5yEMV1ygUiecD5TIsO8csgMorHl9rRko/qxYH5n8oBlXy NEpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=/qfGzAK4HrrfJU1Ji9gh2I8HWVZ+puKbxsNGaTMmAqQ=; b=ViJRknK+8xsYMDEwwLyiao15bZb2TLad+bjbZE5CefPNxpePzzClJJqtwnb6g31wf3 tle3EJePdqUmFSwzMa+/SlhtwbM7c8WLyKkIKWuvuOyTzHMsxSZCTF6+L98OZ3EmQtSi 170pMIZ2PjjS35wE/4bxJkWc4MWfW9ADtG1SQlHHexpPWMWjHCv2gwclmcPzoTM9FGoH Qv/3zQ2nOTH7FrQNr7gy0yEkuf8gzvB0Mn0nNNVemjp5i8dh7GZvmrNgFh67XP/KOAVN uxKDs36oClpOdxySXjPG+wvgfLwE+wIAyH6I5X8qchI9gG33n8e81YVk9csAed6OZQE+ EkNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=p+f6yZGt; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id t4-20020a056402240400b00443088b40b7si10338941eda.123.2022.11.14.14.53.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Nov 2022 14:53:07 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=p+f6yZGt; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 168283858D1E for ; Mon, 14 Nov 2022 22:53:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 168283858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668466386; bh=/qfGzAK4HrrfJU1Ji9gh2I8HWVZ+puKbxsNGaTMmAqQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=p+f6yZGtlmybYLeI5P0LD55iXN1HlhOssZRtBszJCyeGQa1G2JqfJbXY72rmB7IX7 3omQmm8UMLYH7dwilcFbdadpSM/v+u5T1vempX6wr3XNf2re4yhaIOWEXZxcTyBxY0 t9iT8RUdxaiFcnmE/FZG3QH5fRIj8cUemhFe9vqs= 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-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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749513810682972638?= X-GMAIL-MSGID: =?utf-8?q?1749513810682972638?= 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.