From d3b0dc686c00bfe9e7f4fe0490def68e9b92157a Mon Sep 17 00:00:00 2001 From: Patrick Palka Date: Sat, 12 Mar 2022 15:00:51 -0500 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::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) : Set tf_tst_ok when substituting the type. (tsubst): Clear tf_tst_ok and remember if it was set. : Pass tf_tst_ok to make_typename_type appropriately. (tsubst_copy) : Set tf_tst_ok when substituting the type. (tsubst_copy_and_build) : Likewise. : Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/class-deduction107.C: New test. --- gcc/cp/cp-tree.h | 2 ++ gcc/cp/decl.cc | 18 +++++++++--- gcc/cp/pt.cc | 29 +++++++++++++++---- .../g++.dg/cpp1z/class-deduction107.C | 24 +++++++++++++++ 4 files changed, 63 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 02054142cb1..a4bc48a4a20 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -5563,6 +5563,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 (C++17 or later). */ /* 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 c42be60b704..8afda8264c4 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -4204,16 +4204,26 @@ 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 ("% names %q#T, which is not a type", - context, name, t); - return error_mark_node; + if ((complain & tf_tst_ok) && cxx_dialect >= cxx17 + && 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 ("% 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)) + 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 67a49c7c67b..715eea27577 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 *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..1492f391ec1 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C @@ -0,0 +1,24 @@ +// PR c++/104641 +// { dg-do compile { target c++11 } } + +template +struct A { + template struct B { B(U); }; +private: + template struct C { C(U); }; +}; + +template +void f() { + typename A::B x = 0; + auto y = typename A::B(0); + auto z = typename A::B{0}; + typename A::C w(0); // { dg-error "private" "" { target c++17 } } +} + +template void f(); + +// { dg-error "not a type" "" { target c++14_down } 13 } +// { dg-error "not a type" "" { target c++14_down } 14 } +// { dg-error "not a type" "" { target c++14_down } 15 } +// { dg-error "not a type" "" { target c++14_down } 16 }