From patchwork Wed Apr 5 16:59:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 79820 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp455581vqo; Wed, 5 Apr 2023 10:00:23 -0700 (PDT) X-Google-Smtp-Source: AKy350atwd+jvld0t6NNGBBKjMMrom4w44avJhr30lcDCd6pbbsYdEQAHW+hXXbfSovKDX/+jOoi X-Received: by 2002:a50:ee16:0:b0:4fc:7201:6e4 with SMTP id g22-20020a50ee16000000b004fc720106e4mr2781093eds.20.1680714023051; Wed, 05 Apr 2023 10:00:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680714023; cv=none; d=google.com; s=arc-20160816; b=Vjyo/dB6AP8t3erE5xbAJYHQBUbZK2sTKMy58ekLupMq+RwnQHg8MR61i0dQBkzsEn xYmGmjSx/rE7m3dCrnhT1eYuGVggO7YkzAouBZqy9BEaqoKYlswYIgRkpqBHw4kcI+yM gqG4H4j8D5OroXstVTny4Se3yr0KVBD0w5CC/B3a0qNyrNLZwAH6jC5eV4eQfWIjbPJf RCOLY/iFxbkYU1zbuZNZv2lNj/5fyDPTrYObGWXnKWLt8V2NHYw9oAtRWIkIKgUEEmy1 YOSMs/z0pJqJVinY4EsaAWyPg9TAhNVOOl/5GVNWA6nvLA3dFGtHkhWmqSiDdEPsV1do uKag== 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:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=tOG7QlBwHOTJEgGz0VcfQyzY6VOiIUwFy5SNO2QMojQ=; b=IDpqT8fvRN+czrjdwkFeWDI3XUhLx5mHEKDwcWPSf9mxBCPf+m7AXUgJw8zAu+2A94 bBsbtGWf3hlzfF7raSefcJcMUfzsQrnGH3S7eVV5aVy8F1pj8yMC6K4bU7Mi89g6aQwj YR2n3ZI3wHw70t+4tSWn1SZxvc81uq661ljhdn+R+HjT21+MUnwHUbc0J1K4/ULHLE8L djCBi1Di4hbIybvuIkBfNFo++1ezPDHkmzgDRlYU5pz+Wgy+E07NW4PUwQ1XJqbndVhs ItJwEexvZhiWqdk0XVxgqnTjFYmF2uEKSsUQ8VmZgaWdJsjaW59mJR7XwiKNq180o9+y FPUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=q9yfxAfx; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d11-20020a056402078b00b004fd236f945csi3492585edy.63.2023.04.05.10.00.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 10:00:23 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=q9yfxAfx; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 E02DE3858C54 for ; Wed, 5 Apr 2023 17:00:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E02DE3858C54 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680714021; bh=tOG7QlBwHOTJEgGz0VcfQyzY6VOiIUwFy5SNO2QMojQ=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=q9yfxAfxK9PFNOp3ynYPiZo7XXH48PnSLo2SsOajSCK6VSPqF4LONWQEDqBQkOhnP 64bRZpjltobNKCc/cQdtks+i9IQ1igsWUDh3Iy/XIHN65JI8aagua2t6hgfpwC1GHy mxaZeS3yOwvoPBW59TuBvQOaOjb1GJ+FAy/wx3lk= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id C53043858D20 for ; Wed, 5 Apr 2023 16:59:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C53043858D20 Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-527-F08DXzTEPCqw7NuZ41Ue-w-1; Wed, 05 Apr 2023 12:59:30 -0400 X-MC-Unique: F08DXzTEPCqw7NuZ41Ue-w-1 Received: by mail-qt1-f200.google.com with SMTP id x5-20020a05622a000500b003e259c363f9so24737627qtw.22 for ; Wed, 05 Apr 2023 09:59:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680713969; 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=tOG7QlBwHOTJEgGz0VcfQyzY6VOiIUwFy5SNO2QMojQ=; b=TNMKAAMYbatFthiU1HykdZkhkbhlRGsiqEJyCY6w1+5q62aMuj2q8aFzX1UZlo36vf 2zvXoCZCODFWgAfJ2fTKBQDIB6yLWlkutExRpcjFD1l1bMlZQ+meGXOYnvn0yBQ1VyTF 28kN4vLtxg2AvjJhJQQUawQpWuaksncpjPgrKugq41zZqSZJngb+s49JlcwxCpuLyEr+ h9A6Sa56u6Fkhs5QhjdujJ8wKzSFP6DDaiF0mruNlHueymNHfsHxiCSe799q4dAyBmwB SaMU4XEQpKd98tnlpy1HgJEKLuRDq7ZudfP8OjisrQjrHBWV4BM6BEuLqNBaLg/S7Lap Y7lw== X-Gm-Message-State: AAQBX9cd7A8TKTywf0/b4SXzcLmKh9pbokbj2NE4+eyHtn6c8M8EhfG9 eqmJudrX5G6fCfmGTD1asNhLsrcguEZ7Y1Ap1kZ20vkljqjmlmXC+4RGfd/cxHafHPTbEohMcdh FBA7+kmSOTyjTI+FIkTjpC10+lYAVzlDlPcgZ55CM7lKne7oaowfV7Evqbdi1k6zVaE9SnPRusn o= X-Received: by 2002:a05:6214:2aaf:b0:5dd:aee7:dffb with SMTP id js15-20020a0562142aaf00b005ddaee7dffbmr10159095qvb.22.1680713969719; Wed, 05 Apr 2023 09:59:29 -0700 (PDT) X-Received: by 2002:a05:6214:2aaf:b0:5dd:aee7:dffb with SMTP id js15-20020a0562142aaf00b005ddaee7dffbmr10159061qvb.22.1680713969336; Wed, 05 Apr 2023 09:59:29 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id m11-20020a0cfbab000000b005dd8b93457bsm4372979qvp.19.2023.04.05.09.59.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 09:59:28 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: 'typename T::X' vs 'struct T::X' lookup [PR109420] Date: Wed, 5 Apr 2023 12:59:27 -0400 Message-Id: <20230405165927.1987914-1-ppalka@redhat.com> X-Mailer: git-send-email 2.40.0.153.g6369acd968 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-24.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762356387660121749?= X-GMAIL-MSGID: =?utf-8?q?1762356387660121749?= r13-6098-g46711ff8e60d64 made make_typename_type no longer ignore non-types during the lookup, unless the TYPENAME_TYPE in question was followed by the :: scope resolution operator. But there is another exception to this rule: we need to ignore non-types during the lookup also if the TYPENAME_TYPE was named with a tag other than 'typename', such as 'struct' or 'enum', as per [dcl.type.elab]/5. This patch implements this additional exception. It occurred to me that the tf_qualifying_scope flag is probably unnecessary if we'd use the scope_type tag more thoroughly, but that requires parser changes that are probably too risky at this stage. (I'm working on addressing the FIXME/TODOs here for GCC 14.) Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/109420 gcc/cp/ChangeLog: * decl.cc (make_typename_type): Also ignore non-types during the lookup if tag_type is something other than none_type or typename_type. * pt.cc (tsubst) : Pass class_type or enum_type as tag_type to make_typename_type as appropriate instead of always passing typename_type. gcc/testsuite/ChangeLog: * g++.dg/template/typename27.C: New test. --- gcc/cp/decl.cc | 9 ++++++++- gcc/cp/pt.cc | 9 ++++++++- gcc/testsuite/g++.dg/template/typename27.C | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/template/typename27.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 5369714f9b3..a0a20c5accc 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -4307,7 +4307,14 @@ make_typename_type (tree context, tree name, enum tag_types tag_type, lookup will stop when we hit a dependent base. */ if (!dependent_scope_p (context)) { - bool want_type = (complain & tf_qualifying_scope); + /* As per [dcl.type.elab]/5 and [temp.res.general]/3, ignore non-types if + the tag corresponds to a class-key or 'enum' (or is scope_type), or if + this typename is followed by :: as per [basic.lookup.qual.general]/1. + TODO: If we'd set the scope_type tag accurately on all TYPENAME_TYPEs + that are followed by :: then we wouldn't need the tf_qualifying_scope + flag. */ + bool want_type = (tag_type != none_type && tag_type != typename_type) + || (complain & tf_qualifying_scope); t = lookup_member (context, name, /*protect=*/2, want_type, complain); } else diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 821e0035c08..1e778e6e786 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -16580,9 +16580,16 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) return error_mark_node; } + /* FIXME: TYPENAME_IS_CLASS_P conflates 'union' vs 'struct' vs 'class' + tags. TYPENAME_TYPE should probably remember the exact tag that + was used. */ + enum tag_types tag + = TYPENAME_IS_CLASS_P (t) ? class_type + : TYPENAME_IS_ENUM_P (t) ? enum_type + : typename_type; tsubst_flags_t tcomplain = complain | tf_keep_type_decl; tcomplain |= tst_ok_flag | qualifying_scope_flag; - f = make_typename_type (ctx, f, typename_type, tcomplain); + f = make_typename_type (ctx, f, tag, tcomplain); if (f == error_mark_node) return f; if (TREE_CODE (f) == TYPE_DECL) diff --git a/gcc/testsuite/g++.dg/template/typename27.C b/gcc/testsuite/g++.dg/template/typename27.C new file mode 100644 index 00000000000..61b3efd998e --- /dev/null +++ b/gcc/testsuite/g++.dg/template/typename27.C @@ -0,0 +1,19 @@ +// PR c++/109420 + +struct A { + struct X { }; + int X; +}; + +struct B { + enum E { }; + enum F { E }; +}; + +template +void f() { + struct T::X x; // OK, lookup ignores the data member 'int A::X' + enum U::E e; // OK, lookup ignores the enumerator 'B::F::E' +} + +template void f();