Bug 88727 - Diagnostics improvement: Detection of undefined behaviour. Incomplete type in tenative definition with internal linkage. [-Wtentative-definition-incomplete-type]
Summary: Diagnostics improvement: Detection of undefined behaviour. Incomplete type in...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 9.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid, diagnostic
: 102821 (view as bug list)
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2019-01-06 21:20 UTC by Anders Granlund
Modified: 2021-10-18 22:05 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-01-09 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Anders Granlund 2019-01-06 21:20:45 UTC
Test case (prog.c):

  static struct S s;

  int main()
  {
  }

  struct S { int x; };

Compilation command line:

  gcc prog.c -Wall -Wextra -std=c11 -pedantic-errors 

Observed behaviour:

  No error messages outputes.

Possible improvement of behaviour:

  Outputing an error message about using an incomplete type in the tenative
  definition  static struct S s; .

  The program has undefined behaviour becuase of a violation of 6.9.2/2:

  "If the declaration of an identifier for an object is a tentative definition
   and has internal linkage, the declared type shall not be an incomplete type."

  GCC detects such undefined behaviour in other cases (for example using the 
  incomplete type int []). It would be good if it could also hande the case in
  the test case for this bug report.

Note:

  Clang detects the undefined behaviour for this program and outputs an error
  message.
Comment 1 jsm-csl@polyomino.org.uk 2019-01-07 18:34:01 UTC
This was the case mentioned in bug 26581 comment 4, but without a separate 
bug filed for it at the time.
Comment 2 Eric Gallager 2019-01-09 18:30:47 UTC
(In reply to Anders Granlund from comment #0)
> Test case (prog.c):
> 
>   static struct S s;
> 
>   int main()
>   {
>   }
> 
>   struct S { int x; };
> 
> Compilation command line:
> 
>   gcc prog.c -Wall -Wextra -std=c11 -pedantic-errors 
> 
> Observed behaviour:
> 
>   No error messages outputes.

No errors, sure, but I do get a warning from -Wunused-variable:

$ gcc -c -Wall -Wextra -std=c11 -pedantic-errors 88727.c
88727.c:1:17: warning: 's' defined but not used [-Wunused-variable]
 static struct S s;
                 ^
$

> 
> Possible improvement of behaviour:
> 
>   Outputing an error message about using an incomplete type in the tenative
>   definition  static struct S s; .
> 
>   The program has undefined behaviour becuase of a violation of 6.9.2/2:
> 
>   "If the declaration of an identifier for an object is a tentative
> definition
>    and has internal linkage, the declared type shall not be an incomplete
> type."
> 
>   GCC detects such undefined behaviour in other cases (for example using the 
>   incomplete type int []). It would be good if it could also hande the case
> in
>   the test case for this bug report.
> 
> Note:
> 
>   Clang detects the undefined behaviour for this program and outputs an error
>   message.

Confirmed that clang errors, its output is:

$ clang -c -Wall -Wextra -std=c11 -pedantic-errors 88727.c
88727.c:1:17: error: tentative definition of variable with internal linkage has incomplete non-array type 'struct S' [-Werror,-Wtentative-definition-incomplete-type]
static struct S s;
                ^
88727.c:1:15: note: forward declaration of 'struct S'
static struct S s;
              ^
1 error generated.
$

Since it's a warning-turned-into-an-error I'm making this block the new-warning bug.
Comment 3 Joseph S. Myers 2021-10-18 22:05:23 UTC
*** Bug 102821 has been marked as a duplicate of this bug. ***