public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Patrick Palka <ppalka@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r9-10154] c++: Fix deduction with reference NTTP [PR83476]
Date: Wed, 11 May 2022 14:59:46 +0000 (GMT)	[thread overview]
Message-ID: <20220511145946.CD7BE388457F@sourceware.org> (raw)

https://gcc.gnu.org/g:c6432e17960799509c48df6b500a8d069110980b

commit r9-10154-gc6432e17960799509c48df6b500a8d069110980b
Author: Patrick Palka <ppalka@redhat.com>
Date:   Wed Apr 14 08:54:30 2021 -0400

    c++: Fix deduction with reference NTTP [PR83476]
    
    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.
    
    (cherry picked from commit 2ccc05a5141506fde0e20dec702c717fd67bf6ee)

Diff:
---
 gcc/cp/pt.c                                    |  6 ++++--
 gcc/testsuite/g++.dg/cpp1z/class-deduction85.C | 16 ++++++++++++++++
 gcc/testsuite/g++.dg/template/ref11.C          |  9 +++++++++
 3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 612557bb717..08909e2c863 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -20824,8 +20824,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
index 00000000000..0b22f8eb982
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction85.C
@@ -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
index 00000000000..c43c67edf70
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/ref11.C
@@ -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);
+}


                 reply	other threads:[~2022-05-11 14:59 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220511145946.CD7BE388457F@sourceware.org \
    --to=ppalka@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).