Patchwork [C++] PR 44524

login
register
mail settings
Submitter Paolo Carlini
Date Oct. 17, 2011, 1:32 p.m.
Message ID <4E9C2E61.80109@oracle.com>
Download mbox | patch
Permalink /patch/120205/
State New
Headers show

Comments

Paolo Carlini - Oct. 17, 2011, 1:32 p.m.
Hi,

here submitter requests a more accurate error message for X.Y where X is 
a pointer to class type. Thus the below, tested x86_64-linux.

Ok for mainline?

Thanks,
Paolo.

//////////////////////////
/cp
2011-10-17  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/44524
	* typeck.c (build_class_member_access_expr): Provide a better error
	message for X.Y where X is a pointer to class type.
	(finish_class_member_access_expr): Likewise.

/testsuite
2011-10-17  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/44524
	* g++.dg/parse/error41.C: New.
	* g++.dg/parse/error20.C: Adjust.
Gabriel Dos Reis - Oct. 17, 2011, 1:39 p.m.
On Mon, Oct 17, 2011 at 8:32 AM, Paolo Carlini <paolo.carlini@oracle.com> wrote:
> Hi,
>
> here submitter requests a more accurate error message for X.Y where X is a
> pointer to class type. Thus the below, tested x86_64-linux.
>
> Ok for mainline?

s/is of pointer type/has pointer type/g


>
> Thanks,
> Paolo.
>
> //////////////////////////
>
Paolo Carlini - Oct. 17, 2011, 1:39 p.m.
On 10/17/2011 03:39 PM, Gabriel Dos Reis wrote:
> On Mon, Oct 17, 2011 at 8:32 AM, Paolo Carlini<paolo.carlini@oracle.com>  wrote:
>> Hi,
>>
>> here submitter requests a more accurate error message for X.Y where X is a
>> pointer to class type. Thus the below, tested x86_64-linux.
>>
>> Ok for mainline?
> s/is of pointer type/has pointer type/g
Thanks, changed in my local tree. By the way, I wondered that, then 
found a couple of instances of 'has pointer' in the tree, but only in 
comments, if I remember correctly.

Paolo.
Jason Merrill - Oct. 17, 2011, 5:42 p.m.
OK.

Jason

Patch

Index: testsuite/g++.dg/parse/error20.C
===================================================================
--- testsuite/g++.dg/parse/error20.C	(revision 180087)
+++ testsuite/g++.dg/parse/error20.C	(working copy)
@@ -12,7 +12,7 @@  struct C {
 };
 int main() {
   C c;
-  A(c.p.i); // { dg-error "9:request for member 'i' in 'c.C::p', which is of non-class type 'B" }
+  A(c.p.i); // { dg-error "9:request for member 'i' in 'c.C::p', which has pointer type 'B" }
   return 0;
 }
 
Index: testsuite/g++.dg/parse/error41.C
===================================================================
--- testsuite/g++.dg/parse/error41.C	(revision 0)
+++ testsuite/g++.dg/parse/error41.C	(revision 0)
@@ -0,0 +1,11 @@ 
+// PR c++/44524
+
+template<typename, typename>
+struct map
+{
+  bool empty();
+};
+
+int bar(map<int, float> *X) {
+  return X.empty();  // { dg-error "which has pointer type 'map" }
+}
Index: cp/typeck.c
===================================================================
--- cp/typeck.c	(revision 180087)
+++ cp/typeck.c	(working copy)
@@ -2128,8 +2128,16 @@  build_class_member_access_expr (tree object, tree
   if (!CLASS_TYPE_P (object_type))
     {
       if (complain & tf_error)
-	error ("request for member %qD in %qE, which is of non-class type %qT",
-	       member, object, object_type);
+	{
+	  if (POINTER_TYPE_P (object_type)
+	      && CLASS_TYPE_P (TREE_TYPE (object_type)))
+	    error ("request for member %qD in %qE, which has pointer "
+		   "type %qT (maybe you meant to use %<->%> ?)",
+		   member, object, object_type);
+	  else
+	    error ("request for member %qD in %qE, which is of non-class "
+		   "type %qT", member, object, object_type);
+	}
       return error_mark_node;
     }
 
@@ -2508,8 +2516,16 @@  finish_class_member_access_expr (tree object, tree
   if (!CLASS_TYPE_P (object_type))
     {
       if (complain & tf_error)
-	error ("request for member %qD in %qE, which is of non-class type %qT",
-	       name, object, object_type);
+	{
+	  if (POINTER_TYPE_P (object_type)
+	      && CLASS_TYPE_P (TREE_TYPE (object_type)))
+	    error ("request for member %qD in %qE, which has pointer "
+		   "type %qT (maybe you meant to use %<->%> ?)",
+		   name, object, object_type);
+	  else
+	    error ("request for member %qD in %qE, which is of non-class "
+		   "type %qT", name, object, object_type);
+	}
       return error_mark_node;
     }