Skip to content

Commit b896d26

Browse files
authored
[C23][N3006] Documented behavior of underspecified object declarations (#140911)
This PR is documenting the behavior of Clang towards underspecified object declarations in C23 as advised by @AaronBallman.
1 parent cc2d5fa commit b896d26

File tree

4 files changed

+190
-1
lines changed

4 files changed

+190
-1
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6502,3 +6502,77 @@ qualifications.
65026502
Note, Clang does not allow an ``_Atomic`` function type because
65036503
of explicit constraints against atomically qualified (arrays and) function
65046504
types.
6505+
6506+
6507+
Underspecified Object Declarations in C
6508+
=======================================
6509+
6510+
C23 introduced the notion of `underspecified object declarations <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3006.htm>`_
6511+
(note, the final standards text is different from WG14 N3006 due to changes
6512+
during national body comment review). When an object is declared with the
6513+
``constexpr`` storage class specifier or has a deduced type (with the ``auto``
6514+
specifier), it is said to be "underspecified". Underspecified declarations have
6515+
different requirements than non-underspecified declarations. In particular, the
6516+
identifier being declared cannot be used in its initialization. e.g.,
6517+
6518+
.. code-block:: c
6519+
6520+
auto x = x; // Invalid
6521+
constexpr int y = y; // Invalid
6522+
6523+
The standard leaves it implementation-defined whether an underspecified
6524+
declaration may introduce additional identifiers as part of the declaration.
6525+
6526+
Clang allows additional identifiers to be declared in the following cases:
6527+
6528+
* A compound literal may introduce a new type. e.g.,
6529+
6530+
.. code-block:: c
6531+
6532+
auto x = (struct S { int x, y; }){ 1, 2 }; // Accepted by Clang
6533+
constexpr int i = (struct T { int x; }){ 1 }.x; // Accepted by Clang
6534+
6535+
* The type specifier for a ``constexpr`` declaration may define a new type.
6536+
e.g.,
6537+
6538+
.. code-block:: c
6539+
6540+
constexpr struct S { int x; } s = { 1 }; // Accepted by Clang
6541+
6542+
* A function declarator may be declared with parameters, including parameters
6543+
which introduce a new type. e.g.,
6544+
6545+
.. code-block:: c
6546+
6547+
constexpr int (*fp)(int x) = nullptr; // Accepted by Clang
6548+
auto f = (void (*)(struct S { int x; } s))nullptr; // Accepted by Clang
6549+
6550+
* The initializer may contain a GNU statement expression which defines new
6551+
types or objects. e.g.,
6552+
6553+
.. code-block:: c
6554+
6555+
constexpr int i = ({ // Accepted by Clang
6556+
constexpr int x = 12;
6557+
constexpr struct S { int x; } s = { x };
6558+
s.x;
6559+
});
6560+
auto x = ({ struct S { int x; } s = { 0 }; s; }); // Accepted by Clang
6561+
6562+
Clang intentionally does not implement the changed scoping rules from C23
6563+
for underspecified declarations. Doing so would significantly complicate the
6564+
implementation in order to get reasonable diagnostic behavior and also means
6565+
Clang fails to reject some code that should be rejected. e.g.,
6566+
6567+
.. code-block:: c
6568+
6569+
// This should be rejected because 'x' is not in scope within the initializer
6570+
// of an underspecified declaration. Clang accepts because it treats the scope
6571+
// of the identifier as beginning immediately after the declarator, same as with
6572+
// a non-underspecified declaration.
6573+
constexpr int x = sizeof(x);
6574+
6575+
// Clang rejects this code with a diagnostic about using the variable within its
6576+
// own initializer rather than rejecting the code with an undeclared identifier
6577+
// diagnostic.
6578+
auto x = x;

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ C23 Feature Support
289289
directive. Fixes #GH126940.
290290
- Fixed a crash when a declaration of a ``constexpr`` variable with an invalid
291291
type. Fixes #GH140887
292+
- Documented `WG14 N3006 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3006.htm>`_
293+
which clarified how Clang is handling underspecified object declarations.
292294

293295
C11 Feature Support
294296
^^^^^^^^^^^^^^^^^^^

clang/test/C/C23/n3006.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// RUN: %clang_cc1 -std=c23 -verify %s
2+
3+
/* WG14 N3006: Yes
4+
* Underspecified object declarations
5+
*/
6+
7+
void struct_test(void) {
8+
struct S1 { int x, y; }; // expected-note {{field 'x' has type 'int' here}}
9+
10+
auto normal_struct = (struct S1){ 1, 2 };
11+
auto normal_struct2 = (struct S1) { .x = 1, .y = 2 };
12+
auto underspecified_struct = (struct S2 { int x, y; }){ 1, 2 };
13+
auto underspecified_struct_redef = (struct S1 { char x, y; }){ 'A', 'B'}; // expected-error {{type 'struct S1' has incompatible definitions}} \
14+
expected-error {{cannot use 'auto' with array in C}} \
15+
expected-note {{field 'x' has type 'char' here}}
16+
auto underspecified_empty_struct = (struct S3 { }){ };
17+
auto zero_init_struct = (struct S4 { int x; }){ 0 };
18+
int field_struct = (struct S5 { int y; }){ 0 }.y;
19+
}
20+
21+
void union_test(void) {
22+
union U1 { int a; double b; }; // expected-note {{field 'a' has type 'int' here}}
23+
24+
auto normal_union_int = (union U1){ .a = 12 };
25+
auto normal_union_double = (union U1){ .b = 2.4 };
26+
auto underspecified_union = (union U2 { int a; double b; }){ .a = 34 };
27+
auto underspecified_union_redef = (union U1 { char a; double b; }){ .a = 'A' }; // expected-error {{type 'union U1' has incompatible definitions}} \
28+
expected-error {{cannot use 'auto' with array in C}} \
29+
expected-note {{field 'a' has type 'char' here}}
30+
auto underspecified_empty_union = (union U3 { }){ };
31+
}
32+
33+
void enum_test(void) {
34+
enum E1 { FOO, BAR }; // expected-note {{enumerator 'BAR' with value 1 here}}
35+
36+
auto normal_enum_foo = (enum E1){ FOO };
37+
auto normal_enum_bar = (enum E1){ BAR };
38+
auto underspecified_enum = (enum E2 { BAZ, QUX }){ BAZ };
39+
auto underspecified_enum_redef = (enum E1 { ONE, TWO }){ ONE }; // expected-error {{type 'enum E1' has incompatible definitions}} \
40+
expected-error {{cannot use 'auto' with array in C}} \
41+
expected-note {{enumerator 'ONE' with value 0 here}}
42+
auto underspecified_empty_enum = (enum E3 { }){ }; // expected-error {{use of empty enum}}
43+
auto underspecified_undeclared_enum = (enum E4){ FOO }; // expected-error {{variable has incomplete type 'enum E4'}} \
44+
expected-note {{forward declaration of 'enum E4'}}
45+
}
46+
47+
void constexpr_test(void) {
48+
constexpr auto ce_struct = (struct S1){ 1, 2 }; // expected-error {{variable has incomplete type 'struct S1'}} \
49+
expected-note {{forward declaration of 'struct S1'}}
50+
constexpr auto ce_struct_zero_init = (struct S2 { int x; }){ 0 };
51+
constexpr int ce_struct_field = (struct S3 { int y; }){ 0 }.y;
52+
constexpr auto ce_union = (union U1){ .a = 12 }; // expected-error {{variable has incomplete type 'union U1'}} \
53+
expected-note {{forward declaration of 'union U1'}}
54+
55+
constexpr auto ce_enum = (enum E1 { BAZ, QUX }){ BAZ };
56+
constexpr auto ce_empty_enum = (enum E2){ FOO }; // expected-error {{use of undeclared identifier 'FOO'}}
57+
}
58+
59+
void self_reference_test(void) {
60+
constexpr int i = i; // expected-error {{constexpr variable 'i' must be initialized by a constant expression}} \
61+
expected-note {{read of object outside its lifetime is not allowed in a constant expression}}
62+
auto j = j; // expected-error {{variable 'j' declared with deduced type 'auto' cannot appear in its own initializer}}
63+
}
64+
65+
void redefinition_test(void) {
66+
const struct S { int x; } s; // expected-warning {{default initialization of an object of type 'const struct S' leaves the object uninitialized}} \
67+
expected-note {{previous definition is here}}
68+
constexpr struct S s = {0}; // expected-error {{redefinition of 's'}}
69+
}
70+
71+
void declaring_an_underspecified_defied_object_test(void) {
72+
struct S { int x, y; };
73+
constexpr int i = (struct T { int a, b; }){0, 1}.a;
74+
75+
struct T t = { 1, 2 };
76+
}
77+
78+
void constexpr_complience_test(void) {
79+
int x = (struct Foo { int x; }){ 0 }.x;
80+
constexpr int y = (struct Bar { int x; }){ 0 }.x;
81+
}
82+
83+
void builtin_functions_test(void) {
84+
constexpr typeof(struct s *) x = 0;
85+
auto so = sizeof(struct S {});
86+
auto to = (typeof(struct S {})){};
87+
}
88+
89+
void misc_test(void) {
90+
constexpr struct S { int a, b; } y = { 0 };
91+
constexpr int a = 0, b = 0;
92+
auto c = (struct T { int x, y; }){0, 0};
93+
auto s2 = ({struct T { int x; } s = {}; s.x; });
94+
auto s3 = ((struct {}){},0); // expected-warning {{left operand of comma operator has no effect}}
95+
constexpr int (*fp)(struct X { int x; } val) = 0;
96+
auto v = (void (*)(int y))0;
97+
}
98+
99+
void misc_struct_test(void) {
100+
constexpr struct {
101+
int a;
102+
} a = {};
103+
104+
constexpr struct {
105+
int b;
106+
} b = (struct S { int x; }){ 0 }; // expected-error-re {{initializing 'const struct (unnamed struct at {{.*}}n3006.c:104:13)' with an expression of incompatible type 'struct S'}}
107+
108+
auto z = ({
109+
int a = 12;
110+
struct {} s;
111+
a;
112+
});
113+
}

clang/www/c_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ <h2 id="c2x">C23 implementation status</h2>
864864
<tr>
865865
<td>Underspecified object definitions</td>
866866
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3006.htm">N3006</a></td>
867-
<td class="none" align="center">No</td>
867+
<td class="unreleased" align="center">Yes</td>
868868
</tr>
869869
<tr>
870870
<td>Type inference for object declarations</td>

0 commit comments

Comments
 (0)