Bug 767538

Summary: Compilation error when defining the same type twice with typedef
Product: Red Hat Enterprise Linux 6 Reporter: Christophe Fergeau <cfergeau>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact: qe-baseos-tools-bugs
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 6.2CC: dodji, dodji
Target Milestone: rc   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2011-12-14 13:28:18 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Christophe Fergeau 2011-12-14 10:31:59 UTC
Description of problem:

Original problem can be found at https://bugzilla.gnome.org/show_bug.cgi?id=666081

This can be reduced to:

typedef int Foo;
typedef int Foo;

which on RHEL 6.2 gives

$ LC_ALL=C gcc -c typedef.c
typedef.c:2: error: redefinition of typedef 'Foo'
typedef.c:1: note: previous declaration of 'Foo' was here

The same program compiles with no errors nor warnings (-Wall -Wextra) on Fedora 16


https://bugzilla.gnome.org/show_bug.cgi?id=666081#c1 says:

"Dodji Seketeli [developer] 2011-12-13 12:59:18 UTC

Although the re-definition is unfortunate and should trigger a
warning, I think the code is valid, so this is a GCC issue.  I
believe this code is accepted in recent GCCs.  AIUI, the part of
the code in the C front-end in GCC that deals with this (in
trunk) is, in gcc/c-decl.c:

static bool
diagnose_mismatched_decls (tree newdecl, tree olddecl,
               tree *newtypep, tree *oldtypep)
{
[...]
      else if (pedantic && !flag_isoc1x)
    {
      pedwarn (input_location, OPT_pedantic,
           "redefinition of typedef %q+D", newdecl);
      locate_old_decl (olddecl);
    }
[...]
}"


Version-Release number of selected component (if applicable):

$ gcc --version
gcc (GCC) 4.4.6 20110731 (Red Hat 4.4.6-3)

$ rpm -q gcc
gcc-4.4.6-3.el6.x86_64

Comment 2 Jakub Jelinek 2011-12-14 13:28:18 UTC
The duplicate typedefs are invalid in both ISO C89 and C99, it is only valid in the upcoming C1X standard that is being worked on.  GCC 4.6 and later allow it as an extension (except for -pedantic-errors) in preparation for C1X standard, but that doesn't change anything on it being invalid in C89 and C99, so I'm against backporting those changes to GCC 4.4 or earlier.  Just fix up your sources.

Comment 3 Christophe Fergeau 2011-12-14 14:14:36 UTC
Oh ok, I assumed that this was valid C since gcc 4.6 accepted it, didn't think of the various revisions of the standard. Thanks a lot for the input, next time I'll do more homework before opening a bug.

Comment 4 Dodji Seketeli 2011-12-14 15:47:13 UTC
The C specification is a source of never-ending learning to me.  :-)

So it looks like the part of the C99 specification that forbids re-declarations of typedefs is 6.7/3:

"If an identifier has no linkage, there shall be no more than one declaration of the identifier (in a declarator or type specifier) with the same scope and in the same name space, except for tags as specified in 6.7.2.3."

And typedef names are not tags.

Later, in C1X, it was decided to allow the redeclaration of typedefs of the same type in the same tag, as C++ allows: http://www.velocityreviews.com/forums/t674488-benign-typedef-definition.html.