Deutsch   English   Français   Italiano  
<vsnh11$28q4m$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.tomockey.net!news.samoylyk.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Janis Papanagnou <janis_papanagnou+ng@hotmail.com>
Newsgroups: comp.lang.c
Subject: Re: "A diagram of C23 basic types"
Date: Fri, 4 Apr 2025 04:43:42 +0200
Organization: A noiseless patient Spider
Lines: 66
Message-ID: <vsnh11$28q4m$1@dont-email.me>
References: <87y0wjaysg.fsf@gmail.com> <vsj1m8$1f8h2$1@dont-email.me>
 <vsj2l9$1j0as$1@dont-email.me> <vsjef3$1u4nk$1@dont-email.me>
 <vsjg6t$20pdb$1@dont-email.me> <vsjjd1$23ukt$1@dont-email.me>
 <vsjkvb$25mtg$1@dont-email.me> <vpdHP.1828825$TBhc.94105@fx16.iad>
 <vslhrm$7uv3$1@dont-email.me> <vsll4b$8mfb$3@dont-email.me>
 <vslq6b$ginf$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 04 Apr 2025 04:43:45 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="ac03f2dc200c60912e1ee472e742a9fb";
	logging-data="2386070"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+p7gkQmKF9ldVARLjnINZl"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
 Thunderbird/45.8.0
Cancel-Lock: sha1:TYvFN3K7LAsssSVy+BO9ZjEU1Zc=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <vslq6b$ginf$1@dont-email.me>
Bytes: 4222

On 03.04.2025 13:07, Muttley@DastardlyHQ.org wrote:
> On Thu, 3 Apr 2025 11:41:31 +0200
> David Brown <david.brown@hesbynett.no> wibbled:

[ "unreachable()" is now standard. ]

>> I can't tell you what Scott uses it for, but I have used gcc's 
>> __builtin_unreachable() a fair number of times in my coding.  I use it 
>> to inform both the compiler and human readers that a path is unreachable:
> 
> What for? The compiler doesn't care and a human reader would probably 
> prefer a meaningful comment if its not obvious. If you're worried about the
> code accidently going there use an assert.
> 
>> 	switch (x) {
>> 		case 1 : ...
>> 		case 2 : ...
>> 		case 3 : ...
>> 		default : __builtin_unreachable();
>> 	}
>>
>> I can also use it to inform the compiler about data :
>>
>> 	if ((x < 0) || (x > 10)) __builtin_unreachable();
>> 	// x must be 1 .. 10
> 
> And that'll do what? You want the compiler to compile in a hidden value check?

I also don't see a point here; myself I'd write some sort of assertion
in such cases, depending on the application case either just temporary
for tests or a static one with sensible handling of the case.

> 
>> Good use of __builtin_unreachable() can result in smaller and faster 
>> code, and possibly improved static error checking.  It is related to the 
> 
> Sorry, don't see how. If you think a piece of code is unreachable then don't
> put it in in the first place!

Let me give that another spin...

In cases like above 'switch' code I have the habit to (often) provide
a default branch that contains a fprintf(stderr, "Internal error: ..."
or a similar logging command and some form of exit or trap/catch code.
I want some safety for the cases where in the _evolving_ program bugs
sneak in by an oversight.[*]

Personally I don't care about a compiler who is clever enough to warn
me, say, about a lacking default branch but not clever enough to notice
that it's intentionally, cannot be reached (say, in context of enums).
I can understand that it might be of use for others, though. (There's
certainly some demand if it's now standard.)

I'm uninformed about __builtin_unreachable(), I don't know whether it
can be overloaded, user-defined, or anything. If that's not the case
I'd anyway write my own "Internal error: unexpected ..." function to
use that in all such cases for error detection and tracking of bugs.

Janis

[*] This habit is actually a very old one and most probably resulting
from an early observation with one of my first Simula programs coded
on a mainframe that told me: "Internal error! Please contact the NCC
in Oslo." - BTW; a nice suggestion, but useless since back these days
there was no Email available to me and the NCC was in another country.