1

Is applying _Atomic type qualifier to an incomplete type valid?

Example:

_Atomic void* p;
_Atomic struct S* p1;
_Atomic struct S x;
struct S { int i; };

Invocations:

$ gcc t0.c -std=c11 -pedantic -Wall -Wextra -c
<nothing>

$ clang t0.c -std=c11 -pedantic -Wall -Wextra -c
<source>:1:1: error: _Atomic cannot be applied to incomplete type 'void' 
<source>:2:1: error: _Atomic cannot be applied to incomplete type 'struct S' 
<source>:3:1: error: _Atomic cannot be applied to incomplete type 'struct S'

In the C11 standard (as well as in the C2x draft n2596.pdf) I couldn't find any constraints / semantics, which forbid / restrict applying _Atomic to an incomplete type.

pmor
  • 5,392
  • 4
  • 17
  • 36
  • I'm not sure what the point is. You can't dereference a void pointer. – Barmar Mar 24 '22 at 21:56
  • You need to cast it before doing anything useful with it, and you can use `_Atomic` in the typecast. – Barmar Mar 24 '22 at 21:57
  • @Barmar: A point could be that implicit conversions cannot remove qualifiers (a diagnostic would be required), so you could have an `_Atomic void *p` whose data type can be completed later but for which the `_Atomic` qualifier could not be accidentally removed. This is in fact useful with `const`, where the comparison routine passed to `qsort` has parameters of type `const void *`. – Eric Postpischil Mar 24 '22 at 22:06
  • 1
    In the C standard, I do not see a restriction on using `_Atomic` with an incomplete type. – Eric Postpischil Mar 24 '22 at 22:16
  • It's true that you won't *"find any constraints / semantics, which forbid / restrict applying _Atomic to an incomplete type."* However, if incomplete atomic types *are* forbidden, they are **forbidden by omission** per 6.2.5/27 which states *"The phrase "qualified or unqualified type", without specific mention of atomic, does not include the atomic types."* That makes it rather hard to do a quick search, since you're searching for the lack of the word "atomic". – user3386109 Mar 24 '22 at 23:28
  • @user3386109 Is there a dependency between an incomplete type and type qualifiers? I couldn't find it. – pmor Mar 25 '22 at 14:40
  • @pmor I think you need to go through the entire specification, find every paragraph that contains any mention of a qualified, unqualified, or atomic type, and then add those to your question. Then we can debate the meaning of those individual paragraphs, and what they mean when taken as a whole. – user3386109 Mar 25 '22 at 18:16

1 Answers1

1

From https://github.com/llvm/llvm-project/issues/36585:

it does appear to be valid

pmor
  • 5,392
  • 4
  • 17
  • 36