Message ID | 20220302183231.2399647-1-ppalka@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> 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 A2FDA3858424 for <patchwork@sourceware.org>; Wed, 2 Mar 2022 18:33:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A2FDA3858424 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1646246018; bh=OF9I1gyS0olMxsJg6vK+6WOE/OYYb2S4SEGimEp4NqE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=d88BB2VsaS+OgEN8VTVsyfAiiJt3LVhmUfjpLG3/CVlqzUhcCxM7aSOh0OEqEjHTx hC9V0XJwF8Kobu7zlo1ZFhAl7Tjx866Z83y3SAHWr67iU+hu6vP3aMFhHx2G9K1Qxz vSvCpMT6qDh+11eb1rOm1Me2rfwkWX0s7+uNw8cE= 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 693F2385800C for <gcc-patches@gcc.gnu.org>; Wed, 2 Mar 2022 18:32:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 693F2385800C Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-671-vKvtWaBqOeWlG1qbG1wjoQ-1; Wed, 02 Mar 2022 13:32:54 -0500 X-MC-Unique: vKvtWaBqOeWlG1qbG1wjoQ-1 Received: by mail-qv1-f71.google.com with SMTP id x16-20020a0ce250000000b00432ec6eaf85so2267735qvl.15 for <gcc-patches@gcc.gnu.org>; Wed, 02 Mar 2022 10:32:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=OF9I1gyS0olMxsJg6vK+6WOE/OYYb2S4SEGimEp4NqE=; b=VPTbo7OU8XOOZWFxpNGuNuq2BpFa+sAH9m9XjpSDNYjctSV8KqsLfznrAfbxg/sG8h WuunHv5QbVL5h/SD+bqk/M1NqjptZE5PuifCGEnwUQgMkgiJkSWNSD6+rvd3f2uRE1u6 GSMZjfs1+tPI2rRltS3vlnxNBzQbCkMBwLv+oq9oWKDHFqvJSboZ9aGWHG0UPjeYbV4P KlakK7Aig3trWbYuSO3Bovt/zK7/Nsm2JcUCRd5swwA/o4T6qaibvlYD6pUnpyQU2rjf hrQBXRC+Kuh36M+mn7Zpp+uj0II9zF6t7EKGwH0a9fXk9usbn3auLWjrNkaTgW3Rr6TQ 58yg== X-Gm-Message-State: AOAM531jOUR8nGsvpAvMNRd5rz15uj00IxXujZPVt1hGPoHy18EZFEWd 0gyd4UrYJMyS8BSEHMsW0252O2XObBZFWRPQSRDujZORGhqtDZMpxP1vPCLki1WmE23iIFpx7Xd zSFqJXhnCon0dd6IAbvQoE/vhjBhi+zDMhMQZj8fJQOyHrY/5a/nOLQ4U+ckJLaDQ7tY= X-Received: by 2002:ae9:eb09:0:b0:47d:8d40:6cb with SMTP id b9-20020ae9eb09000000b0047d8d4006cbmr17084379qkg.672.1646245973786; Wed, 02 Mar 2022 10:32:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJwS+DFDWfTvh2ebf8NS3V+QU6/3O79kSWv4m5A1nP9Kx9UGdRwHP1sqXhrxywQMrssTIkIINQ== X-Received: by 2002:ae9:eb09:0:b0:47d:8d40:6cb with SMTP id b9-20020ae9eb09000000b0047d8d4006cbmr17084361qkg.672.1646245973367; Wed, 02 Mar 2022 10:32:53 -0800 (PST) Received: from localhost.localdomain (ool-18e40894.dyn.optonline.net. [24.228.8.148]) by smtp.gmail.com with ESMTPSA id o21-20020ac85a55000000b002d7847160c0sm11256664qta.82.2022.03.02.10.32.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 10:32:52 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: naming a dependently-scoped template for CTAD [PR104641] Date: Wed, 2 Mar 2022 13:32:31 -0500 Message-Id: <20220302183231.2399647-1-ppalka@redhat.com> X-Mailer: git-send-email 2.35.1.354.g715d08a9e5 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-14.5 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Patrick Palka via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Patrick Palka <ppalka@redhat.com> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
c++: naming a dependently-scoped template for CTAD [PR104641]
|
|
Commit Message
Patrick Palka
March 2, 2022, 6:32 p.m. UTC
In order to be able to perform CTAD for a dependently-scoped template such as A<T>::B in the testcase below, we need to permit a typename-specifier to resolve to a template as per [dcl.type.simple]/2, at least when it appears in a CTAD-enabled context. This patch implements this using a new tsubst flag tf_tst_ok to control when a TYPENAME_TYPE is allowed to name a template, and sets this flag when substituting into the type of a CAST_EXPR, CONSTRUCTOR or VAR_DECL (each of which is a CTAD-enabled context). Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk perhaps? PR c++/104641 gcc/cp/ChangeLog: * cp-tree.h (tsubst_flags::tf_tst_ok): New flag. * decl.cc (make_typename_type): Allow a typename-specifier to resolve to a template when tf_tst_ok. * pt.cc (tsubst_decl) <case VAR_DECL>: Set tf_tst_ok when substituting the type. (tsubst): Clear tf_tst_ok and remember if it was set. <case TYPENAME_TYPE>: Pass tf_tst_ok to make_typename_type appropriately. Do make_template_placeholder when when make_typename_type returns a TEMPLATE_DECL. (tsubst_copy) <case CAST_EXPR>: Set tf_tst_ok when substituting the type. (tsubst_copy_and_build) <case CAST_EXPR>: Likewise. <case CONSTRUCTOR>: Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/class-deduction107.C: New test. --- gcc/cp/cp-tree.h | 2 ++ gcc/cp/decl.cc | 14 +++++--- gcc/cp/pt.cc | 35 +++++++++++++++---- .../g++.dg/cpp1z/class-deduction107.C | 20 +++++++++++ 4 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction107.C
Comments
On 3/2/22 14:32, Patrick Palka wrote: > In order to be able to perform CTAD for a dependently-scoped template > such as A<T>::B in the testcase below, we need to permit a > typename-specifier to resolve to a template as per [dcl.type.simple]/2, > at least when it appears in a CTAD-enabled context. > > This patch implements this using a new tsubst flag tf_tst_ok to control > when a TYPENAME_TYPE is allowed to name a template, and sets this flag > when substituting into the type of a CAST_EXPR, CONSTRUCTOR or VAR_DECL > (each of which is a CTAD-enabled context). What breaks if we always allow that, or at least in -std that support CTAD? > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk perhaps? > > PR c++/104641 > > gcc/cp/ChangeLog: > > * cp-tree.h (tsubst_flags::tf_tst_ok): New flag. > * decl.cc (make_typename_type): Allow a typename-specifier to > resolve to a template when tf_tst_ok. > * pt.cc (tsubst_decl) <case VAR_DECL>: Set tf_tst_ok when > substituting the type. > (tsubst): Clear tf_tst_ok and remember if it was set. > <case TYPENAME_TYPE>: Pass tf_tst_ok to make_typename_type > appropriately. Do make_template_placeholder when when > make_typename_type returns a TEMPLATE_DECL. > (tsubst_copy) <case CAST_EXPR>: Set tf_tst_ok when substituting > the type. > (tsubst_copy_and_build) <case CAST_EXPR>: Likewise. > <case CONSTRUCTOR>: Likewise. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp1z/class-deduction107.C: New test. > --- > gcc/cp/cp-tree.h | 2 ++ > gcc/cp/decl.cc | 14 +++++--- > gcc/cp/pt.cc | 35 +++++++++++++++---- > .../g++.dg/cpp1z/class-deduction107.C | 20 +++++++++++ > 4 files changed, 61 insertions(+), 10 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction107.C > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > index da63d51d9bc..8a44218611f 100644 > --- a/gcc/cp/cp-tree.h > +++ b/gcc/cp/cp-tree.h > @@ -5551,6 +5551,8 @@ enum tsubst_flags { > (build_target_expr and friends) */ > tf_norm = 1 << 11, /* Build diagnostic information during > constraint normalization. */ > + tf_tst_ok = 1 << 12, /* Allow a typename-specifier to name > + a template. */ > /* Convenient substitution flags combinations. */ > tf_warning_or_error = tf_warning | tf_error > }; > diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc > index 7f80f9d4d7a..23c06655bde 100644 > --- a/gcc/cp/decl.cc > +++ b/gcc/cp/decl.cc > @@ -4148,10 +4148,16 @@ make_typename_type (tree context, tree name, enum tag_types tag_type, > } > if (!want_template && TREE_CODE (t) != TYPE_DECL) > { > - if (complain & tf_error) > - error ("%<typename %T::%D%> names %q#T, which is not a type", > - context, name, t); > - return error_mark_node; > + if ((complain & tf_tst_ok) && DECL_TYPE_TEMPLATE_P (t)) > + /* The caller permits this typename-specifier to name a template > + (because it appears in a CTAD-enabled context). */; > + else > + { > + if (complain & tf_error) > + error ("%<typename %T::%D%> names %q#T, which is not a type", > + context, name, t); > + return error_mark_node; > + } > } > > if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE, > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > index 8fb17349ee1..18a21572ce3 100644 > --- a/gcc/cp/pt.cc > +++ b/gcc/cp/pt.cc > @@ -14926,7 +14926,10 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain) > && VAR_HAD_UNKNOWN_BOUND (t) > && type != error_mark_node) > type = strip_array_domain (type); > - type = tsubst (type, args, complain, in_decl); > + tsubst_flags_t tcomplain = complain; > + if (VAR_P (t)) > + tcomplain |= tf_tst_ok; > + type = tsubst (type, args, tcomplain, in_decl); > /* Substituting the type might have recursively instantiated this > same alias (c++/86171). */ > if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl) > @@ -15612,6 +15615,9 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) > bool fndecl_type = (complain & tf_fndecl_type); > complain &= ~tf_fndecl_type; > > + bool tst_ok = (complain & tf_tst_ok); > + complain &= ~tf_tst_ok; > + > if (type > && code != TYPENAME_TYPE > && code != TEMPLATE_TYPE_PARM > @@ -16199,8 +16205,10 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) > return error_mark_node; > } > > - f = make_typename_type (ctx, f, typename_type, > - complain | tf_keep_type_decl); > + tsubst_flags_t tcomplain = complain | tf_keep_type_decl; > + if (tst_ok) > + tcomplain |= tf_tst_ok; > + f = make_typename_type (ctx, f, typename_type, tcomplain); > if (f == error_mark_node) > return f; > if (TREE_CODE (f) == TYPE_DECL) > @@ -16229,6 +16237,12 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) > } > } > > + if (TREE_CODE (f) == TEMPLATE_DECL) > + { > + gcc_checking_assert (tst_ok); > + f = make_template_placeholder (f); > + } > + > return cp_build_qualified_type_real > (f, cp_type_quals (f) | cp_type_quals (t), complain); > } > @@ -17107,7 +17121,10 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) > case CONVERT_EXPR: > case NOP_EXPR: > { > - tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); > + tsubst_flags_t tcomplain = complain; > + if (code == CAST_EXPR) > + tcomplain |= tf_tst_ok; > + tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl); > tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); > return build1 (code, type, op0); > } > @@ -20130,7 +20147,10 @@ tsubst_copy_and_build (tree t, > tree type; > tree op, r = NULL_TREE; > > - type = tsubst (TREE_TYPE (t), args, complain, in_decl); > + tsubst_flags_t tcomplain = complain; > + if (TREE_CODE (t) == CAST_EXPR) > + tcomplain |= tf_tst_ok; > + type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl); > if (integral_constant_expression_p > && !cast_valid_in_integral_constant_expression_p (type)) > { > @@ -21090,12 +21110,15 @@ tsubst_copy_and_build (tree t, > vec<constructor_elt, va_gc> *n; > constructor_elt *ce; > unsigned HOST_WIDE_INT idx; > - tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); > bool process_index_p; > int newlen; > bool need_copy_p = false; > tree r; > > + tsubst_flags_t tcomplain = complain; > + if (COMPOUND_LITERAL_P (t)) > + tcomplain |= tf_tst_ok; > + tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl); > if (type == error_mark_node) > RETURN (error_mark_node); > > diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C > new file mode 100644 > index 00000000000..462da1dcdc2 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C > @@ -0,0 +1,20 @@ > +// PR c++/104641 > +// { dg-do compile { target c++17 } } > + > +template<class T> > +struct A { > + template<class U> struct B { B(U); }; > +private: > + template<class U> struct C { C(U); }; > +}; > + > +template<class T> > +void f() { > + typename A<T>::B x = 0; > + auto y = typename A<T>::B(0); > + auto z = typename A<T>::B{0}; > + > + typename A<T>::C w(0); // { dg-error "private" } > +} > + > +template void f<void>();
On Tue, 8 Mar 2022, Jason Merrill wrote: > On 3/2/22 14:32, Patrick Palka wrote: > > In order to be able to perform CTAD for a dependently-scoped template > > such as A<T>::B in the testcase below, we need to permit a > > typename-specifier to resolve to a template as per [dcl.type.simple]/2, > > at least when it appears in a CTAD-enabled context. > > > > This patch implements this using a new tsubst flag tf_tst_ok to control > > when a TYPENAME_TYPE is allowed to name a template, and sets this flag > > when substituting into the type of a CAST_EXPR, CONSTRUCTOR or VAR_DECL > > (each of which is a CTAD-enabled context). > > What breaks if we always allow that, or at least in -std that support CTAD? AFAICT no significant breakage, but some accepts-invalid and diagnostic regressions crop up, e.g. accepts-invalid for using type = typename A<T>::B; // no more diagnostic if typename resolves to a // template at instantiation time and diagnostic regression for template<class T, class = typename A<T>::B> void f(); // no more elaboration why deduction failed if typename resolves // to a template
On 3/9/22 10:39, Patrick Palka wrote: > On Tue, 8 Mar 2022, Jason Merrill wrote: > >> On 3/2/22 14:32, Patrick Palka wrote: >>> In order to be able to perform CTAD for a dependently-scoped template >>> such as A<T>::B in the testcase below, we need to permit a >>> typename-specifier to resolve to a template as per [dcl.type.simple]/2, >>> at least when it appears in a CTAD-enabled context. >>> >>> This patch implements this using a new tsubst flag tf_tst_ok to control >>> when a TYPENAME_TYPE is allowed to name a template, and sets this flag >>> when substituting into the type of a CAST_EXPR, CONSTRUCTOR or VAR_DECL >>> (each of which is a CTAD-enabled context). >> >> What breaks if we always allow that, or at least in -std that support CTAD? > > AFAICT no significant breakage, but some accepts-invalid and diagnostic > regressions crop up, e.g. accepts-invalid for > > using type = typename A<T>::B; // no more diagnostic if typename resolves to a > // template at instantiation time > > and diagnostic regression for > > template<class T, class = typename A<T>::B> void f(); > // no more elaboration why deduction failed if typename resolves > // to a template Ah, sure, the cost is that we would need to check for this case in various callers, rather than setting a flag in a different set of callers. Fair enough. > @@ -16229,6 +16237,12 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) > } > } > > + if (TREE_CODE (f) == TEMPLATE_DECL) > + { > + gcc_checking_assert (tst_ok); > + f = make_template_placeholder (f); > + } How about calling make_template_placeholder in make_typename_type? Jason
On Wed, 9 Mar 2022, Jason Merrill wrote: > On 3/9/22 10:39, Patrick Palka wrote: > > On Tue, 8 Mar 2022, Jason Merrill wrote: > > > > > On 3/2/22 14:32, Patrick Palka wrote: > > > > In order to be able to perform CTAD for a dependently-scoped template > > > > such as A<T>::B in the testcase below, we need to permit a > > > > typename-specifier to resolve to a template as per [dcl.type.simple]/2, > > > > at least when it appears in a CTAD-enabled context. > > > > > > > > This patch implements this using a new tsubst flag tf_tst_ok to control > > > > when a TYPENAME_TYPE is allowed to name a template, and sets this flag > > > > when substituting into the type of a CAST_EXPR, CONSTRUCTOR or VAR_DECL > > > > (each of which is a CTAD-enabled context). > > > > > > What breaks if we always allow that, or at least in -std that support > > > CTAD? > > > > AFAICT no significant breakage, but some accepts-invalid and diagnostic > > regressions crop up, e.g. accepts-invalid for > > > > using type = typename A<T>::B; // no more diagnostic if typename resolves > > to a > > // template at instantiation time > > > > and diagnostic regression for > > > > template<class T, class = typename A<T>::B> void f(); > > // no more elaboration why deduction failed if typename resolves > > // to a template > > Ah, sure, the cost is that we would need to check for this case in various > callers, rather than setting a flag in a different set of callers. Fair > enough. Yes exactly, and presumably the set of callers for which typename is permitted to resolve to a template is much smaller, so making the behavior opt-in instead of opt-out seems more desirable. Alternatively we could add a new flag to TYPENAME_TYPE set carefully at parse time that controls this behavior, but seems overall simpler to not use a new tree flag if we can get away with it. > > > @@ -16229,6 +16237,12 @@ tsubst (tree t, tree args, tsubst_flags_t complain, > > tree in_decl) > > } > > } > > + if (TREE_CODE (f) == TEMPLATE_DECL) > > + { > > + gcc_checking_assert (tst_ok); > > + f = make_template_placeholder (f); > > + } > > How about calling make_template_placeholder in make_typename_type? That works nicely too, like so? -- >8 -- Subject: [PATCH] c++: naming a dependently-scoped template for CTAD [PR104641] In order to be able to perform CTAD for a dependently-scoped template (such as A<T>::B in the testcase below), we need to permit a typename-specifier to resolve to a template as per [dcl.type.simple]/3, at least when it appears in a CTAD-enabled context. This patch implements this using a new tsubst flag tf_tst_ok to control when a TYPENAME_TYPE is allowed to name a template, and sets this flag when substituting into the type of a CAST_EXPR, CONSTRUCTOR or VAR_DECL (each of which is a CTAD-enabled context). PR c++/104641 gcc/cp/ChangeLog: * cp-tree.h (tsubst_flags::tf_tst_ok): New flag. * decl.cc (make_typename_type): Allow a typename-specifier to resolve to a template when tf_tst_ok, in which case return a CTAD placeholder for the template. * pt.cc (tsubst_decl) <case VAR_DECL>: Set tf_tst_ok when substituting the type. (tsubst): Clear tf_tst_ok and remember if it was set. <case TYPENAME_TYPE>: Pass tf_tst_ok to make_typename_type appropriately. (tsubst_copy) <case CAST_EXPR>: Set tf_tst_ok when substituting the type. (tsubst_copy_and_build) <case CAST_EXPR>: Likewise. <case CONSTRUCTOR>: Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/class-deduction107.C: New test. --- gcc/cp/cp-tree.h | 2 ++ gcc/cp/decl.cc | 20 ++++++++++--- gcc/cp/pt.cc | 29 +++++++++++++++---- .../g++.dg/cpp1z/class-deduction107.C | 20 +++++++++++++ 4 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction107.C diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index b71bce1ab97..b7606f22287 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -5557,6 +5557,8 @@ enum tsubst_flags { (build_target_expr and friends) */ tf_norm = 1 << 11, /* Build diagnostic information during constraint normalization. */ + tf_tst_ok = 1 << 12, /* Allow a typename-specifier to name + a template. */ /* Convenient substitution flags combinations. */ tf_warning_or_error = tf_warning | tf_error }; diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 992e38385c2..d2d46915068 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -4204,16 +4204,28 @@ make_typename_type (tree context, tree name, enum tag_types tag_type, } if (!want_template && TREE_CODE (t) != TYPE_DECL) { - if (complain & tf_error) - error ("%<typename %T::%D%> names %q#T, which is not a type", - context, name, t); - return error_mark_node; + if ((complain & tf_tst_ok) && DECL_TYPE_TEMPLATE_P (t)) + /* The caller permits this typename-specifier to name a template + (because it appears in a CTAD-enabled context). */; + else + { + if (complain & tf_error) + error ("%<typename %T::%D%> names %q#T, which is not a type", + context, name, t); + return error_mark_node; + } } if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE, context, complain)) return error_mark_node; + if (!want_template && DECL_TYPE_TEMPLATE_P (t)) + { + gcc_assert (complain & tf_tst_ok); + return make_template_placeholder (t); + } + if (want_template) { t = lookup_template_class (t, TREE_OPERAND (fullname, 1), diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index f7ee33a6dfd..f890d92d715 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -14896,7 +14896,10 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain) && VAR_HAD_UNKNOWN_BOUND (t) && type != error_mark_node) type = strip_array_domain (type); - type = tsubst (type, args, complain, in_decl); + tsubst_flags_t tcomplain = complain; + if (VAR_P (t)) + tcomplain |= tf_tst_ok; + type = tsubst (type, args, tcomplain, in_decl); /* Substituting the type might have recursively instantiated this same alias (c++/86171). */ if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl) @@ -15582,6 +15585,9 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) bool fndecl_type = (complain & tf_fndecl_type); complain &= ~tf_fndecl_type; + bool tst_ok = (complain & tf_tst_ok); + complain &= ~tf_tst_ok; + if (type && code != TYPENAME_TYPE && code != TEMPLATE_TYPE_PARM @@ -16169,8 +16175,10 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) return error_mark_node; } - f = make_typename_type (ctx, f, typename_type, - complain | tf_keep_type_decl); + tsubst_flags_t tcomplain = complain | tf_keep_type_decl; + if (tst_ok) + tcomplain |= tf_tst_ok; + f = make_typename_type (ctx, f, typename_type, tcomplain); if (f == error_mark_node) return f; if (TREE_CODE (f) == TYPE_DECL) @@ -17077,7 +17085,10 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) case CONVERT_EXPR: case NOP_EXPR: { - tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); + tsubst_flags_t tcomplain = complain; + if (code == CAST_EXPR) + tcomplain |= tf_tst_ok; + tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl); tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); return build1 (code, type, op0); } @@ -20100,7 +20111,10 @@ tsubst_copy_and_build (tree t, tree type; tree op, r = NULL_TREE; - type = tsubst (TREE_TYPE (t), args, complain, in_decl); + tsubst_flags_t tcomplain = complain; + if (TREE_CODE (t) == CAST_EXPR) + tcomplain |= tf_tst_ok; + type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl); if (integral_constant_expression_p && !cast_valid_in_integral_constant_expression_p (type)) { @@ -21060,12 +21074,15 @@ tsubst_copy_and_build (tree t, vec<constructor_elt, va_gc> *n; constructor_elt *ce; unsigned HOST_WIDE_INT idx; - tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); bool process_index_p; int newlen; bool need_copy_p = false; tree r; + tsubst_flags_t tcomplain = complain; + if (COMPOUND_LITERAL_P (t)) + tcomplain |= tf_tst_ok; + tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl); if (type == error_mark_node) RETURN (error_mark_node); diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C new file mode 100644 index 00000000000..462da1dcdc2 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C @@ -0,0 +1,20 @@ +// PR c++/104641 +// { dg-do compile { target c++17 } } + +template<class T> +struct A { + template<class U> struct B { B(U); }; +private: + template<class U> struct C { C(U); }; +}; + +template<class T> +void f() { + typename A<T>::B x = 0; + auto y = typename A<T>::B(0); + auto z = typename A<T>::B{0}; + + typename A<T>::C w(0); // { dg-error "private" } +} + +template void f<void>();
On 3/10/22 12:41, Patrick Palka wrote: > On Wed, 9 Mar 2022, Jason Merrill wrote: > >> On 3/9/22 10:39, Patrick Palka wrote: >>> On Tue, 8 Mar 2022, Jason Merrill wrote: >>> >>>> On 3/2/22 14:32, Patrick Palka wrote: >>>>> In order to be able to perform CTAD for a dependently-scoped template >>>>> such as A<T>::B in the testcase below, we need to permit a >>>>> typename-specifier to resolve to a template as per [dcl.type.simple]/2, >>>>> at least when it appears in a CTAD-enabled context. >>>>> >>>>> This patch implements this using a new tsubst flag tf_tst_ok to control >>>>> when a TYPENAME_TYPE is allowed to name a template, and sets this flag >>>>> when substituting into the type of a CAST_EXPR, CONSTRUCTOR or VAR_DECL >>>>> (each of which is a CTAD-enabled context). >>>> >>>> What breaks if we always allow that, or at least in -std that support >>>> CTAD? >>> >>> AFAICT no significant breakage, but some accepts-invalid and diagnostic >>> regressions crop up, e.g. accepts-invalid for >>> >>> using type = typename A<T>::B; // no more diagnostic if typename resolves >>> to a >>> // template at instantiation time >>> >>> and diagnostic regression for >>> >>> template<class T, class = typename A<T>::B> void f(); >>> // no more elaboration why deduction failed if typename resolves >>> // to a template >> >> Ah, sure, the cost is that we would need to check for this case in various >> callers, rather than setting a flag in a different set of callers. Fair >> enough. > > Yes exactly, and presumably the set of callers for which typename is > permitted to resolve to a template is much smaller, so making the > behavior opt-in instead of opt-out seems more desirable. Alternatively > we could add a new flag to TYPENAME_TYPE set carefully at parse time > that controls this behavior, but seems overall simpler to not use a > new tree flag if we can get away with it. > >> >>> @@ -16229,6 +16237,12 @@ tsubst (tree t, tree args, tsubst_flags_t complain, >>> tree in_decl) >>> } >>> } >>> + if (TREE_CODE (f) == TEMPLATE_DECL) >>> + { >>> + gcc_checking_assert (tst_ok); >>> + f = make_template_placeholder (f); >>> + } >> >> How about calling make_template_placeholder in make_typename_type? > > That works nicely too, like so? > > -- >8 -- > > Subject: [PATCH] c++: naming a dependently-scoped template for CTAD [PR104641] > > In order to be able to perform CTAD for a dependently-scoped template > (such as A<T>::B in the testcase below), we need to permit a > typename-specifier to resolve to a template as per [dcl.type.simple]/3, > at least when it appears in a CTAD-enabled context. > > This patch implements this using a new tsubst flag tf_tst_ok to control > when a TYPENAME_TYPE is allowed to name a template, and sets this flag > when substituting into the type of a CAST_EXPR, CONSTRUCTOR or VAR_DECL > (each of which is a CTAD-enabled context). > > PR c++/104641 > > gcc/cp/ChangeLog: > > * cp-tree.h (tsubst_flags::tf_tst_ok): New flag. > * decl.cc (make_typename_type): Allow a typename-specifier to > resolve to a template when tf_tst_ok, in which case return > a CTAD placeholder for the template. > * pt.cc (tsubst_decl) <case VAR_DECL>: Set tf_tst_ok when > substituting the type. > (tsubst): Clear tf_tst_ok and remember if it was set. > <case TYPENAME_TYPE>: Pass tf_tst_ok to make_typename_type > appropriately. > (tsubst_copy) <case CAST_EXPR>: Set tf_tst_ok when substituting > the type. > (tsubst_copy_and_build) <case CAST_EXPR>: Likewise. > <case CONSTRUCTOR>: Likewise. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp1z/class-deduction107.C: New test. > --- > gcc/cp/cp-tree.h | 2 ++ > gcc/cp/decl.cc | 20 ++++++++++--- > gcc/cp/pt.cc | 29 +++++++++++++++---- > .../g++.dg/cpp1z/class-deduction107.C | 20 +++++++++++++ > 4 files changed, 61 insertions(+), 10 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction107.C > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > index b71bce1ab97..b7606f22287 100644 > --- a/gcc/cp/cp-tree.h > +++ b/gcc/cp/cp-tree.h > @@ -5557,6 +5557,8 @@ enum tsubst_flags { > (build_target_expr and friends) */ > tf_norm = 1 << 11, /* Build diagnostic information during > constraint normalization. */ > + tf_tst_ok = 1 << 12, /* Allow a typename-specifier to name > + a template. */ > /* Convenient substitution flags combinations. */ > tf_warning_or_error = tf_warning | tf_error > }; > diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc > index 992e38385c2..d2d46915068 100644 > --- a/gcc/cp/decl.cc > +++ b/gcc/cp/decl.cc > @@ -4204,16 +4204,28 @@ make_typename_type (tree context, tree name, enum tag_types tag_type, > } > if (!want_template && TREE_CODE (t) != TYPE_DECL) > { > - if (complain & tf_error) > - error ("%<typename %T::%D%> names %q#T, which is not a type", > - context, name, t); > - return error_mark_node; > + if ((complain & tf_tst_ok) && DECL_TYPE_TEMPLATE_P (t)) > + /* The caller permits this typename-specifier to name a template > + (because it appears in a CTAD-enabled context). */; > + else > + { > + if (complain & tf_error) > + error ("%<typename %T::%D%> names %q#T, which is not a type", > + context, name, t); > + return error_mark_node; > + } > } > > if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE, > context, complain)) > return error_mark_node; > > + if (!want_template && DECL_TYPE_TEMPLATE_P (t)) > + { > + gcc_assert (complain & tf_tst_ok); I'm not sure we need this assert so close to the test above, but OK either way. > + return make_template_placeholder (t); > + } > + > if (want_template) > { > t = lookup_template_class (t, TREE_OPERAND (fullname, 1), > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > index f7ee33a6dfd..f890d92d715 100644 > --- a/gcc/cp/pt.cc > +++ b/gcc/cp/pt.cc > @@ -14896,7 +14896,10 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain) > && VAR_HAD_UNKNOWN_BOUND (t) > && type != error_mark_node) > type = strip_array_domain (type); > - type = tsubst (type, args, complain, in_decl); > + tsubst_flags_t tcomplain = complain; > + if (VAR_P (t)) > + tcomplain |= tf_tst_ok; > + type = tsubst (type, args, tcomplain, in_decl); > /* Substituting the type might have recursively instantiated this > same alias (c++/86171). */ > if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl) > @@ -15582,6 +15585,9 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) > bool fndecl_type = (complain & tf_fndecl_type); > complain &= ~tf_fndecl_type; > > + bool tst_ok = (complain & tf_tst_ok); > + complain &= ~tf_tst_ok; > + > if (type > && code != TYPENAME_TYPE > && code != TEMPLATE_TYPE_PARM > @@ -16169,8 +16175,10 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) > return error_mark_node; > } > > - f = make_typename_type (ctx, f, typename_type, > - complain | tf_keep_type_decl); > + tsubst_flags_t tcomplain = complain | tf_keep_type_decl; > + if (tst_ok) > + tcomplain |= tf_tst_ok; > + f = make_typename_type (ctx, f, typename_type, tcomplain); > if (f == error_mark_node) > return f; > if (TREE_CODE (f) == TYPE_DECL) > @@ -17077,7 +17085,10 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) > case CONVERT_EXPR: > case NOP_EXPR: > { > - tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); > + tsubst_flags_t tcomplain = complain; > + if (code == CAST_EXPR) > + tcomplain |= tf_tst_ok; > + tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl); > tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); > return build1 (code, type, op0); > } > @@ -20100,7 +20111,10 @@ tsubst_copy_and_build (tree t, > tree type; > tree op, r = NULL_TREE; > > - type = tsubst (TREE_TYPE (t), args, complain, in_decl); > + tsubst_flags_t tcomplain = complain; > + if (TREE_CODE (t) == CAST_EXPR) > + tcomplain |= tf_tst_ok; > + type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl); > if (integral_constant_expression_p > && !cast_valid_in_integral_constant_expression_p (type)) > { > @@ -21060,12 +21074,15 @@ tsubst_copy_and_build (tree t, > vec<constructor_elt, va_gc> *n; > constructor_elt *ce; > unsigned HOST_WIDE_INT idx; > - tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); > bool process_index_p; > int newlen; > bool need_copy_p = false; > tree r; > > + tsubst_flags_t tcomplain = complain; > + if (COMPOUND_LITERAL_P (t)) > + tcomplain |= tf_tst_ok; > + tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl); > if (type == error_mark_node) > RETURN (error_mark_node); > > diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C > new file mode 100644 > index 00000000000..462da1dcdc2 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C > @@ -0,0 +1,20 @@ > +// PR c++/104641 > +// { dg-do compile { target c++17 } } > + > +template<class T> > +struct A { > + template<class U> struct B { B(U); }; > +private: > + template<class U> struct C { C(U); }; > +}; > + > +template<class T> > +void f() { > + typename A<T>::B x = 0; > + auto y = typename A<T>::B(0); > + auto z = typename A<T>::B{0}; > + > + typename A<T>::C w(0); // { dg-error "private" } > +} > + > +template void f<void>();
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index da63d51d9bc..8a44218611f 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -5551,6 +5551,8 @@ enum tsubst_flags { (build_target_expr and friends) */ tf_norm = 1 << 11, /* Build diagnostic information during constraint normalization. */ + tf_tst_ok = 1 << 12, /* Allow a typename-specifier to name + a template. */ /* Convenient substitution flags combinations. */ tf_warning_or_error = tf_warning | tf_error }; diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 7f80f9d4d7a..23c06655bde 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -4148,10 +4148,16 @@ make_typename_type (tree context, tree name, enum tag_types tag_type, } if (!want_template && TREE_CODE (t) != TYPE_DECL) { - if (complain & tf_error) - error ("%<typename %T::%D%> names %q#T, which is not a type", - context, name, t); - return error_mark_node; + if ((complain & tf_tst_ok) && DECL_TYPE_TEMPLATE_P (t)) + /* The caller permits this typename-specifier to name a template + (because it appears in a CTAD-enabled context). */; + else + { + if (complain & tf_error) + error ("%<typename %T::%D%> names %q#T, which is not a type", + context, name, t); + return error_mark_node; + } } if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE, diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 8fb17349ee1..18a21572ce3 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -14926,7 +14926,10 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain) && VAR_HAD_UNKNOWN_BOUND (t) && type != error_mark_node) type = strip_array_domain (type); - type = tsubst (type, args, complain, in_decl); + tsubst_flags_t tcomplain = complain; + if (VAR_P (t)) + tcomplain |= tf_tst_ok; + type = tsubst (type, args, tcomplain, in_decl); /* Substituting the type might have recursively instantiated this same alias (c++/86171). */ if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl) @@ -15612,6 +15615,9 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) bool fndecl_type = (complain & tf_fndecl_type); complain &= ~tf_fndecl_type; + bool tst_ok = (complain & tf_tst_ok); + complain &= ~tf_tst_ok; + if (type && code != TYPENAME_TYPE && code != TEMPLATE_TYPE_PARM @@ -16199,8 +16205,10 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) return error_mark_node; } - f = make_typename_type (ctx, f, typename_type, - complain | tf_keep_type_decl); + tsubst_flags_t tcomplain = complain | tf_keep_type_decl; + if (tst_ok) + tcomplain |= tf_tst_ok; + f = make_typename_type (ctx, f, typename_type, tcomplain); if (f == error_mark_node) return f; if (TREE_CODE (f) == TYPE_DECL) @@ -16229,6 +16237,12 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) } } + if (TREE_CODE (f) == TEMPLATE_DECL) + { + gcc_checking_assert (tst_ok); + f = make_template_placeholder (f); + } + return cp_build_qualified_type_real (f, cp_type_quals (f) | cp_type_quals (t), complain); } @@ -17107,7 +17121,10 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) case CONVERT_EXPR: case NOP_EXPR: { - tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); + tsubst_flags_t tcomplain = complain; + if (code == CAST_EXPR) + tcomplain |= tf_tst_ok; + tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl); tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); return build1 (code, type, op0); } @@ -20130,7 +20147,10 @@ tsubst_copy_and_build (tree t, tree type; tree op, r = NULL_TREE; - type = tsubst (TREE_TYPE (t), args, complain, in_decl); + tsubst_flags_t tcomplain = complain; + if (TREE_CODE (t) == CAST_EXPR) + tcomplain |= tf_tst_ok; + type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl); if (integral_constant_expression_p && !cast_valid_in_integral_constant_expression_p (type)) { @@ -21090,12 +21110,15 @@ tsubst_copy_and_build (tree t, vec<constructor_elt, va_gc> *n; constructor_elt *ce; unsigned HOST_WIDE_INT idx; - tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); bool process_index_p; int newlen; bool need_copy_p = false; tree r; + tsubst_flags_t tcomplain = complain; + if (COMPOUND_LITERAL_P (t)) + tcomplain |= tf_tst_ok; + tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl); if (type == error_mark_node) RETURN (error_mark_node); diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C new file mode 100644 index 00000000000..462da1dcdc2 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C @@ -0,0 +1,20 @@ +// PR c++/104641 +// { dg-do compile { target c++17 } } + +template<class T> +struct A { + template<class U> struct B { B(U); }; +private: + template<class U> struct C { C(U); }; +}; + +template<class T> +void f() { + typename A<T>::B x = 0; + auto y = typename A<T>::B(0); + auto z = typename A<T>::B{0}; + + typename A<T>::C w(0); // { dg-error "private" } +} + +template void f<void>();