From patchwork Wed Jan 25 20:16:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 63690 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 BA6D53858413 for ; Wed, 25 Jan 2023 20:17:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BA6D53858413 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674677854; bh=LovS3PAbfruAfpyMgB0xIrlbX6Lt4E9QGzMHg5RzFGE=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=UR3ZqkBbzzjaUMdwoGksaaKJfcky5RWd/P22RAEnM53xDUF9nZxCY8K2z5OVIe8jb RkkTPTUd7D7OQVA9teWLhz5XArRve2+tzkUufWrrg2yupuW2SAUN2msGOobhcWIQ0M BG4XPM3lg6iZf4zw7E2S1lzyAE5cGUEUTKaaa1Hs= 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 44C303858D38 for ; Wed, 25 Jan 2023 20:17:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 44C303858D38 Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-462-NnXKGHd7OtayLji9eMVVEg-1; Wed, 25 Jan 2023 15:17:04 -0500 X-MC-Unique: NnXKGHd7OtayLji9eMVVEg-1 Received: by mail-qk1-f199.google.com with SMTP id bm30-20020a05620a199e00b007090f3c5ec0so11862272qkb.21 for ; Wed, 25 Jan 2023 12:17:04 -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:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=LovS3PAbfruAfpyMgB0xIrlbX6Lt4E9QGzMHg5RzFGE=; b=QdRKaBQbC6WwD8Lxz5hS2zZfIJ35uLbZJpbFMGno9GenxtjtBc9FDfvQBVcZ6W8Zx+ NgGF28VybdR/Up9pIZeEmeYjSEjhCm48tq3DJOhY2KRUcEGU6Z6A50c4/EdhUOLdY541 W3E6OB3u+6C+PbXQR53mei3fhRyZUbEeTwYDWbklBIKdWB2T0ylflNz5pgqeUEHD6Hy8 UnJcelqGdVuKbsNIzXBVHJaoyFBYxCp+oLxXO23o23+yD/O/iPNxV/X9KXvNlbShSx40 Sb0KmkgPZ0Zabhnh/OUIfoQxZPQmXHzU0stjNiLG4/Xe2Q2wc0wJq3kGOvRuFmOXXNBU y3Dg== X-Gm-Message-State: AFqh2ko8nc0P54kWSWkLIfwPGp/h78GC4il4N0MnkfIUbOkUpyYUuuqE K2Bwx1SrG+3vgOdLEphV8OJNq8JwxgW20H2vSCxGDMnNT5BImaE/X1SH6xHNJNjaM4O8Mg07Pvy wv09FIJVvScumqRj+p5dZ0/G53fcyrJt5/0jAEF1NY0k21sTRiM2pwVZ63Cu/9x3xYXo= X-Received: by 2002:a05:6214:2b98:b0:535:5d43:fa16 with SMTP id kr24-20020a0562142b9800b005355d43fa16mr29177456qvb.42.1674677823458; Wed, 25 Jan 2023 12:17:03 -0800 (PST) X-Google-Smtp-Source: AMrXdXu1QQDNfu6YZLy/y6mRuShrkPQjYxuGmzXqSvK6dtkDkC7AerNRpLem/Fhr156eqpc3d/ESew== X-Received: by 2002:a05:6214:2b98:b0:535:5d43:fa16 with SMTP id kr24-20020a0562142b9800b005355d43fa16mr29177418qvb.42.1674677823041; Wed, 25 Jan 2023 12:17:03 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id r9-20020a37a809000000b006bb82221013sm4245798qke.0.2023.01.25.12.17.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Jan 2023 12:17:02 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, nathan@acm.org, Patrick Palka Subject: [PATCH] c++ modules: uninstantiated template friend class [PR104234] Date: Wed, 25 Jan 2023 15:16:43 -0500 Message-Id: <20230125201643.506666-1-ppalka@redhat.com> X-Mailer: git-send-email 2.39.1.348.g5dec958dcf MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.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: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Here we're not clearing DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P for the instantiated/injected template friend class B, which confuses a later call to get_originating_module_decl for B. This patch fixes this by clearing the flag in tsubst_friend_class (as is already done for template friend functions by r11-5730-gf7aeb823d9b0de). After fixing that, we still fail to compile the testcase, rejecting the later definition of B with friend-6_a.C:10:26: error: cannot declare ‘struct B’ in a different module ultimately because DECL_MODULE_ATTACH_P wasn't set on the original (injected) declaration of B. This patch fixes this by calling set_originating_module in tsubst_friend_class, but for that to work it seems we need to relax the assert in this latter function since get_originating_module_decl when called on the TYPE_DECL for B returns the corresponding TEMPLATE_DECL. (Alternatively we can instead call set_originating_module on the TYPE_DECL B as soon as it's created in lookup_template_class (which is what pushtag does), which doesn't need this assert change because at this point the TYPE_DECL doesn't have any TEMPLATE_INFO so get_originating_module_decl becomes a no-op. Would that be preferable?) Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/104234 gcc/cp/ChangeLog: * module.cc (set_originating_module): Document default argument. Relax assert to look through DECL_TEMPLATE_RESULT in the result of get_originating_module_decl. * pt.cc (tsubst_friend_class): Clear DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P and call set_originating_module on the instantiated template friend class. gcc/testsuite/ChangeLog: * g++.dg/modules/friend-6_a.C: New test. --- gcc/cp/module.cc | 8 ++++++-- gcc/cp/pt.cc | 3 +++ gcc/testsuite/g++.dg/modules/friend-6_a.C | 10 ++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/friend-6_a.C diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 7133009dba5..234ce43b70f 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -18843,14 +18843,18 @@ set_defining_module (tree decl) } void -set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED) +set_originating_module (tree decl, bool friend_p /* = false */) { set_instantiating_module (decl); if (!DECL_NAMESPACE_SCOPE_P (decl)) return; - gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl)); + if (!friend_p) + { + tree o = get_originating_module_decl (decl); + gcc_checking_assert (STRIP_TEMPLATE (o) == decl); + } if (module_attach_p ()) { diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index cbe5898b553..f2ee74025e7 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -11520,6 +11520,9 @@ tsubst_friend_class (tree friend_tmpl, tree args) CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)) = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))); + DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = false; + set_originating_module (DECL_TEMPLATE_RESULT (tmpl)); + /* Substitute into and set the constraints on the new declaration. */ if (tree ci = get_constraints (friend_tmpl)) { diff --git a/gcc/testsuite/g++.dg/modules/friend-6_a.C b/gcc/testsuite/g++.dg/modules/friend-6_a.C new file mode 100644 index 00000000000..97017e4ee78 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/friend-6_a.C @@ -0,0 +1,10 @@ +// PR c++/104234 +// { dg-additional-options "-fmodules-ts" } +// { dg-module-cmi pr104234 } +export module pr104234; + +template struct A { + template friend struct B; +}; +A a; +template struct B { };