On Sun, Feb 09, 2020 at 01:51:13PM +0100, Jason Merrill wrote: > On 2/6/20 7:30 PM, Marek Polacek wrote: > > In ed4f2c001a883b2456fc607a33f1c59f9c4ee65d I changed the call to > > fold_non_dependent_expr in check_narrowing to maybe_constant_value. > > That was the wrong thing to do as these tests show: check_narrowing > > bails out for dependent expressions but we can still have template > > codes like CAST_EXPR that don't have anything dependent in it so are > > considered non-dependent. But cxx_eval_* don't grok template codes, > > so we need to call fold_non_dependent_expr instead which knows what > > to do with template codes. (I fully accept a "told you so".) > > > > I'm passing tf_none to it, otherwise we'd emit a bogus error for > > constexpr-ex4.C: there INIT is "A::operator int(&a)" and while > > instantiating this CALL_EXPR (in a template) we call finish_call_expr > > and that sees a BASELINK and so emits a new dummy object for 'this', > > and then we complain about the wrong number of arguments, because now > > we basically have two 'this's. Which is exactly the problem I saw > > recently in c++/92948. > > Yeah, the problem continues to be that build_converted_constant_expr is > breaking the boundary between template and non-template codes: > convert_like_real produces trees that aren't suitable for later > substitution, so substituting them breaks. Perhaps if we're looking at a > non-dependent constant expression in a template, > build_converted_constant_expr should instantiate_non_dependent_expr, pass > the result to convert_like, and then if successful throw away the result in > favor of an IMPLICIT_CONV_EXPR.
That seems to work (if I adjust two spots to handle an I_C_E). So something like this? I don't like that we create an I_C_E in convert_nontype_argument and in build_converted_constant_expr too, but both are important. And we should not forget to set IMPLICIT_CONV_EXPR_NONTYPE_ARG. Not the does not fix PR92031, another "taking address of rvalue" because substitution creates an & around a TARGET_EXPR. I suspect creating an I_C_E somewhere will be the fix for that too. -- >8 -- In ed4f2c001a883b2456fc607a33f1c59f9c4ee65d I changed the call to fold_non_dependent_expr in check_narrowing to maybe_constant_value. That was the wrong thing to do as these tests show: check_narrowing bails out for dependent expressions but we can still have template codes like CAST_EXPR that don't have anything dependent in it so are considered non-dependent. But cxx_eval_* don't grok template codes, so we need to call fold_non_dependent_expr instead which knows what to do with template codes. (I fully accept a "told you so".) This patch also includes further tweaks so as to avoid a bogus error for constexpr-ex4.C: there INIT is "A::operator int(&a)" and while instantiating this CALL_EXPR (in a template) we call finish_call_expr and that sees a BASELINK and so emits a new dummy object for 'this', and then we would complain about the wrong number of arguments, because of the two 'this's. Which is exactly the problem as in c++/92948. So create an IMPLICIT_CONV_EXPR for a non-dependent expression in a template in build_converted_constant_expr_internal, and adjust some spots to handle that. PR c++/91465 - ICE with template codes in check_narrowing. * call.c (build_converted_constant_expr_internal): Use an IMPLICIT_CONV_EXPR for a non-dependent expression in a template. * decl.c (compute_array_index_type_loc): Use fold_non_dependent_expr instead of maybe_constant_value. * pt.c (convert_nontype_argument): Don't pass IMPLICIT_CONV_EXPRs to maybe_constant_value. * typeck2.c (check_narrowing): Use fold_non_dependent_expr instead of maybe_constant_value. * g++.dg/cpp0x/pr91465.C: New test. * g++.dg/cpp1z/pr91465.C: New test. --- gcc/cp/call.c | 11 +++++++++++ gcc/cp/decl.c | 5 ++--- gcc/cp/pt.c | 7 +++++++ gcc/cp/typeck2.c | 4 +++- gcc/testsuite/g++.dg/cpp0x/pr91465.C | 16 ++++++++++++++++ gcc/testsuite/g++.dg/cpp1z/pr91465.C | 10 ++++++++++ 6 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/pr91465.C create mode 100644 gcc/testsuite/g++.dg/cpp1z/pr91465.C diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 51621b7dd87..91f5259d957 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -4366,6 +4366,17 @@ build_converted_constant_expr_internal (tree type, tree expr, && processing_template_decl) conv = next_conversion (conv); + /* convert_like produces trees that aren't suitable for + substitution, so use an IMPLICIT_CONV_EXPR. */ + if (processing_template_decl + && is_nondependent_constant_expression (expr)) + { + tree e = instantiate_non_dependent_expr (expr); + e = convert_like (conv, e, complain); + if (e != error_mark_node) + return build1 (IMPLICIT_CONV_EXPR, type, expr); + } + conv->check_narrowing = true; conv->check_narrowing_const_only = true; expr = convert_like (conv, expr, complain); diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 31a556a0a1f..5eb91007e9e 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -10281,9 +10281,8 @@ compute_array_index_type_loc (location_t name_loc, tree name, tree size, /* Pedantically a constant expression is required here and so __builtin_is_constant_evaluated () should fold to true if it is successfully folded into a constant. */ - size = maybe_constant_value (size, NULL_TREE, - /*manifestly_const_eval=*/true); - + size = fold_non_dependent_expr (size, complain, + /*manifestly_const_eval=*/true); if (!TREE_CONSTANT (size)) size = origsize; } diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index c2d3a98b1c5..5d207da2f5b 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -7099,6 +7099,13 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain) /* Make sure we return NULL_TREE only if we have really issued an error, as described above. */ return (complain & tf_error) ? NULL_TREE : error_mark_node; + /* Don't pass any IMPLICIT_CONV_EXPRs to maybe_constant_value + because that can't handle it. */ + else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR) + { + IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true; + return expr; + } expr = maybe_constant_value (expr, NULL_TREE, /*manifestly_const_eval=*/true); expr = convert_from_reference (expr); diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index 48920894b3b..59998d38c04 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -981,7 +981,9 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain, return ok; } - init = maybe_constant_value (init); + init = fold_non_dependent_expr (init, complain); + if (init == error_mark_node) + return ok; /* If we were asked to only check constants, return early. */ if (const_only && !TREE_CONSTANT (init)) diff --git a/gcc/testsuite/g++.dg/cpp0x/pr91465.C b/gcc/testsuite/g++.dg/cpp0x/pr91465.C new file mode 100644 index 00000000000..e2021aa13e1 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/pr91465.C @@ -0,0 +1,16 @@ +// PR c++/91465 - ICE with template codes in check_narrowing. +// { dg-do compile { target c++11 } } + +enum class D { X }; +enum class S { Z }; + +D foo(S) { return D{}; } +D foo(double) { return D{}; } + +template <typename> +struct Bar { + D baz(S s) + { + return D{foo(s)}; + } +}; diff --git a/gcc/testsuite/g++.dg/cpp1z/pr91465.C b/gcc/testsuite/g++.dg/cpp1z/pr91465.C new file mode 100644 index 00000000000..5b1205349d0 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/pr91465.C @@ -0,0 +1,10 @@ +// PR c++/91465 - ICE with template codes in check_narrowing. +// { dg-do compile { target c++17 } } + +enum class E { Z }; + +template <typename F> +void foo(F) +{ + E{char(0)}; +} base-commit: f348846e25573bc1f62f5a26317c331ad8dce041 -- Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA