Here the template context for the atomic constraint has two levels of
template arguments, but since it depends only on the innermost argument
T we use a single-level argument vector during substitution into the
constraint (built by get_mapped_args).  We eventually pass this vector
to do_auto_deduction as part of checking the return-type-requirement
inside the atom, but do_auto_deduction expects outer_targs to be a full
set of arguments for sake of satisfaction.

do_auto_deduction has a workaround in place to compensate for callers
that pass only the innermost arguments as outer_targs, but here we're
passing the _outermost_ arguments.  Since the former situation should
now (after r12-7101) only occur with adc_unify callers and the latter
only with adc_requirement callers, this patch conditions the existing
workaround according to the auto_deduction_context: if the context is
adc_requirement, we add dummy innermost levels, otherwise we add dummy
outermost levels as before and also assert that the context is adc_unify.

Bootstrapped and regtested on x86_64-pc-linux-gnu and tested on cmcstl2
and range-v3, does this look OK for trunk?

        PR c++/104527

gcc/cp/ChangeLog:

        * pt.cc (do_auto_deduction): When template argument levels are
        missing from outer_targs, fill in the innermost rather than the
        outermost levels with dummy args if the context is
        adc_requirement, otherwise also assert that the context is
        adc_unify.

gcc/testsuite/ChangeLog:

        * g++.dg/cpp2a/concepts-return-req4.C: New test.
---
 gcc/cp/pt.cc                                  | 28 +++++++++++++------
 .../g++.dg/cpp2a/concepts-return-req4.C       | 24 ++++++++++++++++
 2 files changed, 44 insertions(+), 8 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-return-req4.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 1b18e2a7787..4ff2710b8ba 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -30215,20 +30215,32 @@ do_auto_deduction (tree type, tree init, tree 
auto_node,
 
       tree full_targs = add_to_template_args (outer_targs, targs);
 
-      /* HACK: Compensate for callers not always communicating all levels of
-        outer template arguments by filling in the outermost missing levels
-        with dummy levels before checking satisfaction.  We'll still crash
-        if the constraint depends on a template argument belonging to one of
-        these missing levels, but this hack otherwise allows us to handle a
-        large subset of possible constraints (including all non-dependent
-        constraints).  */
       if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node)
                                - TMPL_ARGS_DEPTH (full_targs)))
        {
          tree dummy_levels = make_tree_vec (missing_levels);
          for (int i = 0; i < missing_levels; ++i)
            TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
-         full_targs = add_to_template_args (dummy_levels, full_targs);
+         if (context == adc_requirement)
+           /* We're checking a requires-expr's return-type-requirement that's
+              part of an atomic constraint that doesn't depend on any innermost
+              template arguments, so OUTER_TARGS (built by get_mapped_args) is
+              missing at least one innermost level.  Fill in the innermost
+              levels of OUTER_TARGS with dummy levels.  */
+           full_targs = add_to_template_args
+             (add_to_template_args (outer_targs, dummy_levels), targs);
+         else
+           {
+             /* Otherwise, fill in the _outermost_ levels with dummy levels.
+                This compensates for adc_unify callers that only pass the
+                innermost level of template arguments as OUTER_TARGS.  We'll
+                still crash if the constraint depends on a template argument
+                belonging to one of these missing levels, but this hack
+                otherwise allows us to handle a large subset of possible
+                constraints (including all non-dependent constraints).  */
+             gcc_checking_assert (context == adc_unify);
+             full_targs = add_to_template_args (dummy_levels, full_targs);
+           }
        }
 
       if (!constraints_satisfied_p (auto_node, full_targs))
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-return-req4.C 
b/gcc/testsuite/g++.dg/cpp2a/concepts-return-req4.C
new file mode 100644
index 00000000000..471946bc8eb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-return-req4.C
@@ -0,0 +1,24 @@
+// PR c++/104527
+// { dg-do compile { target c++20 } }
+
+template<class T, class U>
+concept is_same = __is_same(T, U);
+
+template<class T>
+struct A {
+  template<class...>
+    requires requires { { 0 } -> is_same<T>; }
+  struct B {};
+
+  template<class...>
+    requires requires { { 1 } -> is_same<T>; }
+  static void f();
+};
+
+A<int>::B<> a1;
+A<bool>::B<> a2; // { dg-error "constraint" }
+
+int main() {
+  A<int>::f();
+  A<bool>::f(); // { dg-error "no match" }
+}
-- 
2.35.1.102.g2b9c120970

Reply via email to