Hello /tccm,

(I'm replying in reverse order of delivery -- sorry.)

On Sat, 21 Nov 2026 08:50:48 +0100, [email protected] wrote:
> Hello Christopher,
>
> On Fri, Nov 20, 2026 at 09:12:47PM +0000, Christopher Bazley wrote:
> > I pushed 205f264b9e01 to mob earlier. It adds -Wnull-to-nonoptional,
> > which warns when a null pointer constant is implicitly converted to a
> > pointer whose target type lacks the _Optional qualifier.
> ...
> > off by default because such conversions are commonplace
>
> Indeed. This is, as Michael Ackermann also seems to point out, already
> a reason to hesitate about whether the addition would be worth it.

I have found it invaluable in large C-language projects where
documentation alone is insufficient to ensure null pointer safety such
as in https://github.com/chrisbazley/SFEditor/

For example, if I transpose a required argument and an optional one,
TCC can point out the mistake at the call, even if the function body
is unavailable.

> Tinycc can hardly aim to include all potentially useful diagnostics,
> because each one adds the size and complexity.

Indeed, but at the heart of the design for _Optional is the idea that
a C compiler is already required to diagnose constraint violations
resulting from misuse of type qualifiers.  Diagnosing such misuse of
_Optional can therefore reuse all of the existing machinery for const,
volatile and restrict.

> > Annex B of ISO/IEC DTS 26716 (WG14 N3951) lists this conversion as one
> > for which an implementation can usefully warn.
>
> Do you have a readable link?
>
> The document at https://www.open-std.org/JTC1/SC22/WG14/www/docs/n3951.pdf
> "2026/08/16 Bazley, _Optional Technical Specification (Committee Draft)"
>
> looks password protected, which other documents which I checked in the
> log https://www.open-std.org/JTC1/SC22/WG14/www/wg14_document_log are not.

Yes, N3951 is password-protected. That is annoying, but unfortunately
the committee manager tells me that ISO's rules require it. N3422 is
an earlier, publicly readable account of the qualifier, with examples
and proposed wording:

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3422.pdf

> Before adding an implementation it is crucial to know _what_ it is
> supposed to correspond in the standard. For the moment there seems to
> exist only your own standardization proposal, as above.

I understand your frustration. N3951 retains the design proposed in
N3422: _Optional qualifies the referenced type, and ordinary pointer
types can still hold null values. -Wnull-to-nonoptional was suggested
in N3422's informative annex and is carried forward into Annex B of
N3951.

The most significant change in N3951 wrt. N3422 is that adding or
subtracting an integer now drops _Optional from the referenced type,
so p + n and &p[n] have the same type:

int *foo(_Optional int *p, int n)
{
    return p + n; // recommended diagnostic but not a constraint violation
}

int *bar(_Optional int *p, int n)
{
    return &p[n]; // recommended diagnostic but not a constraint violation
}

See https://godbolt.org/z/vfYPs8nf4

Another change in N3951 is that array-to-pointer and
function-to-pointer conversions remove _Optional from the resulting
pointer's referenced type:

int *foo(_Optional int (*p)[10])
{
    return *p; // recommended diagnostic but not a constraint violation
}

typedef struct { int a[10]; } S;

int *bar(_Optional S *p)
{
    return p->a; // recommended diagnostic but not a constraint violation
}

typedef int F(void);

F *baz(_Optional F *p)
{
    return *p; // recommended diagnostic but not a constraint violation
}

See https://godbolt.org/z/seKc8nf1z

A minor change in N3951 is that optional-qualified return types are
not a constraint violation: the return type’s top-level qualifier is
ignored as if it had been omitted (just like 'const' or 'volatile'):

int foo(void);
_Optional int foo(void); // compatible redeclaration
const int foo(void); // compatible redeclaration
volatile int foo(void); // compatible redeclaration

See https://godbolt.org/z/r4YPqTvW8

Those changes were a result of real-world experience and/or
reconciling the design with corner-cases of the language.

Experience with SDCC also showed me that useful flow-sensitive
analysis need not impose the costs of a separate, path-sensitive
analysis pass, as it does with Clang's static analyser. That
influenced the balance between automatically removing _Optional in
certain situations and retaining it for analysis.

> According to the log the committee had some concerns about the proposal.

N3952 discusses the design concerns you saw in the document log.
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3952.pdf

At Graz, WG14 voted 12 in favour, 6 against and 7 abstaining to take
the proposed syntax and semantics forward in a Technical
Specification. That work is now ISO/IEC DTS 26716. There were
objections, and the specification is still a draft.  The most common
objection was that the qualifier is not in the same syntactic position
as Clang's _Nullable attribute. N3952 explains why it is actually
preferable that the two approaches can readily be distinguished
syntactically, and that neither approach invalidates the other.

> Non-standard/unstable features are hardly reasonable to support.

I understand and agree with your concern. In this case, I do not think
there is significant risk of the design changing further because:

- it has been discussed extensively with C language experts, including
in WG14's _Optional study group
(https://mailmanlists.uk/mailman/listinfo/optional-study-group).
Please join if you are interested. You do not need to be a member of
the committee to join.
- it has been implemented in Clang, SDCC and TCC, providing
implementation experience across compilers with very different
architectures. SDCC already has users of the feature, giving its
maintainers a reason to preserve compatibility.
- it has been used in real-world C projects, and that usage has
informed the design at every step.

Of course, more implementation and user experience is always valuable,
but at its core the proposal is not radical. An alternative feature
that is not a type qualifier would necessarily need to have very
different syntax and semantics.  The committee has not been
considering any alternative specification.

> > The compiler changes are small
>
> Still, it has grown 25 lines plus about a hundred lines in tests and
> documentation to take care of, if the feature changes. It the feature
> does not end up in the standard, the corresponding code in tinycc
> becomes a source of confusion.
>
> I would strongly suggest to keep your proposed addition to tinycc as
> a patch available separately but not as a part of the tinycc project,
> at least until the feature is standardized.
>
> My 2 cents as a tinycc user.
>
> Friendly,
> /tccm

There is a bit of a "chicken and egg" problem with how the C committee
works. The committee asks "Has the feature been deployed in a
compiler?" whilst compiler maintainers ask "Has the feature been
approved by the committee?"

ISO/IEC DTS 26716 is the vehicle for standardisation. It is true that
the vehicle could yet be derailed procedurally despite all the work
that has gone into it, but either way, the feature already exists in
SDCC and a thorough specification is vital to maintaining it. In
particular, the design is intended to make useful nullability checking
available even in minimalist compilers, without requiring
sophisticated static analysis.

I take your point that every diagnostic costs something to maintain.
My reason for favouring support in TCC itself is that SDCC already
accepts _Optional. Someone compiling the same source with both can
use the same declarations and get useful checking from TCC without
having to maintain a patched compiler.

TCC already supports extensions for compatibility with other
compilers. One of the first results of my work was a fix to one of
them: TCC's __builtin_types_compatible_p gave a different answer from
GCC for qualified array types (4666db3288a9). Implementing _Optional
also exposed cases where TCC lost const or volatile when adjusting
parameter types (5f1425ef2191), or gave a conditional expression
different qualifiers when its operands were reversed (0cd015b4ed36).

Testing _Optional exposed a gap in TCC's C23 support. It lacked
typeof_unqual, which was straightforward to add using the existing
typeof parser (51effecceccd). Once I could compile SFEditor with TCC,
I also found that passing a const structure through an ellipsis on
x86-64 could elicit an "assignment of read-only location" warning. TCC
was warning about its own copy of the argument! I fixed that in
fc1fc50f7a5b.

Those changes are useful to people who will never use _Optional. For
me, the case for keeping the warning is that it lets TCC check
interfaces shared with SDCC using its existing conversion machinery.
The wider benefit of doing the work in TCC is that it has also helped
me find and fix problems in the compiler.

Thanks,
Christopher

_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to