From e58c9d921da423712db8369e8bb69d4ae992e94d Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Wed, 6 Jun 2018 17:51:19 +0000 Subject: [PATCH] re PR c++/85977 (Incorrect handling of array reference size deduction) PR c++/85977 * pt.c (unify): If ELTTYPE has no deducible template parms, skip deduction from the list elements. (type_unification_real): Check convertibility of list elements. * g++.dg/cpp0x/initlist102.C: New test. * g++.dg/cpp0x/initlist103.C: New test. * g++.dg/cpp0x/initlist104.C: New test. From-SVN: r261241 --- gcc/cp/ChangeLog | 7 +++ gcc/cp/pt.c | 54 +++++++++++++++++------- gcc/testsuite/ChangeLog | 7 +++ gcc/testsuite/g++.dg/cpp0x/initlist102.C | 39 +++++++++++++++++ gcc/testsuite/g++.dg/cpp0x/initlist103.C | 11 +++++ gcc/testsuite/g++.dg/cpp0x/initlist104.C | 11 +++++ 6 files changed, 113 insertions(+), 16 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist102.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist103.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist104.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index f03c9df3d52..68df0544517 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2018-06-06 Marek Polacek + + PR c++/85977 + * pt.c (unify): If ELTTYPE has no deducible template parms, skip + deduction from the list elements. + (type_unification_real): Check convertibility of list elements. + 2018-06-06 Jason Merrill PR c++/86060 - ICE on range for with -std=c++98. diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 448cd69b722..d8880eb138d 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -20370,6 +20370,22 @@ type_unification_real (tree tparms, if (check_non_deducible_conversion (parm, arg, strict, flags, explain_p)) return 1; + + if (BRACE_ENCLOSED_INITIALIZER_P (arg) + && (TREE_CODE (parm) == ARRAY_TYPE || is_std_init_list (parm))) + { + tree elt, elttype; + unsigned int i; + + if (TREE_CODE (parm) == ARRAY_TYPE) + elttype = TREE_TYPE (parm); + else + elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0); + FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt) + if (check_non_deducible_conversion (elttype, elt, strict, + flags, explain_p)) + return 1; + } } /* Now substitute into the default template arguments. */ @@ -21404,24 +21420,30 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict, return unify_success (explain_p); } - FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt) - { - int elt_strict = strict; + if (strict != DEDUCE_EXACT + && TYPE_P (elttype) + && !uses_deducible_template_parms (elttype)) + /* If ELTTYPE has no deducible template parms, skip deduction from + the list elements. */; + else + FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt) + { + int elt_strict = strict; - if (elt == error_mark_node) - return unify_invalid (explain_p); + if (elt == error_mark_node) + return unify_invalid (explain_p); - if (!BRACE_ENCLOSED_INITIALIZER_P (elt)) - { - tree type = TREE_TYPE (elt); - if (type == error_mark_node) - return unify_invalid (explain_p); - /* It should only be possible to get here for a call. */ - gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL); - elt_strict |= maybe_adjust_types_for_deduction - (DEDUCE_CALL, &elttype, &type, elt); - elt = type; - } + if (!BRACE_ENCLOSED_INITIALIZER_P (elt)) + { + tree type = TREE_TYPE (elt); + if (type == error_mark_node) + return unify_invalid (explain_p); + /* It should only be possible to get here for a call. */ + gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL); + elt_strict |= maybe_adjust_types_for_deduction + (DEDUCE_CALL, &elttype, &type, elt); + elt = type; + } RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict, explain_p); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index f63c9d75a8c..14f575e6e79 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2018-06-06 Marek Polacek + + PR c++/85977 + * g++.dg/cpp0x/initlist102.C: New test. + * g++.dg/cpp0x/initlist103.C: New test. + * g++.dg/cpp0x/initlist104.C: New test. + 2018-06-06 Eric Botcazou * gcc.dg/torture/pr86066.c: New test. diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist102.C b/gcc/testsuite/g++.dg/cpp0x/initlist102.C new file mode 100644 index 00000000000..e114866292f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist102.C @@ -0,0 +1,39 @@ +// PR c++/85977, Incorrect handling of array reference size deduction +// { dg-do compile { target c++11 } } + +template +void fn1 (const char (&)[N]) { static_assert (N == 3, "fn1");} + +template +void fn2 (const short (&)[N]) { static_assert (N == 3, "fn2");} + +template +void fn3 (const int (&)[N]) { static_assert (N == 3, "fn2");} + +template +void fn4 (const long (&)[N]) { static_assert (N == 3, "fn4");} + +template +void fn5 (const unsigned char (&)[N]) { static_assert (N == 3, "fn5");} + +template +void fn6 (const unsigned short (&)[N]) { static_assert (N == 3, "fn6");} + +template +void fn7 (const unsigned int (&)[N]) { static_assert (N == 3, "fn7");} + +template +void fn8 (const unsigned int (&)[N]) { static_assert (N == 3, "fn8");} + +void +bar () +{ + fn1 ({1, 2, 3}); + fn2 ({1, 2, 3}); + fn3 ({1, 2, 3}); + fn4 ({1, 2, 3}); + fn5 ({1, 2, 3}); + fn6 ({1, 2, 3}); + fn7 ({1, 2, 3}); + fn8 ({1, 2, 3}); +} diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist103.C b/gcc/testsuite/g++.dg/cpp0x/initlist103.C new file mode 100644 index 00000000000..0c1923c6ab0 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist103.C @@ -0,0 +1,11 @@ +// PR c++/85977, Incorrect handling of array reference size deduction +// { dg-do compile { target c++11 } } + +template +void fn (const char (&)[N]) { } + +void +bar () +{ + fn ({1.2}); // { dg-error "narrowing" } +} diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist104.C b/gcc/testsuite/g++.dg/cpp0x/initlist104.C new file mode 100644 index 00000000000..99487a70607 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist104.C @@ -0,0 +1,11 @@ +// PR c++/85977, Incorrect handling of array reference size deduction +// { dg-do compile { target c++11 } } + +template +void fn (const T (&)[N]) { static_assert (N == 3, "fn"); } + +void +bar () +{ + fn ({1, 2, 3}); +} -- 2.30.2