From patchwork Tue Mar 1 21:12:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1599503 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=HNuAwuDq; dkim-atps=neutral Authentication-Results: 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=) 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4K7VMX0hLXz9sFv for ; Wed, 2 Mar 2022 08:12:55 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 49EA2385842E for ; Tue, 1 Mar 2022 21:12:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 49EA2385842E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1646169172; bh=rkmAhjx/yn2TuIujANLDXJkd8+WN/3DPhYXlZFDN2DY=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=HNuAwuDqtjm5qeB2aG6UXSjIAF/h1GBP4ccILLAbJMM2+fHfDSC1zAJ2woe9Vs4tV vMRqFgEsMfYOVYuf7HPNj8ypEVQ0iWubEub2MDys+Qhf2+zs+zK+wPMCbP79SZFKfO 1D34YEQJ7839TLOElKZ5TFxhuOVs7rzs0txHIAJo= 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 2A33E3858D39 for ; Tue, 1 Mar 2022 21:12:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2A33E3858D39 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.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-139-IQv74fr7OKGQpEpk0zddag-1; Tue, 01 Mar 2022 16:12:07 -0500 X-MC-Unique: IQv74fr7OKGQpEpk0zddag-1 Received: by mail-qt1-f198.google.com with SMTP id e11-20020ac8670b000000b002e03737b763so886298qtp.11 for ; Tue, 01 Mar 2022 13:12:06 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=rkmAhjx/yn2TuIujANLDXJkd8+WN/3DPhYXlZFDN2DY=; b=aVrzk7zYkExoNdDR1b35DoqouQSPd+tOcmV8vRUL/ke2UypaqaNEsz84Vgi8b3/HBE chyjOWzAIIFnT4tPgrLvmJlc7ruu3sjl+fmI9MHupvyEND0xmFcbakk8UxQb9JUSgc9o qXhJKzsJhq2rg2jCtla1GV9lWo6AuU1DmMZaM5ebEhe4ZLWUx/sLh1qFZprHPDrWwQF6 C6ASbM0XMouFGqkKH+K0yzG4yIiWeHLJiC/vObdhCKSE05uOpyqRw44bU4et5RVmG7Mw C85bpvwhdLtdCzEhiikrTC4mqMSiScIMITOBJSoIMxYClaYyhwl8Do2J+8FaXPYG60L/ 7x5g== X-Gm-Message-State: AOAM531KToMIgDokrhK2/LPBSJbj37Vw0ADLP4KnTVco9gmqh4zSAVoC A67dAGN+ZGg3JrRLQjv7ccnb1EuEHU08UGFQXeqfI87OeP+zBF3P5+HN8ftlnaN0XKDxUx3UMkQ /W2D3ruHsqDyHhfgcbQ== X-Received: by 2002:ac8:5648:0:b0:2ce:dd03:59fa with SMTP id 8-20020ac85648000000b002cedd0359famr21774243qtt.288.1646169126282; Tue, 01 Mar 2022 13:12:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJwgoi+YCgG7VO73oNjjaqZ5BicaxE/EJYJMd57sVt+ZztPdocneQF0l3LiMxafe4mCkGSA5GQ== X-Received: by 2002:ac8:5648:0:b0:2ce:dd03:59fa with SMTP id 8-20020ac85648000000b002cedd0359famr21774234qtt.288.1646169125889; Tue, 01 Mar 2022 13:12:05 -0800 (PST) Received: from redhat.com ([2601:184:4780:4310::3f37]) by smtp.gmail.com with ESMTPSA id bp34-20020a05620a45a200b00662c9b66208sm5365064qkb.44.2022.03.01.13.12.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Mar 2022 13:12:05 -0800 (PST) Date: Tue, 1 Mar 2022 16:12:03 -0500 To: Jason Merrill Subject: [PATCH v2] c++: ->template and implicit typedef [PR104608] Message-ID: References: <20220222214617.1949703-1-polacek@redhat.com> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.1.5 (2021-12-30) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-13.0 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_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Cc: GCC Patches Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" On Mon, Feb 28, 2022 at 12:31:37PM -0400, Jason Merrill wrote: > On 2/22/22 17:46, Marek Polacek wrote: > > Here we have a forward declaration of Parameter for which we create > > an implicit typedef, which is a TYPE_DECL. Then, when looking it up > > at template definition time, cp_parser_template_id gets (since r12-6754) > > this TYPE_DECL which it can't handle. > > Hmm, getting that global TYPE_DECL from lookup seems like a bug; isn't the > lookup earlier in cp_parser_template_name in object scope? Yes, it is (in Function), but I think we do the pre-DR1835 lookup. For this->template Parameter(); we don't find Parameter in the object expression's type (Function), so we do unqualified lookup in the enclosing context and find the global Parameter TYPE_DECL. This is implemented in cp_parser_lookup_name: decl = lookup_member (object_type, name, ...); if (!decl) decl = lookup_name (name, ...); [basic.lookup.qual.general] now says that we only perform unqualified lookup if the object type isn't dependent. But I don't think we can fix this by implementing DR1835 because that would only help in C++23(?). My v1 patch is wrong in any case; I've come up with template-keyword4.C where we find a TYPE_DECL which is not an implicit typedef. Since cp_parser_template_id is only able to handle these TYPE_DECLs: 18353 else if (TREE_CODE (templ) == TYPE_DECL 18354 && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE) this v2 patch fixes the problem by repeating lookup of TYPE_DECLs whose TREE_TYPE is *not* TYPENAME_TYPE. That fixes my testcases and doesn't regress any. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- Here we have a forward declaration of Parameter for which we create an implicit typedef, which is a TYPE_DECL. Then, when looking it up at template definition time, cp_parser_template_id gets (since r12-6754) this TYPE_DECL which it can't handle. This patch defers lookup for TYPE_DECLs that cp_parser_template_id can't handle, a la r12-6879. PR c++/104608 gcc/cp/ChangeLog: * parser.cc (cp_parser_template_name): Repeat lookup of TYPE_DECLs. gcc/testsuite/ChangeLog: * g++.dg/parse/template-keyword3.C: New test. * g++.dg/parse/template-keyword4.C: New test. --- gcc/cp/parser.cc | 5 ++++- gcc/testsuite/g++.dg/parse/template-keyword3.C | 12 ++++++++++++ gcc/testsuite/g++.dg/parse/template-keyword4.C | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/parse/template-keyword3.C create mode 100644 gcc/testsuite/g++.dg/parse/template-keyword4.C base-commit: 4a1c20df82c9e14478d79fbe1ae9690a36285ac1 diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 03d99aba13e..02c34bf964e 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -18681,7 +18681,10 @@ cp_parser_template_name (cp_parser* parser, return error_mark_node; } else if ((!DECL_P (decl) && !is_overloaded_fn (decl)) - || TREE_CODE (decl) == USING_DECL) + || TREE_CODE (decl) == USING_DECL + /* cp_parser_template_id can only handle some TYPE_DECLs. */ + || (TREE_CODE (decl) == TYPE_DECL + && TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)) /* Repeat the lookup at instantiation time. */ decl = identifier; } diff --git a/gcc/testsuite/g++.dg/parse/template-keyword3.C b/gcc/testsuite/g++.dg/parse/template-keyword3.C new file mode 100644 index 00000000000..59fe0fc180b --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/template-keyword3.C @@ -0,0 +1,12 @@ +// PR c++/104608 + +class Parameter; +template class Function +: public R +{ + Function(); +}; +template +Function::Function() { + this->template Parameter(); +} diff --git a/gcc/testsuite/g++.dg/parse/template-keyword4.C b/gcc/testsuite/g++.dg/parse/template-keyword4.C new file mode 100644 index 00000000000..c688094bcf2 --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/template-keyword4.C @@ -0,0 +1,17 @@ +// PR c++/104608 +// { dg-do compile { target c++11 } } + +class S; +using Parameter = S; +typedef S Parameter2; + +template class Function +: public R +{ + Function(); +}; +template +Function::Function() { + this->template Parameter(); + this->template Parameter2(); +}