From: Patrick Palka Date: Wed, 13 Dec 2023 20:55:14 +0000 (-0500) Subject: c++: unifying constants vs their type [PR99186, PR104867] X-Git-Tag: basepoints/gcc-15~3619 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=35ba3add7d0a9fc6ce955ba8ad82b0413e86ad7d;p=thirdparty%2Fgcc.git c++: unifying constants vs their type [PR99186, PR104867] When unifying constants we need to treat constants of different types but same value as different in light of auto template parameters since otherwise e.g. A<1> will unify with A<1u> (where A's template-head is template). 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, or even just cp_tree_equal since the CONVERT_EXPR_P loop seems to be dead code, but that's a simplification we could consider during next stage 1. PR c++/99186 PR c++/104867 gcc/cp/ChangeLog: * pt.cc (unify) : Compare types as well. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/nontype-auto23.C: New test. * g++.dg/cpp1z/nontype-auto24.C: New test. --- diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 9a21467bf219..b6a450c4ad40 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -24706,6 +24706,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 000000000000..62a571ef84ab --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/nontype-auto23.C @@ -0,0 +1,22 @@ +// PR c++/99186 +// { dg-do compile { target c++17 } } + +template +struct tuple_impl : tuple_impl { }; + +template +struct tuple_impl { }; + +template +struct tuple : tuple_impl<0, T, U> { }; + +template +void get(const tuple_impl&); + +template struct S; + +int main() { + tuple,S<1U>> x; + get>(x); + get>(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 000000000000..52e4c134ccdb --- /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 struct enum_; + +template struct list { }; + +template void f(list, V>); + +struct enum_type_map : list, int>, list, double> { }; + +int main() { + f(enum_type_map()); +}