* [PATCH 1/2] c++: refine dependent_alias_template_spec_p [PR90679]
@ 2023-06-01 14:49 Patrick Palka
2023-06-01 14:49 ` [PATCH 2/2] c++: partial ordering and dep alias tmpl specs [PR90679] Patrick Palka
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Patrick Palka @ 2023-06-01 14:49 UTC (permalink / raw)
To: gcc-patches; +Cc: jason, Patrick Palka
For a complex alias template-id, dependent_alias_template_spec_p returns
true if any template argument of the template-id is dependent. This
predicate indicates that substitution into the template-id may behave
differently with respect to SFINAE than substitution into the expanded
alias, and so the alias is in a way non-transparent. For example
'first_t<T, T&>' in
template<class T, class...> using first_t = T;
template<class T> first_t<T, T&> f();
is such an alias template-id since first_t doesn't use its second
template parameter and so the substitution into the expanded alias would
discard the SFINAE effects of the corresponding (dependent) argument 'T&'.
But this predicate is overly conservative since what really matters for
sake of SFINAE equivalence is whether a template argument corresponding
to an _unused_ template parameter is dependent. So the predicate should
return false for e.g. 'first_t<T, int>' or 'first_t<T>'.
This patch refines the predicate appropriately. We need to be able to
efficiently determine which template parameters of a complex alias
template are unused, so to that end we add a new out parameter to
complex_alias_template_p and cache its result in an on-the-side
hash_map that replaces the existing TEMPLATE_DECL_COMPLEX_ALIAS_P
flag. And in doing so, we fix a latent bug that this flag wasn't
being propagated during partial instantiation, and so we were treating
all partially instantiated member alias templates as non-complex.
PR c++/90679
gcc/cp/ChangeLog:
* cp-tree.h (TEMPLATE_DECL_COMPLEX_ALIAS_P): Remove.
(most_general_template): Constify parameter.
* pt.cc (push_template_decl): Adjust after removing
TEMPLATE_DECL_COMPLEX_ALIAS_P.
(complex_alias_tmpl_info): New hash_map.
(uses_all_template_parms_data::seen): Change type to
tree* from bool*.
(complex_alias_template_r): Adjust accordingly.
(complex_alias_template_p): Add 'seen_out' out parameter.
Call most_general_template and check PRIMARY_TEMPLATE_P.
Use complex_alias_tmpl_info to cache the result and set
'*seen_out' accordigly.
(dependent_alias_template_spec_p): Add !processing_template_decl
early exit test. Consider dependence of only template arguments
corresponding to seen template parameters as per
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/alias-decl-75.C: New test.
---
gcc/cp/cp-tree.h | 7 +-
gcc/cp/pt.cc | 101 +++++++++++++++------
gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C | 24 +++++
3 files changed, 100 insertions(+), 32 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index a1b882f11fe..5330d1e1f62 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -543,7 +543,6 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX];
2: DECL_THIS_EXTERN (in VAR_DECL, FUNCTION_DECL or PARM_DECL)
DECL_IMPLICIT_TYPEDEF_P (in a TYPE_DECL)
DECL_CONSTRAINT_VAR_P (in a PARM_DECL)
- TEMPLATE_DECL_COMPLEX_ALIAS_P (in TEMPLATE_DECL)
DECL_INSTANTIATING_NSDMI_P (in a FIELD_DECL)
USING_DECL_UNRELATED_P (in USING_DECL)
3: DECL_IN_AGGR_P.
@@ -3655,10 +3654,6 @@ struct GTY(()) lang_decl {
#define TYPE_DECL_ALIAS_P(NODE) \
DECL_LANG_FLAG_6 (TYPE_DECL_CHECK (NODE))
-/* Nonzero for TEMPLATE_DECL means that it is a 'complex' alias template. */
-#define TEMPLATE_DECL_COMPLEX_ALIAS_P(NODE) \
- DECL_LANG_FLAG_2 (TEMPLATE_DECL_CHECK (NODE))
-
/* Nonzero for a type which is an alias for another type; i.e, a type
which declaration was written 'using name-of-type =
another-type'. */
@@ -7403,7 +7398,7 @@ extern tree tsubst_argument_pack (tree, tree, tsubst_flags_t, tree);
extern tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
extern tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
extern tree tsubst_function_parms (tree, tree, tsubst_flags_t, tree);
-extern tree most_general_template (tree);
+extern tree most_general_template (const_tree);
extern tree get_mostly_instantiated_function_type (tree);
extern bool problematic_instantiation_changed (void);
extern void record_last_problematic_instantiation (void);
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 7fb3e75bceb..1b28195e10d 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -211,7 +211,6 @@ static tree listify (tree);
static tree listify_autos (tree, tree);
static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
-static bool complex_alias_template_p (const_tree tmpl);
static tree get_underlying_template (tree);
static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
static tree canonicalize_expr_argument (tree, tsubst_flags_t);
@@ -6233,8 +6232,6 @@ push_template_decl (tree decl, bool is_friend)
constr = build_constraints (constr, NULL_TREE);
set_constraints (decl, constr);
}
- if (complex_alias_template_p (tmpl))
- TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
}
}
@@ -6526,12 +6523,16 @@ alias_template_specialization_p (const_tree t,
return NULL_TREE;
}
+/* A cache of the result of complex_alias_template_p. */
+
+static GTY(()) hash_map<const_tree, tree> *complex_alias_tmpl_info;
+
/* Data structure for complex_alias_template_*. */
struct uses_all_template_parms_data
{
int level;
- bool *seen;
+ tree *seen;
};
/* walk_tree callback for complex_alias_template_p. */
@@ -6551,7 +6552,7 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
{
tree idx = get_template_parm_index (t);
if (TEMPLATE_PARM_LEVEL (idx) == data.level)
- data.seen[TEMPLATE_PARM_IDX (idx)] = true;
+ data.seen[TEMPLATE_PARM_IDX (idx)] = boolean_true_node;
}
default:;
@@ -6576,7 +6577,7 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
return t;
/* Consider the expanded packs to be used outside the expansion... */
- data.seen[idx] = true;
+ data.seen[idx] = boolean_true_node;
}
/* ...but don't walk into the pattern. Consider PR104008:
@@ -6598,12 +6599,17 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
using that alias can be ill-formed when the expansion is not, as with
the void_t template.
- Returns 1 if always complex, 0 if not complex, -1 if complex iff any of the
- template arguments are empty packs. */
+ If this predicate returns true in the ordinary case, the out parameter
+ SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if
+ the I'th template parameter of the alias template is used in the alias. */
static bool
-complex_alias_template_p (const_tree tmpl)
+complex_alias_template_p (const_tree tmpl, tree *seen_out)
{
+ tmpl = most_general_template (tmpl);
+ if (!PRIMARY_TEMPLATE_P (tmpl))
+ return false;
+
/* A renaming alias isn't complex. */
if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
return false;
@@ -6612,26 +6618,52 @@ complex_alias_template_p (const_tree tmpl)
if (get_constraints (tmpl))
return true;
+ if (tree *slot = hash_map_safe_get (complex_alias_tmpl_info, tmpl))
+ {
+ tree result = *slot;
+ if (result == boolean_false_node)
+ return false;
+ if (result == boolean_true_node)
+ return true;
+ gcc_assert (TREE_CODE (result) == TREE_VEC);
+ if (seen_out)
+ *seen_out = result;
+ return true;
+ }
+
struct uses_all_template_parms_data data;
tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
tree parms = DECL_TEMPLATE_PARMS (tmpl);
data.level = TMPL_PARMS_DEPTH (parms);
int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
- data.seen = XALLOCAVEC (bool, len);
+ tree seen = make_tree_vec (len);
+ data.seen = TREE_VEC_BEGIN (seen);
for (int i = 0; i < len; ++i)
- data.seen[i] = false;
+ data.seen[i] = boolean_false_node;
if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data))
- return true;
- for (int i = 0; i < len; ++i)
- if (!data.seen[i])
+ {
+ hash_map_safe_put<hm_ggc> (complex_alias_tmpl_info, tmpl, boolean_true_node);
return true;
+ }
+
+ for (int i = 0; i < len; ++i)
+ if (data.seen[i] != boolean_true_node)
+ {
+ hash_map_safe_put<hm_ggc> (complex_alias_tmpl_info, tmpl, seen);
+ if (seen_out)
+ *seen_out = seen;
+ return true;
+ }
+
+ hash_map_safe_put<hm_ggc> (complex_alias_tmpl_info, tmpl, boolean_false_node);
return false;
}
-/* If T is a specialization of a complex alias template with dependent
- template-arguments, return it; otherwise return NULL_TREE. If T is a
- typedef to such a specialization, return the specialization. */
+/* If T is a specialization of a complex alias template with a dependent
+ argument for an unused template parameter, return it; otherwise return
+ NULL_TREE. If T is a typedef to such a specialization, return the
+ specialization. */
tree
dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
@@ -6640,15 +6672,32 @@ dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
return NULL_TREE;
gcc_assert (TYPE_P (t));
- if (!typedef_variant_p (t))
+ if (!processing_template_decl || !typedef_variant_p (t))
return NULL_TREE;
- tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
- if (tinfo
- && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
- && (any_dependent_template_arguments_p
- (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
- return CONST_CAST_TREE (t);
+ if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
+ {
+ tree seen = NULL_TREE;
+ if (complex_alias_template_p (TI_TEMPLATE (tinfo), &seen))
+ {
+ tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
+ if (!seen)
+ {
+ if (any_dependent_template_arguments_p (args))
+ return CONST_CAST_TREE (t);
+ }
+ else
+ {
+ gcc_assert (TREE_VEC_LENGTH (args) == TREE_VEC_LENGTH (seen));
+ for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
+ if (TREE_VEC_ELT (seen, i) != boolean_true_node
+ && dependent_template_arg_p (TREE_VEC_ELT (args, i)))
+ return CONST_CAST_TREE (t);
+ }
+
+ return NULL_TREE;
+ }
+ }
if (transparent_typedefs)
{
@@ -25958,7 +26007,7 @@ most_specialized_instantiation (tree templates)
`template <class T> template <class U> S<T*>::f(U)'. */
tree
-most_general_template (tree decl)
+most_general_template (const_tree decl)
{
if (TREE_CODE (decl) != TEMPLATE_DECL)
{
@@ -25992,7 +26041,7 @@ most_general_template (tree decl)
decl = DECL_TI_TEMPLATE (decl);
}
- return decl;
+ return CONST_CAST_TREE (decl);
}
/* Return the most specialized of the template partial specializations
diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C
new file mode 100644
index 00000000000..10592f521a0
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C
@@ -0,0 +1,24 @@
+// PR c++/90679
+// { dg-do compile { target c++11 } }
+
+template<class T, class...>
+using first_t = T;
+
+template<class T>
+struct A;
+
+template<class T>
+struct traits;
+
+template<class T>
+struct traits<A<first_t<T>>> {
+ static constexpr int value = 1;
+};
+
+template<class T>
+struct traits<A<first_t<const T>>> {
+ static constexpr int value = 2;
+};
+
+static_assert(traits<A<int>>::value == 1, "");
+static_assert(traits<A<const int>>::value == 2, ""); // { dg-bogus "ambiguous" }
--
2.41.0.rc1.10.g9e49351c30
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 2/2] c++: partial ordering and dep alias tmpl specs [PR90679]
2023-06-01 14:49 [PATCH 1/2] c++: refine dependent_alias_template_spec_p [PR90679] Patrick Palka
@ 2023-06-01 14:49 ` Patrick Palka
2023-09-11 14:19 ` Patrick Palka
2023-12-15 19:07 ` Patrick Palka
2023-06-02 16:20 ` [PATCH 1/2] c++: refine dependent_alias_template_spec_p [PR90679] Patrick Palka
2023-09-11 14:19 ` Patrick Palka
2 siblings, 2 replies; 9+ messages in thread
From: Patrick Palka @ 2023-06-01 14:49 UTC (permalink / raw)
To: gcc-patches; +Cc: jason, Patrick Palka
During partial ordering, we want to look through dependent alias
template specializations within template arguments and otherwise
treat them as opaque in other contexts (see e.g. r7-7116-g0c942f3edab108
and r11-7011-g6e0a231a4aa240). To that end template_args_equal was
given a partial_order flag that controls this behavior. This flag
does the right thing when a dependent alias template specialization
appears as template argument of the partial specialization, e.g. in
template<class T, class...> using first_t = T;
template<class T> struct traits;
template<class T> struct traits<first_t<T, T&>> { }; // #1
template<class T> struct traits<first_t<const T, T&>> { }; // #2
we correctly consider #2 to be more specialized than #1. But if
the alias specialization appears as a template argument of another
class template specialization, e.g. in
template<class T> struct traits<A<first_t<T, T&>>> { }; // #1
template<class T> struct traits<A<first_t<const T, T&>>> { }; // #2
then we incorrectly consider #1 and #2 to be unordered. This is because
1. we don't propagate the flag to recursive template_args_equal calls
2. we don't use structural equality for class template specializations
written in terms of dependent alias template specializations
This patch fixes the first issue by turning the partial_order flag into
a global. This patch fixes the second issue by making us propagate
structural equality appropriately when building a class template
specialization. In passing this patch also improves hashing of
specializations that use structural equality.
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?
PR c++/90679
gcc/cp/ChangeLog:
* cp-tree.h (comp_template_args): Remove partial_order
parameter.
(template_args_equal): Likewise.
* pt.cc (iterative_hash_template_arg) <case tcc_type>: Hash
the template and arguments for specializations that use
structural equality.
(comparing_for_partial_ordering): New flag.
(template_args_equal): Remove partial order parameter and
use comparing_for_partial_ordering instead.
(comp_template_args): Likewise.
(comp_template_args_porder): Set comparing_for_partial_ordering
instead. Make static.
(any_template_arguments_need_structural_equality_p): Return true
for an argument that's a dependent alias template specialization
or a class template specialization that itself needs structural
equality.
* tree.cc (cp_tree_equal) <case TREE_VEC>: Adjust call to
comp_template_args.
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/alias-decl-75a.C: New test.
* g++.dg/cpp0x/alias-decl-75b.C: New test.
---
gcc/cp/cp-tree.h | 4 +--
gcc/cp/pt.cc | 40 +++++++++++++++++----
gcc/cp/tree.cc | 2 +-
gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C | 26 ++++++++++++++
gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C | 26 ++++++++++++++
5 files changed, 88 insertions(+), 10 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C
create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 5330d1e1f62..f08e5630a5c 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7381,8 +7381,8 @@ extern int template_class_depth (tree);
extern int is_specialization_of (tree, tree);
extern bool is_specialization_of_friend (tree, tree);
extern bool comp_template_args (tree, tree, tree * = NULL,
- tree * = NULL, bool = false);
-extern int template_args_equal (tree, tree, bool = false);
+ tree * = NULL);
+extern int template_args_equal (tree, tree);
extern tree maybe_process_partial_specialization (tree);
extern tree most_specialized_instantiation (tree);
extern tree most_specialized_partial_spec (tree, tsubst_flags_t);
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 1b28195e10d..1a32f10b22b 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -1913,6 +1913,11 @@ iterative_hash_template_arg (tree arg, hashval_t val)
default:
if (tree canonical = TYPE_CANONICAL (arg))
val = iterative_hash_object (TYPE_HASH (canonical), val);
+ else if (tree ti = TYPE_TEMPLATE_INFO (arg))
+ {
+ val = iterative_hash_template_arg (TI_TEMPLATE (ti), val);
+ val = iterative_hash_template_arg (TI_ARGS (ti), val);
+ }
break;
}
@@ -9296,6 +9301,12 @@ coerce_template_parms (tree parms,
return return_full_args ? new_args : new_inner_args;
}
+/* Whether we are comparing template arguments during partial ordering
+ (and therefore want the comparison to look through dependent alias
+ template specializations). */
+
+static int comparing_for_partial_ordering;
+
/* Returns true if T is a wrapper to make a C++20 template parameter
object const. */
@@ -9312,7 +9323,7 @@ class_nttp_const_wrapper_p (tree t)
/* Returns 1 if template args OT and NT are equivalent. */
int
-template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
+template_args_equal (tree ot, tree nt)
{
if (nt == ot)
return 1;
@@ -9335,7 +9346,7 @@ template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
During partial ordering, however, we need to treat them normally so we can
order uses of the same alias with different cv-qualification (79960). */
auto cso = make_temp_override (comparing_dependent_aliases);
- if (!partial_order)
+ if (!comparing_for_partial_ordering)
++comparing_dependent_aliases;
if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
@@ -9383,8 +9394,7 @@ template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
bool
comp_template_args (tree oldargs, tree newargs,
- tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */,
- bool partial_order /* = false */)
+ tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */)
{
if (oldargs == newargs)
return true;
@@ -9400,7 +9410,7 @@ comp_template_args (tree oldargs, tree newargs,
tree nt = TREE_VEC_ELT (newargs, i);
tree ot = TREE_VEC_ELT (oldargs, i);
- if (! template_args_equal (ot, nt, partial_order))
+ if (! template_args_equal (ot, nt))
{
if (oldarg_ptr != NULL)
*oldarg_ptr = ot;
@@ -9412,10 +9422,13 @@ comp_template_args (tree oldargs, tree newargs,
return true;
}
-inline bool
+static bool
comp_template_args_porder (tree oargs, tree nargs)
{
- return comp_template_args (oargs, nargs, NULL, NULL, true);
+ ++comparing_for_partial_ordering;
+ bool equal = comp_template_args (oargs, nargs);
+ --comparing_for_partial_ordering;
+ return equal;
}
/* Implement a freelist interface for objects of type T.
@@ -28839,6 +28852,19 @@ any_template_arguments_need_structural_equality_p (tree args)
mutated after the fact by duplicate_decls), so just require
structural equality in this case (PR52830). */
return true;
+ else if (TYPE_P (arg)
+ && TYPE_STRUCTURAL_EQUALITY_P (arg)
+ && dependent_alias_template_spec_p (arg, nt_transparent))
+ /* Require structural equality for specializations written
+ in terms of a dependent alias template specialization. */
+ return true;
+ else if (CLASS_TYPE_P (arg)
+ && TYPE_TEMPLATE_INFO (arg)
+ && TYPE_STRUCTURAL_EQUALITY_P (arg))
+ /* Require structural equality for specializations written
+ in terms of a class template specialization that itself
+ needs structural equality. */
+ return true;
}
}
}
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index 19dfb3ed782..775c0c9cd57 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -4136,7 +4136,7 @@ cp_tree_equal (tree t1, tree t2)
case TREE_VEC:
/* These are template args. Really we should be getting the
caller to do this as it knows it to be true. */
- if (!comp_template_args (t1, t2, NULL, NULL, false))
+ if (!comp_template_args (t1, t2))
return false;
return true;
diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C
new file mode 100644
index 00000000000..8adbd6f65ac
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C
@@ -0,0 +1,26 @@
+// PR c++/90679
+// A version of alias-decl-75.C where the specializations of the
+// complex alias template first_t are dependent.
+// { dg-do compile { target c++11 } }
+
+template<class T, class...>
+using first_t = T;
+
+template<class T>
+struct A;
+
+template<class T>
+struct traits;
+
+template<class T>
+struct traits<A<first_t<T, T&>>> {
+ static constexpr int value = 1;
+};
+
+template<class T>
+struct traits<A<first_t<const T, T&>>> {
+ static constexpr int value = 2;
+};
+
+static_assert(traits<A<int>>::value == 1, "");
+static_assert(traits<A<const int>>::value == 2, ""); // { dg-bogus "ambiguous" }
diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C
new file mode 100644
index 00000000000..b89ea5cad1e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C
@@ -0,0 +1,26 @@
+// PR c++/90679
+// A version of alias-decl-75a.C where the alias template specialization
+// appears as a more deeply nested template argument.
+// { dg-do compile { target c++11 } }
+
+template<class T, class...>
+using first_t = T;
+
+template<class T>
+struct A;
+
+template<class T>
+struct traits;
+
+template<class T>
+struct traits<A<A<first_t<T, T&>>>> {
+ static constexpr int value = 1;
+};
+
+template<class T>
+struct traits<A<A<first_t<const T, T&>>>> {
+ static constexpr int value = 2;
+};
+
+static_assert(traits<A<A<int>>>::value == 1, "");
+static_assert(traits<A<A<const int>>>::value == 2, ""); // { dg-bogus "ambiguous" }
--
2.41.0.rc1.10.g9e49351c30
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 1/2] c++: refine dependent_alias_template_spec_p [PR90679]
2023-06-01 14:49 [PATCH 1/2] c++: refine dependent_alias_template_spec_p [PR90679] Patrick Palka
2023-06-01 14:49 ` [PATCH 2/2] c++: partial ordering and dep alias tmpl specs [PR90679] Patrick Palka
@ 2023-06-02 16:20 ` Patrick Palka
2023-09-11 14:19 ` Patrick Palka
2 siblings, 0 replies; 9+ messages in thread
From: Patrick Palka @ 2023-06-02 16:20 UTC (permalink / raw)
To: Patrick Palka; +Cc: gcc-patches, jason
On Thu, 1 Jun 2023, Patrick Palka wrote:
> For a complex alias template-id, dependent_alias_template_spec_p returns
> true if any template argument of the template-id is dependent. This
> predicate indicates that substitution into the template-id may behave
> differently with respect to SFINAE than substitution into the expanded
> alias, and so the alias is in a way non-transparent. For example
> 'first_t<T, T&>' in
>
> template<class T, class...> using first_t = T;
> template<class T> first_t<T, T&> f();
>
> is such an alias template-id since first_t doesn't use its second
> template parameter and so the substitution into the expanded alias would
> discard the SFINAE effects of the corresponding (dependent) argument 'T&'.
>
> But this predicate is overly conservative since what really matters for
> sake of SFINAE equivalence is whether a template argument corresponding
> to an _unused_ template parameter is dependent. So the predicate should
> return false for e.g. 'first_t<T, int>' or 'first_t<T>'.
>
> This patch refines the predicate appropriately. We need to be able to
> efficiently determine which template parameters of a complex alias
> template are unused, so to that end we add a new out parameter to
> complex_alias_template_p and cache its result in an on-the-side
> hash_map that replaces the existing TEMPLATE_DECL_COMPLEX_ALIAS_P
> flag. And in doing so, we fix a latent bug that this flag wasn't
> being propagated during partial instantiation, and so we were treating
> all partially instantiated member alias templates as non-complex.
Whoops, this last sentence is wrong I think. The flag propagation would
have happened via the call to copy_decl from tsubst_template_decl, so
there was no latent bug.
>
> PR c++/90679
>
> gcc/cp/ChangeLog:
>
> * cp-tree.h (TEMPLATE_DECL_COMPLEX_ALIAS_P): Remove.
> (most_general_template): Constify parameter.
> * pt.cc (push_template_decl): Adjust after removing
> TEMPLATE_DECL_COMPLEX_ALIAS_P.
> (complex_alias_tmpl_info): New hash_map.
> (uses_all_template_parms_data::seen): Change type to
> tree* from bool*.
> (complex_alias_template_r): Adjust accordingly.
> (complex_alias_template_p): Add 'seen_out' out parameter.
> Call most_general_template and check PRIMARY_TEMPLATE_P.
> Use complex_alias_tmpl_info to cache the result and set
> '*seen_out' accordigly.
> (dependent_alias_template_spec_p): Add !processing_template_decl
> early exit test. Consider dependence of only template arguments
> corresponding to seen template parameters as per
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp0x/alias-decl-75.C: New test.
> ---
> gcc/cp/cp-tree.h | 7 +-
> gcc/cp/pt.cc | 101 +++++++++++++++------
> gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C | 24 +++++
> 3 files changed, 100 insertions(+), 32 deletions(-)
> create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C
>
> diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
> index a1b882f11fe..5330d1e1f62 100644
> --- a/gcc/cp/cp-tree.h
> +++ b/gcc/cp/cp-tree.h
> @@ -543,7 +543,6 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX];
> 2: DECL_THIS_EXTERN (in VAR_DECL, FUNCTION_DECL or PARM_DECL)
> DECL_IMPLICIT_TYPEDEF_P (in a TYPE_DECL)
> DECL_CONSTRAINT_VAR_P (in a PARM_DECL)
> - TEMPLATE_DECL_COMPLEX_ALIAS_P (in TEMPLATE_DECL)
> DECL_INSTANTIATING_NSDMI_P (in a FIELD_DECL)
> USING_DECL_UNRELATED_P (in USING_DECL)
> 3: DECL_IN_AGGR_P.
> @@ -3655,10 +3654,6 @@ struct GTY(()) lang_decl {
> #define TYPE_DECL_ALIAS_P(NODE) \
> DECL_LANG_FLAG_6 (TYPE_DECL_CHECK (NODE))
>
> -/* Nonzero for TEMPLATE_DECL means that it is a 'complex' alias template. */
> -#define TEMPLATE_DECL_COMPLEX_ALIAS_P(NODE) \
> - DECL_LANG_FLAG_2 (TEMPLATE_DECL_CHECK (NODE))
> -
> /* Nonzero for a type which is an alias for another type; i.e, a type
> which declaration was written 'using name-of-type =
> another-type'. */
> @@ -7403,7 +7398,7 @@ extern tree tsubst_argument_pack (tree, tree, tsubst_flags_t, tree);
> extern tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
> extern tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
> extern tree tsubst_function_parms (tree, tree, tsubst_flags_t, tree);
> -extern tree most_general_template (tree);
> +extern tree most_general_template (const_tree);
> extern tree get_mostly_instantiated_function_type (tree);
> extern bool problematic_instantiation_changed (void);
> extern void record_last_problematic_instantiation (void);
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 7fb3e75bceb..1b28195e10d 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -211,7 +211,6 @@ static tree listify (tree);
> static tree listify_autos (tree, tree);
> static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
> static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
> -static bool complex_alias_template_p (const_tree tmpl);
> static tree get_underlying_template (tree);
> static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
> static tree canonicalize_expr_argument (tree, tsubst_flags_t);
> @@ -6233,8 +6232,6 @@ push_template_decl (tree decl, bool is_friend)
> constr = build_constraints (constr, NULL_TREE);
> set_constraints (decl, constr);
> }
> - if (complex_alias_template_p (tmpl))
> - TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
> }
> }
>
> @@ -6526,12 +6523,16 @@ alias_template_specialization_p (const_tree t,
> return NULL_TREE;
> }
>
> +/* A cache of the result of complex_alias_template_p. */
> +
> +static GTY(()) hash_map<const_tree, tree> *complex_alias_tmpl_info;
> +
> /* Data structure for complex_alias_template_*. */
>
> struct uses_all_template_parms_data
> {
> int level;
> - bool *seen;
> + tree *seen;
> };
>
> /* walk_tree callback for complex_alias_template_p. */
> @@ -6551,7 +6552,7 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
> {
> tree idx = get_template_parm_index (t);
> if (TEMPLATE_PARM_LEVEL (idx) == data.level)
> - data.seen[TEMPLATE_PARM_IDX (idx)] = true;
> + data.seen[TEMPLATE_PARM_IDX (idx)] = boolean_true_node;
> }
>
> default:;
> @@ -6576,7 +6577,7 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
> return t;
>
> /* Consider the expanded packs to be used outside the expansion... */
> - data.seen[idx] = true;
> + data.seen[idx] = boolean_true_node;
> }
>
> /* ...but don't walk into the pattern. Consider PR104008:
> @@ -6598,12 +6599,17 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
> using that alias can be ill-formed when the expansion is not, as with
> the void_t template.
>
> - Returns 1 if always complex, 0 if not complex, -1 if complex iff any of the
> - template arguments are empty packs. */
> + If this predicate returns true in the ordinary case, the out parameter
> + SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if
> + the I'th template parameter of the alias template is used in the alias. */
>
> static bool
> -complex_alias_template_p (const_tree tmpl)
> +complex_alias_template_p (const_tree tmpl, tree *seen_out)
> {
> + tmpl = most_general_template (tmpl);
> + if (!PRIMARY_TEMPLATE_P (tmpl))
> + return false;
> +
> /* A renaming alias isn't complex. */
> if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
> return false;
> @@ -6612,26 +6618,52 @@ complex_alias_template_p (const_tree tmpl)
> if (get_constraints (tmpl))
> return true;
>
> + if (tree *slot = hash_map_safe_get (complex_alias_tmpl_info, tmpl))
> + {
> + tree result = *slot;
> + if (result == boolean_false_node)
> + return false;
> + if (result == boolean_true_node)
> + return true;
> + gcc_assert (TREE_CODE (result) == TREE_VEC);
> + if (seen_out)
> + *seen_out = result;
> + return true;
> + }
> +
> struct uses_all_template_parms_data data;
> tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
> tree parms = DECL_TEMPLATE_PARMS (tmpl);
> data.level = TMPL_PARMS_DEPTH (parms);
> int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
> - data.seen = XALLOCAVEC (bool, len);
> + tree seen = make_tree_vec (len);
> + data.seen = TREE_VEC_BEGIN (seen);
> for (int i = 0; i < len; ++i)
> - data.seen[i] = false;
> + data.seen[i] = boolean_false_node;
>
> if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data))
> - return true;
> - for (int i = 0; i < len; ++i)
> - if (!data.seen[i])
> + {
> + hash_map_safe_put<hm_ggc> (complex_alias_tmpl_info, tmpl, boolean_true_node);
> return true;
> + }
> +
> + for (int i = 0; i < len; ++i)
> + if (data.seen[i] != boolean_true_node)
> + {
> + hash_map_safe_put<hm_ggc> (complex_alias_tmpl_info, tmpl, seen);
> + if (seen_out)
> + *seen_out = seen;
> + return true;
> + }
> +
> + hash_map_safe_put<hm_ggc> (complex_alias_tmpl_info, tmpl, boolean_false_node);
> return false;
> }
>
> -/* If T is a specialization of a complex alias template with dependent
> - template-arguments, return it; otherwise return NULL_TREE. If T is a
> - typedef to such a specialization, return the specialization. */
> +/* If T is a specialization of a complex alias template with a dependent
> + argument for an unused template parameter, return it; otherwise return
> + NULL_TREE. If T is a typedef to such a specialization, return the
> + specialization. */
>
> tree
> dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
> @@ -6640,15 +6672,32 @@ dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
> return NULL_TREE;
> gcc_assert (TYPE_P (t));
>
> - if (!typedef_variant_p (t))
> + if (!processing_template_decl || !typedef_variant_p (t))
> return NULL_TREE;
>
> - tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
> - if (tinfo
> - && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
> - && (any_dependent_template_arguments_p
> - (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
> - return CONST_CAST_TREE (t);
> + if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
> + {
> + tree seen = NULL_TREE;
> + if (complex_alias_template_p (TI_TEMPLATE (tinfo), &seen))
> + {
> + tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
> + if (!seen)
> + {
> + if (any_dependent_template_arguments_p (args))
> + return CONST_CAST_TREE (t);
> + }
> + else
> + {
> + gcc_assert (TREE_VEC_LENGTH (args) == TREE_VEC_LENGTH (seen));
> + for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
> + if (TREE_VEC_ELT (seen, i) != boolean_true_node
> + && dependent_template_arg_p (TREE_VEC_ELT (args, i)))
> + return CONST_CAST_TREE (t);
> + }
> +
> + return NULL_TREE;
> + }
> + }
>
> if (transparent_typedefs)
> {
> @@ -25958,7 +26007,7 @@ most_specialized_instantiation (tree templates)
> `template <class T> template <class U> S<T*>::f(U)'. */
>
> tree
> -most_general_template (tree decl)
> +most_general_template (const_tree decl)
> {
> if (TREE_CODE (decl) != TEMPLATE_DECL)
> {
> @@ -25992,7 +26041,7 @@ most_general_template (tree decl)
> decl = DECL_TI_TEMPLATE (decl);
> }
>
> - return decl;
> + return CONST_CAST_TREE (decl);
> }
>
> /* Return the most specialized of the template partial specializations
> diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C
> new file mode 100644
> index 00000000000..10592f521a0
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C
> @@ -0,0 +1,24 @@
> +// PR c++/90679
> +// { dg-do compile { target c++11 } }
> +
> +template<class T, class...>
> +using first_t = T;
> +
> +template<class T>
> +struct A;
> +
> +template<class T>
> +struct traits;
> +
> +template<class T>
> +struct traits<A<first_t<T>>> {
> + static constexpr int value = 1;
> +};
> +
> +template<class T>
> +struct traits<A<first_t<const T>>> {
> + static constexpr int value = 2;
> +};
> +
> +static_assert(traits<A<int>>::value == 1, "");
> +static_assert(traits<A<const int>>::value == 2, ""); // { dg-bogus "ambiguous" }
> --
> 2.41.0.rc1.10.g9e49351c30
>
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 1/2] c++: refine dependent_alias_template_spec_p [PR90679]
2023-06-01 14:49 [PATCH 1/2] c++: refine dependent_alias_template_spec_p [PR90679] Patrick Palka
2023-06-01 14:49 ` [PATCH 2/2] c++: partial ordering and dep alias tmpl specs [PR90679] Patrick Palka
2023-06-02 16:20 ` [PATCH 1/2] c++: refine dependent_alias_template_spec_p [PR90679] Patrick Palka
@ 2023-09-11 14:19 ` Patrick Palka
2023-12-15 19:06 ` Patrick Palka
2 siblings, 1 reply; 9+ messages in thread
From: Patrick Palka @ 2023-09-11 14:19 UTC (permalink / raw)
To: Patrick Palka; +Cc: gcc-patches, jason
On Thu, 1 Jun 2023, Patrick Palka wrote:
> For a complex alias template-id, dependent_alias_template_spec_p returns
> true if any template argument of the template-id is dependent. This
> predicate indicates that substitution into the template-id may behave
> differently with respect to SFINAE than substitution into the expanded
> alias, and so the alias is in a way non-transparent. For example
> 'first_t<T, T&>' in
>
> template<class T, class...> using first_t = T;
> template<class T> first_t<T, T&> f();
>
> is such an alias template-id since first_t doesn't use its second
> template parameter and so the substitution into the expanded alias would
> discard the SFINAE effects of the corresponding (dependent) argument 'T&'.
>
> But this predicate is overly conservative since what really matters for
> sake of SFINAE equivalence is whether a template argument corresponding
> to an _unused_ template parameter is dependent. So the predicate should
> return false for e.g. 'first_t<T, int>' or 'first_t<T>'.
>
> This patch refines the predicate appropriately. We need to be able to
> efficiently determine which template parameters of a complex alias
> template are unused, so to that end we add a new out parameter to
> complex_alias_template_p and cache its result in an on-the-side
> hash_map that replaces the existing TEMPLATE_DECL_COMPLEX_ALIAS_P
> flag. And in doing so, we fix a latent bug that this flag wasn't
> being propagated during partial instantiation, and so we were treating
> all partially instantiated member alias templates as non-complex.
>
> PR c++/90679
>
> gcc/cp/ChangeLog:
>
> * cp-tree.h (TEMPLATE_DECL_COMPLEX_ALIAS_P): Remove.
> (most_general_template): Constify parameter.
> * pt.cc (push_template_decl): Adjust after removing
> TEMPLATE_DECL_COMPLEX_ALIAS_P.
> (complex_alias_tmpl_info): New hash_map.
> (uses_all_template_parms_data::seen): Change type to
> tree* from bool*.
> (complex_alias_template_r): Adjust accordingly.
> (complex_alias_template_p): Add 'seen_out' out parameter.
> Call most_general_template and check PRIMARY_TEMPLATE_P.
> Use complex_alias_tmpl_info to cache the result and set
> '*seen_out' accordigly.
> (dependent_alias_template_spec_p): Add !processing_template_decl
> early exit test. Consider dependence of only template arguments
> corresponding to seen template parameters as per
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp0x/alias-decl-75.C: New test.
Ping.
> ---
> gcc/cp/cp-tree.h | 7 +-
> gcc/cp/pt.cc | 101 +++++++++++++++------
> gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C | 24 +++++
> 3 files changed, 100 insertions(+), 32 deletions(-)
> create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C
>
> diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
> index a1b882f11fe..5330d1e1f62 100644
> --- a/gcc/cp/cp-tree.h
> +++ b/gcc/cp/cp-tree.h
> @@ -543,7 +543,6 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX];
> 2: DECL_THIS_EXTERN (in VAR_DECL, FUNCTION_DECL or PARM_DECL)
> DECL_IMPLICIT_TYPEDEF_P (in a TYPE_DECL)
> DECL_CONSTRAINT_VAR_P (in a PARM_DECL)
> - TEMPLATE_DECL_COMPLEX_ALIAS_P (in TEMPLATE_DECL)
> DECL_INSTANTIATING_NSDMI_P (in a FIELD_DECL)
> USING_DECL_UNRELATED_P (in USING_DECL)
> 3: DECL_IN_AGGR_P.
> @@ -3655,10 +3654,6 @@ struct GTY(()) lang_decl {
> #define TYPE_DECL_ALIAS_P(NODE) \
> DECL_LANG_FLAG_6 (TYPE_DECL_CHECK (NODE))
>
> -/* Nonzero for TEMPLATE_DECL means that it is a 'complex' alias template. */
> -#define TEMPLATE_DECL_COMPLEX_ALIAS_P(NODE) \
> - DECL_LANG_FLAG_2 (TEMPLATE_DECL_CHECK (NODE))
> -
> /* Nonzero for a type which is an alias for another type; i.e, a type
> which declaration was written 'using name-of-type =
> another-type'. */
> @@ -7403,7 +7398,7 @@ extern tree tsubst_argument_pack (tree, tree, tsubst_flags_t, tree);
> extern tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
> extern tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
> extern tree tsubst_function_parms (tree, tree, tsubst_flags_t, tree);
> -extern tree most_general_template (tree);
> +extern tree most_general_template (const_tree);
> extern tree get_mostly_instantiated_function_type (tree);
> extern bool problematic_instantiation_changed (void);
> extern void record_last_problematic_instantiation (void);
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 7fb3e75bceb..1b28195e10d 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -211,7 +211,6 @@ static tree listify (tree);
> static tree listify_autos (tree, tree);
> static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
> static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
> -static bool complex_alias_template_p (const_tree tmpl);
> static tree get_underlying_template (tree);
> static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
> static tree canonicalize_expr_argument (tree, tsubst_flags_t);
> @@ -6233,8 +6232,6 @@ push_template_decl (tree decl, bool is_friend)
> constr = build_constraints (constr, NULL_TREE);
> set_constraints (decl, constr);
> }
> - if (complex_alias_template_p (tmpl))
> - TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
> }
> }
>
> @@ -6526,12 +6523,16 @@ alias_template_specialization_p (const_tree t,
> return NULL_TREE;
> }
>
> +/* A cache of the result of complex_alias_template_p. */
> +
> +static GTY(()) hash_map<const_tree, tree> *complex_alias_tmpl_info;
Consider this changed to be (deletable).
> +
> /* Data structure for complex_alias_template_*. */
>
> struct uses_all_template_parms_data
> {
> int level;
> - bool *seen;
> + tree *seen;
> };
>
> /* walk_tree callback for complex_alias_template_p. */
> @@ -6551,7 +6552,7 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
> {
> tree idx = get_template_parm_index (t);
> if (TEMPLATE_PARM_LEVEL (idx) == data.level)
> - data.seen[TEMPLATE_PARM_IDX (idx)] = true;
> + data.seen[TEMPLATE_PARM_IDX (idx)] = boolean_true_node;
> }
>
> default:;
> @@ -6576,7 +6577,7 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
> return t;
>
> /* Consider the expanded packs to be used outside the expansion... */
> - data.seen[idx] = true;
> + data.seen[idx] = boolean_true_node;
> }
>
> /* ...but don't walk into the pattern. Consider PR104008:
> @@ -6598,12 +6599,17 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
> using that alias can be ill-formed when the expansion is not, as with
> the void_t template.
>
> - Returns 1 if always complex, 0 if not complex, -1 if complex iff any of the
> - template arguments are empty packs. */
> + If this predicate returns true in the ordinary case, the out parameter
> + SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if
> + the I'th template parameter of the alias template is used in the alias. */
>
> static bool
> -complex_alias_template_p (const_tree tmpl)
> +complex_alias_template_p (const_tree tmpl, tree *seen_out)
> {
> + tmpl = most_general_template (tmpl);
> + if (!PRIMARY_TEMPLATE_P (tmpl))
> + return false;
> +
> /* A renaming alias isn't complex. */
> if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
> return false;
> @@ -6612,26 +6618,52 @@ complex_alias_template_p (const_tree tmpl)
> if (get_constraints (tmpl))
> return true;
>
> + if (tree *slot = hash_map_safe_get (complex_alias_tmpl_info, tmpl))
> + {
> + tree result = *slot;
> + if (result == boolean_false_node)
> + return false;
> + if (result == boolean_true_node)
> + return true;
> + gcc_assert (TREE_CODE (result) == TREE_VEC);
> + if (seen_out)
> + *seen_out = result;
> + return true;
> + }
> +
> struct uses_all_template_parms_data data;
> tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
> tree parms = DECL_TEMPLATE_PARMS (tmpl);
> data.level = TMPL_PARMS_DEPTH (parms);
> int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
> - data.seen = XALLOCAVEC (bool, len);
> + tree seen = make_tree_vec (len);
> + data.seen = TREE_VEC_BEGIN (seen);
> for (int i = 0; i < len; ++i)
> - data.seen[i] = false;
> + data.seen[i] = boolean_false_node;
>
> if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data))
> - return true;
> - for (int i = 0; i < len; ++i)
> - if (!data.seen[i])
> + {
> + hash_map_safe_put<hm_ggc> (complex_alias_tmpl_info, tmpl, boolean_true_node);
> return true;
> + }
> +
> + for (int i = 0; i < len; ++i)
> + if (data.seen[i] != boolean_true_node)
> + {
> + hash_map_safe_put<hm_ggc> (complex_alias_tmpl_info, tmpl, seen);
> + if (seen_out)
> + *seen_out = seen;
> + return true;
> + }
> +
> + hash_map_safe_put<hm_ggc> (complex_alias_tmpl_info, tmpl, boolean_false_node);
> return false;
> }
>
> -/* If T is a specialization of a complex alias template with dependent
> - template-arguments, return it; otherwise return NULL_TREE. If T is a
> - typedef to such a specialization, return the specialization. */
> +/* If T is a specialization of a complex alias template with a dependent
> + argument for an unused template parameter, return it; otherwise return
> + NULL_TREE. If T is a typedef to such a specialization, return the
> + specialization. */
>
> tree
> dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
> @@ -6640,15 +6672,32 @@ dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
> return NULL_TREE;
> gcc_assert (TYPE_P (t));
>
> - if (!typedef_variant_p (t))
> + if (!processing_template_decl || !typedef_variant_p (t))
> return NULL_TREE;
>
> - tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
> - if (tinfo
> - && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
> - && (any_dependent_template_arguments_p
> - (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
> - return CONST_CAST_TREE (t);
> + if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
> + {
> + tree seen = NULL_TREE;
> + if (complex_alias_template_p (TI_TEMPLATE (tinfo), &seen))
> + {
> + tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
> + if (!seen)
> + {
> + if (any_dependent_template_arguments_p (args))
> + return CONST_CAST_TREE (t);
> + }
> + else
> + {
> + gcc_assert (TREE_VEC_LENGTH (args) == TREE_VEC_LENGTH (seen));
> + for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
> + if (TREE_VEC_ELT (seen, i) != boolean_true_node
> + && dependent_template_arg_p (TREE_VEC_ELT (args, i)))
> + return CONST_CAST_TREE (t);
> + }
> +
> + return NULL_TREE;
> + }
> + }
>
> if (transparent_typedefs)
> {
> @@ -25958,7 +26007,7 @@ most_specialized_instantiation (tree templates)
> `template <class T> template <class U> S<T*>::f(U)'. */
>
> tree
> -most_general_template (tree decl)
> +most_general_template (const_tree decl)
> {
> if (TREE_CODE (decl) != TEMPLATE_DECL)
> {
> @@ -25992,7 +26041,7 @@ most_general_template (tree decl)
> decl = DECL_TI_TEMPLATE (decl);
> }
>
> - return decl;
> + return CONST_CAST_TREE (decl);
> }
>
> /* Return the most specialized of the template partial specializations
> diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C
> new file mode 100644
> index 00000000000..10592f521a0
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C
> @@ -0,0 +1,24 @@
> +// PR c++/90679
> +// { dg-do compile { target c++11 } }
> +
> +template<class T, class...>
> +using first_t = T;
> +
> +template<class T>
> +struct A;
> +
> +template<class T>
> +struct traits;
> +
> +template<class T>
> +struct traits<A<first_t<T>>> {
> + static constexpr int value = 1;
> +};
> +
> +template<class T>
> +struct traits<A<first_t<const T>>> {
> + static constexpr int value = 2;
> +};
> +
> +static_assert(traits<A<int>>::value == 1, "");
> +static_assert(traits<A<const int>>::value == 2, ""); // { dg-bogus "ambiguous" }
> --
> 2.41.0.rc1.10.g9e49351c30
>
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 2/2] c++: partial ordering and dep alias tmpl specs [PR90679]
2023-06-01 14:49 ` [PATCH 2/2] c++: partial ordering and dep alias tmpl specs [PR90679] Patrick Palka
@ 2023-09-11 14:19 ` Patrick Palka
2023-12-15 19:07 ` Patrick Palka
1 sibling, 0 replies; 9+ messages in thread
From: Patrick Palka @ 2023-09-11 14:19 UTC (permalink / raw)
To: Patrick Palka; +Cc: gcc-patches, jason
On Thu, 1 Jun 2023, Patrick Palka wrote:
> During partial ordering, we want to look through dependent alias
> template specializations within template arguments and otherwise
> treat them as opaque in other contexts (see e.g. r7-7116-g0c942f3edab108
> and r11-7011-g6e0a231a4aa240). To that end template_args_equal was
> given a partial_order flag that controls this behavior. This flag
> does the right thing when a dependent alias template specialization
> appears as template argument of the partial specialization, e.g. in
>
> template<class T, class...> using first_t = T;
> template<class T> struct traits;
> template<class T> struct traits<first_t<T, T&>> { }; // #1
> template<class T> struct traits<first_t<const T, T&>> { }; // #2
>
> we correctly consider #2 to be more specialized than #1. But if
> the alias specialization appears as a template argument of another
> class template specialization, e.g. in
>
> template<class T> struct traits<A<first_t<T, T&>>> { }; // #1
> template<class T> struct traits<A<first_t<const T, T&>>> { }; // #2
>
> then we incorrectly consider #1 and #2 to be unordered. This is because
>
> 1. we don't propagate the flag to recursive template_args_equal calls
> 2. we don't use structural equality for class template specializations
> written in terms of dependent alias template specializations
>
> This patch fixes the first issue by turning the partial_order flag into
> a global. This patch fixes the second issue by making us propagate
> structural equality appropriately when building a class template
> specialization. In passing this patch also improves hashing of
> specializations that use structural equality.
>
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?
Ping.
>
> PR c++/90679
>
> gcc/cp/ChangeLog:
>
> * cp-tree.h (comp_template_args): Remove partial_order
> parameter.
> (template_args_equal): Likewise.
> * pt.cc (iterative_hash_template_arg) <case tcc_type>: Hash
> the template and arguments for specializations that use
> structural equality.
> (comparing_for_partial_ordering): New flag.
> (template_args_equal): Remove partial order parameter and
> use comparing_for_partial_ordering instead.
> (comp_template_args): Likewise.
> (comp_template_args_porder): Set comparing_for_partial_ordering
> instead. Make static.
> (any_template_arguments_need_structural_equality_p): Return true
> for an argument that's a dependent alias template specialization
> or a class template specialization that itself needs structural
> equality.
> * tree.cc (cp_tree_equal) <case TREE_VEC>: Adjust call to
> comp_template_args.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp0x/alias-decl-75a.C: New test.
> * g++.dg/cpp0x/alias-decl-75b.C: New test.
> ---
> gcc/cp/cp-tree.h | 4 +--
> gcc/cp/pt.cc | 40 +++++++++++++++++----
> gcc/cp/tree.cc | 2 +-
> gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C | 26 ++++++++++++++
> gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C | 26 ++++++++++++++
> 5 files changed, 88 insertions(+), 10 deletions(-)
> create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C
> create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C
>
> diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
> index 5330d1e1f62..f08e5630a5c 100644
> --- a/gcc/cp/cp-tree.h
> +++ b/gcc/cp/cp-tree.h
> @@ -7381,8 +7381,8 @@ extern int template_class_depth (tree);
> extern int is_specialization_of (tree, tree);
> extern bool is_specialization_of_friend (tree, tree);
> extern bool comp_template_args (tree, tree, tree * = NULL,
> - tree * = NULL, bool = false);
> -extern int template_args_equal (tree, tree, bool = false);
> + tree * = NULL);
> +extern int template_args_equal (tree, tree);
> extern tree maybe_process_partial_specialization (tree);
> extern tree most_specialized_instantiation (tree);
> extern tree most_specialized_partial_spec (tree, tsubst_flags_t);
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 1b28195e10d..1a32f10b22b 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -1913,6 +1913,11 @@ iterative_hash_template_arg (tree arg, hashval_t val)
> default:
> if (tree canonical = TYPE_CANONICAL (arg))
> val = iterative_hash_object (TYPE_HASH (canonical), val);
> + else if (tree ti = TYPE_TEMPLATE_INFO (arg))
> + {
> + val = iterative_hash_template_arg (TI_TEMPLATE (ti), val);
> + val = iterative_hash_template_arg (TI_ARGS (ti), val);
> + }
> break;
> }
>
> @@ -9296,6 +9301,12 @@ coerce_template_parms (tree parms,
> return return_full_args ? new_args : new_inner_args;
> }
>
> +/* Whether we are comparing template arguments during partial ordering
> + (and therefore want the comparison to look through dependent alias
> + template specializations). */
> +
> +static int comparing_for_partial_ordering;
> +
> /* Returns true if T is a wrapper to make a C++20 template parameter
> object const. */
>
> @@ -9312,7 +9323,7 @@ class_nttp_const_wrapper_p (tree t)
> /* Returns 1 if template args OT and NT are equivalent. */
>
> int
> -template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
> +template_args_equal (tree ot, tree nt)
> {
> if (nt == ot)
> return 1;
> @@ -9335,7 +9346,7 @@ template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
> During partial ordering, however, we need to treat them normally so we can
> order uses of the same alias with different cv-qualification (79960). */
> auto cso = make_temp_override (comparing_dependent_aliases);
> - if (!partial_order)
> + if (!comparing_for_partial_ordering)
> ++comparing_dependent_aliases;
>
> if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
> @@ -9383,8 +9394,7 @@ template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
>
> bool
> comp_template_args (tree oldargs, tree newargs,
> - tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */,
> - bool partial_order /* = false */)
> + tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */)
> {
> if (oldargs == newargs)
> return true;
> @@ -9400,7 +9410,7 @@ comp_template_args (tree oldargs, tree newargs,
> tree nt = TREE_VEC_ELT (newargs, i);
> tree ot = TREE_VEC_ELT (oldargs, i);
>
> - if (! template_args_equal (ot, nt, partial_order))
> + if (! template_args_equal (ot, nt))
> {
> if (oldarg_ptr != NULL)
> *oldarg_ptr = ot;
> @@ -9412,10 +9422,13 @@ comp_template_args (tree oldargs, tree newargs,
> return true;
> }
>
> -inline bool
> +static bool
> comp_template_args_porder (tree oargs, tree nargs)
> {
> - return comp_template_args (oargs, nargs, NULL, NULL, true);
> + ++comparing_for_partial_ordering;
> + bool equal = comp_template_args (oargs, nargs);
> + --comparing_for_partial_ordering;
> + return equal;
> }
>
> /* Implement a freelist interface for objects of type T.
> @@ -28839,6 +28852,19 @@ any_template_arguments_need_structural_equality_p (tree args)
> mutated after the fact by duplicate_decls), so just require
> structural equality in this case (PR52830). */
> return true;
> + else if (TYPE_P (arg)
> + && TYPE_STRUCTURAL_EQUALITY_P (arg)
> + && dependent_alias_template_spec_p (arg, nt_transparent))
> + /* Require structural equality for specializations written
> + in terms of a dependent alias template specialization. */
> + return true;
> + else if (CLASS_TYPE_P (arg)
> + && TYPE_TEMPLATE_INFO (arg)
> + && TYPE_STRUCTURAL_EQUALITY_P (arg))
> + /* Require structural equality for specializations written
> + in terms of a class template specialization that itself
> + needs structural equality. */
> + return true;
> }
> }
> }
> diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
> index 19dfb3ed782..775c0c9cd57 100644
> --- a/gcc/cp/tree.cc
> +++ b/gcc/cp/tree.cc
> @@ -4136,7 +4136,7 @@ cp_tree_equal (tree t1, tree t2)
> case TREE_VEC:
> /* These are template args. Really we should be getting the
> caller to do this as it knows it to be true. */
> - if (!comp_template_args (t1, t2, NULL, NULL, false))
> + if (!comp_template_args (t1, t2))
> return false;
> return true;
>
> diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C
> new file mode 100644
> index 00000000000..8adbd6f65ac
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C
> @@ -0,0 +1,26 @@
> +// PR c++/90679
> +// A version of alias-decl-75.C where the specializations of the
> +// complex alias template first_t are dependent.
> +// { dg-do compile { target c++11 } }
> +
> +template<class T, class...>
> +using first_t = T;
> +
> +template<class T>
> +struct A;
> +
> +template<class T>
> +struct traits;
> +
> +template<class T>
> +struct traits<A<first_t<T, T&>>> {
> + static constexpr int value = 1;
> +};
> +
> +template<class T>
> +struct traits<A<first_t<const T, T&>>> {
> + static constexpr int value = 2;
> +};
> +
> +static_assert(traits<A<int>>::value == 1, "");
> +static_assert(traits<A<const int>>::value == 2, ""); // { dg-bogus "ambiguous" }
> diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C
> new file mode 100644
> index 00000000000..b89ea5cad1e
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C
> @@ -0,0 +1,26 @@
> +// PR c++/90679
> +// A version of alias-decl-75a.C where the alias template specialization
> +// appears as a more deeply nested template argument.
> +// { dg-do compile { target c++11 } }
> +
> +template<class T, class...>
> +using first_t = T;
> +
> +template<class T>
> +struct A;
> +
> +template<class T>
> +struct traits;
> +
> +template<class T>
> +struct traits<A<A<first_t<T, T&>>>> {
> + static constexpr int value = 1;
> +};
> +
> +template<class T>
> +struct traits<A<A<first_t<const T, T&>>>> {
> + static constexpr int value = 2;
> +};
> +
> +static_assert(traits<A<A<int>>>::value == 1, "");
> +static_assert(traits<A<A<const int>>>::value == 2, ""); // { dg-bogus "ambiguous" }
> --
> 2.41.0.rc1.10.g9e49351c30
>
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 1/2] c++: refine dependent_alias_template_spec_p [PR90679]
2023-09-11 14:19 ` Patrick Palka
@ 2023-12-15 19:06 ` Patrick Palka
2023-12-18 21:53 ` Jason Merrill
0 siblings, 1 reply; 9+ messages in thread
From: Patrick Palka @ 2023-12-15 19:06 UTC (permalink / raw)
To: Patrick Palka; +Cc: gcc-patches, jason
On Mon, 11 Sep 2023, Patrick Palka wrote:
> On Thu, 1 Jun 2023, Patrick Palka wrote:
>
> > For a complex alias template-id, dependent_alias_template_spec_p returns
> > true if any template argument of the template-id is dependent. This
> > predicate indicates that substitution into the template-id may behave
> > differently with respect to SFINAE than substitution into the expanded
> > alias, and so the alias is in a way non-transparent. For example
> > 'first_t<T, T&>' in
> >
> > template<class T, class...> using first_t = T;
> > template<class T> first_t<T, T&> f();
> >
> > is such an alias template-id since first_t doesn't use its second
> > template parameter and so the substitution into the expanded alias would
> > discard the SFINAE effects of the corresponding (dependent) argument 'T&'.
> >
> > But this predicate is overly conservative since what really matters for
> > sake of SFINAE equivalence is whether a template argument corresponding
> > to an _unused_ template parameter is dependent. So the predicate should
> > return false for e.g. 'first_t<T, int>' or 'first_t<T>'.
> >
> > This patch refines the predicate appropriately. We need to be able to
> > efficiently determine which template parameters of a complex alias
> > template are unused, so to that end we add a new out parameter to
> > complex_alias_template_p and cache its result in an on-the-side
> > hash_map that replaces the existing TEMPLATE_DECL_COMPLEX_ALIAS_P
> > flag. And in doing so, we fix a latent bug that this flag wasn't
> > being propagated during partial instantiation, and so we were treating
> > all partially instantiated member alias templates as non-complex.
> >
> > PR c++/90679
> >
> > gcc/cp/ChangeLog:
> >
> > * cp-tree.h (TEMPLATE_DECL_COMPLEX_ALIAS_P): Remove.
> > (most_general_template): Constify parameter.
> > * pt.cc (push_template_decl): Adjust after removing
> > TEMPLATE_DECL_COMPLEX_ALIAS_P.
> > (complex_alias_tmpl_info): New hash_map.
> > (uses_all_template_parms_data::seen): Change type to
> > tree* from bool*.
> > (complex_alias_template_r): Adjust accordingly.
> > (complex_alias_template_p): Add 'seen_out' out parameter.
> > Call most_general_template and check PRIMARY_TEMPLATE_P.
> > Use complex_alias_tmpl_info to cache the result and set
> > '*seen_out' accordigly.
> > (dependent_alias_template_spec_p): Add !processing_template_decl
> > early exit test. Consider dependence of only template arguments
> > corresponding to seen template parameters as per
> >
> > gcc/testsuite/ChangeLog:
> >
> > * g++.dg/cpp0x/alias-decl-75.C: New test.
>
> Ping.
Ping. Here's a rebased patch:
-- >8 --
Subject: [PATCH 1/2] c++: refine dependent_alias_template_spec_p [PR90679]
For a (complex) alias template-id, dependent_alias_template_spec_p
returns true if any template argument of the template-id is dependent.
This predicate indicates that substitution into the template-id may
behave differently with respect to SFINAE than substitution into the
expanded alias, and so the alias is in a way non-transparent.
For example, 'first_t<T, T&>' in
template<class T, class...> using first_t = T;
template<class T> first_t<T, T&> f();
is such an alias template-id since first_t doesn't use its second
template parameter and so the substitution into the expanded alias would
discard the SFINAE effects of the corresponding (dependent) argument 'T&'.
But this predicate is overly conservative since what really matters for
sake of SFINAE equivalence is whether a template argument corresponding
to an _unused_ template parameter is dependent. So the predicate should
return false for e.g. 'first_t<T&, int>'.
This patch refines the predicate appropriately. We need to be able to
efficiently determine which template parameters of a complex alias
template are unused, so to that end we add a new out parameter to
complex_alias_template_p and cache its result in an on-the-side
hash_map that replaces the existing TEMPLATE_DECL_COMPLEX_ALIAS_P
flag.
PR c++/90679
gcc/cp/ChangeLog:
* cp-tree.h (TEMPLATE_DECL_COMPLEX_ALIAS_P): Remove.
(most_general_template): Constify parameter.
* pt.cc (push_template_decl): Adjust after removing
TEMPLATE_DECL_COMPLEX_ALIAS_P.
(complex_alias_tmpl_info): New hash_map.
(uses_all_template_parms_data::seen): Change type to
tree* from bool*.
(complex_alias_template_r): Adjust accordingly.
(complex_alias_template_p): Add 'seen_out' out parameter.
Call most_general_template and check PRIMARY_TEMPLATE_P.
Use complex_alias_tmpl_info to cache the result and set
'*seen_out' accordigly.
(dependent_alias_template_spec_p): Add !processing_template_decl
early exit test. Consider dependence of only template arguments
corresponding to seen template parameters as per
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/alias-decl-76.C: New test.
---
gcc/cp/cp-tree.h | 7 +-
gcc/cp/pt.cc | 104 +++++++++++++++------
gcc/testsuite/g++.dg/cpp0x/alias-decl-76.C | 24 +++++
3 files changed, 103 insertions(+), 32 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-76.C
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index f99f6cb26c4..85980c9ad9b 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -552,7 +552,6 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX];
2: DECL_THIS_EXTERN (in VAR_DECL, FUNCTION_DECL or PARM_DECL)
DECL_IMPLICIT_TYPEDEF_P (in a TYPE_DECL)
DECL_CONSTRAINT_VAR_P (in a PARM_DECL)
- TEMPLATE_DECL_COMPLEX_ALIAS_P (in TEMPLATE_DECL)
DECL_INSTANTIATING_NSDMI_P (in a FIELD_DECL)
USING_DECL_UNRELATED_P (in USING_DECL)
3: DECL_IN_AGGR_P.
@@ -3700,10 +3699,6 @@ struct GTY(()) lang_decl {
#define TYPE_DECL_ALIAS_P(NODE) \
DECL_LANG_FLAG_6 (TYPE_DECL_CHECK (NODE))
-/* Nonzero for TEMPLATE_DECL means that it is a 'complex' alias template. */
-#define TEMPLATE_DECL_COMPLEX_ALIAS_P(NODE) \
- DECL_LANG_FLAG_2 (TEMPLATE_DECL_CHECK (NODE))
-
/* Nonzero for a type which is an alias for another type; i.e, a type
which declaration was written 'using name-of-type =
another-type'. */
@@ -7528,7 +7523,7 @@ extern tree tsubst_argument_pack (tree, tree, tsubst_flags_t, tree);
extern tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
extern tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
extern tree tsubst_function_parms (tree, tree, tsubst_flags_t, tree);
-extern tree most_general_template (tree);
+extern tree most_general_template (const_tree);
extern tree get_mostly_instantiated_function_type (tree);
extern bool problematic_instantiation_changed (void);
extern void record_last_problematic_instantiation (void);
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index a82d7ae93aa..a3a79713236 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -213,7 +213,6 @@ static tree listify (tree);
static tree listify_autos (tree, tree);
static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
-static bool complex_alias_template_p (const_tree tmpl);
static tree get_underlying_template (tree);
static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
static tree canonicalize_expr_argument (tree, tsubst_flags_t);
@@ -6208,8 +6207,6 @@ push_template_decl (tree decl, bool is_friend)
constr = build_constraints (constr, NULL_TREE);
set_constraints (decl, constr);
}
- if (complex_alias_template_p (tmpl))
- TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
}
}
@@ -6499,12 +6496,16 @@ alias_template_specialization_p (const_tree t,
return NULL_TREE;
}
+/* A cache of the result of complex_alias_template_p. */
+
+static GTY((deletable)) hash_map<const_tree, tree> *complex_alias_tmpl_info;
+
/* Data structure for complex_alias_template_*. */
struct uses_all_template_parms_data
{
int level;
- bool *seen;
+ tree *seen;
};
/* walk_tree callback for complex_alias_template_p. */
@@ -6524,7 +6525,7 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
{
tree idx = get_template_parm_index (t);
if (TEMPLATE_PARM_LEVEL (idx) == data.level)
- data.seen[TEMPLATE_PARM_IDX (idx)] = true;
+ data.seen[TEMPLATE_PARM_IDX (idx)] = boolean_true_node;
}
default:;
@@ -6549,7 +6550,7 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
return t;
/* Consider the expanded packs to be used outside the expansion... */
- data.seen[idx] = true;
+ data.seen[idx] = boolean_true_node;
}
/* ...but don't walk into the pattern. Consider PR104008:
@@ -6571,12 +6572,17 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
using that alias can be ill-formed when the expansion is not, as with
the void_t template.
- Returns 1 if always complex, 0 if not complex, -1 if complex iff any of the
- template arguments are empty packs. */
+ If this predicate returns true in the ordinary case, the out parameter
+ SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if
+ the I'th template parameter of the alias template is used in the alias. */
static bool
-complex_alias_template_p (const_tree tmpl)
+complex_alias_template_p (const_tree tmpl, tree *seen_out)
{
+ tmpl = most_general_template (tmpl);
+ if (!PRIMARY_TEMPLATE_P (tmpl))
+ return false;
+
/* A renaming alias isn't complex. */
if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
return false;
@@ -6585,26 +6591,55 @@ complex_alias_template_p (const_tree tmpl)
if (get_constraints (tmpl))
return true;
+ if (!complex_alias_tmpl_info)
+ complex_alias_tmpl_info = hash_map<const_tree, tree>::create_ggc (13);
+
+ if (tree *slot = complex_alias_tmpl_info->get (tmpl))
+ {
+ tree result = *slot;
+ if (result == boolean_false_node)
+ return false;
+ if (result == boolean_true_node)
+ return true;
+ gcc_assert (TREE_CODE (result) == TREE_VEC);
+ if (seen_out)
+ *seen_out = result;
+ return true;
+ }
+
struct uses_all_template_parms_data data;
tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
tree parms = DECL_TEMPLATE_PARMS (tmpl);
data.level = TMPL_PARMS_DEPTH (parms);
int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
- data.seen = XALLOCAVEC (bool, len);
+ tree seen = make_tree_vec (len);
+ data.seen = TREE_VEC_BEGIN (seen);
for (int i = 0; i < len; ++i)
- data.seen[i] = false;
+ data.seen[i] = boolean_false_node;
if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data))
- return true;
- for (int i = 0; i < len; ++i)
- if (!data.seen[i])
+ {
+ complex_alias_tmpl_info->put (tmpl, boolean_true_node);
return true;
+ }
+
+ for (int i = 0; i < len; ++i)
+ if (data.seen[i] != boolean_true_node)
+ {
+ complex_alias_tmpl_info->put (tmpl, seen);
+ if (seen_out)
+ *seen_out = seen;
+ return true;
+ }
+
+ complex_alias_tmpl_info->put (tmpl, boolean_false_node);
return false;
}
-/* If T is a specialization of a complex alias template with dependent
- template-arguments, return it; otherwise return NULL_TREE. If T is a
- typedef to such a specialization, return the specialization. */
+/* If T is a specialization of a complex alias template with a dependent
+ argument for an unused template parameter, return it; otherwise return
+ NULL_TREE. If T is a typedef to such a specialization, return the
+ specialization. */
tree
dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
@@ -6613,15 +6648,32 @@ dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
return NULL_TREE;
gcc_assert (TYPE_P (t));
- if (!typedef_variant_p (t))
+ if (!processing_template_decl || !typedef_variant_p (t))
return NULL_TREE;
- tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
- if (tinfo
- && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
- && (any_dependent_template_arguments_p
- (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
- return CONST_CAST_TREE (t);
+ if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
+ {
+ tree seen = NULL_TREE;
+ if (complex_alias_template_p (TI_TEMPLATE (tinfo), &seen))
+ {
+ tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
+ if (!seen)
+ {
+ if (any_dependent_template_arguments_p (args))
+ return CONST_CAST_TREE (t);
+ }
+ else
+ {
+ gcc_assert (TREE_VEC_LENGTH (args) == TREE_VEC_LENGTH (seen));
+ for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
+ if (TREE_VEC_ELT (seen, i) != boolean_true_node
+ && dependent_template_arg_p (TREE_VEC_ELT (args, i)))
+ return CONST_CAST_TREE (t);
+ }
+
+ return NULL_TREE;
+ }
+ }
if (transparent_typedefs)
{
@@ -25817,7 +25869,7 @@ most_specialized_instantiation (tree templates)
`template <class T> template <class U> S<T*>::f(U)'. */
tree
-most_general_template (tree decl)
+most_general_template (const_tree decl)
{
if (TREE_CODE (decl) != TEMPLATE_DECL)
{
@@ -25854,7 +25906,7 @@ most_general_template (tree decl)
decl = DECL_TI_TEMPLATE (decl);
}
- return decl;
+ return CONST_CAST_TREE (decl);
}
/* Return the most specialized of the template partial specializations
diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-76.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-76.C
new file mode 100644
index 00000000000..10592f521a0
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-76.C
@@ -0,0 +1,24 @@
+// PR c++/90679
+// { dg-do compile { target c++11 } }
+
+template<class T, class...>
+using first_t = T;
+
+template<class T>
+struct A;
+
+template<class T>
+struct traits;
+
+template<class T>
+struct traits<A<first_t<T>>> {
+ static constexpr int value = 1;
+};
+
+template<class T>
+struct traits<A<first_t<const T>>> {
+ static constexpr int value = 2;
+};
+
+static_assert(traits<A<int>>::value == 1, "");
+static_assert(traits<A<const int>>::value == 2, ""); // { dg-bogus "ambiguous" }
--
2.43.0.76.g1a87c842ec
-- >8 --
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 2/2] c++: partial ordering and dep alias tmpl specs [PR90679]
2023-06-01 14:49 ` [PATCH 2/2] c++: partial ordering and dep alias tmpl specs [PR90679] Patrick Palka
2023-09-11 14:19 ` Patrick Palka
@ 2023-12-15 19:07 ` Patrick Palka
2023-12-18 21:57 ` Jason Merrill
1 sibling, 1 reply; 9+ messages in thread
From: Patrick Palka @ 2023-12-15 19:07 UTC (permalink / raw)
To: Patrick Palka; +Cc: gcc-patches, jason
On Thu, 1 Jun 2023, Patrick Palka wrote:
> During partial ordering, we want to look through dependent alias
> template specializations within template arguments and otherwise
> treat them as opaque in other contexts (see e.g. r7-7116-g0c942f3edab108
> and r11-7011-g6e0a231a4aa240). To that end template_args_equal was
> given a partial_order flag that controls this behavior. This flag
> does the right thing when a dependent alias template specialization
> appears as template argument of the partial specialization, e.g. in
>
> template<class T, class...> using first_t = T;
> template<class T> struct traits;
> template<class T> struct traits<first_t<T, T&>> { }; // #1
> template<class T> struct traits<first_t<const T, T&>> { }; // #2
>
> we correctly consider #2 to be more specialized than #1. But if
> the alias specialization appears as a template argument of another
> class template specialization, e.g. in
>
> template<class T> struct traits<A<first_t<T, T&>>> { }; // #1
> template<class T> struct traits<A<first_t<const T, T&>>> { }; // #2
>
> then we incorrectly consider #1 and #2 to be unordered. This is because
>
> 1. we don't propagate the flag to recursive template_args_equal calls
> 2. we don't use structural equality for class template specializations
> written in terms of dependent alias template specializations
>
> This patch fixes the first issue by turning the partial_order flag into
> a global. This patch fixes the second issue by making us propagate
> structural equality appropriately when building a class template
> specialization. In passing this patch also improves hashing of
> specializations that use structural equality.
>
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?
>
> PR c++/90679
>
> gcc/cp/ChangeLog:
>
> * cp-tree.h (comp_template_args): Remove partial_order
> parameter.
> (template_args_equal): Likewise.
> * pt.cc (iterative_hash_template_arg) <case tcc_type>: Hash
> the template and arguments for specializations that use
> structural equality.
> (comparing_for_partial_ordering): New flag.
> (template_args_equal): Remove partial order parameter and
> use comparing_for_partial_ordering instead.
> (comp_template_args): Likewise.
> (comp_template_args_porder): Set comparing_for_partial_ordering
> instead. Make static.
> (any_template_arguments_need_structural_equality_p): Return true
> for an argument that's a dependent alias template specialization
> or a class template specialization that itself needs structural
> equality.
> * tree.cc (cp_tree_equal) <case TREE_VEC>: Adjust call to
> comp_template_args.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp0x/alias-decl-75a.C: New test.
> * g++.dg/cpp0x/alias-decl-75b.C: New test.
Ping. Here's the rebased patch:
-- >8 --
Subject: [PATCH 2/2] c++: partial ordering and dep alias tmpl specs [PR90679]
During partial ordering, we want to look through dependent alias
template specializations within template arguments and otherwise
treat them as opaque in other contexts (see e.g. r7-7116-g0c942f3edab108
and r11-7011-g6e0a231a4aa240). To that end template_args_equal was
given a partial_order flag that controls this behavior. This flag
does the right thing when a dependent alias template specialization
appears as template argument of the partial specialization, e.g. in
template<class T, class...> using first_t = T;
template<class T> struct traits;
template<class T> struct traits<first_t<T, T&>> { }; // #1
template<class T> struct traits<first_t<const T, T&>> { }; // #2
we correctly consider #2 to be more specialized than #1. But if
the alias specialization appears as a template argument of another
class template specialization, e.g. in
template<class T> struct traits<A<first_t<T, T&>>> { }; // #1
template<class T> struct traits<A<first_t<const T, T&>>> { }; // #2
then we incorrectly consider #1 and #2 to be unordered. This is because
1. we don't propagate the flag to recursive template_args_equal calls
2. we don't use structural equality for class template specializations
written in terms of dependent alias template specializations
This patch fixes the first issue by turning the partial_order flag into
a global. This patch fixes the second issue by making us propagate
structural equality appropriately when building a class template
specialization. In passing this patch also improves hashing of
specializations that use structural equality.
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?
PR c++/90679
gcc/cp/ChangeLog:
* cp-tree.h (comp_template_args): Remove partial_order
parameter.
(template_args_equal): Likewise.
* pt.cc (iterative_hash_template_arg) <case tcc_type>: Hash
the template and arguments for specializations that use
structural equality.
(comparing_for_partial_ordering): New flag.
(template_args_equal): Remove partial order parameter and
use comparing_for_partial_ordering instead.
(comp_template_args): Likewise.
(comp_template_args_porder): Set comparing_for_partial_ordering
instead. Make static.
(any_template_arguments_need_structural_equality_p): Return true
for an argument that's a dependent alias template specialization
or a class template specialization that itself needs structural
equality.
* tree.cc (cp_tree_equal) <case TREE_VEC>: Adjust call to
comp_template_args.
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/alias-decl-75a.C: New test.
* g++.dg/cpp0x/alias-decl-75b.C: New test.
---
gcc/cp/cp-tree.h | 4 +--
gcc/cp/pt.cc | 40 +++++++++++++++++----
gcc/cp/tree.cc | 2 +-
gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C | 26 ++++++++++++++
gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C | 26 ++++++++++++++
5 files changed, 88 insertions(+), 10 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C
create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 85980c9ad9b..1979572c365 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7507,8 +7507,8 @@ extern int template_class_depth (tree);
extern int is_specialization_of (tree, tree);
extern bool is_specialization_of_friend (tree, tree);
extern bool comp_template_args (tree, tree, tree * = NULL,
- tree * = NULL, bool = false);
-extern int template_args_equal (tree, tree, bool = false);
+ tree * = NULL);
+extern int template_args_equal (tree, tree);
extern tree maybe_process_partial_specialization (tree);
extern tree most_specialized_instantiation (tree);
extern tree most_specialized_partial_spec (tree, tsubst_flags_t, bool = false);
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index a3a79713236..6b0ef496dc5 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -1894,6 +1894,11 @@ iterative_hash_template_arg (tree arg, hashval_t val)
default:
if (tree canonical = TYPE_CANONICAL (arg))
val = iterative_hash_object (TYPE_HASH (canonical), val);
+ else if (tree ti = TYPE_TEMPLATE_INFO (arg))
+ {
+ val = iterative_hash_template_arg (TI_TEMPLATE (ti), val);
+ val = iterative_hash_template_arg (TI_ARGS (ti), val);
+ }
break;
}
@@ -9306,6 +9311,12 @@ coerce_template_parms (tree parms,
return return_full_args ? new_args : new_inner_args;
}
+/* Whether we are comparing template arguments during partial ordering
+ (and therefore want the comparison to look through dependent alias
+ template specializations). */
+
+static int comparing_for_partial_ordering;
+
/* Returns true if T is a wrapper to make a C++20 template parameter
object const. */
@@ -9322,7 +9333,7 @@ class_nttp_const_wrapper_p (tree t)
/* Returns 1 if template args OT and NT are equivalent. */
int
-template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
+template_args_equal (tree ot, tree nt)
{
if (nt == ot)
return 1;
@@ -9345,7 +9356,7 @@ template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
During partial ordering, however, we need to treat them normally so we can
order uses of the same alias with different cv-qualification (79960). */
auto cso = make_temp_override (comparing_dependent_aliases);
- if (!partial_order)
+ if (!comparing_for_partial_ordering)
++comparing_dependent_aliases;
if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
@@ -9393,8 +9404,7 @@ template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
bool
comp_template_args (tree oldargs, tree newargs,
- tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */,
- bool partial_order /* = false */)
+ tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */)
{
if (oldargs == newargs)
return true;
@@ -9410,7 +9420,7 @@ comp_template_args (tree oldargs, tree newargs,
tree nt = TREE_VEC_ELT (newargs, i);
tree ot = TREE_VEC_ELT (oldargs, i);
- if (! template_args_equal (ot, nt, partial_order))
+ if (! template_args_equal (ot, nt))
{
if (oldarg_ptr != NULL)
*oldarg_ptr = ot;
@@ -9422,10 +9432,13 @@ comp_template_args (tree oldargs, tree newargs,
return true;
}
-inline bool
+static bool
comp_template_args_porder (tree oargs, tree nargs)
{
- return comp_template_args (oargs, nargs, NULL, NULL, true);
+ ++comparing_for_partial_ordering;
+ bool equal = comp_template_args (oargs, nargs);
+ --comparing_for_partial_ordering;
+ return equal;
}
/* Implement a freelist interface for objects of type T.
@@ -28727,6 +28740,19 @@ any_template_arguments_need_structural_equality_p (tree args)
mutated after the fact by duplicate_decls), so just require
structural equality in this case (PR52830). */
return true;
+ else if (TYPE_P (arg)
+ && TYPE_STRUCTURAL_EQUALITY_P (arg)
+ && dependent_alias_template_spec_p (arg, nt_transparent))
+ /* Require structural equality for specializations written
+ in terms of a dependent alias template specialization. */
+ return true;
+ else if (CLASS_TYPE_P (arg)
+ && TYPE_TEMPLATE_INFO (arg)
+ && TYPE_STRUCTURAL_EQUALITY_P (arg))
+ /* Require structural equality for specializations written
+ in terms of a class template specialization that itself
+ needs structural equality. */
+ return true;
}
}
}
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index d26e73aaf95..d17b9b34891 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -4131,7 +4131,7 @@ cp_tree_equal (tree t1, tree t2)
case TREE_VEC:
/* These are template args. Really we should be getting the
caller to do this as it knows it to be true. */
- if (!comp_template_args (t1, t2, NULL, NULL, false))
+ if (!comp_template_args (t1, t2))
return false;
return true;
diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C
new file mode 100644
index 00000000000..8adbd6f65ac
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C
@@ -0,0 +1,26 @@
+// PR c++/90679
+// A version of alias-decl-75.C where the specializations of the
+// complex alias template first_t are dependent.
+// { dg-do compile { target c++11 } }
+
+template<class T, class...>
+using first_t = T;
+
+template<class T>
+struct A;
+
+template<class T>
+struct traits;
+
+template<class T>
+struct traits<A<first_t<T, T&>>> {
+ static constexpr int value = 1;
+};
+
+template<class T>
+struct traits<A<first_t<const T, T&>>> {
+ static constexpr int value = 2;
+};
+
+static_assert(traits<A<int>>::value == 1, "");
+static_assert(traits<A<const int>>::value == 2, ""); // { dg-bogus "ambiguous" }
diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C
new file mode 100644
index 00000000000..b89ea5cad1e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C
@@ -0,0 +1,26 @@
+// PR c++/90679
+// A version of alias-decl-75a.C where the alias template specialization
+// appears as a more deeply nested template argument.
+// { dg-do compile { target c++11 } }
+
+template<class T, class...>
+using first_t = T;
+
+template<class T>
+struct A;
+
+template<class T>
+struct traits;
+
+template<class T>
+struct traits<A<A<first_t<T, T&>>>> {
+ static constexpr int value = 1;
+};
+
+template<class T>
+struct traits<A<A<first_t<const T, T&>>>> {
+ static constexpr int value = 2;
+};
+
+static_assert(traits<A<A<int>>>::value == 1, "");
+static_assert(traits<A<A<const int>>>::value == 2, ""); // { dg-bogus "ambiguous" }
--
2.43.0.76.g1a87c842ec
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 1/2] c++: refine dependent_alias_template_spec_p [PR90679]
2023-12-15 19:06 ` Patrick Palka
@ 2023-12-18 21:53 ` Jason Merrill
0 siblings, 0 replies; 9+ messages in thread
From: Jason Merrill @ 2023-12-18 21:53 UTC (permalink / raw)
To: Patrick Palka; +Cc: gcc-patches
On 12/15/23 14:06, Patrick Palka wrote:
> On Mon, 11 Sep 2023, Patrick Palka wrote:
>
>> On Thu, 1 Jun 2023, Patrick Palka wrote:
>>
>>> For a complex alias template-id, dependent_alias_template_spec_p returns
>>> true if any template argument of the template-id is dependent. This
>>> predicate indicates that substitution into the template-id may behave
>>> differently with respect to SFINAE than substitution into the expanded
>>> alias, and so the alias is in a way non-transparent. For example
>>> 'first_t<T, T&>' in
>>>
>>> template<class T, class...> using first_t = T;
>>> template<class T> first_t<T, T&> f();
>>>
>>> is such an alias template-id since first_t doesn't use its second
>>> template parameter and so the substitution into the expanded alias would
>>> discard the SFINAE effects of the corresponding (dependent) argument 'T&'.
>>>
>>> But this predicate is overly conservative since what really matters for
>>> sake of SFINAE equivalence is whether a template argument corresponding
>>> to an _unused_ template parameter is dependent. So the predicate should
>>> return false for e.g. 'first_t<T, int>' or 'first_t<T>'.
>>>
>>> This patch refines the predicate appropriately. We need to be able to
>>> efficiently determine which template parameters of a complex alias
>>> template are unused, so to that end we add a new out parameter to
>>> complex_alias_template_p and cache its result in an on-the-side
>>> hash_map that replaces the existing TEMPLATE_DECL_COMPLEX_ALIAS_P
>>> flag. And in doing so, we fix a latent bug that this flag wasn't
>>> being propagated during partial instantiation, and so we were treating
>>> all partially instantiated member alias templates as non-complex.
>>>
>>> PR c++/90679
>>>
>>> gcc/cp/ChangeLog:
>>>
>>> * cp-tree.h (TEMPLATE_DECL_COMPLEX_ALIAS_P): Remove.
>>> (most_general_template): Constify parameter.
>>> * pt.cc (push_template_decl): Adjust after removing
>>> TEMPLATE_DECL_COMPLEX_ALIAS_P.
>>> (complex_alias_tmpl_info): New hash_map.
>>> (uses_all_template_parms_data::seen): Change type to
>>> tree* from bool*.
>>> (complex_alias_template_r): Adjust accordingly.
>>> (complex_alias_template_p): Add 'seen_out' out parameter.
>>> Call most_general_template and check PRIMARY_TEMPLATE_P.
>>> Use complex_alias_tmpl_info to cache the result and set
>>> '*seen_out' accordigly.
>>> (dependent_alias_template_spec_p): Add !processing_template_decl
>>> early exit test. Consider dependence of only template arguments
>>> corresponding to seen template parameters as per
>>>
>>> gcc/testsuite/ChangeLog:
>>>
>>> * g++.dg/cpp0x/alias-decl-75.C: New test.
>>
>> Ping.
>
> Ping. Here's a rebased patch:
OK.
> -- >8 --
>
> Subject: [PATCH 1/2] c++: refine dependent_alias_template_spec_p [PR90679]
>
> For a (complex) alias template-id, dependent_alias_template_spec_p
> returns true if any template argument of the template-id is dependent.
> This predicate indicates that substitution into the template-id may
> behave differently with respect to SFINAE than substitution into the
> expanded alias, and so the alias is in a way non-transparent.
>
> For example, 'first_t<T, T&>' in
>
> template<class T, class...> using first_t = T;
> template<class T> first_t<T, T&> f();
>
> is such an alias template-id since first_t doesn't use its second
> template parameter and so the substitution into the expanded alias would
> discard the SFINAE effects of the corresponding (dependent) argument 'T&'.
>
> But this predicate is overly conservative since what really matters for
> sake of SFINAE equivalence is whether a template argument corresponding
> to an _unused_ template parameter is dependent. So the predicate should
> return false for e.g. 'first_t<T&, int>'.
>
> This patch refines the predicate appropriately. We need to be able to
> efficiently determine which template parameters of a complex alias
> template are unused, so to that end we add a new out parameter to
> complex_alias_template_p and cache its result in an on-the-side
> hash_map that replaces the existing TEMPLATE_DECL_COMPLEX_ALIAS_P
> flag.
>
> PR c++/90679
>
> gcc/cp/ChangeLog:
>
> * cp-tree.h (TEMPLATE_DECL_COMPLEX_ALIAS_P): Remove.
> (most_general_template): Constify parameter.
> * pt.cc (push_template_decl): Adjust after removing
> TEMPLATE_DECL_COMPLEX_ALIAS_P.
> (complex_alias_tmpl_info): New hash_map.
> (uses_all_template_parms_data::seen): Change type to
> tree* from bool*.
> (complex_alias_template_r): Adjust accordingly.
> (complex_alias_template_p): Add 'seen_out' out parameter.
> Call most_general_template and check PRIMARY_TEMPLATE_P.
> Use complex_alias_tmpl_info to cache the result and set
> '*seen_out' accordigly.
> (dependent_alias_template_spec_p): Add !processing_template_decl
> early exit test. Consider dependence of only template arguments
> corresponding to seen template parameters as per
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp0x/alias-decl-76.C: New test.
> ---
> gcc/cp/cp-tree.h | 7 +-
> gcc/cp/pt.cc | 104 +++++++++++++++------
> gcc/testsuite/g++.dg/cpp0x/alias-decl-76.C | 24 +++++
> 3 files changed, 103 insertions(+), 32 deletions(-)
> create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-76.C
>
> diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
> index f99f6cb26c4..85980c9ad9b 100644
> --- a/gcc/cp/cp-tree.h
> +++ b/gcc/cp/cp-tree.h
> @@ -552,7 +552,6 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX];
> 2: DECL_THIS_EXTERN (in VAR_DECL, FUNCTION_DECL or PARM_DECL)
> DECL_IMPLICIT_TYPEDEF_P (in a TYPE_DECL)
> DECL_CONSTRAINT_VAR_P (in a PARM_DECL)
> - TEMPLATE_DECL_COMPLEX_ALIAS_P (in TEMPLATE_DECL)
> DECL_INSTANTIATING_NSDMI_P (in a FIELD_DECL)
> USING_DECL_UNRELATED_P (in USING_DECL)
> 3: DECL_IN_AGGR_P.
> @@ -3700,10 +3699,6 @@ struct GTY(()) lang_decl {
> #define TYPE_DECL_ALIAS_P(NODE) \
> DECL_LANG_FLAG_6 (TYPE_DECL_CHECK (NODE))
>
> -/* Nonzero for TEMPLATE_DECL means that it is a 'complex' alias template. */
> -#define TEMPLATE_DECL_COMPLEX_ALIAS_P(NODE) \
> - DECL_LANG_FLAG_2 (TEMPLATE_DECL_CHECK (NODE))
> -
> /* Nonzero for a type which is an alias for another type; i.e, a type
> which declaration was written 'using name-of-type =
> another-type'. */
> @@ -7528,7 +7523,7 @@ extern tree tsubst_argument_pack (tree, tree, tsubst_flags_t, tree);
> extern tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
> extern tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
> extern tree tsubst_function_parms (tree, tree, tsubst_flags_t, tree);
> -extern tree most_general_template (tree);
> +extern tree most_general_template (const_tree);
> extern tree get_mostly_instantiated_function_type (tree);
> extern bool problematic_instantiation_changed (void);
> extern void record_last_problematic_instantiation (void);
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index a82d7ae93aa..a3a79713236 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -213,7 +213,6 @@ static tree listify (tree);
> static tree listify_autos (tree, tree);
> static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
> static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
> -static bool complex_alias_template_p (const_tree tmpl);
> static tree get_underlying_template (tree);
> static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
> static tree canonicalize_expr_argument (tree, tsubst_flags_t);
> @@ -6208,8 +6207,6 @@ push_template_decl (tree decl, bool is_friend)
> constr = build_constraints (constr, NULL_TREE);
> set_constraints (decl, constr);
> }
> - if (complex_alias_template_p (tmpl))
> - TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
> }
> }
>
> @@ -6499,12 +6496,16 @@ alias_template_specialization_p (const_tree t,
> return NULL_TREE;
> }
>
> +/* A cache of the result of complex_alias_template_p. */
> +
> +static GTY((deletable)) hash_map<const_tree, tree> *complex_alias_tmpl_info;
> +
> /* Data structure for complex_alias_template_*. */
>
> struct uses_all_template_parms_data
> {
> int level;
> - bool *seen;
> + tree *seen;
> };
>
> /* walk_tree callback for complex_alias_template_p. */
> @@ -6524,7 +6525,7 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
> {
> tree idx = get_template_parm_index (t);
> if (TEMPLATE_PARM_LEVEL (idx) == data.level)
> - data.seen[TEMPLATE_PARM_IDX (idx)] = true;
> + data.seen[TEMPLATE_PARM_IDX (idx)] = boolean_true_node;
> }
>
> default:;
> @@ -6549,7 +6550,7 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
> return t;
>
> /* Consider the expanded packs to be used outside the expansion... */
> - data.seen[idx] = true;
> + data.seen[idx] = boolean_true_node;
> }
>
> /* ...but don't walk into the pattern. Consider PR104008:
> @@ -6571,12 +6572,17 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
> using that alias can be ill-formed when the expansion is not, as with
> the void_t template.
>
> - Returns 1 if always complex, 0 if not complex, -1 if complex iff any of the
> - template arguments are empty packs. */
> + If this predicate returns true in the ordinary case, the out parameter
> + SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if
> + the I'th template parameter of the alias template is used in the alias. */
>
> static bool
> -complex_alias_template_p (const_tree tmpl)
> +complex_alias_template_p (const_tree tmpl, tree *seen_out)
> {
> + tmpl = most_general_template (tmpl);
> + if (!PRIMARY_TEMPLATE_P (tmpl))
> + return false;
> +
> /* A renaming alias isn't complex. */
> if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
> return false;
> @@ -6585,26 +6591,55 @@ complex_alias_template_p (const_tree tmpl)
> if (get_constraints (tmpl))
> return true;
>
> + if (!complex_alias_tmpl_info)
> + complex_alias_tmpl_info = hash_map<const_tree, tree>::create_ggc (13);
> +
> + if (tree *slot = complex_alias_tmpl_info->get (tmpl))
> + {
> + tree result = *slot;
> + if (result == boolean_false_node)
> + return false;
> + if (result == boolean_true_node)
> + return true;
> + gcc_assert (TREE_CODE (result) == TREE_VEC);
> + if (seen_out)
> + *seen_out = result;
> + return true;
> + }
> +
> struct uses_all_template_parms_data data;
> tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
> tree parms = DECL_TEMPLATE_PARMS (tmpl);
> data.level = TMPL_PARMS_DEPTH (parms);
> int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
> - data.seen = XALLOCAVEC (bool, len);
> + tree seen = make_tree_vec (len);
> + data.seen = TREE_VEC_BEGIN (seen);
> for (int i = 0; i < len; ++i)
> - data.seen[i] = false;
> + data.seen[i] = boolean_false_node;
>
> if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data))
> - return true;
> - for (int i = 0; i < len; ++i)
> - if (!data.seen[i])
> + {
> + complex_alias_tmpl_info->put (tmpl, boolean_true_node);
> return true;
> + }
> +
> + for (int i = 0; i < len; ++i)
> + if (data.seen[i] != boolean_true_node)
> + {
> + complex_alias_tmpl_info->put (tmpl, seen);
> + if (seen_out)
> + *seen_out = seen;
> + return true;
> + }
> +
> + complex_alias_tmpl_info->put (tmpl, boolean_false_node);
> return false;
> }
>
> -/* If T is a specialization of a complex alias template with dependent
> - template-arguments, return it; otherwise return NULL_TREE. If T is a
> - typedef to such a specialization, return the specialization. */
> +/* If T is a specialization of a complex alias template with a dependent
> + argument for an unused template parameter, return it; otherwise return
> + NULL_TREE. If T is a typedef to such a specialization, return the
> + specialization. */
>
> tree
> dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
> @@ -6613,15 +6648,32 @@ dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
> return NULL_TREE;
> gcc_assert (TYPE_P (t));
>
> - if (!typedef_variant_p (t))
> + if (!processing_template_decl || !typedef_variant_p (t))
> return NULL_TREE;
>
> - tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
> - if (tinfo
> - && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
> - && (any_dependent_template_arguments_p
> - (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
> - return CONST_CAST_TREE (t);
> + if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
> + {
> + tree seen = NULL_TREE;
> + if (complex_alias_template_p (TI_TEMPLATE (tinfo), &seen))
> + {
> + tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
> + if (!seen)
> + {
> + if (any_dependent_template_arguments_p (args))
> + return CONST_CAST_TREE (t);
> + }
> + else
> + {
> + gcc_assert (TREE_VEC_LENGTH (args) == TREE_VEC_LENGTH (seen));
> + for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
> + if (TREE_VEC_ELT (seen, i) != boolean_true_node
> + && dependent_template_arg_p (TREE_VEC_ELT (args, i)))
> + return CONST_CAST_TREE (t);
> + }
> +
> + return NULL_TREE;
> + }
> + }
>
> if (transparent_typedefs)
> {
> @@ -25817,7 +25869,7 @@ most_specialized_instantiation (tree templates)
> `template <class T> template <class U> S<T*>::f(U)'. */
>
> tree
> -most_general_template (tree decl)
> +most_general_template (const_tree decl)
> {
> if (TREE_CODE (decl) != TEMPLATE_DECL)
> {
> @@ -25854,7 +25906,7 @@ most_general_template (tree decl)
> decl = DECL_TI_TEMPLATE (decl);
> }
>
> - return decl;
> + return CONST_CAST_TREE (decl);
> }
>
> /* Return the most specialized of the template partial specializations
> diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-76.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-76.C
> new file mode 100644
> index 00000000000..10592f521a0
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-76.C
> @@ -0,0 +1,24 @@
> +// PR c++/90679
> +// { dg-do compile { target c++11 } }
> +
> +template<class T, class...>
> +using first_t = T;
> +
> +template<class T>
> +struct A;
> +
> +template<class T>
> +struct traits;
> +
> +template<class T>
> +struct traits<A<first_t<T>>> {
> + static constexpr int value = 1;
> +};
> +
> +template<class T>
> +struct traits<A<first_t<const T>>> {
> + static constexpr int value = 2;
> +};
> +
> +static_assert(traits<A<int>>::value == 1, "");
> +static_assert(traits<A<const int>>::value == 2, ""); // { dg-bogus "ambiguous" }
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 2/2] c++: partial ordering and dep alias tmpl specs [PR90679]
2023-12-15 19:07 ` Patrick Palka
@ 2023-12-18 21:57 ` Jason Merrill
0 siblings, 0 replies; 9+ messages in thread
From: Jason Merrill @ 2023-12-18 21:57 UTC (permalink / raw)
To: Patrick Palka; +Cc: gcc-patches
On 12/15/23 14:07, Patrick Palka wrote:
> On Thu, 1 Jun 2023, Patrick Palka wrote:
>
>> During partial ordering, we want to look through dependent alias
>> template specializations within template arguments and otherwise
>> treat them as opaque in other contexts (see e.g. r7-7116-g0c942f3edab108
>> and r11-7011-g6e0a231a4aa240). To that end template_args_equal was
>> given a partial_order flag that controls this behavior. This flag
>> does the right thing when a dependent alias template specialization
>> appears as template argument of the partial specialization, e.g. in
>>
>> template<class T, class...> using first_t = T;
>> template<class T> struct traits;
>> template<class T> struct traits<first_t<T, T&>> { }; // #1
>> template<class T> struct traits<first_t<const T, T&>> { }; // #2
>>
>> we correctly consider #2 to be more specialized than #1. But if
>> the alias specialization appears as a template argument of another
>> class template specialization, e.g. in
>>
>> template<class T> struct traits<A<first_t<T, T&>>> { }; // #1
>> template<class T> struct traits<A<first_t<const T, T&>>> { }; // #2
>>
>> then we incorrectly consider #1 and #2 to be unordered. This is because
>>
>> 1. we don't propagate the flag to recursive template_args_equal calls
>> 2. we don't use structural equality for class template specializations
>> written in terms of dependent alias template specializations
>>
>> This patch fixes the first issue by turning the partial_order flag into
>> a global. This patch fixes the second issue by making us propagate
>> structural equality appropriately when building a class template
>> specialization. In passing this patch also improves hashing of
>> specializations that use structural equality.
>>
>> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
>> trunk?
>>
>> PR c++/90679
>>
>> gcc/cp/ChangeLog:
>>
>> * cp-tree.h (comp_template_args): Remove partial_order
>> parameter.
>> (template_args_equal): Likewise.
>> * pt.cc (iterative_hash_template_arg) <case tcc_type>: Hash
>> the template and arguments for specializations that use
>> structural equality.
>> (comparing_for_partial_ordering): New flag.
>> (template_args_equal): Remove partial order parameter and
>> use comparing_for_partial_ordering instead.
>> (comp_template_args): Likewise.
>> (comp_template_args_porder): Set comparing_for_partial_ordering
>> instead. Make static.
>> (any_template_arguments_need_structural_equality_p): Return true
>> for an argument that's a dependent alias template specialization
>> or a class template specialization that itself needs structural
>> equality.
>> * tree.cc (cp_tree_equal) <case TREE_VEC>: Adjust call to
>> comp_template_args.
>>
>> gcc/testsuite/ChangeLog:
>>
>> * g++.dg/cpp0x/alias-decl-75a.C: New test.
>> * g++.dg/cpp0x/alias-decl-75b.C: New test.
>
> Ping. Here's the rebased patch:
>
> -- >8 --
>
> Subject: [PATCH 2/2] c++: partial ordering and dep alias tmpl specs [PR90679]
>
> During partial ordering, we want to look through dependent alias
> template specializations within template arguments and otherwise
> treat them as opaque in other contexts (see e.g. r7-7116-g0c942f3edab108
> and r11-7011-g6e0a231a4aa240). To that end template_args_equal was
> given a partial_order flag that controls this behavior. This flag
> does the right thing when a dependent alias template specialization
> appears as template argument of the partial specialization, e.g. in
>
> template<class T, class...> using first_t = T;
> template<class T> struct traits;
> template<class T> struct traits<first_t<T, T&>> { }; // #1
> template<class T> struct traits<first_t<const T, T&>> { }; // #2
>
> we correctly consider #2 to be more specialized than #1. But if
> the alias specialization appears as a template argument of another
> class template specialization, e.g. in
>
> template<class T> struct traits<A<first_t<T, T&>>> { }; // #1
> template<class T> struct traits<A<first_t<const T, T&>>> { }; // #2
>
> then we incorrectly consider #1 and #2 to be unordered. This is because
>
> 1. we don't propagate the flag to recursive template_args_equal calls
> 2. we don't use structural equality for class template specializations
> written in terms of dependent alias template specializations
>
> This patch fixes the first issue by turning the partial_order flag into
> a global. This patch fixes the second issue by making us propagate
> structural equality appropriately when building a class template
> specialization. In passing this patch also improves hashing of
> specializations that use structural equality.
>
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?
>
> PR c++/90679
>
> gcc/cp/ChangeLog:
>
> * cp-tree.h (comp_template_args): Remove partial_order
> parameter.
> (template_args_equal): Likewise.
> * pt.cc (iterative_hash_template_arg) <case tcc_type>: Hash
> the template and arguments for specializations that use
> structural equality.
> (comparing_for_partial_ordering): New flag.
> (template_args_equal): Remove partial order parameter and
> use comparing_for_partial_ordering instead.
> (comp_template_args): Likewise.
> (comp_template_args_porder): Set comparing_for_partial_ordering
> instead. Make static.
> (any_template_arguments_need_structural_equality_p): Return true
> for an argument that's a dependent alias template specialization
> or a class template specialization that itself needs structural
> equality.
> * tree.cc (cp_tree_equal) <case TREE_VEC>: Adjust call to
> comp_template_args.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp0x/alias-decl-75a.C: New test.
> * g++.dg/cpp0x/alias-decl-75b.C: New test.
> ---
> gcc/cp/cp-tree.h | 4 +--
> gcc/cp/pt.cc | 40 +++++++++++++++++----
> gcc/cp/tree.cc | 2 +-
> gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C | 26 ++++++++++++++
> gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C | 26 ++++++++++++++
> 5 files changed, 88 insertions(+), 10 deletions(-)
> create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C
> create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C
>
> diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
> index 85980c9ad9b..1979572c365 100644
> --- a/gcc/cp/cp-tree.h
> +++ b/gcc/cp/cp-tree.h
> @@ -7507,8 +7507,8 @@ extern int template_class_depth (tree);
> extern int is_specialization_of (tree, tree);
> extern bool is_specialization_of_friend (tree, tree);
> extern bool comp_template_args (tree, tree, tree * = NULL,
> - tree * = NULL, bool = false);
> -extern int template_args_equal (tree, tree, bool = false);
> + tree * = NULL);
> +extern int template_args_equal (tree, tree);
> extern tree maybe_process_partial_specialization (tree);
> extern tree most_specialized_instantiation (tree);
> extern tree most_specialized_partial_spec (tree, tsubst_flags_t, bool = false);
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index a3a79713236..6b0ef496dc5 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -1894,6 +1894,11 @@ iterative_hash_template_arg (tree arg, hashval_t val)
> default:
> if (tree canonical = TYPE_CANONICAL (arg))
> val = iterative_hash_object (TYPE_HASH (canonical), val);
> + else if (tree ti = TYPE_TEMPLATE_INFO (arg))
> + {
> + val = iterative_hash_template_arg (TI_TEMPLATE (ti), val);
> + val = iterative_hash_template_arg (TI_ARGS (ti), val);
> + }
> break;
> }
>
> @@ -9306,6 +9311,12 @@ coerce_template_parms (tree parms,
> return return_full_args ? new_args : new_inner_args;
> }
>
> +/* Whether we are comparing template arguments during partial ordering
> + (and therefore want the comparison to look through dependent alias
> + template specializations). */
> +
> +static int comparing_for_partial_ordering;
How about putting this next to comparing_dependent_aliases? OK with
that change.
Jason
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2023-12-18 21:57 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-01 14:49 [PATCH 1/2] c++: refine dependent_alias_template_spec_p [PR90679] Patrick Palka
2023-06-01 14:49 ` [PATCH 2/2] c++: partial ordering and dep alias tmpl specs [PR90679] Patrick Palka
2023-09-11 14:19 ` Patrick Palka
2023-12-15 19:07 ` Patrick Palka
2023-12-18 21:57 ` Jason Merrill
2023-06-02 16:20 ` [PATCH 1/2] c++: refine dependent_alias_template_spec_p [PR90679] Patrick Palka
2023-09-11 14:19 ` Patrick Palka
2023-12-15 19:06 ` Patrick Palka
2023-12-18 21:53 ` Jason Merrill
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).