Error Abi
Postedabout 2 months agoActiveabout 2 months ago
matklad.github.ioTechstory
calmmixed
Debate
70/100
Error HandlingRustAbi
Key topics
Error Handling
Rust
Abi
The article discusses the 'Error ABI' problem in Rust and proposes various solutions, sparking a discussion on error handling mechanisms and their performance implications.
Snapshot generated from the HN discussion
Discussion Activity
Active discussionFirst comment
19h
Peak period
16
18-21h
Avg / period
5.4
Comment distribution43 data points
Loading chart...
Based on 43 loaded comments
Key moments
- 01Story posted
Nov 9, 2025 at 9:31 PM EST
about 2 months ago
Step 01 - 02First comment
Nov 10, 2025 at 4:53 PM EST
19h after posting
Step 02 - 03Peak activity
16 comments in 18-21h
Hottest window of the conversation
Step 03 - 04Latest activity
Nov 11, 2025 at 6:38 PM EST
about 2 months ago
Step 04
Generating AI Summary...
Analyzing up to 500 comments to identify key contributors and discussion patterns
ID: 45871688Type: storyLast synced: 11/20/2025, 4:32:26 PM
Want the full context?
Jump to the original sources
Read the primary article or dive into the live Hacker News thread when you're ready.
1. exceptions 2. garbage collection
Sometimes in slightly modified forms or names, and often with very well-articulated, technically competent justifications (as is the case here).
Just say no!
The article is about how we represent errors not their control flow (i.e., exceptions).
"Instead, when returning an error, rather than jumping to the return address, we look it up in the side table to find a corresponding error recovery address, and jump to that. Stack unwinding!
The bold claim is that unwinding is the optimal thing to do! .."
My opinion (which one need not approve of) is that it asymptotically approaches the language-level control flow constructs.
It's about borrowing a technique from languages that do have exceptions (and Rust's own panic unwinding) to implement Rust's existing error handling without making any changes to the language.
The post is light on how this would actually work, though. I think that re-using the name "stack unwinding" is a little misleading. The stack would only unwind if the error was bubbled up, which would only happen if the programmer coded it to. Indeed, the error could just get dropped anywhere up the stack, stopping it from unwinding further.
I think this would be tricky to implement, since the compiler would have to figure out which code actually is on the error path, and any given function could have multiple error paths (as many as one per each incoming error return, if they're all handled differently). It'd also make Result<T,E> even more special than it already is.
All that having been said, if you squint a bit, the end result does vaguely resemble Java's checked exceptions.
It is interesting to think why it is so for exceptions specifically. The discussion here offers some of the possible reasons, I think, including violent disagreement of what we all even mean by exceptions vs stack unwinding vs error propagation.
Adding ref to the SEH part of Microsoft C extensions https://learn.microsoft.com/en-us/cpp/cpp/structured-excepti...
IMHO this is the next logical step in LTO; today we leave a lot of code size and performance on the floor in order to meet some arbitrary ABI.
Partly it's due to lack of better ideas for effective inter-procedural analysis and specialization, but it could also be a symptom of working around the cost of ABIs.
In the current software landscape, I don’t see these additional optimizations as a priority.
Not to mention issues like the op mentions making it impossible to properly take advantage of RVO with stuff like Result<T> and the default ABI.
Soon people will demand it just figures out what you are implementing and rewrites your whole codebase
We have this now, it is indeed very slow lol. Gemini is pretty fast however.
Speak for yourself. On embedded platforms I'd happily make my compiles twice as slow for 10% code size improvements.
This isn't true; proof by counter-example: anyhow::Error.
For example, a lot of Rust code uses "anyhow", a crate which provides sort of a catch-all "anyhow::Error" type. Any other error can be put into an anyhow::Error, and an anyhow::Error is not good for much except displaying, and adding additional context to it. (For that reason, anyhow::Error is usually used at a high-level, where you don't care what specifically went wrong, b/c the only thing you'll use it for is propagation & display.)
No matter what error we put into an anyhow::Error, the stack size is 8 B. (Because it's a pointer to the error E, effectively, though in practice "it's a bit more complicated", but not in any way that harms the argument here.) So clearly, here, we can stuff as much context/data/etc. into the error type E without virally infecting the whole stack with a larger Result<T, E>.
(Rust does allow you to make E larger, and that can mean a Result<T, E> gets larger, yes. But you're one pointer away from moving that to the heap & fixing that. Rust, being a low level language, … permits you that / leaves that up to you. The stack space isn't free — as TFA points out, spilling registers has a cost — but nor are heap allocations free. Rust leaves it up to you, effectively.)
My understanding of Zig the other day is that it doesn't permit associated data at all, and errors are just integer error code, effectively, under the hood. This is a pretty sad state of affairs — I hate the classic unix problem where you get something like,
Which I now special path in the neurons in my head so-as to short circuit wandering the desert of "yeah, no such directory … that's why I'm asking you to create it". (And all other variations of this pattern.)All of that could have been avoided if Unix had the ability to tell us what didn't exist. (And there are so many variants: what exists unexpectedly? what perm did we lack? what device failed I/O?)
(And I suppose you could make Result<T, E> special / known to the compiler, and it could implement stack unwinding specifically. I don't think that leave me with good vibes in the language design dept., and there are other types that have similar stack-propagating behavior to Result (Option, Poll, maybe someday a generator type). What about them?)
> That is the reason why mature error handling libraries hide the error behind a thin pointer, approached pioneered in Rust by failure and deployed across the ecosystem in anyhow. But this requires global allocator, which is also not entirely zero cost.
> But this requires global allocator, which is also not entirely zero cost.
Heap allocs are not free. But then, IDK that the approach of using the unwinding infra is any better. You still have to store the associated data somewhere, & then unwind the stack. That "somewhere" might require a global allocator¹.
(¹Say you put the associated data on the stack, and unwind, and your "recovery point"/catch/etc. site might get a pointer to it. Put what if that recovery point then calls a function, and that function requires more stack depth that exists prior to the object?
I supposed you could put it somewhere, and then move it up the stack into the stack frame of the recovery function, but that's more expensive. That might work, though.
But since C++ impls put it on the heap, that leads me to assume there's a gotcha somewhere here.)
Basically, supposing T alone fits in a register or two, but E is so big that the union of T and E would spill onto the stack, treat them as two different values instead of one.
Put another way - it would be a lot easier for the programmer to write code if there were no types checked by the compiler at all, but we recognize that the safety net they give us is worth the additional effort at times when refactoring. So why would the benefits of static type checking be worth it, but not the benefits of static error type checking? It seems to me that either both are good ideas, or neither is.
Works great in monorepo, but not sure if code is spread out.
It's not so much a "pitfall" as it is an intended part of the deal.
It just turns out that many people hated it, so most of the time functions omit the 'throws' list and throw unchecked subclasses of RuntimeException. Which also has its trade offs! (Or "pitfalls", if you want to use the same term.)
As another example, the exception type hierarchy doesn't pull enough weight. Exception is the base class of all checked exceptions and RuntimeException is the base class of all "ordinary" unchecked exceptions, but it confusingly subclasses Exception. So there's no way to catch only "all checked exceptions". Then, Error is distinct from that hierarchy, but some things that smell like errors were made into exceptions instead (e.g. NullPointerException).
This was compounded by the fact that, in the original design, you could only call out one exception type in a catch statement. So if you had 3 different disjoint exception types that you simply wanted to wrap and rethrow, you had to write 3 different catch blocks for them. Java 7 added the ability to catch multiple exceptions in the same block, but it was too little, too late (as far as redeeming checked exceptions goes).
Agreed. There's a proposal for exception catching in switch [0] which I'm hopeful will alleviate a lot of this. I think that jep plus combining exceptions with sealed types the error handling will be convenient and easy.
> As another example, the exception type hierarchy doesn't pull enough weight.Kotlin has an interesting proposal for their language that creates their own "error" type that will allow type unions [1]. The only thing I worry about is that it further puts Kotlin away from Java making interop a lot harder.
[0] https://openjdk.org/jeps/8323658
[1] https://github.com/Kotlin/KEEP/blob/main/proposals/KEEP-0441...
Since this is a custom ABI, how about just adding something large enough (cacheline sized, e.g. 64) to the return address? Saves the table shenanigans. Just tell the codegen to force emit the error path at that address... (I wonder if LLVM can actually do that...)
No, it doesn't require a global allocator. You make the thinly-pointed-to error object have a vtable and, in this vtable, you provide a "deallocate" or "drop" function. No need for a single global allocator.
> (this requires the errors to be register-sized).
Uh, what? No, you can make it a pointer, and honestly, in every real-world ABI, there are tons of callee-clobbered registers we COULD use for errors or other side information and just... don't.
As you say, the more limited implementation can be done more efficiently than the post post claims, but it has to be restricted and managed in a way that existing runtimes/compilers/languages haven't succeeded at so far... "just say no" earlier.
> Finally, another option is to say that -> Result<T, E> behaves exactly as -> T ABI-wise, no error affordances whatsoever. Instead, when returning an error, rather than jumping to the return address, we look it up in the side table to find a corresponding error recovery address, and jump to that. Stack unwinding!
And at least based on the listed benchmarks it can indeed result in better performance than "regular" Result<T, E>.
(Might be nice to mention this on the corresponding lobste.rs thread as well to see if anyone has anything interesting to add, if anyone has access)
[0]: https://github.com/iex-rs/iex