Bug 31489 - error says struct when it should say class
Summary: error says struct when it should say class
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.2
: P3 normal
Target Milestone: 4.7.0
Assignee: Paolo Carlini
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-04-05 23:49 UTC by Jonny Grant
Modified: 2011-09-27 21:54 UTC (History)
0 users

See Also:
Host: i686-gnu-linux
Target: i686-gnu-linux
Build: i686-gnu-linux
Known to work:
Known to fail:
Last reconfirmed: 2007-04-07 16:23:28


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonny Grant 2007-04-05 23:49:53 UTC
When a forward declared class is not found the error message describes it incorrectly as a struct. See below for an example.

I'd like to submit a patch to correctly display class. Let me know if such a patch would be considered.

$ g++ -Wall -ggdb -O0 -o t test6.cpp
test6.cpp: In function ‘int main()’:
test6.cpp:8: error: invalid use of undefined type ‘struct foobar’
test6.cpp:4: error: forward declaration of ‘struct foobar’

File contains:
=====================================
// g++ -Wall -ggdb -O0 -o t test6.cpp


class foobar;

int main()
{
	foobar * o = new foobar;
}
====================================
Comment 1 Andrew Pinski 2007-04-06 00:09:14 UTC
I don't think this matters anyways as class == struct and can be used interchangably for tags.

if you summit a patch, see what this shows you:
struct foobar;

int main()
{
        foobar * o = new foobar;
}

Or even this:

struct foobar;
class foobar;

int main()
{
        foobar * o = new foobar;
}

Since all three are the same code, so it is weird not to have struct == class here really.
Comment 2 Wolfgang Bangerth 2007-04-07 16:23:28 UTC
Confirmed.

I think a patch would be of interest. Maybe one could just tweak
the error to read

x.cc:5: error: invalid use of undefined type 'foobar'
x.cc:1: error: forward declaration of struct or class 'foobar'
Comment 3 Paolo Carlini 2011-09-27 10:06:09 UTC
I just discovered that actually we *do* have machinery to do this, in general terms (see CLASSTYPE_DECLARED_CLASS, used by eg, class_key_or_enum_as_string). But cp_parser_elaborated_type_specifier doesn't use it, ie doesn't set CLASSTYPE_DECLARED_CLASS (at variance with cp_parser_class_head). Thus something like the following patchlet should do the trick. That said, Andrew has a point of course, for its second example we would say struct or class, whichever comes last in the redeclaration. All in all, seems an improvement to me, if the below works pretty well I'm going to submit it.

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

Index: parser.c
===================================================================
--- parser.c    (revision 179242)
+++ parser.c    (working copy)
@@ -13423,8 +13423,15 @@ cp_parser_elaborated_type_specifier (cp_parser* pa
     }
 
   if (tag_type != enum_type)
-    cp_parser_check_class_key (tag_type, type);
+    {
+      /* Indicate whether this class was declared as a `class' or as a
+        `struct'.  */
+      if (TREE_CODE (type) == RECORD_TYPE)
+       CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
 
+      cp_parser_check_class_key (tag_type, type);
+    }
+
   /* A "<" cannot follow an elaborated type specifier.  If that
      happens, the user was probably trying to form a template-id.  */
   cp_parser_check_for_invalid_template_id (parser, type, token->location);
Comment 4 paolo@gcc.gnu.org 2011-09-27 21:52:23 UTC
Author: paolo
Date: Tue Sep 27 21:52:19 2011
New Revision: 179293

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=179293
Log:
/cp
2011-09-27  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/31489
	* parser.c (cp_parser_elaborated_type_specifier): For RECORD_TYPE,
	set CLASSTYPE_DECLARED_CLASS.

/testsuite
2011-09-27  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/31489
	* g++.dg/parse/error40.C: New.
	* g++.dg/warn/incomplete1.C: Adjust.

Added:
    trunk/gcc/testsuite/g++.dg/parse/error40.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/parser.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/warn/incomplete1.C
Comment 5 Paolo Carlini 2011-09-27 21:54:26 UTC
Fixed for 4.7.0.