From: Alejandro Colomar <alx@kernel.org>
To: Seth McDonald <sethmcmail@pm.me>
Cc: linux-man@vger.kernel.org, gcc@gcc.gnu.org
Subject: On restrict (a broken qualifier)
Date: Wed, 21 Jan 2026 15:41:54 +0100 [thread overview]
Message-ID: <aXDecr1b-ke8cN40@devuan> (raw)
In-Reply-To: <aXBuVeyTXLZ67TmH@McDaDebianPC>
[-- Attachment #1: Type: text/plain, Size: 4731 bytes --]
[CC += gcc@]
Hi Seth,
On Wed, Jan 21, 2026 at 06:12:45AM +0000, Seth McDonald wrote:
[...]
> > We might need a qualifiers(7) manual page. Especially, once their rules
> > are modified in ISO C2y. Alternatively, we may need a new section
> > man3qual, with an intro(3qual) page talking about this, and then
> > const(3qual) and volatile(3qual) to document the usual qualifiers, plus
> > a restrict(3qual) documenting how irremediably broken that monster is,
> > and _Atomic(3qual) also documenting that qualifier (which I never really
> > understood well, and from what the committee is talking now, they don't
> > seem to like it either).
>
> I'd be down for a qualifiers(7) man page. Don't know about a whole
> man3qual section though. Unlike library functions (man3), constants
> (man3const), or types (man3type), which are all provided by GNU/Linux,
> qualifiers are a built-in language feature of C.
We have _Countof(3), _Maxof(3), and _Minof(3) manual pages for these
operators (which are also built-in language features (operators) of C.
And we have manual pages for attributes (man3attr).
man3qual would be smaller (the number of qualifiers is rather small),
but it would be consistent, I think.
> One could argue that
> GCC 'provides' them, but I don't think that means they should be
> documented as if they're a feature of GNU/Linux.
The fact that qualifiers are so widespread and unified today that
there's not much variance among implementations doesn't mean that there
aren't outliers. Some implementations (dialects) of C still don't have
qualifiers; an example is Plan9's C compiler, which doesn't have const.
So, const is indeed a feature of GCC (among many others).
> Documenting them in
> the miscellaneous man7 instead conveys how they're important enough to
> document and are related to GNU/Linux, without implying that they're
> part of/provided by GNU/Linux (like other man3* sections).
>
> Btw, I'm curious as to why you say the restrict qualifier is broken.
> I'm yet to encounter much trouble with it, so I'd be interested in its
> flaws.
For a starter, it doesn't follow the usual qualifier rules; being a
qualifier on the pointer, it gets discarded immediately on lvalue
conversion. It essentially behaves as an attribute with the syntax of
a qualifier.
Regarding the semantics, it's defined in terms of accesses, and thus it
is valid to declare two aliasing pointers as restrict as long as they
are not accessed.
alx@devuan:~/tmp$ cat restrict.c
int *f(int *restrict a, int *restrict b);
int
main(void)
{
int x;
f(&x, &x);
}
int *
f(int *restrict a, int *restrict b)
{
*a = 42;
return b;
}
alx@devuan:~/tmp$ gcc -Wall -Wextra restrict.c
restrict.c: In function ‘main’:
restrict.c:8:11: warning: passing argument 1 to ‘restrict’-qualified parameter aliases with argument 2 [-Wrestrict]
8 | f(&x, &x);
| ^~ ~~
The program above is fine according to ISO C. GCC diagnoses in this
case (it is a false positive), but doesn't diagnose on other cases.
Clang doesn't diagnose at all. See below an example that is not
diagnosed by GCC either:
alx@devuan:~/tmp$ cat restrict2.c
int *f(int *restrict a, int **restrict b);
int
main(void)
{
int *x = &(int){0};
f(x, &x);
}
int *
f(int *restrict a, int **restrict b)
{
*a = 42;
*b = a;
return *b;
}
alx@devuan:~/tmp$ gcc -Wall -Wextra restrict2.c
alx@devuan:~/tmp$ clang -Weverything restrict2.c
alx@devuan:~/tmp$
This program is also fine according to ISO C. Neither GCC nor Clang
emit any diagnostics; and they can't! If they diagnosed this, they'd
have to diagnose on valid uses of strtol(3), and that would be
unacceptable.
But if compilers can't diagnose on the call f(x,&x) where f()'s
parameters are restrict, what stops one from passing aliasing pointers
to a function that really accesses its parameters? The compiler can't
know what the function will do with them. Thus, restrict results in
having more UB, and the optimizations it enables can make that UB even
more dangerous.
And unsurprisingly, the rationale for restrict was optimizations, not
diagnostics. That's a bad idea.
restrict should eventually be replaced by an attribute (a qualifier
won't work). Such an attribute should be defined only in terms of
aliasing of pointers, and not on accesses. That would turn it into
a tool for diagnostics instead of a tool for optimization (while it
would still allow for optimizations in most cases), and that is a good
idea.
Have a lovely day!
Alex
--
<https://www.alejandro-colomar.es>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2026-01-21 14:41 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-19 11:54 [PATCH v1 00/19] man/man2/*: Update history of syscalls A-CH Seth McDonald
2026-01-19 11:54 ` [PATCH v1 01/19] man/man2/access.2: HISTORY: Update first POSIX appearance of access(2) Seth McDonald
2026-01-19 11:55 ` [PATCH v1 02/19] man/man2/access.2: HISTORY: Specify different access(2) prototypes Seth McDonald
2026-01-20 1:34 ` Alejandro Colomar
2026-01-20 12:13 ` Seth McDonald
2026-01-20 13:52 ` History of const in C++, C89, and POSIX.1-1988 (was: [PATCH v1 02/19] man/man2/access.2: HISTORY: Specify different) Alejandro Colomar
2026-01-20 13:59 ` Alejandro Colomar
2026-01-21 6:12 ` Seth McDonald
2026-01-21 14:41 ` Alejandro Colomar [this message]
2026-01-21 14:44 ` Alejandro Colomar
2026-01-19 11:55 ` [PATCH v1 03/19] man/man2/access.2: HISTORY: Update first POSIX appearance of faccessat(2) Seth McDonald
2026-01-19 11:55 ` [PATCH v1 04/19] man/man2/alarm.2: HISTORY: Update first POSIX appearance of alarm(2) Seth McDonald
2026-01-19 11:55 ` [PATCH v1 05/19] man/man2/chdir.2: HISTORY: Split chdir(2) and fchdir(2) Seth McDonald
2026-01-19 11:55 ` [PATCH v1 06/19] man/man2/chdir.2: HISTORY: Update first POSIX appearance of chdir(2) Seth McDonald
2026-01-19 11:56 ` [PATCH v1 07/19] man/man2/chdir.2: HISTORY: Specify different chdir(2) prototypes Seth McDonald
2026-01-19 11:56 ` [PATCH v1 08/19] man/man2/chdir.2: HISTORY: Update first POSIX appearance of fchdir(2) Seth McDonald
2026-01-19 11:56 ` [PATCH v1 09/19] man/man2/chmod.2: HISTORY: Split chmod(2) and fchmod(2) Seth McDonald
2026-01-19 11:56 ` [PATCH v1 10/19] man/man2/chmod.2: HISTORY: Update first POSIX appearance of chmod(2) Seth McDonald
2026-01-19 11:57 ` [PATCH v1 11/19] man/man2/chmod.2: HISTORY: Specify different chmod(2) prototypes Seth McDonald
2026-01-19 11:57 ` [PATCH v1 12/19] man/man2/chmod.2: HISTORY: Update first POSIX appearance of fchmod(2) Seth McDonald
2026-01-19 11:57 ` [PATCH v1 13/19] man/man2/chmod.2: HISTORY: Update first POSIX appearance of AT_SYMLINK_NOFOLLOW Seth McDonald
2026-01-19 11:57 ` [PATCH v1 14/19] man/man2/chown.2: HISTORY: Split chown(2), fchown(2), and lchown(2) Seth McDonald
2026-01-19 11:57 ` [PATCH v1 15/19] man/man2/chown.2: HISTORY: Update first POSIX appearance of chown(2) Seth McDonald
2026-01-19 11:58 ` [PATCH v1 16/19] man/man2/chown.2: HISTORY: Specify different chown(2) prototypes Seth McDonald
2026-01-19 11:58 ` [PATCH v1 17/19] man/man2/chown.2: HISTORY: Update first SUS appearance of fchown(2) Seth McDonald
2026-01-19 11:58 ` [PATCH v1 18/19] man/man2/chown.2: HISTORY: Update first POSIX appearance of lchown(2) Seth McDonald
2026-01-19 11:58 ` [PATCH v1 19/19] man/man2/chroot.2: HISTORY: Update first SUS appearance of chroot(2) Seth McDonald
2026-01-20 1:50 ` [PATCH v1 00/19] man/man2/*: Update history of syscalls A-CH Alejandro Colomar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aXDecr1b-ke8cN40@devuan \
--to=alx@kernel.org \
--cc=gcc@gcc.gnu.org \
--cc=linux-man@vger.kernel.org \
--cc=sethmcmail@pm.me \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox