From patchwork Mon Nov 28 21:22:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 26956 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp5951316wrr; Mon, 28 Nov 2022 13:23:02 -0800 (PST) X-Google-Smtp-Source: AA0mqf73VaBDcKRJTsPcfTbi3wB3Guk+TJOATsplWegYCwuwEcLOZI9xXYCdqId3BJU5tGOHVyOL X-Received: by 2002:aa7:d0d4:0:b0:46b:fa4:a827 with SMTP id u20-20020aa7d0d4000000b0046b0fa4a827mr8904853edo.30.1669670582226; Mon, 28 Nov 2022 13:23:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669670582; cv=none; d=google.com; s=arc-20160816; b=Za+6WkDOKg5EjUCjCVdyz2o1Z4cIXK2wcyRkPeN00baK3vDN5NyhBeHWXuX/KWzkPQ c0jLS/WVzaX6zStwD4oJ67hsdGu9LW5n+a9NF9jgjJ15Nw944B9p5192rUKRoMJEdSZU Zst1ZcrxfwF20FgBEeDTRpt+F1m9z4yBQmHfc0dQBf3qYX0QbWie0OYPh1WZf9JEexup cS1gBvf/7f/OBLXfqxeyellI0CEvfgTdR+mCS7YfX2ErO3//Wsqb5VnHpNY98uVDlR2N uqEIpFTHz8S+4xPEg1l597jf/P12GZc0rqH83khaJpCedSNcqtwcxJkL2svFVuduinR/ 5Rww== 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=MBRHyXAPWVs0mUR9/8UnvuclAQ5mWXGQrCCoKfrOI1c=; b=XLrIXHu13oZwWGCgata3jkWt41JkILy2dAfs9INQr9v1BQIqoo2k8U/KjDCavKW3kv owL93ghDtyA0ShjVRpHHmCDghvGC9650JD9TfQDl6UUjGCXDUQ3vDN0pkYXEo0DEku7D mSPkleaRl/6gZmk+M6KyxD84GpCN5dz17RGFPJ36ZnxAg4wwur7/NsFwBwpENcK8kSKP 5odwLlLbretrOw1+XUeIlklunpinfHPHWxVS/R2cta4QZXcCmGbKdBz1zJ2ru8sVrYUI C45VbFZ/iDvEUAVvQ2gaT48AAD0yIc6Ci6BftxgENMRnosX53hJjxSjEHOFb8RbumFWi 8SVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=LSCPZC08; 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 dr7-20020a170907720700b007a11c253eaasi11201723ejc.809.2022.11.28.13.23.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Nov 2022 13:23:02 -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=LSCPZC08; 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 0BD2F3858408 for ; Mon, 28 Nov 2022 21:23:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0BD2F3858408 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669670581; bh=MBRHyXAPWVs0mUR9/8UnvuclAQ5mWXGQrCCoKfrOI1c=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=LSCPZC08XpPdCKF/FIGOEdS3V4mUkMpZRCLDEHPwCNNEiVJ46yBwAXGU4tWbVT8m2 PXnE6phOTZyhik3/sbegEto8q+KGIyjgoQ0HaWfpqSy5jZI1m5X57jFTi8Pdx023ZF 6cywkopg5t/Q7Z/IXeIetqjgjn2H08ktT2WheqWI= 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 3A32A3858408 for ; Mon, 28 Nov 2022 21:22:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3A32A3858408 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-400-iQWkEynlMkKLNR_X73UiMQ-1; Mon, 28 Nov 2022 16:22:15 -0500 X-MC-Unique: iQWkEynlMkKLNR_X73UiMQ-1 Received: by mail-qv1-f70.google.com with SMTP id w1-20020a056214012100b004c6ecf32001so11353328qvs.8 for ; Mon, 28 Nov 2022 13:22:14 -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=MBRHyXAPWVs0mUR9/8UnvuclAQ5mWXGQrCCoKfrOI1c=; b=iahQdV8UsfSedrHFwdWyDZO74RYLjxMJSYfbR9NtvPveo4FjGNTEWGwiJbKc8+IpRM Z6LnvgW817i88AMXTp/iGX0kqQaZ81o4zzoR2F1iP6VLFezXC7ffQmiMdBNV7KVe4ALh wsN2GQmJqUA4WGyfG80pmqAAY/oKklQSjbtA/XRj3EGdQVq6cob4WfOGNTwUdpuP7mv6 BVkKhrYBZW8pKkJofEAUYJ625o2snINiduzlhiZVL9T6MxMEVZ721AArPRtdd+P96dI3 uXvmE7iVh25KvpFGtpy95qRM7WE2kxF8Ygebqwb9ltrYstMIWiCzsS0tna8LndjlKuHQ zkzA== X-Gm-Message-State: ANoB5pnbkHozY8ZbeeknK3RKbhS9TsRuWWsFAU4HtdtlPDDlewHYd/rG tlxBthqoYZ8mBrSqyiWIXpp/LBP+fzSqSYQyJfBI848VzUYmXUbuarv/aeOyDlRGnPAkRXHjQ9N QyOywAh28/psy6Dxp/eZuKWNnusxgf7Yolurv84pm4nF6WlGGm+7YH9U3jUtTx+tq66k= X-Received: by 2002:ad4:5291:0:b0:4c6:e1ba:b1c with SMTP id v17-20020ad45291000000b004c6e1ba0b1cmr19779687qvr.73.1669670533864; Mon, 28 Nov 2022 13:22:13 -0800 (PST) X-Received: by 2002:ad4:5291:0:b0:4c6:e1ba:b1c with SMTP id v17-20020ad45291000000b004c6e1ba0b1cmr19779656qvr.73.1669670533446; Mon, 28 Nov 2022 13:22:13 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id 189-20020a3705c6000000b006fab416015csm8970483qkf.25.2022.11.28.13.22.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Nov 2022 13:22:12 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: TYPENAME_TYPE lookup ignoring non-types [PR107773] Date: Mon, 28 Nov 2022 16:22:11 -0500 Message-Id: <20221128212211.940206-1-ppalka@redhat.com> X-Mailer: git-send-email 2.39.0.rc0.33.g815c1e8202 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750776500112272414?= X-GMAIL-MSGID: =?utf-8?q?1750776500112272414?= [temp.res.general]/3 says, in a note, "the usual qualified name lookup ([basic.lookup.qual]) applies even in the presence of typename". Thus when resolving a TYPENAME_TYPE, it seems we shouldn't be looking past non-type members. This patch fixes this by passing want_type=false instead of =true during the member lookup from make_typename_type. An old nearby comment mentions that we want to continue to set want_type=true when resolving a nested typename type, but it appears that the nested case is handled by resolve_typename_type instead (which passes want_type=true appropriately). In passing, use lookup_member instead of lookup_field so that we give a better diagnostic when a member function is found, and generalize the T format specifier to D in the diagnostic. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/107773 gcc/cp/ChangeLog: * decl.cc (make_typename_type): Use lookup_member instead of lookup_field. Pass want_type=false instead of =true. Use D instead of T format specifier. * search.cc (lookup_member): Document default argument. gcc/testsuite/ChangeLog: * g++.dg/template/typename24.C: New test. * g++.dg/template/typename25.C: New test. --- gcc/cp/decl.cc | 7 +++---- gcc/cp/search.cc | 2 +- gcc/testsuite/g++.dg/template/typename24.C | 16 ++++++++++++++++ gcc/testsuite/g++.dg/template/typename25.C | 20 ++++++++++++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/template/typename24.C create mode 100644 gcc/testsuite/g++.dg/template/typename25.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 238e72f90da..673e10801a6 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -4303,9 +4303,8 @@ make_typename_type (tree context, tree name, enum tag_types tag_type, member of the current instantiation or a non-dependent base; lookup will stop when we hit a dependent base. */ if (!dependent_scope_p (context)) - /* We should only set WANT_TYPE when we're a nested typename type. - Then we can give better diagnostics if we find a non-type. */ - t = lookup_field (context, name, 2, /*want_type=*/true); + t = lookup_member (context, name, /*protect=*/2, /*want_type=*/false, + complain); else t = NULL_TREE; @@ -4357,7 +4356,7 @@ make_typename_type (tree context, tree name, enum tag_types tag_type, else { if (complain & tf_error) - error ("% names %q#T, which is not a type", + error ("% names %q#D, which is not a type", context, name, t); return error_mark_node; } diff --git a/gcc/cp/search.cc b/gcc/cp/search.cc index 0dbb3be1ee7..e5848ebc620 100644 --- a/gcc/cp/search.cc +++ b/gcc/cp/search.cc @@ -1109,7 +1109,7 @@ build_baselink (tree binfo, tree access_binfo, tree functions, tree optype) tree lookup_member (tree xbasetype, tree name, int protect, bool want_type, - tsubst_flags_t complain, access_failure_info *afi) + tsubst_flags_t complain, access_failure_info *afi /* = NULL */) { tree rval, rval_binfo = NULL_TREE; tree type = NULL_TREE, basetype_path = NULL_TREE; diff --git a/gcc/testsuite/g++.dg/template/typename24.C b/gcc/testsuite/g++.dg/template/typename24.C new file mode 100644 index 00000000000..4b1d5e5271b --- /dev/null +++ b/gcc/testsuite/g++.dg/template/typename24.C @@ -0,0 +1,16 @@ +// PR c++/107773 + +struct a { + typedef void get; +}; + +struct b : a { + int get(int i) const; +}; + +template +void f() { + typedef typename T::get type; // { dg-error "'int b::get\\(int\\) const', which is not a type" } +} + +template void f(); diff --git a/gcc/testsuite/g++.dg/template/typename25.C b/gcc/testsuite/g++.dg/template/typename25.C new file mode 100644 index 00000000000..4e6b764a97b --- /dev/null +++ b/gcc/testsuite/g++.dg/template/typename25.C @@ -0,0 +1,20 @@ +// Example 4 from [temp.res.general]/3. + +struct A { + struct X { }; + int X; +}; +struct B { + struct X { }; +}; +template void f(T t) { + typename T::X x; // { dg-error "'int A::X', which is not a type" } +} +void foo() { + A a; + B b; + f(b); // OK, T::X refers to B::X + // { dg-bogus "" "" { target *-*-* } .-1 } + f(a); // error: T::X refers to the data member A::X not the struct A::X + // { dg-message "required from here" "" { target *-*-* } .-1 } +}