public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: ICE with -Wall and constexpr if [PR94937]
@ 2020-05-05 22:17 Marek Polacek
  2020-05-06 21:26 ` Jason Merrill
  0 siblings, 1 reply; 4+ messages in thread
From: Marek Polacek @ 2020-05-05 22:17 UTC (permalink / raw)
  To: Jason Merrill, GCC Patches

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.

As a drive-by, I'm adding a test that tests the "is_constant_evaluated
in constexpr if" warning in a template too.

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

	PR c++/94937
	* cp-tree.h (cp_get_callee_fndecl_template): Declare.
	* cvt.c (cp_get_callee_fndecl_template): New.
	* decl.c (omp_declare_variant_finalize_one): Use
	cp_get_callee_fndecl_template.
	* semantics.c (is_std_constant_evaluated_p): Call
	cp_get_callee_fndecl_template instead of cp_get_callee_fndecl_nofold.

	* g++.dg/cpp1z/constexpr-if34.C: New test.
	* g++.dg/cpp2a/is-constant-evaluated10.C: New test.
---
 gcc/cp/cp-tree.h                              |  1 +
 gcc/cp/cvt.c                                  | 17 +++++++++++
 gcc/cp/decl.c                                 | 12 +-------
 gcc/cp/semantics.c                            |  2 +-
 gcc/testsuite/g++.dg/cpp1z/constexpr-if34.C   | 15 ++++++++++
 .../g++.dg/cpp2a/is-constant-evaluated10.C    | 30 +++++++++++++++++++
 6 files changed, 65 insertions(+), 12 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/cp-tree.h b/gcc/cp/cp-tree.h
index c4b81428e14..ecee0f479b0 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -6477,6 +6477,7 @@ extern tree cp_fold_convert			(tree, tree);
 extern tree cp_get_callee			(tree);
 extern tree cp_get_callee_fndecl		(tree);
 extern tree cp_get_callee_fndecl_nofold		(tree);
+extern tree cp_get_callee_fndecl_template	(tree);
 extern tree cp_get_fndecl_from_callee		(tree, bool fold = true);
 extern tree convert_to_void			(tree, impl_conv_void,
                                  		 tsubst_flags_t);
diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
index 656e7fd3ec0..5d3fba76bb9 100644
--- a/gcc/cp/cvt.c
+++ b/gcc/cp/cvt.c
@@ -1027,6 +1027,23 @@ cp_get_callee_fndecl_nofold (tree call)
   return cp_get_fndecl_from_callee (cp_get_callee (call), false);
 }
 
+/* As above, but is safe to call in a template too where we might get
+   unexpected trees.  Returns the FUNCTION_DECL or NULL_TREE.  */
+
+tree
+cp_get_callee_fndecl_template (tree call)
+{
+  call = cp_get_callee (call);
+  if (call == NULL_TREE)
+    return NULL_TREE;
+  else if (TREE_CODE (call) == FUNCTION_DECL)
+    return call;
+  else if (TREE_TYPE (call) && INDIRECT_TYPE_P (TREE_TYPE (call)))
+    return cp_get_fndecl_from_callee (call, /*fold=*/false);
+  else
+    return NULL_TREE;
+}
+
 /* Subroutine of convert_to_void.  Warn if we're discarding something with
    attribute [[nodiscard]].  */
 
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 3e7ed98fed3..7b63304ebf3 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_template (variant);
   input_location = save_loc;
 
   if (variant)
diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 4d1592ab0d2..716ee48446d 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -734,7 +734,7 @@ is_std_constant_evaluated_p (tree fn)
   if (call_expr_nargs (fn) != 0)
     return false;
 
-  tree fndecl = cp_get_callee_fndecl_nofold (fn);
+  tree fndecl = cp_get_callee_fndecl_template (fn);
   if (fndecl == NULL_TREE)
     return false;
 
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: e42f342652981aa9181cc3ceb04e0eb715506764
-- 
Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA


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

* Re: [PATCH] c++: ICE with -Wall and constexpr if [PR94937]
  2020-05-05 22:17 [PATCH] c++: ICE with -Wall and constexpr if [PR94937] Marek Polacek
@ 2020-05-06 21:26 ` Jason Merrill
  2020-05-07 14:55   ` [PATCH v2] " Marek Polacek
  0 siblings, 1 reply; 4+ messages in thread
From: Jason Merrill @ 2020-05-06 21:26 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

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?

> As a drive-by, I'm adding a test that tests the "is_constant_evaluated
> in constexpr if" warning in a template too.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/10.2?
> 
> 	PR c++/94937
> 	* cp-tree.h (cp_get_callee_fndecl_template): Declare.
> 	* cvt.c (cp_get_callee_fndecl_template): New.
> 	* decl.c (omp_declare_variant_finalize_one): Use
> 	cp_get_callee_fndecl_template.
> 	* semantics.c (is_std_constant_evaluated_p): Call
> 	cp_get_callee_fndecl_template instead of cp_get_callee_fndecl_nofold.
> 
> 	* g++.dg/cpp1z/constexpr-if34.C: New test.
> 	* g++.dg/cpp2a/is-constant-evaluated10.C: New test.
> ---
>   gcc/cp/cp-tree.h                              |  1 +
>   gcc/cp/cvt.c                                  | 17 +++++++++++
>   gcc/cp/decl.c                                 | 12 +-------
>   gcc/cp/semantics.c                            |  2 +-
>   gcc/testsuite/g++.dg/cpp1z/constexpr-if34.C   | 15 ++++++++++
>   .../g++.dg/cpp2a/is-constant-evaluated10.C    | 30 +++++++++++++++++++
>   6 files changed, 65 insertions(+), 12 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/cp-tree.h b/gcc/cp/cp-tree.h
> index c4b81428e14..ecee0f479b0 100644
> --- a/gcc/cp/cp-tree.h
> +++ b/gcc/cp/cp-tree.h
> @@ -6477,6 +6477,7 @@ extern tree cp_fold_convert			(tree, tree);
>   extern tree cp_get_callee			(tree);
>   extern tree cp_get_callee_fndecl		(tree);
>   extern tree cp_get_callee_fndecl_nofold		(tree);
> +extern tree cp_get_callee_fndecl_template	(tree);
>   extern tree cp_get_fndecl_from_callee		(tree, bool fold = true);
>   extern tree convert_to_void			(tree, impl_conv_void,
>                                    		 tsubst_flags_t);
> diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
> index 656e7fd3ec0..5d3fba76bb9 100644
> --- a/gcc/cp/cvt.c
> +++ b/gcc/cp/cvt.c
> @@ -1027,6 +1027,23 @@ cp_get_callee_fndecl_nofold (tree call)
>     return cp_get_fndecl_from_callee (cp_get_callee (call), false);
>   }
>   
> +/* As above, but is safe to call in a template too where we might get
> +   unexpected trees.  Returns the FUNCTION_DECL or NULL_TREE.  */
> +
> +tree
> +cp_get_callee_fndecl_template (tree call)
> +{
> +  call = cp_get_callee (call);
> +  if (call == NULL_TREE)
> +    return NULL_TREE;
> +  else if (TREE_CODE (call) == FUNCTION_DECL)
> +    return call;
> +  else if (TREE_TYPE (call) && INDIRECT_TYPE_P (TREE_TYPE (call)))
> +    return cp_get_fndecl_from_callee (call, /*fold=*/false);
> +  else
> +    return NULL_TREE;
> +}
> +
>   /* Subroutine of convert_to_void.  Warn if we're discarding something with
>      attribute [[nodiscard]].  */
>   
> diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
> index 3e7ed98fed3..7b63304ebf3 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_template (variant);
>     input_location = save_loc;
>   
>     if (variant)
> diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
> index 4d1592ab0d2..716ee48446d 100644
> --- a/gcc/cp/semantics.c
> +++ b/gcc/cp/semantics.c
> @@ -734,7 +734,7 @@ is_std_constant_evaluated_p (tree fn)
>     if (call_expr_nargs (fn) != 0)
>       return false;
>   
> -  tree fndecl = cp_get_callee_fndecl_nofold (fn);
> +  tree fndecl = cp_get_callee_fndecl_template (fn);
>     if (fndecl == NULL_TREE)
>       return false;
>   
> 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: e42f342652981aa9181cc3ceb04e0eb715506764
> 


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

* Re: [PATCH v2] c++: ICE with -Wall and constexpr if [PR94937]
  2020-05-06 21:26 ` Jason Merrill
@ 2020-05-07 14:55   ` Marek Polacek
  2020-05-18 22:36     ` Jason Merrill
  0 siblings, 1 reply; 4+ messages in thread
From: Marek Polacek @ 2020-05-07 14:55 UTC (permalink / raw)
  To: Jason Merrill; +Cc: GCC Patches

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?

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.

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

-- >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
-- 
Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA


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

* Re: [PATCH v2] c++: ICE with -Wall and constexpr if [PR94937]
  2020-05-07 14:55   ` [PATCH v2] " Marek Polacek
@ 2020-05-18 22:36     ` Jason Merrill
  0 siblings, 0 replies; 4+ messages in thread
From: Jason Merrill @ 2020-05-18 22:36 UTC (permalink / raw)
  To: Marek Polacek; +Cc: GCC Patches

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
> 


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

end of thread, other threads:[~2020-05-18 22:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-05 22:17 [PATCH] c++: ICE with -Wall and constexpr if [PR94937] Marek Polacek
2020-05-06 21:26 ` Jason Merrill
2020-05-07 14:55   ` [PATCH v2] " Marek Polacek
2020-05-18 22:36     ` 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).