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

2020-05-06 Thread Jason Merrill via 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 000..6e0b2597a53
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if34.C
@@ -0,0 +1,15 @@
+// PR c++/94937 - ICE with -Wall and constexpr

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

2020-05-05 Thread Marek Polacek via 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 000..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
+struct C {
+  static void bar ()