Error out for incomplete type initialisation

This commit is contained in:
Rob Pilling 2023-11-26 09:29:06 +00:00
parent be8f894710
commit fb164e0ab4
3 changed files with 22 additions and 1 deletions

View file

@ -7996,6 +7996,11 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
toplevel array or the last member of the toplevel struct */
if (size < 0) {
// error out except for top-level incomplete arrays
// (arrays of incomplete types are handled in array parsing)
if (!(type->t & VT_ARRAY))
tcc_error("initialization of incomplete type");
/* If the base type itself was an array type of unspecified size
(like in 'typedef int arr[]; arr x = {1};') then we will
overwrite the unknown size by the real one for this decl.

View file

@ -463,4 +463,17 @@ int main() {
#error \123\\
456
#elif defined test_error_incomplete_type
struct A;
void f(struct A *);
int main()
{
f(&(struct A){});
}
struct A {
int x;
};
#endif

View file

@ -23,7 +23,7 @@
[returns 1]
[test_61_undefined_enum]
60_errors_and_warnings.c:46: error: unknown type size
60_errors_and_warnings.c:46: error: initialization of incomplete type
[test_74_non_const_init]
60_errors_and_warnings.c:49: error: initializer element is not constant
@ -228,3 +228,6 @@ arg[1] = "Y"
[test_error_string]
60_errors_and_warnings.c:464: error: #error \123\456
[test_error_incomplete_type]
60_errors_and_warnings.c:472: error: initialization of incomplete type