Bug 15938 - ICE with anonymous unions
Summary: ICE with anonymous unions
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.0
: P2 minor
Target Milestone: 4.1.0
Assignee: Giovanni Bajo
URL:
Keywords: ice-on-valid-code, monitored
Depends on:
Blocks:
 
Reported: 2004-06-11 12:23 UTC by Volker Reichelt
Modified: 2005-11-27 23:38 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 2.95.3 3.0.4 3.2.2 3.2.3 3.3.1 4.0.0 3.4.0
Last reconfirmed: 2005-09-04 18:20:25


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Volker Reichelt 2004-06-11 12:24:02 UTC
The following code is accepted by the C frontend, but the C++ frontend
segfaults (since gcc 2.95.x).

===================================
typedef union
{
    struct { int i; };
    struct { char c; };
} A;

A a = { 0 };
===================================

bug.cc:7: error: union `A' with no named members cannot be initialized
bug.cc:7: internal compiler error: Segmentation fault
Please submit a full bug report, [etc.]

Apart from the segfault, should the code be accepted by the C++ frontend
as well or should it be rejected as it is now?

BTW, the PR was inspired by a question on the gcc mailing list:
http://gcc.gnu.org/ml/gcc/2004-06/msg00158.html
Comment 1 Andrew Pinski 2004-06-11 13:33:16 UTC
Confirmed.
Comment 2 Paolo Carlini 2005-03-20 20:13:44 UTC
Seenms doable...
Comment 3 Volker Reichelt 2005-07-20 13:17:33 UTC
On mainline the code is accepted since today.

This is probably due to Giovanni's patch
http://gcc.gnu.org/ml/gcc-cvs/2005-07/msg00718.html
Comment 4 Giovanni Bajo 2005-07-21 01:51:07 UTC
Yes, it's because of my patch.

I would like to know if we agree that the code is invalid or not. It's a bit 
hard to have a definitive answer since it is GNU C++ (uses an extension), but 
given that the C frontend accepts it, I don't see why we should reject it. In 
which case, this bug can be closed as fixed.
Comment 5 Gabriel Dos Reis 2005-07-21 02:35:42 UTC
Subject: Re:  ICE with anonymous unions

"giovannibajo at libero dot it" <gcc-bugzilla@gcc.gnu.org> writes:

| Yes, it's because of my patch.
| 
| I would like to know if we agree that the code is invalid or not. It's a bit 
| hard to have a definitive answer since it is GNU C++ (uses an extension), but 
| given that the C frontend accepts it, I don't see why we should reject it. In 
| which case, this bug can be closed as fixed.

[I will address the validity issue as far as GNU C++ is concerned ]

Clearly, this being invalid according to ISO C++ and accepted by the C
front-end (with appropriate diagnostic), the only question that
remains is whether this is valid C++

  typedef union {
     struct unique_name_1 { int i; };
     struct unique_name_2 { char c; };
  } A;

The answer is yes.  Now, the remaning thing is apply the name 
restrictions as for unnamed unions.

Thanks,

-- Gaby
Comment 6 Giovanni Bajo 2005-07-21 13:18:50 UTC
=================================================
typedef union
{
  struct { int i; };
  struct { char c; };
} A;

A a = { 0 };
A b = {{ 0 }};
A c = {{{ 0 }}};     // { dg-error "braces" }
=================================================
pr15938.cc:9: error: braces around scalar initializer for type 'int'

I believe everything is correct.
Volker, would you mind committing this testcase and closing this bug?
Comment 7 CVS Commits 2005-07-21 14:50:51 UTC
Subject: Bug 15938

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	reichelt@gcc.gnu.org	2005-07-21 14:50:47

Modified files:
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/g++.dg/init: union2.C 

Log message:
	PR c++/15938
	* g++.dg/init/union2.C: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.5795&r2=1.5796
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/init/union2.C.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 8 Volker Reichelt 2005-07-21 15:12:35 UTC
The ICE is fixed now. But maybe some additional code cleanup is needed:

Giovanni, what about the following code snippet from typeck2.c?
Is the comment still valid? What about the error message, is it
still triggered in some cases?

      /* Find the first named field.  ANSI decided in September 1990
	 that only named fields count here.  */
      tree field = TYPE_FIELDS (type);
      while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
	field = TREE_CHAIN (field);
      if (!field)
	{
	  error ("union %qT with no named members cannot be initialized",
		 type);
	  ce->value = error_mark_node;
	}
      ce->index = field;
Comment 9 Giovanni Bajo 2005-07-21 15:32:10 UTC
It might indeed be obsolete code: I don't think you can currently create an 
union (or a record) with only unnamed fields. If you want to purse this 
further, you could regtest changing this:

      if (!field)
	{
	  error ("union %qT with no named members cannot be initialized",
		 type);
	  ce->value = error_mark_node;
	}

with "gcc_assert (field);". Which will probably succeed, as there are no 
occurrences of this error message in my last g++.log.
Comment 10 Volker Reichelt 2005-10-13 11:22:09 UTC
Closing, since the problem is fixed on mainline.

I'll submit a patch to remove the obsolete code once 4.1 has branched.
Comment 11 Volker Reichelt 2005-11-25 15:45:03 UTC
I removed the dead code as discussed in comment #8 and later:
http://gcc.gnu.org/ml/gcc-cvs/2005-11/msg01213.html
Comment 12 Giovanni Bajo 2005-11-27 23:38:43 UTC
Thanks Volker