]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: deduction guide using alias [PR99180]
authorJason Merrill <jason@redhat.com>
Fri, 9 Apr 2021 22:02:38 +0000 (18:02 -0400)
committerJason Merrill <jason@redhat.com>
Sat, 10 Apr 2021 04:06:55 +0000 (00:06 -0400)
alias_ctad_tweaks was expecting that all deduction guides for the class
would be suitable for deduction from the alias definition; in this case, the
deduction guide uses 'true' and the alias B uses 'false', so deduction
fails.  But that's OK, we just don't use that deduction guide.  I also
noticed that we were giving up on deduction entirely if substitution failed
for some guide; we should only give up on that particular deduction guide.

We ought to give a better diagnostic about this case when deduction fails,
but that can wait.

gcc/cp/ChangeLog:

PR c++/99180
PR c++/93295
PR c++/93867
PR c++/99118
PR c++/96873
* pt.c (alias_ctad_tweaks): Handle failure better.

gcc/testsuite/ChangeLog:

PR c++/99180
PR c++/93295
PR c++/93867
PR c++/95486
* g++.dg/cpp2a/class-deduction-alias5.C: New test.
* g++.dg/cpp2a/class-deduction-alias6.C: New test.
* g++.dg/cpp2a/class-deduction-alias7.C: New test.
* g++.dg/cpp2a/class-deduction-alias8.C: New test.

gcc/cp/pt.c
gcc/testsuite/g++.dg/cpp2a/class-deduction-alias5.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/class-deduction-alias6.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/class-deduction-alias7.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/class-deduction-alias8.C [new file with mode: 0644]

index b328330038a421a75a45ba9049a5ecc83af4009e..abd1ad4d1a64e7e6808b90897327348aa27470a2 100644 (file)
@@ -29042,7 +29042,8 @@ alias_ctad_tweaks (tree tmpl, tree uguides)
          unsigned len = TREE_VEC_LENGTH (ftparms);
          tree targs = make_tree_vec (len);
          int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
-         gcc_assert (!err);
+         if (err)
+           continue;
 
          /* The number of parms for f' is the number of parms for A plus
             non-deduced parms of f.  */
@@ -29075,7 +29076,7 @@ alias_ctad_tweaks (tree tmpl, tree uguides)
             guideness, and explicit-specifier.  */
          tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
          if (g == error_mark_node)
-           return error_mark_node;
+           continue;
          DECL_USE_TEMPLATE (g) = 0;
          fprime = build_template_decl (g, gtparms, false);
          DECL_TEMPLATE_RESULT (fprime) = g;
@@ -29089,7 +29090,7 @@ alias_ctad_tweaks (tree tmpl, tree uguides)
          if (ci)
            ci = tsubst_constraint_info (ci, targs, complain, in_decl);
          if (ci == error_mark_node)
-           return error_mark_node;
+           continue;
 
          /* Add a constraint that the return type matches the instantiation of
             A with the same template arguments.  */
diff --git a/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias5.C b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias5.C
new file mode 100644 (file)
index 0000000..69143a3
--- /dev/null
@@ -0,0 +1,18 @@
+// PR c++/99180
+// { dg-do compile { target c++17 } }
+
+template <bool, typename... Ts>
+struct A {
+  A(Ts...) {}
+};
+
+template <typename... Ts>
+using B = A<false, Ts...>;
+
+template <typename... Ts>
+A(Ts...) -> A<true, Ts...>;
+
+int main() {
+  B{};                         // { dg-error "alias" "" { target c++17_down } }
+  return 0;
+}
diff --git a/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias6.C b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias6.C
new file mode 100644 (file)
index 0000000..26a3864
--- /dev/null
@@ -0,0 +1,11 @@
+// PR c++/93295
+// { dg-do compile { target c++20 } }
+
+template<typename T, bool B = false>
+struct Foo {
+    Foo(T) {}
+};
+
+template<typename T> Foo(T) -> Foo<T>;
+template<typename T> using Bar = Foo<T, true>;
+Bar b{0};
diff --git a/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias7.C b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias7.C
new file mode 100644 (file)
index 0000000..0d8bff7
--- /dev/null
@@ -0,0 +1,32 @@
+// PR c++/93867
+// { dg-do compile { target c++20 } }
+
+template <typename CharT, unsigned N>
+struct basic_fixed_string
+{
+  constexpr basic_fixed_string(const CharT *p) {
+    for (int i = 0; i < N; ++i) {
+      m_data[i] = p[i];
+    }
+  }
+
+  CharT m_data[N] {};
+};
+
+template <typename CharT, unsigned N>
+basic_fixed_string(const CharT (&)[N]) -> basic_fixed_string<CharT,N>;
+
+template <unsigned N>
+using fixed_string = basic_fixed_string<char, N>;
+
+template <fixed_string path>
+constexpr int foo()
+{
+  return 42;
+}
+
+int main(int argc, char const *argv[])
+{
+  foo<"hello">();
+  return 0;
+}
diff --git a/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias8.C b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias8.C
new file mode 100644 (file)
index 0000000..ec00595
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/95486
+// { dg-do compile { target c++20 } }
+
+template<class T, class U>
+struct X { X(U) requires __is_same(U, int) {} };
+
+template<class U>
+X(U) -> X<char, U>;
+
+template<class U>
+using Y = X<void, U>;
+
+Y y{1};
+Y z{'a'}; // { dg-error "failed|no match" }