On Sat, Dec 15, 2018 at 08:36:25AM +0000, Bernd Edlinger wrote: > this patch implements an error message, for non-static initialization of a > flexible array member. > This duplicates the existing error message from the C-FE, to avoid ICE and > wrong code generation > issues, as pointed out in the PR. > > It is a bit funny that a non-functional feature like that has already rather > much test coverage. > The most easy adjustment seems to change the existing test cases to use > static declarations. > > > Bootstrapped and reg-tested on x86_64-pc-linux-gnu. > Is it OK for trunk?
Will defer to Jason or others, just a few nits. Conceptually I'm all for making C and C++ behave the same here and it seems the C behavior has been the same for years (3.2 behaves the same as current trunk). > gcc/cp: > 2018-12-15 Bernd Edlinger <bernd.edlin...@hotmail.de> > > PR c++/88261 > * typeck2.c (digest_init_r): Add a decl parameter. Raise an error > for non-static initialization of a flexible array member. > (process_init_constructor, digest_init_flags, > massage_init_elt, process_init_constructor_array, > process_init_constructor_record, process_init_constructor_union, > process_init_constructor): Add a decl parameter and pass it thru. > (digest_nsdmi_init): Pass decl parameter to digest_init_flags. > (digest_init): Pass NULL as decl parameter to digest_init_r. > * semantics.c (finish_compound_literal): Likewise. > * cp-tree.h (digest_init_flags): Adjust prototype. 8 spaces instead of tab. > gcc/testsuite: > 2018-12-15 Bernd Edlinger <bernd.edlin...@hotmail.de> > > PR c++/88261 > * gcc.dg/array-6.c: Move to ... > * c-c++-common/array-6.c: ... here. Would be better to have more complete test coverage. In particular, the C FE passes on the following extension of array-6.c, note "str" or {} is rejected, but if there is no initializer at all, it is accepted. And, if it isn't already covered in the testsuite, would be good to verify also how it behaves in new, initializer list and any other kind of C++ style initializer, etc. (check for the errors you've added). So void bar() { str x { 2, "a" }; } etc. /* PR c/5597 */ /* { dg-do compile } */ /* { dg-options "" } */ /* Verify that GCC forbids non-static initialization of flexible array members. */ struct str { int len; char s[]; }; struct str a = { 2, "a" }; void foo() { static struct str b = { 2, "b" }; struct str c = { 2, "c" }; /* { dg-error "(non-static)|(near initialization)" } */ struct str d = (struct str) { 2, "d" }; /* { dg-error "(non-static)|(near initialization)" } */ struct str e = (struct str) { d.len, "e" }; /* { dg-error "(non-static)|(initialization)" } */ } struct str f = { 0, {} }; void bar() { static struct str g = { 0, {} }; struct str h = { 0, {} }; /* { dg-error "(non-static)|(near initialization)" } */ struct str i = (struct str) { 0, {} }; /* { dg-error "(non-static)|(near initialization)" } */ struct str j = (struct str) { i.len, {} }; /* { dg-error "(non-static)|(initialization)" } */ } struct str k = { 0 }; void baz() { static struct str l = { 0 }; struct str m = { 0 }; struct str n = (struct str) { 0 }; struct str o = (struct str) { n.len }; } struct str p = {}; void qux() { static struct str q = {}; struct str r = {}; struct str s = (struct str) {}; } Jakub