public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: unifying constants vs their type [PR99186, PR104867]
@ 2023-12-12 21:21 Patrick Palka
  2023-12-12 23:18 ` Patrick Palka
  2023-12-13 20:12 ` Jason Merrill
  0 siblings, 2 replies; 3+ messages in thread
From: Patrick Palka @ 2023-12-12 21:21 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Patrick Palka

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

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());
+}
-- 
2.43.0.76.g1a87c842ec


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] c++: unifying constants vs their type [PR99186, PR104867]
  2023-12-12 21:21 [PATCH] c++: unifying constants vs their type [PR99186, PR104867] Patrick Palka
@ 2023-12-12 23:18 ` Patrick Palka
  2023-12-13 20:12 ` Jason Merrill
  1 sibling, 0 replies; 3+ messages in thread
From: Patrick Palka @ 2023-12-12 23:18 UTC (permalink / raw)
  To: Patrick Palka; +Cc: gcc-patches, jason

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
> 
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] c++: unifying constants vs their type [PR99186, PR104867]
  2023-12-12 21:21 [PATCH] c++: unifying constants vs their type [PR99186, PR104867] Patrick Palka
  2023-12-12 23:18 ` Patrick Palka
@ 2023-12-13 20:12 ` Jason Merrill
  1 sibling, 0 replies; 3+ messages in thread
From: Jason Merrill @ 2023-12-13 20:12 UTC (permalink / raw)
  To: Patrick Palka, gcc-patches

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());
> +}


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-12-13 20:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-12 21:21 [PATCH] c++: unifying constants vs their type [PR99186, PR104867] Patrick Palka
2023-12-12 23:18 ` Patrick Palka
2023-12-13 20:12 ` 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).