public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C++ Patch] PR 60686
@ 2014-07-07 18:20 Paolo Carlini
  2014-07-08 22:39 ` Jason Merrill
  0 siblings, 1 reply; 6+ messages in thread
From: Paolo Carlini @ 2014-07-07 18:20 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jason Merrill

[-- Attachment #1: Type: text/plain, Size: 758 bytes --]

Hi,

in this bug submitter noticed that talking only about explicit 
constructors without mentioning conversion operators is misleading in 
C++11. Thus Jon suggested simply not mentioning the constructors in the 
error message, which I find reasonable because after all it's about the 
fact that the user has 'explicit' on the definition, not about 
'explicit' being used on the wrong type of declaration. Certainly 
anyway, another option would be extending the current error message and 
mention conversion operators too or even providing different error 
messages for C++98 and C++11 (maybe overkill considering that we do 
*accept* explicit operators in C++98 mode, we only emit a pedwarn). 
Tested x86_64-linux.

Thanks,
Paolo.

///////////////////////

[-- Attachment #2: CL_60686 --]
[-- Type: text/plain, Size: 377 bytes --]

/cp
2014-07-07  Jonathan Wakely  <jwakely@redhat.com>
	    Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/60686
	* decl.c (grokdeclarator): Adjust error message about 'explicit'
	outside class definition for C++11.

/testsuite
2014-07-07  Jonathan Wakely  <jwakely@redhat.com>
	    Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/60686
	* g++.dg/cpp0x/explicit8.C: New.

[-- Attachment #3: patch_60686 --]
[-- Type: text/plain, Size: 1207 bytes --]

Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 212332)
+++ cp/decl.c	(working copy)
@@ -10117,9 +10117,10 @@ grokdeclarator (const cp_declarator *declarator,
 
   if (explicitp == 1 || (explicitp && friendp))
     {
-      /* [dcl.fct.spec] The explicit specifier shall only be used in
-	 declarations of constructors within a class definition.  */
-      error ("only declarations of constructors can be %<explicit%>");
+      /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
+	 in the declaration of a constructor or conversion function within
+	 a class definition.  */
+      error ("only declarations can be marked %<explicit%>");
       explicitp = 0;
     }
 
Index: testsuite/g++.dg/cpp0x/explicit8.C
===================================================================
--- testsuite/g++.dg/cpp0x/explicit8.C	(revision 0)
+++ testsuite/g++.dg/cpp0x/explicit8.C	(working copy)
@@ -0,0 +1,8 @@
+// PR c++/60686
+// { dg-do compile { target c++11 } }
+
+struct A {
+  explicit operator int() const;
+};
+
+explicit inline A::operator int() const { return 1; }  // { dg-error "only declarations can be marked 'explicit'" }

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

* Re: [C++ Patch] PR 60686
  2014-07-07 18:20 [C++ Patch] PR 60686 Paolo Carlini
@ 2014-07-08 22:39 ` Jason Merrill
  2014-07-09 10:11   ` Paolo Carlini
  0 siblings, 1 reply; 6+ messages in thread
From: Jason Merrill @ 2014-07-08 22:39 UTC (permalink / raw)
  To: Paolo Carlini, gcc-patches

On 07/07/2014 11:15 AM, Paolo Carlini wrote:
> +      error ("only declarations can be marked %<explicit%>");

That's pretty unclear, since a definition is a declaration.

Let's split this into three error messages: If the problem is that we're 
outside the class, we should say that.  If the problem is that it's not 
a constructor or conversion function, we should say that.  If the 
problem is that it's not a member of the current class, we should say that.

Jason

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

* Re: [C++ Patch] PR 60686
  2014-07-08 22:39 ` Jason Merrill
@ 2014-07-09 10:11   ` Paolo Carlini
  2014-07-09 20:34     ` Jason Merrill
  0 siblings, 1 reply; 6+ messages in thread
From: Paolo Carlini @ 2014-07-09 10:11 UTC (permalink / raw)
  To: Jason Merrill, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 919 bytes --]

Hi,

On 07/09/2014 12:39 AM, Jason Merrill wrote:
> On 07/07/2014 11:15 AM, Paolo Carlini wrote:
>> +      error ("only declarations can be marked %<explicit%>");
>
> That's pretty unclear, since a definition is a declaration.
>
> Let's split this into three error messages: If the problem is that 
> we're outside the class, we should say that.  If the problem is that 
> it's not a constructor or conversion function, we should say that.  If 
> the problem is that it's not a member of the current class, we should 
> say that.
Ok. In terms of wording, for the first case we can consistently follow 
the example of 'virtual'. For the second case, we can simply extend the 
existing wording. The third case, I don't think it can really happen, 
because there are earlier checks which simply return error_mark_node if 
a declaration is within the wrong class... I tested the below.

Thanks,
Paolo.

///////////////////

[-- Attachment #2: CL_60686_2 --]
[-- Type: text/plain, Size: 292 bytes --]

/cp
2014-07-09  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/60686
	* decl.c (grokdeclarator): Adjust error message about 'explicit'
	outside class declaration for C++11.

/testsuite
2014-07-09  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/60686
	* g++.dg/cpp0x/explicit8.C: New.

[-- Attachment #3: patch_60686_2 --]
[-- Type: text/plain, Size: 1559 bytes --]

Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 212385)
+++ cp/decl.c	(working copy)
@@ -10117,9 +10117,14 @@ grokdeclarator (const cp_declarator *declarator,
 
   if (explicitp == 1 || (explicitp && friendp))
     {
-      /* [dcl.fct.spec] The explicit specifier shall only be used in
-	 declarations of constructors within a class definition.  */
-      error ("only declarations of constructors can be %<explicit%>");
+      /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
+	 in the declaration of a constructor or conversion function within
+	 a class definition.  */
+      if (!current_class_type)
+	error ("%<explicit%> outside class declaration");
+      else
+	error ("only declarations of constructors and conversion operators "
+	       "can be %<explicit%>");
       explicitp = 0;
     }
 
Index: testsuite/g++.dg/cpp0x/explicit8.C
===================================================================
--- testsuite/g++.dg/cpp0x/explicit8.C	(revision 0)
+++ testsuite/g++.dg/cpp0x/explicit8.C	(working copy)
@@ -0,0 +1,14 @@
+// PR c++/60686
+// { dg-do compile { target c++11 } }
+
+struct A {
+  explicit operator int() const;
+};
+
+explicit inline A::operator int() const { return 1; }  // { dg-error "'explicit' outside class declaration" }
+
+struct B {
+  explicit void f();  // { dg-error "only declarations of constructors and conversion operators can be 'explicit'" }
+};
+
+explicit void B::f() { }  // { dg-error "'explicit' outside class declaration" }

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

* Re: [C++ Patch] PR 60686
  2014-07-09 10:11   ` Paolo Carlini
@ 2014-07-09 20:34     ` Jason Merrill
  2014-07-09 21:20       ` Paolo Carlini
  0 siblings, 1 reply; 6+ messages in thread
From: Jason Merrill @ 2014-07-09 20:34 UTC (permalink / raw)
  To: Paolo Carlini, gcc-patches

On 07/09/2014 06:07 AM, Paolo Carlini wrote:
> The third case, I don't think it can really happen,
> because there are earlier checks which simply return error_mark_node if
> a declaration is within the wrong class...

Not if it's a friend declaration:

struct A {
   explicit A(int);
};

struct B {
   explicit friend A::A(int);
};

Jason

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

* Re: [C++ Patch] PR 60686
  2014-07-09 20:34     ` Jason Merrill
@ 2014-07-09 21:20       ` Paolo Carlini
  2014-07-09 21:29         ` Jason Merrill
  0 siblings, 1 reply; 6+ messages in thread
From: Paolo Carlini @ 2014-07-09 21:20 UTC (permalink / raw)
  To: Jason Merrill, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 521 bytes --]

Hi,

On 07/09/2014 10:34 PM, Jason Merrill wrote:
> On 07/09/2014 06:07 AM, Paolo Carlini wrote:
>> The third case, I don't think it can really happen,
>> because there are earlier checks which simply return error_mark_node if
>> a declaration is within the wrong class...
> Not if it's a friend declaration:
>
> struct A {
>   explicit A(int);
> };
>
> struct B {
>   explicit friend A::A(int);
> };
Oops, sorry, we even test for friends... Then I'm finishing testing the 
below.

Thanks,
Paolo.

//////////////////////

[-- Attachment #2: patch_60686_3 --]
[-- Type: text/plain, Size: 1770 bytes --]

Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 212385)
+++ cp/decl.c	(working copy)
@@ -10117,9 +10117,16 @@ grokdeclarator (const cp_declarator *declarator,
 
   if (explicitp == 1 || (explicitp && friendp))
     {
-      /* [dcl.fct.spec] The explicit specifier shall only be used in
-	 declarations of constructors within a class definition.  */
-      error ("only declarations of constructors can be %<explicit%>");
+      /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
+	 in the declaration of a constructor or conversion function within
+	 a class definition.  */
+      if (!current_class_type)
+	error ("%<explicit%> outside class declaration");
+      else if (friendp)
+	error ("%<explicit%> in friend declaration");
+      else
+	error ("only declarations of constructors and conversion operators "
+	       "can be %<explicit%>");
       explicitp = 0;
     }
 
Index: testsuite/g++.dg/cpp0x/explicit8.C
===================================================================
--- testsuite/g++.dg/cpp0x/explicit8.C	(revision 0)
+++ testsuite/g++.dg/cpp0x/explicit8.C	(working copy)
@@ -0,0 +1,22 @@
+// PR c++/60686
+// { dg-do compile { target c++11 } }
+
+struct A {
+  explicit operator int() const;
+};
+
+explicit inline A::operator int() const { return 1; }  // { dg-error "'explicit' outside class declaration" }
+
+struct B {
+  explicit void f();  // { dg-error "only declarations of constructors and conversion operators can be 'explicit'" }
+};
+
+explicit void B::f() { }  // { dg-error "'explicit' outside class declaration" }
+
+struct C {
+  explicit C(int);
+};
+
+struct D {
+  explicit friend C::C(int);  // { dg-error "'explicit' in friend declaration" }
+};

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

* Re: [C++ Patch] PR 60686
  2014-07-09 21:20       ` Paolo Carlini
@ 2014-07-09 21:29         ` Jason Merrill
  0 siblings, 0 replies; 6+ messages in thread
From: Jason Merrill @ 2014-07-09 21:29 UTC (permalink / raw)
  To: Paolo Carlini, gcc-patches

OK, thanks.

Jason

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

end of thread, other threads:[~2014-07-09 21:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-07 18:20 [C++ Patch] PR 60686 Paolo Carlini
2014-07-08 22:39 ` Jason Merrill
2014-07-09 10:11   ` Paolo Carlini
2014-07-09 20:34     ` Jason Merrill
2014-07-09 21:20       ` Paolo Carlini
2014-07-09 21:29         ` 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).