public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/53196] New: unknown struct name in C99 compound initializer doesn't generate error
@ 2012-05-02 17:55 lzie59p1 at frolickingcow dot net
  2012-05-02 19:13 ` [Bug c/53196] " manu at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: lzie59p1 at frolickingcow dot net @ 2012-05-02 17:55 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53196

             Bug #: 53196
           Summary: unknown struct name in C99 compound initializer
                    doesn't generate error
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: lzie59p1@frolickingcow.net


I think this code shouldn't compile (see foo_typo). It compiles on 4.5.3 and
4.7.0.

#include <stdio.h>

struct foo {
    int i;
};

int
main(void)
{
    struct foo f = (struct foo_typo) { };
    printf("%d\n", f.i);
    return 0;
}


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

* [Bug c/53196] unknown struct name in C99 compound initializer doesn't generate error
  2012-05-02 17:55 [Bug c/53196] New: unknown struct name in C99 compound initializer doesn't generate error lzie59p1 at frolickingcow dot net
@ 2012-05-02 19:13 ` manu at gcc dot gnu.org
  2012-05-02 19:32 ` redi at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: manu at gcc dot gnu.org @ 2012-05-02 19:13 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53196

Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manu at gcc dot gnu.org

--- Comment #1 from Manuel López-Ibáñez <manu at gcc dot gnu.org> 2012-05-02 19:12:56 UTC ---
It fails to give an error with gcc 4.3 and gcc 4.8 also. Clang complains:

/home/manuel/pr53196.c:8:18: error: variable has incomplete type 'struct
foo_typo'
  struct foo f = (struct foo_typo) { };
                 ^~~~~~~~~~~~~~~~~~~~~
/home/manuel/pr53196.c:8:26: note: forward declaration of 'struct foo_typo'     
  struct foo f = (struct foo_typo) { };
                         ^
1 error generated.

Interestingly, the code without the typo:

struct foo {
  int i;
};

int
main(void)
{
  struct foo f = (struct foo) { };
  return 0;
}

is rejected by g++:


/home/manuel/pr53196.c:8:19: error: expected primary-expression before ‘struct’
   struct foo f = (struct foo) { };
                   ^
/home/manuel/pr53196.c:8:19: error: expected ‘)’ before ‘struct’
   struct foo f = (struct foo) { };
                   ^


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

* [Bug c/53196] unknown struct name in C99 compound initializer doesn't generate error
  2012-05-02 17:55 [Bug c/53196] New: unknown struct name in C99 compound initializer doesn't generate error lzie59p1 at frolickingcow dot net
  2012-05-02 19:13 ` [Bug c/53196] " manu at gcc dot gnu.org
@ 2012-05-02 19:32 ` redi at gcc dot gnu.org
  2012-05-02 19:52 ` manu at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2012-05-02 19:32 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53196

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-05-02 19:32:03 UTC ---
(In reply to comment #1)
> Interestingly, the code without the typo:
> 
> struct foo {
>   int i;
> };
> 
> int
> main(void)
> {
>   struct foo f = (struct foo) { };
>   return 0;
> }
> 
> is rejected by g++:

That's simply because compound literals aren't valid in C++.

Also, to be valid C the initializer should be { 0 }, allowing an empty
initializer is a GNU C extension.


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

* [Bug c/53196] unknown struct name in C99 compound initializer doesn't generate error
  2012-05-02 17:55 [Bug c/53196] New: unknown struct name in C99 compound initializer doesn't generate error lzie59p1 at frolickingcow dot net
  2012-05-02 19:13 ` [Bug c/53196] " manu at gcc dot gnu.org
  2012-05-02 19:32 ` redi at gcc dot gnu.org
@ 2012-05-02 19:52 ` manu at gcc dot gnu.org
  2012-05-02 20:00 ` joseph at codesourcery dot com
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: manu at gcc dot gnu.org @ 2012-05-02 19:52 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53196

--- Comment #3 from Manuel López-Ibáñez <manu at gcc dot gnu.org> 2012-05-02 19:52:08 UTC ---
(In reply to comment #2)
> That's simply because compound literals aren't valid in C++.

So this page is wrong? 
http://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html


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

* [Bug c/53196] unknown struct name in C99 compound initializer doesn't generate error
  2012-05-02 17:55 [Bug c/53196] New: unknown struct name in C99 compound initializer doesn't generate error lzie59p1 at frolickingcow dot net
                   ` (2 preceding siblings ...)
  2012-05-02 19:52 ` manu at gcc dot gnu.org
@ 2012-05-02 20:00 ` joseph at codesourcery dot com
  2012-05-02 20:11 ` manu at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: joseph at codesourcery dot com @ 2012-05-02 20:00 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53196

--- Comment #4 from joseph at codesourcery dot com <joseph at codesourcery dot com> 2012-05-02 19:59:46 UTC ---
On Wed, 2 May 2012, redi at gcc dot gnu.org wrote:

> Also, to be valid C the initializer should be { 0 }, allowing an empty
> initializer is a GNU C extension.

And indeed, with -std=c99 -pedantic-errors GCC rejects the C code for that 
reason.  The more specific error should certainly be detected, though.  
See also bug 51034, a previous report again involving incomplete types in 
compound literals.


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

* [Bug c/53196] unknown struct name in C99 compound initializer doesn't generate error
  2012-05-02 17:55 [Bug c/53196] New: unknown struct name in C99 compound initializer doesn't generate error lzie59p1 at frolickingcow dot net
                   ` (3 preceding siblings ...)
  2012-05-02 20:00 ` joseph at codesourcery dot com
@ 2012-05-02 20:11 ` manu at gcc dot gnu.org
  2012-05-02 21:17 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: manu at gcc dot gnu.org @ 2012-05-02 20:11 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53196

Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |DUPLICATE

--- Comment #5 from Manuel López-Ibáñez <manu at gcc dot gnu.org> 2012-05-02 20:11:37 UTC ---
I think this is exactly the same issue as:

struct S { int x[sizeof((struct T){})];} s;

*** This bug has been marked as a duplicate of bug 51034 ***


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

* [Bug c/53196] unknown struct name in C99 compound initializer doesn't generate error
  2012-05-02 17:55 [Bug c/53196] New: unknown struct name in C99 compound initializer doesn't generate error lzie59p1 at frolickingcow dot net
                   ` (4 preceding siblings ...)
  2012-05-02 20:11 ` manu at gcc dot gnu.org
@ 2012-05-02 21:17 ` redi at gcc dot gnu.org
  2012-05-02 21:22 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2012-05-02 21:17 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53196

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-05-02 21:17:01 UTC ---
(In reply to comment #3)
> (In reply to comment #2)
> > That's simply because compound literals aren't valid in C++.
> 
> So this page is wrong? 
> http://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html

I didn't realise it was claimed to work in C++, but yes, I guess the docs are
wrong.  C++ provides several portable forms of initialization, there's really
no need to support another non-standard one.

These will all work for aggregate types (including "C structs")

foo f{};
foo f = {};
foo f = foo{};
foo f = foo();

I'll propose a patch to fix the docs.


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

* [Bug c/53196] unknown struct name in C99 compound initializer doesn't generate error
  2012-05-02 17:55 [Bug c/53196] New: unknown struct name in C99 compound initializer doesn't generate error lzie59p1 at frolickingcow dot net
                   ` (5 preceding siblings ...)
  2012-05-02 21:17 ` redi at gcc dot gnu.org
@ 2012-05-02 21:22 ` redi at gcc dot gnu.org
  2012-06-12 21:16 ` jakub at gcc dot gnu.org
  2013-02-19 20:37 ` LpSolit at netscape dot net
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2012-05-02 21:22 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53196

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-05-02 21:21:49 UTC ---
Actually, I'm wrong, G++ does still support compound literals, but only with a
non-empty initializer.  Maybe I'll open a separate bug for that.


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

* [Bug c/53196] unknown struct name in C99 compound initializer doesn't generate error
  2012-05-02 17:55 [Bug c/53196] New: unknown struct name in C99 compound initializer doesn't generate error lzie59p1 at frolickingcow dot net
                   ` (6 preceding siblings ...)
  2012-05-02 21:22 ` redi at gcc dot gnu.org
@ 2012-06-12 21:16 ` jakub at gcc dot gnu.org
  2013-02-19 20:37 ` LpSolit at netscape dot net
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2012-06-12 21:16 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53196

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> 2012-06-12 21:16:24 UTC ---
Author: jakub
Date: Tue Jun 12 21:16:20 2012
New Revision: 188483

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=188483
Log:
    PR c/53532
    PR c/51034
    PR c/53196
    * c-decl.c (build_compound_literal): Call c_incomplete_type_error
    if type isn't complete.

    * gcc.dg/pr53532.c: New test.
    * gcc.dg/c99-complit-2.c: Add two new dg-error directives,
    adjust line numbers.
    * gcc.dg/noncompile/950825-1.c: Expect incomplete type error message.
    * gcc.dg/Wcxx-compat-8.c: Likewise.
    * gcc.dg/pr51034.c: New test.
    * gcc.dg/pr53196-1.c: New test.
    * gcc.dg/pr53196-2.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/pr51034.c
    trunk/gcc/testsuite/gcc.dg/pr53196-1.c
    trunk/gcc/testsuite/gcc.dg/pr53196-2.c
    trunk/gcc/testsuite/gcc.dg/pr53532.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-decl.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.dg/Wcxx-compat-8.c
    trunk/gcc/testsuite/gcc.dg/c99-complit-2.c
    trunk/gcc/testsuite/gcc.dg/noncompile/950825-1.c


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

* [Bug c/53196] unknown struct name in C99 compound initializer doesn't generate error
  2012-05-02 17:55 [Bug c/53196] New: unknown struct name in C99 compound initializer doesn't generate error lzie59p1 at frolickingcow dot net
                   ` (7 preceding siblings ...)
  2012-06-12 21:16 ` jakub at gcc dot gnu.org
@ 2013-02-19 20:37 ` LpSolit at netscape dot net
  8 siblings, 0 replies; 10+ messages in thread
From: LpSolit at netscape dot net @ 2013-02-19 20:37 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53196

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> 2012-06-12 21:16:24 UTC ---
Author: jakub
Date: Tue Jun 12 21:16:20 2012
New Revision: 188483

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=188483
Log:
    PR c/53532
    PR c/51034
    PR c/53196
    * c-decl.c (build_compound_literal): Call c_incomplete_type_error
    if type isn't complete.

    * gcc.dg/pr53532.c: New test.
    * gcc.dg/c99-complit-2.c: Add two new dg-error directives,
    adjust line numbers.
    * gcc.dg/noncompile/950825-1.c: Expect incomplete type error message.
    * gcc.dg/Wcxx-compat-8.c: Likewise.
    * gcc.dg/pr51034.c: New test.
    * gcc.dg/pr53196-1.c: New test.
    * gcc.dg/pr53196-2.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/pr51034.c
    trunk/gcc/testsuite/gcc.dg/pr53196-1.c
    trunk/gcc/testsuite/gcc.dg/pr53196-2.c
    trunk/gcc/testsuite/gcc.dg/pr53532.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-decl.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.dg/Wcxx-compat-8.c
    trunk/gcc/testsuite/gcc.dg/c99-complit-2.c
    trunk/gcc/testsuite/gcc.dg/noncompile/950825-1.c


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

end of thread, other threads:[~2013-02-19 20:37 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-02 17:55 [Bug c/53196] New: unknown struct name in C99 compound initializer doesn't generate error lzie59p1 at frolickingcow dot net
2012-05-02 19:13 ` [Bug c/53196] " manu at gcc dot gnu.org
2012-05-02 19:32 ` redi at gcc dot gnu.org
2012-05-02 19:52 ` manu at gcc dot gnu.org
2012-05-02 20:00 ` joseph at codesourcery dot com
2012-05-02 20:11 ` manu at gcc dot gnu.org
2012-05-02 21:17 ` redi at gcc dot gnu.org
2012-05-02 21:22 ` redi at gcc dot gnu.org
2012-06-12 21:16 ` jakub at gcc dot gnu.org
2013-02-19 20:37 ` LpSolit at netscape dot net

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).