Zig 0.7.1 Released – 69 bugs fixed
ziglang.org
ziglang.org
- Continued progress on the new "stage2" compiler, which is written in Zig itself (long promised, work started in earnest I think about six months ago). There will be an optional non-LLVM backend this time, which is good news for build times. Even in debug mode, most of build time currently is spent waiting for LLVM.
- Incremental compilation with live reloading (in-place binary patching). The stage2 compiler is being written with this feature from the start.
- I can't find the link, but I believe the plan for stage1 (the current compiler written in C++, with all its warts) is to simply delete it. Since the new compiler will also support compiling to C, the devs will simply have stage2 compile its own source into C at some point, and that will be the stage1 from that point forward.
Here you go! https://github.com/ziglang/zig/issues/6378
P.S. Amazing work up to now! Kudos! I'm a fan just for everything it does what other language designers ignore, like designing the language to both compile and link fast. Cross compiling always available is a masterpiece. I guess that feature wouldn't have to be present on the platforms which don't have LLVM, if the language is to work on such too?
That is awesome! I’ve longed for such a feature when I work with Go and Rust although Go is usually fast enough that I can use a custom watch script.
I hope Zig succeeds in becoming a stable and minimal language. Few languages manage it, they tend to steadily bloat over time. The most obvious exceptions are C and Scheme, which are famously ascetic.
Of course, even if you understand the rules you'll still accidentally write code that invokes undefined behaviour, which is much of the reason languages like Zig exist.
Most DDJ or The C/C++ Users Journal issues will have them.
How recently? Both gcc 4.1.2 (2007) and clang 3.0.0 (2011) optimizes `x+1 > x` to true for a signed int `x` on -O1. And it probably goes way back, these are just the oldest compilers I found on godbolt.
The thing is it's very hard to draw the line once you go that route. Different people expect different things from undefined behavior. The best thing is to not expect anything sane. And if you are unhappy about certain undefined behaviors in the standard then it's better to push the standard to define more behavior. Certain unnecessary undefined behaviors get resolved with newer standards, although I would expect significant pushback on defining the behavior signed integer overflow.
Do you have a source for this?
As I mentioned elsewhere in the thread, you can ask gcc to trap if your code is about to dereference NULL, but the compiler can't easily detect all instances of out-of-bounds array access, due to the way arrays and pointers work in C. I believe Valgrind can help detect out-of-bounds errors at runtime, but in most languages you don't need a sledgehammer like Valgrind to find these common errors.
edit: As lmm says, it's likely you have UB issues in your code you aren't aware of. That's not quite the same thing as having issues in your code due to not being a good enough language-lawyer. I've resolved some very subtle issues that found their ways into a 'serious' C++ codebase, and I didn't spend that long in the C++ world. In most languages those issues simply couldn't have happened in the first place.
GCC compiles to alot of architectures. I have a hard time imagining any modern language compiling to all those platforms without quirks in practice.
Right. A battle-tested codebase only has subtle errors, as the obvious ones will all have been fixed. An immature codebase has subtle errors and more obvious ones.
> GCC compiles to alot of architectures. I have a hard time imagining any modern language compiling to all those platforms without quirks in practice.
Compiler bugs are a separate issue from undefined behaviour and surprising language subtleties. With mature compilers they're pretty rare, but they do happen.
JavaScript is a good example. There's no undefined behaviour in JavaScript. That's vitally important given that JavaScript engines have to be able to run untrusted code. If JavaScript code is able to cause undefined behaviour, that's a serious security issue in the engine. Such bugs do happen, of course, but they aren't all that common. Generally, JavaScript runs fine regardless of whether you're running on x86, AMD64, or AArch64. Same goes for Java.
(I admit I'm ignoring the possibility of a constrained/contained kind of undefined behaviour where the JavaScript context might see things go haywire but the process containing the JavaScript environment is unaffected.)
See whole section here
https://ziglang.org/documentation/master/#Undefined-Behavior
the difference is, they try to check for them at compile time, and if you compile with safety checks, also at runtime; however, if you compile with ReleaseFast (which I assume most people will in production), those runtime checks are turned off and the undefined behavior still exists.
No, people are supposed to use ReleaseSafe, especially "in production". You can still disable runtime checks in specific execution paths.
ReleaseFast is for applications where there are no disastrous consequences to a bug in the code, like videogames.
Zig's approach is essentially that of Ada. You can ask the Ada compiler for runtime checks, or promise the compiler that your code is free of undefined behaviour and have your code run at the speed of light (C), or go haywire if you got it wrong. Sadly C is less suited to runtime checks, arrays are dealt with through raw pointers so range checks aren't easy for the compiler to add automatically. You can though ask GCC to generate checks (trap-on-failure) for things like dereferencing NULL, or signed arithmetic overflow.
You can’t really check if an integer variable is greater than 2^32, since that itself would cause an integer overflow.
Also, the maximum value that can be represented in a variable of type uint32_t is (2^32) - 1, not 2^32.
Of course, in practice this is too much effort for most people most of the time, so actual deployed C and C++ code is full of undefined behavior due to integer overflow. This paper has a great overview:
True, but on the other hand... C _does_ have preprocessor macros, which from a pure language standpoint adds a lot of complexity.
I think it's important to keep in mind that with C you might end up using a bunch of extenions, complex pre-processor macros or pragma magic to achieve essential things.
C in itself is relatively simple, but using it in practice can end up becoming quite complex.
Zig ends up being simpler in some ways by having less magic and special ways of achieving things. "printf" is very magical in most C compilers, but the equivalent in Zig is nothing particularly special from either the language or the compilers side.
I think the only thing that truly adds complexity without being strictly necessary, is the async stuff. But you can argue that async IO is becoming a really essential thing for systems programming, and using it without explicit language suppport is a nightmare.
[0] https://blog.stephencleary.com/2012/07/dont-block-on-async-c...
[1] https://docs.microsoft.com/en-us/archive/msdn-magazine/2013/...
It really is just a control flow structure in zig (which is why it's a keyword)
1. https://github.com/bminor/glibc/blob/glibc-2.32/stdio-common...
Also the compiler is likely to optimise most calls to printf, transforming them into calls to other functions like puts [0], or apparently [1] even to fwrite. Probably doesn't count as magic, but almost.
Well if you include preprocessor, make, and cmake (which I think is basically standard now), the level of complexity feels "close to parity".
* Clojure (JVM): https://clojure.org/
* ClojureScript (JavaScript): https://clojurescript.org/
* Fennel (Lua): https://fennel-lang.org/
* Hy (Python): https://docs.hylang.org/
* Lisp-Flavored Erlang (BEAM): https://lfe.io/
And there's a similar growing trend of "strongly-typed functional language that compiles to X":
* Elm (ML inspired -> JavaScript): https://elm-lang.org/
* Elixir (Its own thing -> BEAM): https://elixir-lang.org/
* Fable (F# inspired -> JavaScript): https://fable.io/
* PureScript (Haskell inspired -> JavaScript): https://www.purescript.org/
* ReasonML (ML inspired -> JavaScript): https://reasonml.github.io/
* Teal (Lua with strict type annotations -> Lua): https://github.com/teal-language/tl
As well as the gazillion languages that compile to and interoperate well with C, such as Nim and several Scheme dialects (Chez and Chicken, probably others I'm forgetting). Oh and Haxe, which apparently can compile to like 10 different languages and I don't understand why it isn't more popular.
I like all of these languages because they don't require the user to switch to a different library ecosystem in order to use a programming language that they find more comfortable/enjoyable/powerful/expressive. I consider this extremely important because of how complicated some low-level protocols and systems can be, and reimplementing everything again for every new language is a waste of effort in my opinion.
Also, the proliferation of things that specifically compile to JS enforces a only-half-joking saying I heard a long time ago: JavaScript is a better compiler target than programming language. I think I've heard the same said about C as well.
Please accept pull requests that fix older example code, or please make the provided examples work on newer versions of zig.
Average PR lifetime in ziglang/zig repo itself is about 12 days. This is actually pretty fast compared to other projects. I'm proud of the fact that the oldest open PR in zig is from October 2, 2020 - only 2 months and 11 days old.
From the perspective of someone submitting a pull request, it's frustrating to not get a response within a day or two. I completely get that. But it's just not possible when you have a team of 1-3 people who are able to engage with PRs, and hundreds of people submitting PRs.
Since this project has started, the Earth has completed 1732 rotations, and yet 2877 pull requests have been closed. That's 1.7 per day, including weekends and holidays. Consider that for the first couple of years of this project, I was moonlighting it with a day job.
I'm not trying to throw shade at you, just saying, man, cut me some slack. I'll get to your PR.
Thanks for your reply and thank you for Zig.
I'll reopen that particular issue, and will try to fix any example zig apps that I can't compile, and will submit PRs for them.
The question of when will Zig be as mature as Rust is impossible to answer.
Here is a talk on using Zig in exactly this kind of environment in production:
https://www.youtube.com/watch?v=124wdTckHNY
It also depends on your project's timeline. If your project will be stable in a year or two, that might well coincide with Zig's stability program.
If Zig's design goals suit your project then it might be worth investing for the long term and gaining deep understanding of the language as it grows.
I mean, FWIW, Rust is fairly ready for embedded systems, even when OOM-safety is essential. There are crates dedicated to providing fallible memory allocation primitives for exactly those use-cases.
With that said: I do believe Zig would work a lot better than Rust in severely resource-constrained environments. Rust has a few pitfalls that can easily cause a code size explosion (anything using core::fmt, for instance). And zig's constexpr appear more powerful than Rust's current stable const fn, which can be critical to writing maintainable, small code.
I've worked on a Rust reimplementation of a bootloader that had to fit in 8KiB of RAM, and I often went over accidentally by having a panic handler pull core::fmt. I haven't tried Zig for the same use-case (yet), but from a cursory glance, it appears to be less prone to this kind of code-size explosions.
* Things that would be in scope, but the Zig project is not yet capable of supporting. An example of this would be compiling stuff to GPUs, e.g. SPIR-V. Such things are planned but not implemented. https://github.com/ziglang/zig/issues/2683
* Things that will never be in scope. An example of this would be code obfuscation, which is a use case the Zig project does not recognize.
* Object formats that are drastically incompatible with the von Neumann architecture. It would be infeasible for Zig code to be compiled to such things. For example, attempting to compile to CSS (Cascading Style Sheets).
I think that's it. Anything else would be supported, provided there were enough people interested in providing maintenance for the respective parts of the codebase.
I've even plugged zig into another highly concurrent vm and gotten it to be a "good citizen": https://youtu.be/l848TOmI6LI (these constructs are running concurrently in test and CI so I know they are robust)
Mobile app dev?
PS: My daily work involves writing Java/Kotlin for Android, and just started learning Flutter for cross platform mobile app dev.
I mean, if one day Zig support those, that's definitely super awesome. Of course I'm aware that more menpower on that direction is needed :)
It's better to mention in which areas it really excels, where it beats the competition hands down.
I’m of the impression that C will never die.
There are just too many benefits, not least developer velocity and readability.
Zig is also very approachable to anyone who is familiar with TypeScript, in a way that C isn't. This means more of your team can get excited and get involved, at least in terms of reading the code.
And with Zig, where you have to spend a little extra maintenance time paying for the language being pre 1.0, that's still nothing compared to the upfront and long-lived costs associated with C. You can develop so much faster in Zig compared to C, that you're ahead even pre 1.0.
const print = @import("std").debug.print;
I’m looking for a new systems programming language. For all the hype of Rust, I don’t think this is it.How does Zig compare to Nim?
Does Zig have a future in games programming? I feel games programming is the ultimate test, to validate the true success of a programming language.
I do like that Zig compiles to LLVM. Although this in itself will exact a minor performance penalty, in exchange for wider CPU coverage.
Nim transpiles its code down to C, and I’m not certain how that will play out. As you’re now writing your new language on an already somewhat buggy language.
But this does give Nim the possible speed advantage, of getting the code compiled closer to the metal, whereas Zig will compile to the intermedia LLVM layer.
It's going to start compiling to zig IR soon (if it doesn't already), and zig IR will be translated to LLVM IR or fed into zigc. So I think the recommended development cycle will be - use zigc for fast iteration on localdev, then when releasing, use slower LLVM for extra optimization passes and also for cross-compilation.
> gamedev
I think the highest interest in zig is currently gamedev, but there are SO MANY uses for zig.
If I may be permitted some shameless self-promotion, zig is pretty awesome: https://www.youtube.com/watch?v=l848TOmI6LI
This is actually a surprisingly underserved field. There's relly very few language that actually tries to be just C but better.
Go: Uses garbage collection, not very applicable
Rust: Too complex, more like C++ but better. Bit too cumbersome to write "unsafe" code
D: More like Java/C++, although they have a "better C" mode, but feels like an afterthought
Nim: Uses garbage collection.. like D they're kind of trying to reach down into the plain C usecase, now with some nice reference counting stuff. Also relatively complex
Some actual attemps that I know of: C2, Odin
Some reasons why Zig is a better attempt at "C but better":
- Complexity of the language is just a little higher than C, and mostly in ways that resolves issues with C that leads to its own complexities (Like, the C pre-processor is simple, but solving problems with it can be surpringly complex. Or how you may have to resort to compiler extensions/pragmas to solve real problems)
- The Zig compiler is also a fantastic C compiler
- C headers can be imported and used directly in many cases
- Significant parts of the language is dedicated to excellent interop with C (even has syntax for 0-terminated strings)
- Cross-compilation is a highly prioritized use-case (C is very popular in embedded programming, where cross-compilation is essential)
That said, I am super excited about the Zig project, and it hits a sweet spot for me in a way that none of the other "C but better" projects have. Calling it another "C but better" language wasn't an attempt to disparage the project, but to classify it.
Right, but that's a (slightly) different goal from being a C replacement. There are other systems languages besides C, and C is used beyond systems programming.
E.g., if somebody came along and said that C would be bloody perfect if it just had a garbage collector then that alternative language would be totally unsuitable as a replacement for C in many contexts, but for a ton of other scenarios it'd still be "C but better," and I don't know that it should be disqualified just because it isn't a full replacement.
V, elixir, D, nim.
But zig seems to really the best of all of those, at least it's my favorite.
Any other similar language? (except go and rust, of course)
Maybe Crystal, Free Pascal even.
If Elixir is in, then Kotlin, Crystal and OCaml should be in as well. I don't think Elixir is similar though, not at all.
> Any other similar language?
I only think Rust, C, C++ and Go are similar enough, in order or similarness.