From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 9D8A73858404 for ; Wed, 5 Apr 2023 17:31:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9D8A73858404 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680715908; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=fOb6dZfVIpBP0t/3pEJ9fHoq4x68nQ4dFIxMXcG26I4=; b=dgxEW0/TSbFCKjVZoXAObTET6u6EWAh/N9rWyvcF6ZMcy6rq95+828Z/EuMZiTTFRITrdG g/y/VczzlDlllA9XJVsHM8Ay2sn6YddjOTUOLcw48njEutzODDPsLKlrtcR1L4eGMnGCSK tjM9zLWzHUrRAE53nsw7oN5Aq9Ik7hw= 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-256-Wnb39yY3Oim3T3AbIm5xKA-1; Wed, 05 Apr 2023 13:31:47 -0400 X-MC-Unique: Wnb39yY3Oim3T3AbIm5xKA-1 Received: by mail-qt1-f200.google.com with SMTP id u1-20020a05622a198100b003e12a0467easo24775187qtc.11 for ; Wed, 05 Apr 2023 10:31:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680715906; h=mime-version:references:message-id:in-reply-to:subject:cc:to:date :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fOb6dZfVIpBP0t/3pEJ9fHoq4x68nQ4dFIxMXcG26I4=; b=BImvr3VkEWr36urR42pthFLGxf5scgNtBbtTBX9MWVijKOUCyRH4vppJVI/63bHVor B4Tkzbu+XGaSm4mcqZLXwMhwiZkPGkZJ0fUBSZoKIOmzxfRG3Eh35pmVshhDpvXkf627 bOlB9vCZfB1HPVs0D0bD8dm2wWnlvcdysiOuDe7mr7gmtcTe36xeOIyF62Ek0KWz0zKh Lmj0g8RD2EjBolPEvUQ7L7GayIVwXXcxIrkoZFXtrbOsxD8UcoQ0M7BGETh/RoCT5Sjr eu4G5b49qvPiDQU9XB8KNM6aily2YCvNPwRRb/VfRPEKk0TJmfeB+N2xkaBOFP4ZAyz8 LQ0w== X-Gm-Message-State: AAQBX9ebZ8c2+WVFNJwRX+UXaKrvS+4vti7VvlwuOsxAe4hBV3R1OgZr nxjCU42Nej9lRN30GMy3R1rXmG7S1F8UI7SyWvjz5sZIU5vae2y4ptwfFzo1OFcQ3QQ9zn6QXwG +FmM2Bsre3FfT8aeBVw== X-Received: by 2002:a05:6214:21a9:b0:5a9:d6dd:271f with SMTP id t9-20020a05621421a900b005a9d6dd271fmr5000545qvc.23.1680715906311; Wed, 05 Apr 2023 10:31:46 -0700 (PDT) X-Google-Smtp-Source: AKy350bFJZKttMoLVJWeaMgaM5AKhKmk6K7VGDmmYOA4cE16Th3A21/ulp/Fz/hhoUs5TrGrZuePDQ== X-Received: by 2002:a05:6214:21a9:b0:5a9:d6dd:271f with SMTP id t9-20020a05621421a900b005a9d6dd271fmr5000495qvc.23.1680715905807; Wed, 05 Apr 2023 10:31:45 -0700 (PDT) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id m6-20020a0cee46000000b005dd8b9345f0sm4334609qvs.136.2023.04.05.10.31.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 10:31:45 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Wed, 5 Apr 2023 13:31:44 -0400 (EDT) To: Patrick Palka cc: gcc-patches@gcc.gnu.org, jason@redhat.com Subject: Re: [PATCH] c++: 'typename T::X' vs 'struct T::X' lookup [PR109420] In-Reply-To: <20230405165927.1987914-1-ppalka@redhat.com> Message-ID: References: <20230405165927.1987914-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-13.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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, 5 Apr 2023, Patrick Palka wrote: > 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); Here's v2 which just slightly improves this comment. I reckon [basic.lookup.elab] is a better reference than [dcl.type.elab]/5 for justifying why the lookup should be type-only for class-key and 'enum' TYPENAME_TYPEs. -- >8 -- PR c++/109420 gcc/cp/ChangeLog: * decl.cc (make_typename_type): Also ignore non-types during the lookup if tag_type corresponds to an elaborated-type-specifier. * 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 | 12 +++++++++++- gcc/cp/pt.cc | 9 ++++++++- gcc/testsuite/g++.dg/template/typename27.C | 19 +++++++++++++++++++ 3 files changed, 38 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..772c059dc2c 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -4307,7 +4307,17 @@ 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); + /* We generally don't ignore non-types during TYPENAME_TYPE lookup + (as per [temp.res.general]/3), unless + - the tag corresponds to a class-key or 'enum' so + [basic.lookup.elab] applies, or + - the tag corresponds to scope_type or tf_qualifying_scope is + set so [basic.lookup.qual]/1 applies. + TODO: If we'd set/track the scope_type tag thoroughly 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..09559c88f29 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 written. */ + 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(); -- 2.40.0.153.g6369acd968