diff mbox series

c++: unifying constants vs their type [PR99186, PR104867]

Message ID 20231212212143.64983-1-ppalka@redhat.com
State New
Headers show
Series c++: unifying constants vs their type [PR99186, PR104867] | expand

Commit Message

Patrick Palka Dec. 12, 2023, 9:21 p.m. UTC
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK
for trunk?

-- >8 --

When unifying constants we need to generally treat constants of
different types but same value as different, in light of auto template
parameters.  This patch fixes this in a minimal way; it seems we could
get away with just using template_args_equal here, as we do in the
default case, but that's a simplification we could look into during next
stage 1.

	PR c++/99186
	PR c++/104867

gcc/cp/ChangeLog:

	* pt.cc (unify) <case INTEGER_CST>: Compare types as well.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp1z/nontype-auto23.C: New test.
	* g++.dg/cpp1z/nontype-auto24.C: New test.
---
 gcc/cp/pt.cc                                |  2 ++
 gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C | 23 +++++++++++++++++++++
 gcc/testsuite/g++.dg/cpp1z/nontype-auto24.C | 18 ++++++++++++++++
 3 files changed, 43 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C
 create mode 100644 gcc/testsuite/g++.dg/cpp1z/nontype-auto24.C

Comments

Patrick Palka Dec. 12, 2023, 11:18 p.m. UTC | #1
On Tue, 12 Dec 2023, Patrick Palka wrote:

> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK
> for trunk?
> 
> -- >8 --
> 
> When unifying constants we need to generally treat constants of
> different types but same value as different, in light of auto template
> parameters.  This patch fixes this in a minimal way; it seems we could
> get away with just using template_args_equal here, as we do in the

or just cp_tree_equal for that matters because ...

> default case, but that's a simplification we could look into during next
> stage 1.
> 
> 	PR c++/99186
> 	PR c++/104867
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (unify) <case INTEGER_CST>: Compare types as well.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp1z/nontype-auto23.C: New test.
> 	* g++.dg/cpp1z/nontype-auto24.C: New test.
> ---
>  gcc/cp/pt.cc                                |  2 ++
>  gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C | 23 +++++++++++++++++++++
>  gcc/testsuite/g++.dg/cpp1z/nontype-auto24.C | 18 ++++++++++++++++
>  3 files changed, 43 insertions(+)
>  create mode 100644 gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C
>  create mode 100644 gcc/testsuite/g++.dg/cpp1z/nontype-auto24.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index a8966e223f1..602dd02d29d 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -24709,6 +24709,8 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
>        /* Type INTEGER_CST can come from ordinary constant template args.  */
>      case INTEGER_CST:
>      case REAL_CST:
> +      if (!same_type_p (TREE_TYPE (parm), TREE_TYPE (arg)))
> +	return unify_template_argument_mismatch (explain_p, parm, arg);
>        while (CONVERT_EXPR_P (arg))
>  	arg = TREE_OPERAND (arg, 0);

... this while loop seems to be dead code.

>  
> diff --git a/gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C b/gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C
> new file mode 100644
> index 00000000000..467559ffdda
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C
> @@ -0,0 +1,23 @@
> +// PR c++/99186
> +// { dg-do compile { target c++17 } }
> +
> +template<int N, typename T, typename... U>
> +struct tuple_impl : tuple_impl<N + 1, U...> { };
> +
> +template<int N, typename T>
> +struct tuple_impl<N, T> { };
> +
> +template<typename T, typename U>
> +struct tuple : tuple_impl<0, T, U> { };
> +
> +template<typename T, int N, typename... U>
> +void get(const tuple_impl<N, T, U...>&);
> +
> +template<auto>
> +struct S;
> +
> +int main() {
> +   tuple<S<1>,S<1U>> x;
> +   get<S<1U>>(x);
> +   get<S<1>>(x);
> +}
> diff --git a/gcc/testsuite/g++.dg/cpp1z/nontype-auto24.C b/gcc/testsuite/g++.dg/cpp1z/nontype-auto24.C
> new file mode 100644
> index 00000000000..52e4c134ccd
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1z/nontype-auto24.C
> @@ -0,0 +1,18 @@
> +// PR c++/104867
> +// { dg-do compile { target c++17 } }
> +
> +enum class Foo { A1 };
> +
> +enum class Bar { B1 };
> +
> +template<auto EnumVal> struct enum_;
> +
> +template<class...> struct list { };
> +
> +template<class V> void f(list<enum_<Bar::B1>, V>);
> +
> +struct enum_type_map : list<enum_<Foo::A1>, int>, list<enum_<Bar::B1>, double> { };
> +
> +int main() {
> +  f(enum_type_map());
> +}
> -- 
> 2.43.0.76.g1a87c842ec
> 
>
Jason Merrill Dec. 13, 2023, 8:12 p.m. UTC | #2
On 12/12/23 16:21, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK
> for trunk?

OK.

> -- >8 --
> 
> When unifying constants we need to generally treat constants of
> different types but same value as different, in light of auto template
> parameters.  This patch fixes this in a minimal way; it seems we could
> get away with just using template_args_equal here, as we do in the
> default case, but that's a simplification we could look into during next
> stage 1.
> 
> 	PR c++/99186
> 	PR c++/104867
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (unify) <case INTEGER_CST>: Compare types as well.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp1z/nontype-auto23.C: New test.
> 	* g++.dg/cpp1z/nontype-auto24.C: New test.
> ---
>   gcc/cp/pt.cc                                |  2 ++
>   gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C | 23 +++++++++++++++++++++
>   gcc/testsuite/g++.dg/cpp1z/nontype-auto24.C | 18 ++++++++++++++++
>   3 files changed, 43 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C
>   create mode 100644 gcc/testsuite/g++.dg/cpp1z/nontype-auto24.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index a8966e223f1..602dd02d29d 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -24709,6 +24709,8 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
>         /* Type INTEGER_CST can come from ordinary constant template args.  */
>       case INTEGER_CST:
>       case REAL_CST:
> +      if (!same_type_p (TREE_TYPE (parm), TREE_TYPE (arg)))
> +	return unify_template_argument_mismatch (explain_p, parm, arg);
>         while (CONVERT_EXPR_P (arg))
>   	arg = TREE_OPERAND (arg, 0);
>   
> diff --git a/gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C b/gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C
> new file mode 100644
> index 00000000000..467559ffdda
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C
> @@ -0,0 +1,23 @@
> +// PR c++/99186
> +// { dg-do compile { target c++17 } }
> +
> +template<int N, typename T, typename... U>
> +struct tuple_impl : tuple_impl<N + 1, U...> { };
> +
> +template<int N, typename T>
> +struct tuple_impl<N, T> { };
> +
> +template<typename T, typename U>
> +struct tuple : tuple_impl<0, T, U> { };
> +
> +template<typename T, int N, typename... U>
> +void get(const tuple_impl<N, T, U...>&);
> +
> +template<auto>
> +struct S;
> +
> +int main() {
> +   tuple<S<1>,S<1U>> x;
> +   get<S<1U>>(x);
> +   get<S<1>>(x);
> +}
> diff --git a/gcc/testsuite/g++.dg/cpp1z/nontype-auto24.C b/gcc/testsuite/g++.dg/cpp1z/nontype-auto24.C
> new file mode 100644
> index 00000000000..52e4c134ccd
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1z/nontype-auto24.C
> @@ -0,0 +1,18 @@
> +// PR c++/104867
> +// { dg-do compile { target c++17 } }
> +
> +enum class Foo { A1 };
> +
> +enum class Bar { B1 };
> +
> +template<auto EnumVal> struct enum_;
> +
> +template<class...> struct list { };
> +
> +template<class V> void f(list<enum_<Bar::B1>, V>);
> +
> +struct enum_type_map : list<enum_<Foo::A1>, int>, list<enum_<Bar::B1>, double> { };
> +
> +int main() {
> +  f(enum_type_map());
> +}
diff mbox series

Patch

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index a8966e223f1..602dd02d29d 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -24709,6 +24709,8 @@  unify (tree tparms, tree targs, tree parm, tree arg, int strict,
       /* Type INTEGER_CST can come from ordinary constant template args.  */
     case INTEGER_CST:
     case REAL_CST:
+      if (!same_type_p (TREE_TYPE (parm), TREE_TYPE (arg)))
+	return unify_template_argument_mismatch (explain_p, parm, arg);
       while (CONVERT_EXPR_P (arg))
 	arg = TREE_OPERAND (arg, 0);
 
diff --git a/gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C b/gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C
new file mode 100644
index 00000000000..467559ffdda
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C
@@ -0,0 +1,23 @@ 
+// PR c++/99186
+// { dg-do compile { target c++17 } }
+
+template<int N, typename T, typename... U>
+struct tuple_impl : tuple_impl<N + 1, U...> { };
+
+template<int N, typename T>
+struct tuple_impl<N, T> { };
+
+template<typename T, typename U>
+struct tuple : tuple_impl<0, T, U> { };
+
+template<typename T, int N, typename... U>
+void get(const tuple_impl<N, T, U...>&);
+
+template<auto>
+struct S;
+
+int main() {
+   tuple<S<1>,S<1U>> x;
+   get<S<1U>>(x);
+   get<S<1>>(x);
+}
diff --git a/gcc/testsuite/g++.dg/cpp1z/nontype-auto24.C b/gcc/testsuite/g++.dg/cpp1z/nontype-auto24.C
new file mode 100644
index 00000000000..52e4c134ccd
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/nontype-auto24.C
@@ -0,0 +1,18 @@ 
+// PR c++/104867
+// { dg-do compile { target c++17 } }
+
+enum class Foo { A1 };
+
+enum class Bar { B1 };
+
+template<auto EnumVal> struct enum_;
+
+template<class...> struct list { };
+
+template<class V> void f(list<enum_<Bar::B1>, V>);
+
+struct enum_type_map : list<enum_<Foo::A1>, int>, list<enum_<Bar::B1>, double> { };
+
+int main() {
+  f(enum_type_map());
+}