public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Fix ICE with template codes in check_narrowing [PR91465]
@ 2020-02-07  0:30 Marek Polacek
  2020-02-09 12:51 ` Jason Merrill
  0 siblings, 1 reply; 4+ messages in thread
From: Marek Polacek @ 2020-02-07  0:30 UTC (permalink / raw)
  To: Jason Merrill, GCC Patches

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.

Bootstrapped/regtested on x86_64-linux, ok for trunk and 9?

	PR c++/91465 - ICE with template codes in check_narrowing.
	* typeck2.c (check_narrowing): Call 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/typeck2.c                     |  4 +++-
 gcc/testsuite/g++.dg/cpp0x/pr91465.C | 16 ++++++++++++++++
 gcc/testsuite/g++.dg/cpp1z/pr91465.C | 10 ++++++++++
 3 files changed, 29 insertions(+), 1 deletion(-)
 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/typeck2.c b/gcc/cp/typeck2.c
index 371b203c29b..8f8e9703ac8 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, tf_none);
+  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: cb273d81a45092ceee793f0357526e291f03c7b7
-- 
Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA

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

* Re: [PATCH] c++: Fix ICE with template codes in check_narrowing [PR91465]
  2020-02-07  0:30 [PATCH] c++: Fix ICE with template codes in check_narrowing [PR91465] Marek Polacek
@ 2020-02-09 12:51 ` Jason Merrill
  2020-02-11 16:07   ` [PATCH v2] " Marek Polacek
  0 siblings, 1 reply; 4+ messages in thread
From: Jason Merrill @ 2020-02-09 12:51 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

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.

> Bootstrapped/regtested on x86_64-linux, ok for trunk and 9?


> 	PR c++/91465 - ICE with template codes in check_narrowing.
> 	* typeck2.c (check_narrowing): Call 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/typeck2.c                     |  4 +++-
>   gcc/testsuite/g++.dg/cpp0x/pr91465.C | 16 ++++++++++++++++
>   gcc/testsuite/g++.dg/cpp1z/pr91465.C | 10 ++++++++++
>   3 files changed, 29 insertions(+), 1 deletion(-)
>   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/typeck2.c b/gcc/cp/typeck2.c
> index 371b203c29b..8f8e9703ac8 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, tf_none);
> +  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: cb273d81a45092ceee793f0357526e291f03c7b7
> 

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

* Re: [PATCH v2] c++: Fix ICE with template codes in check_narrowing [PR91465]
  2020-02-09 12:51 ` Jason Merrill
@ 2020-02-11 16:07   ` Marek Polacek
  2020-02-13 23:46     ` Jason Merrill
  0 siblings, 1 reply; 4+ messages in thread
From: Marek Polacek @ 2020-02-11 16:07 UTC (permalink / raw)
  To: Jason Merrill; +Cc: GCC Patches

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

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

* Re: [PATCH v2] c++: Fix ICE with template codes in check_narrowing [PR91465]
  2020-02-11 16:07   ` [PATCH v2] " Marek Polacek
@ 2020-02-13 23:46     ` Jason Merrill
  0 siblings, 0 replies; 4+ messages in thread
From: Jason Merrill @ 2020-02-13 23:46 UTC (permalink / raw)
  To: Marek Polacek; +Cc: GCC Patches

On 2/11/20 5:06 PM, Marek Polacek wrote:
> 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.

Hmm, the one in convert_nontype_argument shouldn't be needed.

I see the pattern in e.g. build_explicit_specifier is

   expr = instantiate_non_dependent_expr_sfinae (expr, complain);
   /* Don't let convert_like_real create more template codes.  */
   processing_template_decl_sentinel s;
   expr = build_converted_constant_bool_expr (expr, complain);
   expr = cxx_constant_value (expr);

That has always seemed unpleasantly complicated.

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

Shouldn't the above be after setting the check_narrowing flags?  We 
could simplify the pattern above by handling all of it here: 
instantiate, set the sentinel, convert, maybe_constant_value, return 
either the constant result or an IMPLICIT_CONV_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);

With the change I suggest above we would drop both this call and the 
earlier instantiate_ call.  In general it's wrong to call 
*_non_dependent_expr twice on the same expression.

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

And this whole if block should also be able to reduce to

           expr = build_converted_constant_expr (type, expr, complain);
           expr = convert_from_reference (expr);


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

And this change should not be needed.

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

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

end of thread, other threads:[~2020-02-13 23:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-07  0:30 [PATCH] c++: Fix ICE with template codes in check_narrowing [PR91465] Marek Polacek
2020-02-09 12:51 ` Jason Merrill
2020-02-11 16:07   ` [PATCH v2] " Marek Polacek
2020-02-13 23:46     ` 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).