]> gcc.gnu.org Git - gcc.git/commitdiff
c++: Fix deduction with reference NTTP [PR83476]
authorPatrick Palka <ppalka@redhat.com>
Wed, 14 Apr 2021 12:54:30 +0000 (08:54 -0400)
committerPatrick Palka <ppalka@redhat.com>
Wed, 14 Apr 2021 12:54:30 +0000 (08:54 -0400)
In the testcase ref11.C below, during deduction for the call f(a),
uses_deducible_template_parms returns false for the dependent
specialization A<V> because the generic template argument V here is
wrapped in an implicit INDIRECT_REF (formed from template_parm_to_arg).
Since uses_deducible_template_parms returns false, unify_one_argument
exits early without ever attempting to deduce 'n' for 'V'.  This patch
fixes this by making deducible_expression look through such implicit
INDIRECT_REFs.

gcc/cp/ChangeLog:

PR c++/83476
PR c++/99885
* pt.c (deducible_expression): Look through implicit
INDIRECT_REFs as well.

gcc/testsuite/ChangeLog:

PR c++/83476
PR c++/99885
* g++.dg/cpp1z/class-deduction85.C: New test.
* g++.dg/template/ref11.C: New test.

gcc/cp/pt.c
gcc/testsuite/g++.dg/cpp1z/class-deduction85.C [new file with mode: 0644]
gcc/testsuite/g++.dg/template/ref11.C [new file with mode: 0644]

index 59df79484bfd826e3d70fb098c558e940aa39558..f488a5a8c12010cc4e12a1f1f3cc20b1cdadcc55 100644 (file)
@@ -21902,8 +21902,10 @@ static bool uses_deducible_template_parms (tree type);
 static bool
 deducible_expression (tree expr)
 {
-  /* Strip implicit conversions.  */
-  while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
+  /* Strip implicit conversions and implicit INDIRECT_REFs.  */
+  while (CONVERT_EXPR_P (expr)
+        || TREE_CODE (expr) == VIEW_CONVERT_EXPR
+        || REFERENCE_REF_P (expr))
     expr = TREE_OPERAND (expr, 0);
   return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
 }
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction85.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction85.C
new file mode 100644 (file)
index 0000000..0b22f8e
--- /dev/null
@@ -0,0 +1,16 @@
+// PR c++/99885
+// { dg-do compile { target c++17 } }
+
+template <auto const& A>
+struct Foo {};
+
+template <auto const& A>
+struct Bar {
+    constexpr auto foo() const -> Foo<A> {
+        return {};
+    }
+};
+
+constexpr int a = 1;
+constexpr Bar<a> bar;
+Foo foo = bar.foo(); // <-- CTAD failure
diff --git a/gcc/testsuite/g++.dg/template/ref11.C b/gcc/testsuite/g++.dg/template/ref11.C
new file mode 100644 (file)
index 0000000..c43c67e
--- /dev/null
@@ -0,0 +1,9 @@
+// PR c++/83476
+
+int n;
+template <int& V> struct A {};
+template <int& V> void f(A<V>);
+int main() {
+  A<n> a;
+  f(a);
+}
This page took 0.096312 seconds and 5 git commands to generate.