diff mbox

[C++] PR 48630 (PR 31423)

Message ID 4E9F4265.6080809@oracle.com
State New
Headers show

Commit Message

Paolo Carlini Oct. 19, 2011, 9:34 p.m. UTC
Hi,

in these two twin PRs filed by Ian and Gerald, it is pointed out that 
cases like:

    struct C {
      int f() { return 1; }
    };

    int f(C&c) {
      return ( 1 == c.f );
    }

where the user actually forgot the open-closed round braces are much 
more likely than cases where an ampersand is missing, still we output

invalid use of member (did you forget the ‘&’ ?)

Thus, the idea of saying instead

invalid use of member (did you forget the ‘()’ ?)

which I implemented in the patchlet below. Alternately we could give 
both hints, or refer to the argument list.

Tested x86_64-linux.

Thanks,
Paolo.

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

	PR c++/31423
	PR c++/48630
	* typeck2.c (cxx_incomplete_type_diagnostic): Improve error message
	for invalid use of member.

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

	PR c++/31423
	PR c++/48630
	* g++.dg/parse/error42.C: New.

Comments

Gabriel Dos Reis Oct. 19, 2011, 9:43 p.m. UTC | #1
On Wed, Oct 19, 2011 at 4:34 PM, Paolo Carlini <paolo.carlini@oracle.com> wrote:
> Hi,
>
> in these two twin PRs filed by Ian and Gerald, it is pointed out that cases
> like:
>
>   struct C {
>     int f() { return 1; }
>   };
>
>   int f(C&c) {
>     return ( 1 == c.f );
>   }
>
> where the user actually forgot the open-closed round braces are much more
> likely than cases where an ampersand is missing, still we output
>
> invalid use of member (did you forget the ‘&’ ?)
>
> Thus, the idea of saying instead
>
> invalid use of member (did you forget the ‘()’ ?)
>
> which I implemented in the patchlet below. Alternately we could give both
> hints, or refer to the argument list.

I agree that '()' is a better default.  But we can do better.
We can use the type context, e.g. in initialization or
return-statement etc., to decide
whether () is intended (by looking at the TREE_TYPE of a member function type),
and only in cases where we can't tell we suggest both alternative:

     (did you intend a function call or address of non-static member function?)
Jason Merrill Oct. 19, 2011, 10:32 p.m. UTC | #2
Surely we should only make this change for function members.

Jason
diff mbox

Patch

Index: testsuite/g++.dg/parse/error42.C
===================================================================
--- testsuite/g++.dg/parse/error42.C	(revision 0)
+++ testsuite/g++.dg/parse/error42.C	(revision 0)
@@ -0,0 +1,5 @@ 
+// PR c++/48630
+// { dg-options "" }
+
+class C { public: C* f(); int get(); }; // { dg-error "forget the '\\(\\)'|base operand" }
+int f(C* p) { return p->f->get(); }
Index: cp/typeck2.c
===================================================================
--- cp/typeck2.c	(revision 180206)
+++ cp/typeck2.c	(working copy)
@@ -429,7 +429,7 @@  cxx_incomplete_type_diagnostic (const_tree value,
     case OFFSET_TYPE:
     bad_member:
       emit_diagnostic (diag_kind, input_location, 0,
-		       "invalid use of member (did you forget the %<&%> ?)");
+		       "invalid use of member (did you forget the %<()%> ?)");
       break;
 
     case TEMPLATE_TYPE_PARM: