Patchwork [C++] PR 51786

login
register
mail settings
Submitter Paolo Carlini
Date July 7, 2013, 4:32 p.m.
Message ID <51D9982C.2040502@oracle.com>
Download mbox | patch
Permalink /patch/257349/
State New
Headers show

Comments

Paolo Carlini - July 7, 2013, 4:32 p.m.
Hi,

we got a couple of duplicates of this. The issue is that things like:

struct A {};
void foo() { decltype(A()); }

are turned by finish_decltype_type, called by 
cp_parser_simple_declaration via cp_parser_decl_specifier_seq, into 
something like:

struct A {};
void foo() { struct A; }

and later check_tag_decl, called via shadow_tag, doesn't see anything 
wrong wrt [dcl.dcl]/3, to wit doesn't see a declaration which doesn't 
declare anything. "Luckily" we have declares_class_or_enum available and 
we can query it to figure out whether a diagnostic is in fact in order.

Tested x86_64-linux.

Thanks,
Paolo.

PS: in fact we issue a permerror, not an error: I did some tests and I 
don't think that unconditionally zeroing the type even if we end up only 
emitting a warning can cause problems. But please double check, thanks!

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

	PR c++/51786
	* parser.c (cp_parser_simple_declaration): Before calling shadow_tag
	also check declares_class_or_enum.

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

	PR c++/51786
	* g++.dg/cpp0x/pr51786.C: New.
	* g++.dg/cpp0x/auto30.C: Update.
Jason Merrill - July 8, 2013, 4:11 p.m.
On 07/07/2013 12:32 PM, Paolo Carlini wrote:
> -auto int; // { dg-error "multiple types|can only be specified for variables" }
> +auto int; // { dg-error "multiple types|does not declare anything" }

This is a regression.  Please limit the change to when the type is a class.

Jason
Paolo Carlini - July 8, 2013, 5:49 p.m.
Hi,

>On 07/07/2013 12:32 PM, Paolo Carlini wrote:
>> -auto int; // { dg-error "multiple types|can only be specified for
>variables" }
>> +auto int; // { dg-error "multiple types|does not declare anything" }
>
>This is a regression.  Please limit the change to when the type is a
>class.

Ah I see. I take your indication as meaning class *or enum*: it seems to me that like this we precisely 'undo' the effect of finish_decltype_type and we cover the other half of the new testcase. I'll send something later today.

Thanks,
Paolo
Jason Merrill - July 8, 2013, 6:01 p.m.
On 07/08/2013 01:49 PM, Paolo Carlini wrote:
> Ah I see. I take your indication as meaning class *or enum*

Yes.

Jason

Patch

Index: cp/parser.c
===================================================================
--- cp/parser.c	(revision 200742)
+++ cp/parser.c	(working copy)
@@ -11009,11 +11009,19 @@  cp_parser_simple_declaration (cp_parser* parser,
 
   /* Issue an error message if no declarators are present, and the
      decl-specifier-seq does not itself declare a class or
-     enumeration.  */
+     enumeration: [dcl.dcl]/3.  */
   if (!saw_declarator)
     {
       if (cp_parser_declares_only_class_p (parser))
-	shadow_tag (&decl_specifiers);
+	{
+	  if (!declares_class_or_enum
+	      && decl_specifiers.type != error_mark_node)
+	    /* Ensure an error is issued anyway when finish_decltype_type,
+	       called via cp_parser_decl_specifier_seq, returns something
+	       sensible (c++/51786).  */
+	    decl_specifiers.type = NULL_TREE;
+	  shadow_tag (&decl_specifiers);
+	}
       /* Perform any deferred access checks.  */
       perform_deferred_access_checks (tf_warning_or_error);
     }
Index: testsuite/g++.dg/cpp0x/auto30.C
===================================================================
--- testsuite/g++.dg/cpp0x/auto30.C	(revision 200742)
+++ testsuite/g++.dg/cpp0x/auto30.C	(working copy)
@@ -6,4 +6,4 @@  struct A
     auto friend struct B; // { dg-error "multiple types|can only be specified|friend" }
 };
 
-auto int; // { dg-error "multiple types|can only be specified for variables" }
+auto int; // { dg-error "multiple types|does not declare anything" }
Index: testsuite/g++.dg/cpp0x/pr51786.C
===================================================================
--- testsuite/g++.dg/cpp0x/pr51786.C	(revision 0)
+++ testsuite/g++.dg/cpp0x/pr51786.C	(working copy)
@@ -0,0 +1,8 @@ 
+// PR c++/51786
+// { dg-do compile { target c++11 } }
+
+enum E {};
+struct A {};
+
+void foo() { decltype(E{}); }  // { dg-error "does not declare anything" }
+void bar() { decltype(A{}); }  // { dg-error "does not declare anything" }