Stream: ideas

Topic: zig compiler


view this post on Zulip Zellyn Hunter (Mar 12 2024 at 19:43):

Very random musings…
I noticed @Richard Feldman saying in a podcast that "if we had to start now, we'd probably write the compiler in Zig", which made me curious about a couple of questions:
• how strongly do you feel that?
• surely, like planting trees, the second best time to switch would be now? (If watching Emacs development has taught me anything, it's that three or four decades later, the compromise you put up with "for backward compatibility" a few years in looks less and less great…)
• is there a plausible way to port incrementally? mechanically? Possibly not… I'm guessing a safe Zig implementation would look very different from a Rust implementation, and a mechanical port to Zig without the borrow checker etc. would be pretty unsafe…
I understand stopping all forward progress to port 300 KLOC would probably be just too painful, but I was curious! :joy:

view this post on Zulip Norbert Hajagos (Mar 12 2024 at 19:56):

I feel like we all know the answer to the question of "will the compiler ever be ported to x". But... now that you have mentioned it, I think this deserves a casual conversation. How big is the regret –if there is one– Richard? :big_smile:

view this post on Zulip Richard Feldman (Mar 12 2024 at 20:03):

I've talked about the idea with a few people, and the short answer is that the plan is to stick with Rust indefinitely

view this post on Zulip Richard Feldman (Mar 12 2024 at 20:07):

that said, we've been incrementally adopting a style that looks more like how Zig encourages doing things architecturally (passing around allocators instead of using the global allocator, using struct-of-arrays more often than is common in Rust), and have learned some techniques for writing Rust code in a way that reduces compile times (e.g. minimizing third-party dependencies, organizing code more using crates than modules to enable better caching) etc.

view this post on Zulip Hristo (Mar 12 2024 at 20:18):

Thanks for bringing the question up, @Zellyn Hunter! I've been always wondering the same since I listened to the same podcast episode, but was waiting for the topic to arise more organically.

view this post on Zulip Zellyn Hunter (Mar 12 2024 at 20:19):

And if a ragtag bunch of community members decided to crowd-source porting the existing codebase to Zig partly mechanically, partly manually, would you use it? (Not volunteering, just curious!)

view this post on Zulip Richard Feldman (Mar 12 2024 at 20:20):

I think the odds of that working out are extremely low haha

view this post on Zulip Zellyn Hunter (Mar 12 2024 at 20:20):

:joy:

view this post on Zulip Richard Feldman (Mar 12 2024 at 20:23):

like if I were starting tomorrow, the benefits of Zig would be things like:

view this post on Zulip Hristo (Mar 12 2024 at 20:24):

Out of curiosity, @Richard Feldman - what would you envision the associated disadvantages might be, in the same hypothetical scenario?

view this post on Zulip Richard Feldman (Mar 12 2024 at 20:24):

but an important benefit of Rust today is that it's what most of our contributors actually want to use; I don't think it's a given that if you could wave a magic wand and port everything to Zig (including the architectural changes I'd want to make) that we'd have the same contributors, and Roc is what it is because of contributors

view this post on Zulip Zellyn Hunter (Mar 12 2024 at 20:25):

That makes sense. Yeah, you've mentioned quite a few times that you're not even the main committer anymore! :smile:

view this post on Zulip Richard Feldman (Mar 12 2024 at 20:25):

put another way, if I were starting tomorrow and I chose Zig and in that alternative timeline the project didn't have the contributors it has today, I think that would be much worse for the success of the project than the delta between the two languages

view this post on Zulip Richard Feldman (Mar 12 2024 at 20:26):

actually I barely pulled back into the lead this month, but we'll see how long that lasts :joy:

view this post on Zulip Richard Feldman (Mar 12 2024 at 20:27):

as an aside, there's another sort of intangible thing which draws me to Zig, namely that the way I think the compiler should be architected fits with what Zig encourages and doesn't really fit with what Rust encourages

view this post on Zulip Richard Feldman (Mar 12 2024 at 20:28):

like the description of the architecture we're moving towards is kind of "use Rust, but write the code in the way Zig encourages and not the way Rust encourages" - e.g. Rust has this whole global allocator and system for automatic allocations and deallocations; don't use any of that, and instead pass around allocators like Zig does

view this post on Zulip Zellyn Hunter (Mar 12 2024 at 20:29):

One of my teammates was asking about Roc, since I mentioned it, and I was explaining that it shares some characterstics with Zig…

It’s also weirdly in the Zig family of modern languages in terms of:
• fast compile is very important, leading to a hand-built dev-mode compiler (aka not llvm)
• incremental compilation is very important, leading to (probably?) a custom linker / custom linking regime

view this post on Zulip Richard Feldman (Mar 12 2024 at 20:29):

so if I were starting tomorrow, it would be weird to say "let's plan to write Zig-style Rust on this greenfield project"

view this post on Zulip Zellyn Hunter (Mar 12 2024 at 20:30):

Now that Zig has caught on so much, it sometimes seems a bit odd that Rust doesn't make you pass allocators around…

view this post on Zulip Richard Feldman (Mar 12 2024 at 20:37):

they've been working on letting collections use custom allocators, but I don't think it will ever happen that the ecosystem shifts to accepting allocators for everything

view this post on Zulip Zellyn Hunter (Mar 12 2024 at 20:38):

Accepting custom allocators for collections is probably good enough. IIRC, that's what C++ does too…

view this post on Zulip Hristo (Mar 12 2024 at 20:38):

@Richard Feldman, the Zig-style Rust reference reminds me of how you mentioned in interviews how you'd transitioned to functional programming :sweat_smile:

Writing in one language, in a style which is better suited for a different one.

view this post on Zulip Brendan Hansknecht (Mar 12 2024 at 23:47):

I think another big advantage of zig is that I find it forces more thought and simplicity. Rust is a complicated beast and makes it easy to overarchitect and pull in too many unnecessary layers.

view this post on Zulip Brendan Hansknecht (Mar 12 2024 at 23:48):

Zig being closer to c has a lot less of that.

view this post on Zulip Brendan Hansknecht (Mar 12 2024 at 23:48):

One thing that makes me sad is that neither will replace c++ (in existing projects).....and I am stuck in a c++ world.

view this post on Zulip Brendan Hansknecht (Mar 12 2024 at 23:49):

Also, comptime in zig simply makes me happy.

view this post on Zulip Brendan Hansknecht (Mar 12 2024 at 23:49):

I really wish I had a big project to mess around with in zig, but currently I don't. Just rust and c++.

view this post on Zulip Brendan Hansknecht (Mar 12 2024 at 23:51):

Aside, given zig and rust live in the same programming model (mostly), it probably would be possible to incrementally port, but probably would be painful and lead to a bad final system unless very large chunks are ported at a time.

view this post on Zulip Norbert Hajagos (Mar 13 2024 at 07:12):

When I started contributing, I wanted to do a specific thing for the Roc compiler. I had some advent of code experience with Rust and have read The Rust Book. I had no hands-on experience with Zig. I just hoped the problem I wanted to tackle was in the portion of the codebase that was written in Zig (it wasn't :laughing: ), because I knew I could get going with it more easily than with Rust. Rust made me a bit anxious with it's complexity, since I came for solving Roc problems, not specifically to write Rust.

view this post on Zulip Hristo (Mar 13 2024 at 10:39):

@Brendan Hansknecht, if you could please excuse my ignorance - on the topic of (partially or otherwise) porting existing C++ codebases to Zig, isn't one of Zig's selling points that one could rewrite any (reasonably) arbitrary portion of a C++ project and be able to incrementally do so?
Or did you mean that there isn't really any incentive to move away from C++ (as the code already does what it's supposed to and there is little to no point in spending dev time on enabling it to work at least as well in Zig, whilst introducing non-zero risk of bugs creeping in), as an analogy to how generally little effort tends to be spent on refactoring messy code in the real world, due to constantly changing (product/roadmap) priorities etc.?

view this post on Zulip Zellyn Hunter (Mar 13 2024 at 14:12):

Zig is more like C than C++, at least as far as direct porting is concerned. You can certainly do anything in Zig you could do in C++, but would typically take quite different approaches.

view this post on Zulip Brendan Hansknecht (Mar 13 2024 at 14:38):

The big problem with using c++ with zig or rust. Is that zig and rust have a more c style memory model and extension system. As such, they can't really deal with complex c++ (class hierarchies, templates, etc). To use c++ with zig or rust is to make all languages fit into a c shaped box. So you have to somehow make the interface not use any of the ++ part of c++. If you have a large c++ codebase this generally means writing bad interfaces and pretty crappy zig/rust that can't even interact with all of the features of your codebase.

If you were to rewrite large enough mostly isolated components, the migration can probably happen (but that is a much bigger commitment and cost). This is what Firefox did. The reason that Google is investing in writing carbon and why other c++ successors are being made is that trying to incrementally port c++ to zig/rust/etc doesn't work well and leads to bad zig/rust/etc.

view this post on Zulip Brendan Hansknecht (Mar 13 2024 at 14:54):

As a note, I think trying to incrementally mix zig and rust would see similar issues but significantly less dramatic than c++ and zig/rust.

view this post on Zulip Hristo (Mar 13 2024 at 16:07):

Thanks for the detailed response, @Brendan Hansknecht!
@Zellyn Hunter thank you, too! :pray:


Last updated: Jun 16 2026 at 16:19 UTC