From patchwork Wed Apr 26 21:15:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1774271 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=q00nSNjC; 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 4Q6BWG3p7Kz23vH for ; Thu, 27 Apr 2023 07:16:24 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7DDB63858409 for ; Wed, 26 Apr 2023 21:16:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7DDB63858409 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682543782; bh=kWSMb9ZkuBPKxDqw3OIgsirVOljYhWjU2p8zc6ED09A=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=q00nSNjCeGtjmFgr99GB0yzsLgjGP1NIJgYGEgOgAiEzE0XS0uFYIRHpgkXA+SobB 4H6vHkEt5/qJ4Fj23tJ//aaKr4SlCLhjLuRjn637AsuXQqaaoPyo0R+DQZH2VnDpNx oaBtgk38AxI79xdD51jbdpW34sGa6jBicc27g4/4= 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 4AB983858C54 for ; Wed, 26 Apr 2023 21:16:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4AB983858C54 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-148-_CbmXjpIMX6_iduYzRxpAg-1; Wed, 26 Apr 2023 17:15:51 -0400 X-MC-Unique: _CbmXjpIMX6_iduYzRxpAg-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-3ef30a89abbso50299921cf.2 for ; Wed, 26 Apr 2023 14:15:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682543750; x=1685135750; 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=kWSMb9ZkuBPKxDqw3OIgsirVOljYhWjU2p8zc6ED09A=; b=lhVm/XFtxVGwIVLicQ0qV9XtqRxL+pzi8qkT9YR9l1/2oYtDxleSwClFjrupA2dmOA eG3z1SnNJ+/5XFtSUIo2avpkbHxSx9c2RVBE+w6m/eR8kpQ1HY/gmqptE1lFxbYnFkwr CCzYQqamOpbgjTDOXjdpwjml8kqp7Nvu7vUWWV5k2kOMuYDfMTUUIqafk0VKmBD1pFl0 PbaFjgCFKo8fo3fkU4wAkJX2HwPB+/ippVgoi9hFBJO7hGQ/HjJmAVG91CNFSvt7HDG0 cgF5BPrRlyBnHw4ZuMtPTVvL1/nYnDBnAKtN6pRSqXfn1gd0eWFIhVKqr8P7lvgXjYv+ SWDg== X-Gm-Message-State: AAQBX9cv7zPWxlSD3AsjKfH4f82XBFcA5l0OqaIVOTZmPCpRv3Dteyit +94pGKIzq3dlDIDgO+xD5Oi6pkykViN+Ud/oDg2mtiWAsOiaJdzKw3xXyvZvuGbRrV1umqN31/5 RT1MSLkp5fp+DnECfMr9EeDujqRYxpcgPO/AZ5vIIvTMPGI4ZYwSLsy0I2x+UWnN7drw741mb8g == X-Received: by 2002:ac8:57c5:0:b0:3e9:353a:b680 with SMTP id w5-20020ac857c5000000b003e9353ab680mr35086325qta.31.1682543750300; Wed, 26 Apr 2023 14:15:50 -0700 (PDT) X-Google-Smtp-Source: AKy350Zui3/8uhTO26NqQYu0WpawRUtvXdm/e1BNcwftSj7rx9KFkz+6bTBBZ+Eqd3YL3zP2bnBp9w== X-Received: by 2002:ac8:57c5:0:b0:3e9:353a:b680 with SMTP id w5-20020ac857c5000000b003e9353ab680mr35086294qta.31.1682543749832; Wed, 26 Apr 2023 14:15:49 -0700 (PDT) Received: from jason.com (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 fd9-20020a05622a4d0900b003d65e257f10sm1983567qtb.79.2023.04.26.14.15.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Apr 2023 14:15:49 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: unique friend shenanigans [PR69836] Date: Wed, 26 Apr 2023 17:15:47 -0400 Message-Id: <20230426211547.463435-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.9 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, 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.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< -- Normally we re-instantiate a function declaration when we start to instantiate the body in case of multiple declarations. In this wacky testcase, this causes a problem because the type of the w_counter parameter depends on its declaration not being in scope yet, so the name lookup only finds the previous declaration. This isn't a problem for member functions, since they aren't subject to argument-dependent lookup. So let's just skip the regeneration for hidden friends. PR c++/69836 gcc/cp/ChangeLog: * pt.cc (regenerate_decl_from_template): Skip unique friends. gcc/testsuite/ChangeLog: * g++.dg/template/friend76.C: New test. --- gcc/cp/pt.cc | 6 ++++ gcc/testsuite/g++.dg/template/friend76.C | 36 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 gcc/testsuite/g++.dg/template/friend76.C base-commit: 50d866038a910ceb9075b97295a12d77a8d09a3a prerequisite-patch-id: 4ed1b4e9cbee2ce090bd119cbf47056c35aaeb57 diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 678cb7930e3..e1c272b9817 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -26458,6 +26458,11 @@ regenerate_decl_from_template (tree decl, tree tmpl, tree args) int args_depth; int parms_depth; + /* Don't bother with this for unique friends that can't be redeclared and + might change type if regenerated (PR69836). */ + if (DECL_UNIQUE_FRIEND_P (decl)) + goto done; + /* Use the source location of the definition. */ DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl); @@ -26528,6 +26533,7 @@ regenerate_decl_from_template (tree decl, tree tmpl, tree args) else gcc_unreachable (); + done: pop_access_scope (decl); } diff --git a/gcc/testsuite/g++.dg/template/friend76.C b/gcc/testsuite/g++.dg/template/friend76.C new file mode 100644 index 00000000000..ce3ed201dda --- /dev/null +++ b/gcc/testsuite/g++.dg/template/friend76.C @@ -0,0 +1,36 @@ +// PR c++/69836 +// { dg-do compile { target c++11 } } + +template struct number : public number { + static constexpr int value = N; + static constexpr number prev() { return {}; } +}; +template<> struct number<0> { static constexpr int value = 0; }; + +template struct S { enum { value = N }; }; + +template constexpr S increment(S) { return {}; } + +#define RETURN(R) -> decltype(R) { return R; } + +#define INIT(TYPE) \ + using W_ThisType = TYPE; \ + friend constexpr S<0> state(number<0>, W_ThisType**) { return {}; } + +#define STUFF \ + friend constexpr auto state(number{}, static_cast(nullptr)))::value+1> w_counter, \ + W_ThisType **w_this) \ + RETURN(increment(state(w_counter.prev(), w_this))) + + +template struct TemplateObject { + INIT(TemplateObject) + STUFF + STUFF +}; + +int main() { + TemplateObject t; + constexpr auto s = state(number<>{}, static_cast**>(nullptr)) ; + static_assert(s.value == 2, ""); +}