Clearly I haven't gotten enough of my 🍑 kicked by the standards process. So, here we go, doing Big Work all over again. The first of many larger changes for C that should've been done 25 years ago, since the days of __try/__finally.

Let's get it done. ⤵

The Defer Technical Specification: It Is Time | Björkus Dorkus | The Pasture | https://thephd.dev/c2y-the-defer-technical-specification-its-time-go-go-go

The Defer Technical Specification: It Is Time

After the Graz, Austria February 2025 WG14 Meeting, I am now confident in the final status of the defer TS, and it is now time.

The Pasture

Article EDIT:

I gave Yodaiken's code too much credit. He actually made the defer version in the proposal worse, lol (loop resources are not properly freed on every go-around, which the defer proposal version fixed). Which is actually a DOWNGRADE from the original source code (that the defer proposal was improving on) too, lmfao.

@thephd that sounds like a perfect counter parry-argument, amazing!

@thephd doesn't it use-after-free everywhere goes like:

freeall(&x);
return h_err_from_errno(errno, x.outpluginsdirphp);

but it seems like he original does also so i must be missing something idk.

but the point about defer running after the return expression is evaluated seemed notable to me as the error is created before resources are freed

@sqwishy I think (thought?) h_err_from_errno is a macro and that the second parameter isn't used "like that" (directly, I thought it just used it as a name and stringized).
@thephd It'd be very amusing (and useful) for C to have the saner implementation of defer compared to Go. It's kinda wild how pragmatic Go is for defer to be lifted to function scope.
@thephd Another absolute banger, nice one!
@thephd good article, especially good comparison with Go, where I didn't realise the "defer attaches it to function scope" could be annoying behaviour compared to "defer attaches to end of current scope", which would make for much interesting/cleaner code, to that degree
@thephd OMG!!! Thank you for writing this up and pitching it!

@thephd not sure where to report possible typos, so I'm replying here

> and whether writing a for loop would accurate

I don't think "accurate" is the word you meant to use here? I think "accrue" or "accumulate" would be more appropriate?

(I'm ESL so sorry if I'm wrong! I checked some dictionaries and didn't see a definition that fit, but maybe it's popular usage that's growing that's not yet captured by dictionaries...)

@kc This is fine, and you're right! That was a think-o (thinking typo) for sure. It'll be fixed when the next push eventually refreshes.
@thephd Excellent! Btw: I think it is just "reverse order" (or "reverse order of appearance in the source code" if you want to be more precise) and not "lexicographic order" because it is not ordered by the spelling of its name (there is no name)

@uecker lexical perhaps? i think ive seen that in use

@thephd

@erisceleste @uecker In the defer TS itself I use "lexical", but "lexicographic order" is also normal, real English too. Lexicographic / lexical here is meant for:

of or relating to words or the vocabulary of a language as distinguished from its grammar and construction

where, in particular, it's meant to be "by each word or token", effectively. But I agree it's probably not the best word here; I can do better in the TS wording at least.

@thephd I am definitely one of those evil C++ people going "just use RAII, duh" ("also stop using C if it won't let you use RAII"), but good luck.
@thephd okay time to actually finish the gcc patch (up to the spec this time!) and send it up!

(great article!)
@thephd The removal of the if checks is not necessary, but it should be noted free is one of the very, VERY few functions in the C standard library that’s explicitly documented to be a no-op with a null pointer input

Oh wow, today I learned.

@thephd I really like these semantics. This is how it should be done in C.
I think Rust also messed up here, because drop of a struct calls drop on the struct fields in declaration order, instead of reverse declaration order, occasionally causing surprise.
@chrisvest Yeah, the Rust thing is awful. But, the compiler is allowed to re-order your drops to prevent any sort of double-free/use-after-free shenanigans.

@thephd

"I’m more than willing to be pleasantly surprised"

I'm stealing that one :D

@thephd I'm really happy to see defer finally have real hope for C, this post is great

Typo note: it says CVE-2023 instead of 2021 in this paragraph https://thephd.dev/c2y-the-defer-technical-specification-its-time-go-go-go#it%E2%80%99s-not-that-there-isn%E2%80%99t-a-way-in-code-as-plain-as-c90-to-write

The Defer Technical Specification: It Is Time

After the Graz, Austria February 2025 WG14 Meeting, I am now confident in the final status of the defer TS, and it is now time.

The Pasture

@thephd you know why i want defer in c++? i mean, simply because the syntax is plain nicer and maybe there is less noexcept shaped bullshit.

but having it in c is really good.

@leftpaddotpy I agree with you. An ideal language would have both, but none of Rust, Zig, C++, et. al. will ever admit that.
@leftpaddotpy (I mention this frequently in discussion of the feature and in the non-TS version of the paper, but alas. I don't think anyone will believe me and instead they'll try to use closures and macros to do it instead.)
@thephd yeah. well. rust having non joke macros makes it a more realistic prospect for them to just ignore the problem. in c++ where exceptions are everywhere and the language does not lend itself to anything being nice, it's a different story.
@thephd thanks for you and the committees work. cleanup is easily my most used language extension.

@thephd likely a detail but what about backward jump ?

{
b:
defer printf("woof");
// or
// b:
}
goto b;

Given your explanation, I understand the deferred statement would be executed everytime but I'm not sure if both jump are allowed

@hderycke The non-commented one is fine! The commented one is not fine and is an error.

The non-commented version produces an infinite loop, constantly printing "woof" again and again and again.

@thephd, long overdue indeed. I can't imagine it not being widely and commonly used, once established in a few generations of compilers.

But… what do you mean, “we can just chock that up”? I've not heard of that before except as something to be done to wheels or other objects which could roll away, and as such it seems to me that it should be “chalk that up”.

(Also, XKCD 37 applies.)