* [PATCH] c++: Fix deduction with reference NTTP [PR83476]
@ 2021-04-13 22:36 Patrick Palka
2021-04-14 1:30 ` Jason Merrill
0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2021-04-13 22:36 UTC (permalink / raw)
To: gcc-patches
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 V is wrapped in an implicit INDIRECT_REF
(formed from template_parm_to_arg), and this causes unify_one_argument
to exit early, causing deduction to fail. This patch fixes this
by making deducible_expression look through such implicit INDIRECT_REFs.
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?
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 | 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(-)
create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction85.C
create mode 100644 gcc/testsuite/g++.dg/template/ref11.C
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 59df79484bf..f488a5a8c12 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -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
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..ef29fb72bef
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/ref11.C
@@ -0,0 +1,9 @@
+// PR c++/83476
+
+template <int& V> struct A {};
+template <int& V> void f(A<V>);
+int n;
+int main() {
+ A<n> a;
+ f(a);
+}
--
2.31.1.272.g89b43f80a5
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] c++: Fix deduction with reference NTTP [PR83476]
2021-04-13 22:36 [PATCH] c++: Fix deduction with reference NTTP [PR83476] Patrick Palka
@ 2021-04-14 1:30 ` Jason Merrill
0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2021-04-14 1:30 UTC (permalink / raw)
To: Patrick Palka, gcc-patches
On 4/13/21 6:36 PM, Patrick Palka wrote:
> 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 V is wrapped in an implicit INDIRECT_REF
> (formed from template_parm_to_arg), and this causes unify_one_argument
> to exit early, causing deduction to fail. This patch fixes this
> by making deducible_expression look through such implicit INDIRECT_REFs.
>
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?
OK.
> 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 | 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(-)
> create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction85.C
> create mode 100644 gcc/testsuite/g++.dg/template/ref11.C
>
> diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
> index 59df79484bf..f488a5a8c12 100644
> --- a/gcc/cp/pt.c
> +++ b/gcc/cp/pt.c
> @@ -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
> 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..ef29fb72bef
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/ref11.C
> @@ -0,0 +1,9 @@
> +// PR c++/83476
> +
> +template <int& V> struct A {};
> +template <int& V> void f(A<V>);
> +int n;
> +int main() {
> + A<n> a;
> + f(a);
> +}
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-04-14 1:30 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-13 22:36 [PATCH] c++: Fix deduction with reference NTTP [PR83476] Patrick Palka
2021-04-14 1:30 ` Jason Merrill
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).