From 111a28c29e1f0af6064dd1a3ba2f9eac09ad00eb Mon Sep 17 00:00:00 2001
From: Dodji Seketeli <dodji@gcc.gnu.org>
Date: Thu, 23 Apr 2009 13:15:33 +0200
Subject: [PATCH] re PR c++/38228 (ICE with invalid use of bound member
 function)

    gcc/cp/ChangeLog:
    	PR c++/38228
    	* pt.c (unify): Do not allow the result of a template argument
    	deduction to be a METHOD_TYPE.
    	* cvt.c (cp_convert): Report a meaningful error for non-valid use
    	of pointer to member functions during conversions.
    	* call.c (build_new_op): Report a meaningful error for non-valid
    	use of pointer to member functions in binary expressions.
    	* typeck.c (invalid_nonstatic_memfn_p): Do not crash when EXPR is
    	NULL;

    gcc/testsuite/ChangeLog:
    	PR c++/38228
    	* g++.dg/expr/bound-mem-fun.C: New test.

From-SVN: r146646
---
 gcc/cp/call.c                             | 16 ++++++++++++++--
 gcc/cp/cvt.c                              | 11 +++++++++--
 gcc/cp/pt.c                               |  7 +++++++
 gcc/cp/typeck.c                           |  2 +-
 gcc/testsuite/g++.dg/expr/bound-mem-fun.C | 18 ++++++++++++++++++
 5 files changed, 49 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/expr/bound-mem-fun.C

diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index feb3004517f1..4eae7338c322 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -4072,8 +4072,20 @@ build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
 	default:
 	  if ((flags & LOOKUP_COMPLAIN) && (complain & tf_error))
 	    {
-	      op_error (code, code2, arg1, arg2, arg3, "no match");
-	      print_z_candidates (candidates);
+		/* If one of the arguments of the operator represents
+		   an invalid use of member function pointer, try to report
+		   a meaningful error ...  */
+		if (invalid_nonstatic_memfn_p (arg1, tf_error)
+		    || invalid_nonstatic_memfn_p (arg2, tf_error)
+		    || invalid_nonstatic_memfn_p (arg3, tf_error))
+		  /* We displayed the error message.  */;
+		else
+		  {
+		    /* ... Otherwise, report the more generic
+		       "no matching operator found" error */
+		    op_error (code, code2, arg1, arg2, arg3, "no match");
+		    print_z_candidates (candidates);
+		  }
 	    }
 	  result = error_mark_node;
 	  break;
diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
index 5032f1c03916..6d35245e8233 100644
--- a/gcc/cp/cvt.c
+++ b/gcc/cp/cvt.c
@@ -760,8 +760,15 @@ ocp_convert (tree type, tree expr, int convtype, int flags)
     }
 
   if (flags & LOOKUP_COMPLAIN)
-    error ("conversion from %qT to non-scalar type %qT requested",
-	   TREE_TYPE (expr), type);
+    {
+      /* If the conversion failed and expr was an invalid use of pointer to
+	 member function, try to report a meaningful error.  */
+      if (invalid_nonstatic_memfn_p (expr, tf_warning_or_error))
+	/* We displayed the error message.  */;
+      else
+	error ("conversion from %qT to non-scalar type %qT requested",
+	       TREE_TYPE (expr), type);
+    }
   return error_mark_node;
 }
 
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index a4a5c10af7d7..4844333770d5 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -13581,6 +13581,13 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict)
 	  && !template_parameter_pack_p (parm))
 	return 1;
 
+      /* If the argument deduction results is a METHOD_TYPE,
+         then there is a problem.
+         METHOD_TYPE doesn't map to any real C++ type the result of
+	 the deduction can not be of that type.  */
+      if (TREE_CODE (arg) == METHOD_TYPE)
+	return 1;
+
       TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
       return 0;
 
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 34a20b8af2a7..d9222dcbd061 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -1508,7 +1508,7 @@ cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
 bool
 invalid_nonstatic_memfn_p (const_tree expr, tsubst_flags_t complain)
 {
-  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
+  if (expr && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
     {
       if (complain & tf_error)
         error ("invalid use of non-static member function");
diff --git a/gcc/testsuite/g++.dg/expr/bound-mem-fun.C b/gcc/testsuite/g++.dg/expr/bound-mem-fun.C
new file mode 100644
index 000000000000..9e699b638e36
--- /dev/null
+++ b/gcc/testsuite/g++.dg/expr/bound-mem-fun.C
@@ -0,0 +1,18 @@
+// Contributed by Dodji Seketeli <dodji@redhat.com>
+// Origin PR c++/38228
+// { dg-do "compile" }
+
+struct A
+{
+  A ();
+  template<typename T> A(T);
+};
+
+struct B
+{
+  int foo();
+};
+
+A a = B().*(&B::foo); // { dg-error "invalid use of non-static member function" }
+
+
-- 
GitLab