[PATCH v2] c++: ICE with -Wall and constexpr if [PR94937]

Jason Merrill jason@redhat.com
Mon May 18 22:36:14 GMT 2020


On 5/7/20 10:55 AM, Marek Polacek wrote:
> On Wed, May 06, 2020 at 05:26:32PM -0400, Jason Merrill wrote:
>> On 5/5/20 6:17 PM, Marek Polacek wrote:
>>> An ICE arises here because we call cp_get_callee_fndecl_nofold in a
>>> template, and we've got a CALL_EXPR whose CALL_EXPR_FN is a BASELINK.
>>> This tickles the INDIRECT_TYPE_P assert in cp_get_fndecl_from_callee.
>>>
>>> Jakub said in the PR that he'd hit a similar problem too and dealt
>>> with it in omp_declare_variant_finalize_one.  I considered tweaking
>>> is_std_constant_evaluated_p to return false for a BASELINK, since the
>>> std::is_constant_evaluated call we're looking for can't be a member
>>> function, but perhaps we could get another unexpected CALL_EXPR and
>>> crash the same.  In which case it might be better to make out the
>>> omp_* code into a new routine and use that, as below.
>>
>> Why not adjust cp_get_fndecl_from_callee to, say, return null instead of
>> aborting when !INDIRECT_TYPE_P?
> 
> It's often hard (for me, anyway) to decide if an assert should stay or can
> be turned into a condition.  In this case we could argue that initially
> cp_get_fndecl_from_callee wasn't meant to be called in a template, but now
> we have reasonable use cases where it actually is called in a template, so
> should cope instead of crashing.  Is that a fair assesment?

Yes, when we hit an assert we always have to decide whether the assert 
is catching a problem elsewhere or being too strict.  I think the 
unsuitability to template trees was accidental rather than deliberate.

> I suspect one could argue that if we see a BASELINK, we might want to use
> BASELINK_FUNCTIONS to get the FUNCTION_DECL, but I don't have a test where
> that would make a difference.

In a template we end up with CALL_EXPR_FN that refers to the overload 
set; even if we were able to do overload resolution and chose a function 
to call, we've discarded that information.

We could restrict the added null return to

   processing_template_decl && is_overloaded_fn (fn)

but I'm not sure that retaining that much of the assert is actually 
valuable.

> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/10.2?

OK.

> -- >8 --
> An ICE arises here because we call cp_get_callee_fndecl_nofold in a
> template, and we've got a CALL_EXPR whose CALL_EXPR_FN is a BASELINK.
> This tickles the INDIRECT_TYPE_P assert in cp_get_fndecl_from_callee.
> 
> Fixed by turning the assert into a condition and returning NULL_TREE
> in that case.
> 
> 	PR c++/94937
> 	* cvt.c (cp_get_fndecl_from_callee): Return NULL_TREE if the function
> 	type is not INDIRECT_TYPE_P.
> 	* decl.c (omp_declare_variant_finalize_one): Call
> 	cp_get_callee_fndecl_nofold instead of looking for the function decl
> 	manually.
> 
> 	* g++.dg/cpp1z/constexpr-if34.C: New test.
> 	* g++.dg/cpp2a/is-constant-evaluated10.C: New test.
> ---
>   gcc/cp/cvt.c                                  |  3 +-
>   gcc/cp/decl.c                                 | 12 +-------
>   gcc/testsuite/g++.dg/cpp1z/constexpr-if34.C   | 15 ++++++++++
>   .../g++.dg/cpp2a/is-constant-evaluated10.C    | 30 +++++++++++++++++++
>   4 files changed, 47 insertions(+), 13 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-if34.C
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated10.C
> 
> diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
> index 656e7fd3ec0..371002833d0 100644
> --- a/gcc/cp/cvt.c
> +++ b/gcc/cp/cvt.c
> @@ -995,9 +995,8 @@ cp_get_fndecl_from_callee (tree fn, bool fold /* = true */)
>     if (TREE_CODE (fn) == FUNCTION_DECL)
>       return fn;
>     tree type = TREE_TYPE (fn);
> -  if (type == unknown_type_node)
> +  if (type == NULL_TREE || !INDIRECT_TYPE_P (type))
>       return NULL_TREE;
> -  gcc_assert (INDIRECT_TYPE_P (type));
>     if (fold)
>       fn = maybe_constant_init (fn);
>     STRIP_NOPS (fn);
> diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
> index 232d7ed4a14..cbdef3e3365 100644
> --- a/gcc/cp/decl.c
> +++ b/gcc/cp/decl.c
> @@ -7297,17 +7297,7 @@ omp_declare_variant_finalize_one (tree decl, tree attr)
>     if (variant == error_mark_node && !processing_template_decl)
>       return true;
>   
> -  variant = cp_get_callee (variant);
> -  if (variant)
> -    {
> -      if (TREE_CODE (variant) == FUNCTION_DECL)
> -	;
> -      else if (TREE_TYPE (variant) && INDIRECT_TYPE_P (TREE_TYPE (variant)))
> -	variant = cp_get_fndecl_from_callee (variant, false);
> -      else
> -	variant = NULL_TREE;
> -    }
> -
> +  variant = cp_get_callee_fndecl_nofold (variant);
>     input_location = save_loc;
>   
>     if (variant)
> diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if34.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if34.C
> new file mode 100644
> index 00000000000..6e0b2597a53
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if34.C
> @@ -0,0 +1,15 @@
> +// PR c++/94937 - ICE with -Wall and constexpr if.
> +// { dg-do compile { target c++17 } }
> +// { dg-options "-Wall" }
> +
> +struct B {
> +  static constexpr bool foo() { return false; }
> +};
> +
> +template<typename T>
> +struct C {
> +  static void bar ()
> +  {
> +    if constexpr (B::foo()) ;
> +  }
> +};
> diff --git a/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated10.C b/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated10.C
> new file mode 100644
> index 00000000000..7b2e345f448
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated10.C
> @@ -0,0 +1,30 @@
> +// { dg-do compile { target c++2a } }
> +// { dg-options "-Wtautological-compare" }
> +
> +namespace std {
> +  constexpr inline bool
> +  is_constant_evaluated () noexcept
> +  {
> +    return __builtin_is_constant_evaluated ();
> +  }
> +}
> +
> +template<typename>
> +constexpr int
> +foo(int i)
> +{
> +  if constexpr (std::is_constant_evaluated ()) // { dg-warning ".std::is_constant_evaluated. always evaluates to true in .if constexpr." }
> +    return 42;
> +  else
> +    return i;
> +}
> +
> +template<typename>
> +constexpr int
> +foo2(int i)
> +{
> +  if constexpr (__builtin_is_constant_evaluated ()) // { dg-warning ".std::is_constant_evaluated. always evaluates to true in .if constexpr." }
> +    return 42;
> +  else
> +    return i;
> +}
> 
> base-commit: 9cd70f3f0fa45cbe899da5ecd0b0c6da0657fd62
> 



More information about the Gcc-patches mailing list