From: Jason Merrill Date: Fri, 12 Jun 2020 03:58:54 +0000 (-0400) Subject: c++: Refinements to "more constrained". X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=57b4daf8dc4ed7b669cc70638866ddb00f5b7746;p=gcc.git c++: Refinements to "more constrained". P2113 from the last C++ meeting clarified that we only compare constraints on functions or function templates that have equivalent template parameters and function parameters. I'm not currently implementing the complicated handling of reversed comparison operators here; thinking about it now, it seems like a lot of complexity to support a very weird usage. If I write two similar comparison operators to be distinguished by their constraints, why would I write one reversed? If they're two unrelated operators, they're very unlikely to be similar enough for the complexity to help. I've started a discussion on the committee reflector about changing these rules. This change breaks some greedy_ops tests in libstdc++ that were relying on comparing constraints on unrelated templates, which seems pretty clearly wrong, so I'm removing those tests for now. gcc/cp/ChangeLog: * call.c (joust): Only compare constraints for non-template candidates with matching parameters. * pt.c (tsubst_pack_expansion): Fix getting a type parameter pack. (more_specialized_fn): Only compare constraints for candidates with matching parameters. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-return-req1.C: Expect error. * g++.dg/cpp2a/concepts-p2113a.C: New test. * g++.dg/cpp2a/concepts-p2113b.C: New test. libstdc++-v3/ChangeLog: * testsuite/24_iterators/move_iterator/rel_ops_c++20.cc: Remove greedy_ops tests. * testsuite/24_iterators/reverse_iterator/rel_ops_c++20.cc: Remove greedy_ops tests. --- diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 5382b7620dc..2b39a3700fc 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -11435,12 +11435,13 @@ joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn, return winner; } - /* Concepts: ... or, if not that, F1 is more constrained than F2. + /* Concepts: F1 and F2 are non-template functions with the same + parameter-type-lists, and F1 is more constrained than F2 according to the + partial ordering of constraints described in 13.5.4. */ - FIXME: For function templates with no winner, this subsumption may - be computed a separate time. This needs to be validated, and if - so, the redundant check removed. */ - if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn)) + if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn) + && !cand1->template_decl && !cand2->template_decl + && cand_parms_match (cand1, cand2)) { winner = more_constrained (cand1->fn, cand2->fn); if (winner) diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 1c0759edcae..efaadf73772 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -12849,6 +12849,10 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, template_parm_level_and_index (parm_pack, &level, &idx); if (level <= levels) arg_pack = TMPL_ARG (args, level, idx); + + if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM + && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack)) + arg_pack = NULL_TREE; } orig_arg = arg_pack; @@ -24061,7 +24065,15 @@ more_specialized_fn (tree pat1, tree pat2, int len) /* If both deductions succeed, the partial ordering selects the more constrained template. */ - if (!lose1 && !lose2) + /* P2113: If the corresponding template-parameters of the + template-parameter-lists are not equivalent ([temp.over.link]) or if + the function parameters that positionally correspond between the two + templates are not of the same type, neither template is more + specialized than the other. */ + if (!lose1 && !lose2 + && comp_template_parms (DECL_TEMPLATE_PARMS (pat1), + DECL_TEMPLATE_PARMS (pat2)) + && compparms (origs1, origs2)) { int winner = more_constrained (decl1, decl2); if (winner > 0) diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-p2113a.C b/gcc/testsuite/g++.dg/cpp2a/concepts-p2113a.C new file mode 100644 index 00000000000..e2652dd12f3 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-p2113a.C @@ -0,0 +1,12 @@ +// Test that the second foo is not considered more specialized because we don't +// compare constraints unless the template parameters and function parameters +// are equivalent (P2113) + +// { dg-do compile { target c++20 } } + +template concept P = true; + +template void foo(int, T); +template

void foo(U, int); + +void bar() { foo(1,2); } // { dg-error "ambiguous" } diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-p2113b.C b/gcc/testsuite/g++.dg/cpp2a/concepts-p2113b.C new file mode 100644 index 00000000000..18c4098d473 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-p2113b.C @@ -0,0 +1,25 @@ +// testcase from P2113 +// { dg-do compile { target c++20 } } + +template constexpr bool True = true; +template concept C = True; + +void f(C auto &, auto &) = delete; +template void f(Q &, C auto &); + +void g(struct A *ap, struct B *bp) { + f(*ap, *bp); // OK: Can use different methods to produce template parameters +} + +template struct X {}; + +template +bool operator==(X, V1) = delete; + +// In P2113 this candidate is reversed. +template +bool operator==(X, V2); + +void h() { + X{} == 0; // OK +} diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-return-req1.C b/gcc/testsuite/g++.dg/cpp2a/concepts-return-req1.C index 84c9ae9d6da..d21a49be14e 100644 --- a/gcc/testsuite/g++.dg/cpp2a/concepts-return-req1.C +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-return-req1.C @@ -15,5 +15,5 @@ int f(...); int main() { - f(); + f(); // { dg-error "ambiguous" } } diff --git a/libstdc++-v3/testsuite/24_iterators/move_iterator/rel_ops_c++20.cc b/libstdc++-v3/testsuite/24_iterators/move_iterator/rel_ops_c++20.cc index a530923f8ad..b298a312aae 100644 --- a/libstdc++-v3/testsuite/24_iterators/move_iterator/rel_ops_c++20.cc +++ b/libstdc++-v3/testsuite/24_iterators/move_iterator/rel_ops_c++20.cc @@ -142,22 +142,3 @@ static_assert( cend > beg ); static_assert( beg <= cend ); static_assert( cend >= beg ); static_assert( std::is_lt(beg <=> cend) ); - -#include - -void test01() -{ - typedef std::move_iterator iterator_type; - - iterator_type it(nullptr); - - it == it; - it != it; - it < it; - it <= it; - it > it; - it >= it; - // it - it; // See PR libstdc++/71771 - 1 + it; - it + 1; -} diff --git a/libstdc++-v3/testsuite/24_iterators/reverse_iterator/rel_ops_c++20.cc b/libstdc++-v3/testsuite/24_iterators/reverse_iterator/rel_ops_c++20.cc index a382ae52483..e513efba57f 100644 --- a/libstdc++-v3/testsuite/24_iterators/reverse_iterator/rel_ops_c++20.cc +++ b/libstdc++-v3/testsuite/24_iterators/reverse_iterator/rel_ops_c++20.cc @@ -169,25 +169,3 @@ static_assert( crend > rbeg ); static_assert( rbeg <= crend ); static_assert( crend >= rbeg ); static_assert( std::is_lt(rbeg <=> crend) ); - -#include - -// copied from 24_iterators/reverse_iterator/greedy_ops.cc -void test01() -{ - typedef std::reverse_iterator iterator_type; - - iterator_type it; - - it == it; - it != it; - it < it; - it <= it; - it > it; - it >= it; -#if __cplusplus < 201103L - it - it; // See PR libstdc++/71771 -#endif - 1 + it; - it + 1; -}