News for hackers. Stuff that matters.
Re:Some Thoughts on Zig by LAC-Tech on Thursday July 21, 2022 @10:27PM (#32187764)
I am all in on Zig. I was more producitve with it in a few weeks than I was after a few months of rust. Yes the memory management is "manual" but the std library functions for dealing with it are top notch, you have to free a lot less due to the ease of using arena allocators, and the testing allocator does a valgrind-esque runtime analysis of your code - very quickly and very thoroughly. In practice, it's not an issue, and unlike with rust I have a very clear idea of when and where memory is being freed and allocated.

That having been said - the core team chooses some incredibly dumb hills to die on. Last time I checked the tab character was illegal in Zig code - though I understand that's changing.

More concerning is that un-named parameters are a compiler error and this cannot be adjusted. Meaning that if you want to test on save, you can't if any of your parameters are unused. Enforcing such a thing at the compiler level because of your personal coding style is so mind-numbingly short sighted. The guy who made it is clearly a very smart and insightful, but I acknowledge you probably have to be fairly stubborn to decide you're going to replace C.

Re:Some Thoughts on Zig by pcwalton on Thursday July 21, 2022 @11:12PM (#32188050)
> In practice, it's not an issue, and unlike with rust I have a very clear idea of when and where memory is being freed and allocated.

I'm very skeptical that the lack of memory safety is not going to be an issue "in practice". That's what every C++ developer thinks too until they write code people actually try to attack.

One of the very nice things about memory safety is that it frees the programmer from having to think about when memory is freed most of the time. The compiler or runtime system will take care of that. Most of the time, manual frees are just noise--this observation is the entire reason behind destructors.

Re:Some Thoughts on Zig by dagmx on Thursday July 21, 2022 @11:19PM (#32188094)
I’ve met a lot of seasoned C++ developers who tell me the memory safety won’t be an issue in practice. Hell, I’m sure I’ve said it before too.

I’ve yet to meet one who actually didn’t end up with some safety issue in their code. Not all of their code mind you, but even a couple times can be significant.

Like you, I like memory safe languages because they let me think about my problem and trust the tooling to catch entire classes of mistakes for me

Re:Some Thoughts on Zig by dataflow on Thursday July 21, 2022 @11:47PM (#32188293)
> I’ve met a lot of seasoned C++ developers who tell me the memory safety won’t be an issue in practice. Hell, I’m sure I’ve said it before too.

The claim I've heard (and I think also made) is a bit different from that. It's more like: "dealing with memory corruption issues isn't a significant portion of the debugging effort in [idiomatic] C++ code". i.e. the claim is not that you somehow won't run into them; rather, the claim is that they form a small fraction of the actual bugs you run into (either count-wise or time-wise), if you're writing C++ "properly". (If I had to make up a number, I'd say < 5%.)

This obviously isn't to say memory safety isn't an issue, or that security is a negligible property. Rather, it's to point out that most issues are of a different kind, and if you think that memory safety will decrease your workload significantly, that's unlikely to be the case if you're writing C++ with the correct idioms.

I think this response is usually in response to claims about how (say) a language like Rust results in N times faster development than C++. If that's truly the case, it's unlikely to be due to memory safety per se, but due to other factors.

Re:Some Thoughts on Zig by verdagon on Thursday July 21, 2022 @11:57PM (#32188343)
To add to that, there are some applications for which memory unsafety just doesn't cause that much harm.

I worked on a very large C++ codebase that compiled to webassembly, which runs sandboxed in people's browsers. It only talked to one (trusted) server.

Like any project, we had plenty of logic bugs every release. They would often result in odd artifacts on the screen and sometimes a crash.

Only 5% of our bugs were related to memory unsafety, and they also resulted in odd artifacts on the screen and sometimes a crash.

In other words, memory unsafety wasn't much of a concern in practice. It was as bad as any logic bug.

This was the main reason we stuck with C++, rather than spend SWE-years migrating to a more memory-safe language.

(I know this is ironic coming from me, who designs a memory-safe language full-time!)

Re:Some Thoughts on Zig by dagmx on Friday July 22, 2022 @11:19AM (#32192887)
The issue isn’t that patching and fixing code to be safe takes a ton of your own time, but it’s about the risk to everyone using your code.

From security risks to simple crashes, C++’s unsafe nature has caused a lot of issues in production in my experience. From renders and apps that crash to subtle security bugs.

Not having to focus on those when shipping software to customers means I can focus more on the actual logic and the associated bugs instead. I’m not getting back crash reports and CVEs that sucks up my time.

My users in turn aren’t getting affected by my crashes and security issues. Every person who reviews my code can also understand the language guarantees and stop passing my code through that lens and instead focus their mental energy on the other aspects of my code, like catching logic bugs.

The effects of a memory safe language extend very far

Re:Some Thoughts on Zig by jolux on Friday July 22, 2022 @12:09AM (#32188426)
Running inside a memory safe virtual machine is obviously safe though, that doesn’t mean the language itself is safe.
Re:Some Thoughts on Zig by adgjlsfhk1 on Friday July 22, 2022 @12:14AM (#32188456)
The problem with memory safety bugs is that they are uniquely exploitable. Even if memory bugs only take 5% of your time as a dev, there have been numerous studies showing that for large projects, memory bugs form roughly 2/3ds of CVE causing bugs.
Re:Some Thoughts on Zig by dataflow on Friday July 22, 2022 @12:39AM (#32188583)
Yeah that's why many C++ folks are continually trying to improve memory safety too.

Here's one of the efforts in this direction (for anyone who hasn't seen it): https://www.chromium.org/Home/chromium-security/memory-safet...

Re:Some Thoughts on Zig by pcwalton on Friday July 22, 2022 @12:02AM (#32188378)
Honestly, I think what you describe is a much more defensible claim--in fact, I suspect it's true that the vast majority of bugs in C++ code aren't memory safety. I've often seen the stronger claim that memory safety problems don't come up at all in idiomatic C++, though, and that isn't true.
Re:Some Thoughts on Zig by templix on Saturday July 23, 2022 @11:58AM (#32204501)
Fun fact: There's been a number of spectacular memory safety bugs in Rust programs where you have no option but to go unsafe. Which, in real systems programming, is quite often...

Zig is vastly safer than Rust unsafe. That's worth noting.

So while Rust's memory safety features is something I love, I absolutely hate the Rust community pushing mem safety as a panacea.

In my line of work, as an embedded programmer, Zig is vastly more appropriate.

Rust and Zig can co-exist just fine.

Re:Some Thoughts on Zig by saurik on Friday July 22, 2022 @09:37AM (#32191740)
As one of the people who often make this argument, this is spot on: I continually see people who say they are spending some ridiculous amount of time actively debugging memory safety issues as part of their normal development, and to me that indicates they are either an amateur or an idiot... like I just can't imagine what that must be like for them, as pretty much the only time I have to spend any serious amount of time tracking down an issue with memory corruption in my projects is when I run into some weird build system issue (which is sometimes caused by actual compiler bugs in clang/gcc that I have to painstakingly bisect and report upstream, but is sometimes an issue with an SDK or linker) that results in incorrect code being executed. I am not, under any circumstance, claiming that my software is free of bugs! I am 100% kept awake at night by the idea that someone will find a serious and exploitable mistake somewhere; but, almost by definition, that obscure issue isn't something that is taking any of my development time because, if I knew about it, I would already have fixed it and that would result in never having exploitable bugs in production software... put differently, unless you are being actively negligent, any dangerous/exploitable memory errors in your code aren't issues you are actively debugging: they are unknown sleeper issues (and then: issues which I will then claim are likely to be trivial to analyze and fix once seen). The point of using a memory safe language isn't to make your development faster: it is to make your development correct.
Re:Some Thoughts on Zig by ghoward on Friday July 22, 2022 @07:52AM (#32190967)
Not GP.

> I'm very skeptical that the lack of memory safety is not going to be an issue "in practice".

For what it's worth, I agree with you. And I'm a C programmer that managed to put out a non-trivial piece of C code ([1]) that has had no memory safety issues triggered in the wild after 1.0 (which was released on 26 Oct 2018, so almost 4 years, yay!) that I know of.

The truth is that I am not able to write such code; I just fuzz the ever-living daylights out of my code with lots of asserts to test all of the assumptions that my code makes, including preconditions, postconditions, everything. I also use static analysis, -Weverything on Clang (without -Wpadded; that's a stupid warning), and a massive test suite with near 100% coverage. In my new projects, I'm even using fuzzing to generate almost all of my test cases so that my path coverage is as good as possible.

That's a lot of work, and it's not fun.

But what would make it even more effective would be to effectively automatically add asserts for me to test the assumption that an index is in range whenever it is used! And that's why memory-safe languages are the only way to go: not only do they save people from bad code, they help programmers turn bad code into good code with the tools at their disposal.

I think the Zig people know this, which is why they have ReleaseSafe for testing and for those cases where you have to be sure. But I also don't see them test their code enough for me to trust that it's not needed. And I doubt that Zig users test their code enough for it too.

All of this is to say, I completely agree with you despite (so far!) being the exception to the rule you articulated, and I dread the day when my reign of "being the exception" comes to an end. I really want to stay being the exception.

[1]: https://git.yzena.com/gavin/bc

Re:Some Thoughts on Zig by Schroedingersat on Friday July 22, 2022 @10:12AM (#32192027)
The goal of the Zig project isn't to get rid of that work. It's to make it easier.

ReleaseSafe is supposed to be the 'normal' way to compile (the same role O2 takes now albeit with a performance penalty compared to O2), with releasefast or releasesmall for those times performance is unacceptable and where you've satisfied yourself with the level of testing you've done.

The philosophy of the project is to build some of those static analyses and runtime checks that help fuzzing into the compiler and stdlib. And build a culture and toolchain where it's easy to start using the rest as part of the status quo (rather than them being the exotic thing used by weird people). Finally they get rid of some of the most egregious footguns ( ostly relevant to beginners), passing slices is much more ergonomic and less error prone for a beginner, the error handling is best in class, and there are optional types and less use of void pointers.

Of course doing the actual hard work is always going to be hard work. But you can turn it from hard and gruelling work where the majority of people have no idea of the things they should even be doing and where you uave to keep a laundry list of gotchyas in your head into hard but satisfying work that feels fathomable.

It's a different philosophy from rust, but one that is no less valid.

Re:Some Thoughts on Zig by ghoward on Friday July 22, 2022 @08:56PM (#32199060)
The error handling is so far from best-in-class that it's not even funny. Lisp conditions and restarts, which can be implemented in C, are best-in-class. Beyond that, does Zig allow arbitrary values as errors now? Anything that doesn't is not best-in-class; they would be worst-in-class.

Footguns are never just relevant to beginners. I say this as something close to an expert C programmer. I hit footguns all of the time. I just don't release them because I always assume I hit them and hunt them down.

Making things easier to fuzz is table stakes; even C can do that now. I set up fuzzing for my code with a simple build option. It's not hard. It's also effective at finding test cases for you, if you use it right.

Does Zig do Design by Contract? That would make fuzzing easier. Does it do Property-Based Testing? That's a more focused version of fuzzing. In fact, it's the same to unit testing as fuzzing is to integration testing. Until Zig has Property-Based Testing, I don't believe it's making testing easier.

For that matter, as far as I know, Zig only makes unit tests easy. Unit tests are okay, but not really effective. The test suite for my code is only integration tests, which makes things easier to refactor. And yet it is effective at finding bugs.

tl;dr: What Zig has is table stakes in 2022. It is nothing special for helping people write correct code.

Re:Some Thoughts on Zig by Schroedingersat on Saturday July 23, 2022 @09:54PM (#32209498)
> The error handling is so far from best-in-class that it's not even funny. Lisp conditions and restarts, which can be implemented in C, are best-in-class.

Adding gotos is not an upgrade to readability and debugability, and comefrom was supposed to be a parody.

Re:Some Thoughts on Zig by ghoward on Saturday July 23, 2022 @11:09PM (#32209954)
I suggest you read up on Lisp conditions and restarts. They are not exceptions. There is no goto other than a function call and a loop back to the spot the programmer picks. And there is no comefrom.

The C equivalent would look like this:

    do
    {
        err = some_operation_that_can_return_errs();
    }
    while (err != NULL && handleError(err) == ERROR_HANDLED);

    if (err != NULL)
    {
        panic("Could not handle error: %s\n", err_str(err));
    }
That is not goto or comefrom. Far from it. And of course, the panic at the end is not necessary; you could then return the error, or throw an exception, etc.
Re:Some Thoughts on Zig by Schroedingersat on Sunday July 24, 2022 @06:26AM (#32212005)
I'm struggling to understand how your example is not just something any language with error types can do trivially.

Every explanation of conditions and restarts I can find explicitly mentions searching the call stack for a handler and jumping into it, then reserving the capability to return to the function the error originated in.

This is explicitly non-local and hidden control flow (ie. the much maligned goto of exceptions).

The only advantage I can see asserted of having handlers (exceptions or conditions) that way is that you don't have to look for the error and pass it up manually but ! does that anyway so it doesn't seem worth throwing out your ability to reason about invariants in the context of a language with mutability and such.

Then there's returning to the stack frame you started in (the part I somewhat unfairly called comefrom). You need to drag around all the machinery required for continuations in order to do this. If the goal is to have available some of the context of an outer stack frame, you could pass the relevant references down and just handle it where you wished to resume.

Further thoughts: In some ways errdefer is the polar opposote of this concept. It allows the function to clean up and preserve invariants whilst passing control up the call chain.

Re:Some Thoughts on Zig by ghoward on Sunday July 24, 2022 @11:32PM (#32220240)
> Every explanation of conditions and restarts I can find explicitly mentions searching the call stack for a handler and jumping into it, then reserving the capability to return to the function the error originated in.

> This is explicitly non-local and hidden control flow (ie. the much maligned goto of exceptions).

No, it's a function call. That's how you jump to code while reserving the capability to return.

I mean, you explicitly "raise" an error, right? What's wrong with code like:

    raise AllocationFailure;
desugaring to a function call? I thought function calls were local and non-hidden control flow, so why do you think otherwise?

> The only advantage I can see asserted of having handlers (exceptions or conditions) that way is that you don't have to look for the error and pass it up manually but ! does that anyway so it doesn't seem worth throwing out your ability to reason about invariants in the context of a language with mutability and such.

One example is that perhaps you do want to ignore certain errors, but let others through.

I have code that returns an error on EOF. Usually. In reality, it calls an error handler and lets the handler decide.

When I open and read files to the end, I'll encounter EOF, but I want to. So I'll push a handler onto the stack of error handlers that ignores EOF but handles all other errors. If I did that with exceptions, the call stack would be destroyed. Not good.

Another example is memory allocation failure. Maybe one part of your program caches allocations and needs to be told to give them up if allocation failed. So in the handler for allocation failure, it calls a function to release those cached allocations, and when the handler returns, the allocator can try again. Hence, the restart.

> You need to drag around all the machinery required for continuations in order to do this. If the goal is to have available some of the context of an outer stack frame, you could pass the relevant references down and just handle it where you wished to resume.

No, you don't. If you did, I wouldn't have been able to implement it in C, but I did. [1]

> Further thoughts: In some ways errdefer is the polar opposote of this concept. It allows the function to clean up and preserve invariants whilst passing control up the call chain.

Passing control up the call chain on error should be a last resort because you're destroying context, but I agree, it should still exist.

The cool thing is that you can do this in C too, even with cleaning up and preserving invariants. See [2], [3], and [4].

[1]: https://git.yzena.com/Yzena/Yc/src/branch/master/src/concurr...

[2]: https://git.yzena.com/Yzena/Yc/src/branch/master/src/alloc/s...

[3]: https://git.yzena.com/Yzena/Yc/src/branch/master/src/alloc/s...

[4]: https://git.yzena.com/Yzena/Yc/src/branch/master/src/alloc/s...

Re:Some Thoughts on Zig by Schroedingersat on Monday July 25, 2022 @03:52AM (#32221646)
> raise AllocationFailure;

This is exception semantics which is hidden control flow present in every language with exceptions. Even in checked exceptions it is often obfuscated (ie. a function call that can raise is not distinguishable from one that can't wkthout manually looking at the types, and if they are distinct, then you just have something equivalent to error types). The point is that, upon calling a function that might transitively call a function that raises, you have no idea if the next line will execute or whether you will wind up in some other piece of code altogether.

It's not equivalent to calling a function because it destroys the stack context and violates the invariants and ability to reason about control flow for every function above it in the call stack.

I am starting to grok algebraic effects, and how they can make exceptions-but-not-objectively-incorrect by preserving context during the interrupted control flow and returning everything how it was left, but they come at a price.

You could guarantee that the stack will be reconstructed and execution will continue where it left off. But now 'raise' implicitly does a bunch of stuff including allocating memory on the heap and copying every stack frame between your error and your handler onto it. How does this function if your error is that you are unable to allocate?

Even then, you don't know where control will end up without hunting for the registration of the handler, which may not even be near the call site.

Sure you can build a stack on the heap in userland in C and dump everything there so as to reconstruct it (I skimmed your code and it seems like some implementation of coroutines or similar, I'm notnsure if this is part of the error machinery). There's nothing in zig that prevents you from doing this, or even using that exact library.

> The cool thing is that you can do this in C too, even with cleaning up and preserving invariants. See [2], [3], and [4].

I either can't follow your code with the amount of effort I'm willing to expend (likely) or there isn't anything resembling the descriptions I can find for conditions and restarts there.

Are you saying that the following can happen?

I can have some function foo that is regular C code with the exception of registering that it handles some condition.

Then foo can call, bar, which calls baz which calls qux, all of which are regular C code.

Then qux can call something which raises a condition of the kind foo registered.

Then control flow passes to foo which handles the error using the context in its current stack frame.

Then control passes back into qux with information provided by foo's handling code.

Then qux returns, followed by baz and bar returning without having to have any knowledge of or participation in your system.

And that all of this happens without constructing a closure over foo's context and sneaking it into qux via a global.

Re:Some Thoughts on Zig by ghoward on Monday July 25, 2022 @12:20PM (#32226591)
> This is exception semantics which is hidden control flow present in every language with exceptions.

This is not exception semantics. A language can make the keyword "raise" do whatever it wants.

Should I have used some other keyword? Fine:

    handle AllocationFailure;
In fact, let me expand it a bit:

    restart
    {
        err = allocate(size);
        if (err != NULL) handle AllocationFailure;
    }
This code calls the AllocationFailure error handler when allocation fails, and has the option of "restarting" (i.e., going back to the restart keyword and trying again). There is no hidden control flow, unless you think `errdefer` is hidden control flow.

> It's not equivalent to calling a function because it destroys the stack context and violates the invariants and ability to reason about control flow for every function above it in the call stack.

No, it doesn't. Again, I implemented this in C. I don't use longjmp(). It does not destroy stack context. You seem to have no idea what you are talking about.

> You could guarantee that the stack will be reconstructed and execution will continue where it left off. But now 'raise' implicitly does a bunch of stuff including allocating memory on the heap and copying every stack frame between your error and your handler onto it. How does this function if your error is that you are unable to allocate?

Yes, by doing a function call. It's a function call. Really.

> Even then, you don't know where control will end up without hunting for the registration of the handler, which may not even be near the call site.

The registration of the handler does not need to use the stack. In my code, it uses a separate heap-allocated stack, which is in the thread-local data.

Did you not read the actual C code I sent you? It's all in there.

> Sure you can build a stack on the heap in userland in C and dump everything there so as to reconstruct it (I skimmed your code and it seems like some implementation of coroutines or similar, I'm notnsure if this is part of the error machinery). There's nothing in zig that prevents you from doing this, or even using that exact library.

It's not coroutines. It's simple function calls. It's using function pointers, yes, but it's just function calls.

> I either can't follow your code with the amount of effort I'm willing to expend (likely) or there isn't anything resembling the descriptions I can find for conditions and restarts there.

Because your mental model of conditions and restarts is completely wrong.

> Are you saying that the following can happen?

No, that is not what happens. This is what happens:

You have some function foo that is regular C code with the exception that it registers ipsom as a handler for some condition until foo returns.

So ipsom is a registered handler until foo is done executing.

Then foo can call bar, which calls baz, which calls qux, all of which are regular C code.

Then qux can call something which raises a condition of the kind foo registered.

Then control flow passes to ipsom, which gets a new stack frame, and which handles the error using the context in its new stack frame.

Then control returns back into qux with information provided by foo's handling code.

Then qux returns, followed by baz and bar returning without having to have any knowledge of or participation in your system.

And that all of this happens without constructing a closure over foo's context and sneaking it into qux via a global because foo's context is never used as the error handler!

Again, it's just function calls and a possible jump to the beginning of a marked block.

To show this, I have fully implemented restarts now ([1]), which I didn't do because I didn't need them yet.

Now, if you had a hypothetical foo that registers ipsom as an error handler, you would do this in foo:

    y_REGISTER_HANDLER(ipsom);
    // The rest of the code.
    y_UNREGISTER_HANDLER;
I need to hook error handler registration into my RAII system so that you only have to register it (and it will automatically be unregistered on function exit), but you get the idea.

Now, in the function that actually raises the condition:

    y_RESTART(restart)
    {
        err = function_that_returns_err();
        if (err != y_ERROR_SUCCESS) y_HANDLE(err, status, restart);
    }
Of course, it's C, so the macros are a little ugly and requiring passing in variable names, but it works.

In this code, if `err` is not a success value, then ipsom is called, which will either not handle the error, handle the error but not allow a restart, or handle the error and allow a restart. If the last one is what happens, the code will jump back up to the beginning of the y_RESTART block and start over.

Edit: I realized my commit forgot to add y_strucon_handleErrorWithRestart(). It was added in [2].

[1]: https://git.yzena.com/Yzena/Yc/commit/95312b059132dc3e8614b4...

[2]: https://git.yzena.com/Yzena/Yc/commit/61c4a3ef735446a9478c58...

Re:Some Thoughts on Zig by Schroedingersat on Monday July 25, 2022 @08:12PM (#32232675)
Then you haven't made conditions and restarts.

You've made error values. This is just one trivial thing you can do with error values without adding keywords. As you said, it's just a function call.

This is the error system that languages like zig and rust have.

Ie. the best paradigm for error handling you can get without all thenoverheadnof a pure FP language.

Except Zig has a bunch of erganomic enhancements to make them far more pleasant to deal with and reduce the mental overhead and scope for mistakes. You can't get the value out of a failable function without acknowledging and passing up or handling the error, and you don't need the verbiage of if err != nil adding noise everywhere. And it comes with batteries included so you're not going to use code that uses something stupid like errno instead without noticing. And you get errdefer to mitigate one of the biggest downsides to saying 'I can't handle this here, passing an error up'.

They can't carry arbitrary data, because where would you put it in a language that has an error system capable of treating allocation failure as a handleable error? There's nothing stopping you from putting some state or context elsewhere, but you have to make those decisions.

Similarly you can have the error get handled in a parent scope without unainding as I demonstrated if that's really really important, but it shouldn't be the default.

Re:Some Thoughts on Zig by ghoward on Monday July 25, 2022 @11:14PM (#32233841)
> You've made error values. This is just one trivial thing you can do with error values without adding keywords. As you said, it's just a function call.

Well, yeah, with some "ergonomics." Like walking a stack of error handlers. Across threads. Go back and read my code; that's what it's doing: a stack walk across threads, followed by a jump backwards depending on certain conditions.

Does Zig do that? Not builtin, for sure.

Could you implement that in Zig? Sure, but then you're outside its "best-in-class" error system.

> This is the error system that languages like zig and rust have.

So you're admitting that Zig does not have any better error handling than C? Because C has return values.

At least Rust lets you use Option values and Result values.

> Ie. the best paradigm for error handling you can get without all thenoverheadnof a pure FP language.

Sigh...I just implemented something from a functional programming language in C. Overhead is minimal.

> Except Zig has a bunch of erganomic enhancements to make them far more pleasant to deal with and reduce the mental overhead and scope for mistakes. You can't get the value out of a failable function without acknowledging and passing up or handling the error, and you don't need the verbiage of if err != nil adding noise everywhere.

That's just manual exceptions with the compiler complaining if you get it wrong. I mean, sure, it's great that the compiler will check your work, but Clang and GCC can give me warnings about unused return values, so...that's not much better than C.

And they are still exceptions. You're still destroying stack frames until some function higher in the stack handles the error. When it does, all of that context is gone.

Using conditions and restarts means that you can make the decision to tear down the context after* handling the error. Exceptions are much less worse in that case. I've actually found them to be useful in certain cases, but only if the error is handled first.

Also, you can add those ergonomics in C. Like so:

    #define try(var, expr) do { typeof(var) ret_ = (expr); if (is_err(ret_)) return ret_; var = ret_; } while (0)
> And it comes with batteries included so you're not going to use code that uses something stupid like errno instead without noticing. And you get errdefer to mitigate one of the biggest downsides to saying 'I can't handle this here, passing an error up'.

I will say that errdefer is a good idea. I did implement basically that.

But no, Zig's error system doesn't come with batteries included because it can't carry arbitrary data. And it doesn't do a walk of a stack of registered handlers. And it doesn't do restarts. And it doesn't have Option or Result, nor interfaces so you could implement the same thing for other types.

Whatever you might think of conditions and restarts, if you seriously think that Zig's error system is best-in-class when Rust's does the same thing and more, while also allowing user code and types to use the same infrastructure, I just don't know what to tell you.

And I'm no fan of Rust. But it's better than Zig.

> They can't carry arbitrary data, because where would you put it in a language that has an error system capable of treating allocation failure as a handleable error?

Preallocate on thread startup. This is not hard.

And on allocation error, every function that allocates is controlled by the language (through the standard library), so that's not a problem in practice. And even if something tries to wrap an allocation failure in more data, and allocation fails again, just panic(). Really. You can even document that behavior in the language.

> There's nothing stopping you from putting some state or context elsewhere, but you have to make those decisions.

Yes, but that's an admission that Zig's error system is not ergonomic. Yet you claim that it is best-in-class simply because it's slightly more ergonomic than C's?

Sigh...

Re:Some Thoughts on Zig by Schroedingersat on Tuesday July 26, 2022 @12:00AM (#32234102)
> And it doesn't have Option or Result, nor interfaces so you could implement the same thing for other types.

It has both of those. The included error types are exactly result with added erganomics (passing up one frame is the easiest thing and is erganomically the same as checked exceptions)

Maybe learn what something is before arguing about it?

Re:Some Thoughts on Zig by Schroedingersat on Sunday July 24, 2022 @06:35AM (#32212050)
Actually, come to think of it, I'm pretty sure this can be achieved with a combination of async/await and error types.

If the inner function yields either a value (or none) or an error, the outer scope handles the error amd returns control to the async function (optionally with state gleaned from the handler) you have all the pieces.

I believe yield lacks any kind of syntactic sugar (planned), but is possibly in current async functions.

Viewing it through that lens, I can see it as a viable alternative to eschewing exceptions altogether.

Re:Some Thoughts on Zig by ghoward on Sunday July 24, 2022 @11:32PM (#32220241)
Adding async to your error handling would just mean that you now have two problems.
Re:Some Thoughts on Zig by Schroedingersat on Monday July 25, 2022 @05:49AM (#32222362)
That you said that seems to heavily imply that you already agree with the point that I'm trying to make about control flow.

Here's my best attempt at demonstrating conditions and restarts with zig-native tools.

Edit: fixed my dumb. https://zig.godbolt.org/z/creoT47Mj

Note that it'd get substantially more erganomic if yield were finished with all of the usual niceties of the monad-ish error keywords.

Also edit: Upon further reflection to be actually correct conditions/restarts, you'd have to unwind starting from inside the inner function if the handler gave up. This is doable by passing some state back down (handled/unhandled) from the outer scope, and then just returning an error from the bottom scope to begin either unwinding or let a non-restartable error handling mechanism do its thing.

Additionally I don't think this is compatible with also using async for its intended purpose without comptime shenanigans.

Re:Some Thoughts on Zig by ghoward on Monday July 25, 2022 @12:26PM (#32226677)
No, I don't really agree. I don't like async because of something you accused conditions and restarts of: hidden control flow.

In your example, I don't see any, which is great. But async can have hidden control flow. (I wonder why Andrew Kelley added it when he is so against even hidden function calls.)

On top of that, I don't think async is a good paradigm, but that's neither here or there to the conversation.

Re:Some Thoughts on Zig by Schroedingersat on Monday July 25, 2022 @08:02PM (#32232603)
That's the entire point I'm making though...exception style semantics are the worst way of handling errors. Algebraic effects make it a little better. Error values are much better as a default paradigm.

Ie. Zig's error system which is not based on algebraic effects is best in class.

Async does carry that same cost, but it comes with capabilities you can't get otherwise and is optional. Any objections to it are a demonstration of my point, not a disagreement with it.

Re:Some Thoughts on Zig by ghoward on Monday July 25, 2022 @10:49PM (#32233695)
Error values suffer from the pit of failure. If you forget to handle them correctly, you have a bug. See [1].

Exceptions are better than async because they do not "reify" stack frames, an incredibly terrible idea. I mean, it works, but it's not a good model, mentally or otherwise.

[1]: http://joeduffyblog.com/2016/02/07/the-error-model/

Re:Some Thoughts on Zig by Schroedingersat on Monday July 25, 2022 @11:54PM (#32234064)
> Error values suffer from the pit of failure. If you forget to handle them correctly, you have a bug. See [1].

Then you put your value behind the error in the type...like rust or zig where you cannot ignore them.

You keep describing the upsides of Zig's error system as reasons why something else is better.

Re:Some Thoughts on Zig by pjmlp on Friday July 22, 2022 @09:18AM (#32191590)
Congratulations for being part of the minority that cares,

On my experience, only small teams with top devs do happen to care for doing the security work you describe, as tends to be proven by surveys regarding the use of unit testing or static analysis tooling,

https://www.jetbrains.com/lp/devecosystem-2021/c/

https://www.jetbrains.com/lp/devecosystem-2021/cpp/

So yeah, pity that memory-safe languages have to fight back to regain what was already possible before UNIX took over the computing world and brought C for the ride.

This is why I would like that Zig could offer more than Modula-2 already did in 1978, in regards to memory corruption.

Re:Some Thoughts on Zig by ghoward on Friday July 22, 2022 @08:42PM (#32198972)
Yeah, I agree, and I like C!

I think the lack of bounds checks where they are not a problem is a massive design flaw nowadays. Zig should have made bounds checks universal and spent effort on compiler passes to elide them.

Allowing people to remove bounds checks is not smart (though Rust's unsafe is a good compromise), especially in such a coarse way as a build option.

In fact, a supposedly memory-safe language, Julia, has memory bugs because it allows users to turn bounds checks off. [1] This should never have happened.

[1]: https://yuri.is/not-julia/

Re:Some Thoughts on Zig by LAC-Tech on Thursday July 21, 2022 @11:15PM (#32188065)
Learn Zig and get back to me. It's nothing at all like managing memory in C++, not even close.
Re:Some Thoughts on Zig by pcwalton on Thursday July 21, 2022 @11:20PM (#32188101)
We've had close to 50 years to answer the question "is memory safety necessary to write secure code under normal industry development practices"? During this time dozens of languages have come and gone, and the evidence still overwhelmingly points to "yes". It's going to take a lot to make a convincing argument otherwise, especially since Zig doesn't really do much different from C here, semantically speaking.
Re:Some Thoughts on Zig by LAC-Tech on Thursday July 21, 2022 @11:39PM (#32188237)
I've explained how Zig memory management is different. You've chosen to ignore that and just compare it C and C++. I've suggested you learn Zig. You've ignored that too, and just zeroed in on the fact it's "manual".

Here's a car analogy - old manual vehicles had unsynchronised transmissions where the clutch had to be depressed twice per gear change. Newer manual vehicles use steering wheel mounted paddle shifters and the clutch only needs to be depressed when stopping or starting.

You're trying to push your weird semi-automatic transmission - that a lot of people find unusable as a daily driver - by ranting about how awful double clutching is.

Re:Some Thoughts on Zig by pcwalton on Thursday July 21, 2022 @11:47PM (#32188291)
Car analogies aren't a substitute for actually writing widely-attacked software.

There are systematic reasons why I'm skeptical Zig's approach will lead to better results than what we see in modern C++. The worst memory safety issues from a security point of view tend to be use-after-free caused by complex, untested code patterns, where one programmer wrote, or implied, "free", without realizing that somewhere else in the millions-of-lines codebase some other part of the code, written by someone else, still had a reference to the object. Unit tests don't catch these problems because they're caused by strange patterns that the programmer didn't anticipate. That is the kind of vulnerability that is difficult to solve without either a compiler or a runtime system (GC) to help you, and I see nothing in Zig that solves it. You can, and people do, make raw pointers to other objects in Zig.

Re:Some Thoughts on Zig by anonymoushn on Friday July 22, 2022 @12:44AM (#32188609)
In support of the parent who already knows all this: you don't even need to take raw pointers to have these problems! You'll usually get a slice from your allocator. Keeping any copy of that or any slice into that (e.g. a string in a struct obtained by deserializing a json document into a short-lived slab of memory) will get you use-after-frees too.
Re:Some Thoughts on Zig by Schroedingersat on Friday July 22, 2022 @04:31AM (#32189850)
Zig is not trying to be a better C++, so I don't see how complaining that it isn't a better C++ is relevant.

It's not trying to be for large, widely attacked projects. It's for things where the complexity overhead (whether resources or mental overhead) of Rust is too much, so you'd have to use C.

It's not meant to make memory errors impossible, it's meant to make them much easier to avoid or find and remove than it is in C.

Noone is suggesting anyone make a web browser in Zig, but it's likely useful for drivers or similar where you'd otherwise be using a lot of unsafe code, so rust's guarantees are at best a partial help, and at worst a false sense of security.

Re:Some Thoughts on Zig by Tozen on Friday July 22, 2022 @07:48PM (#32198548)
But isn't the position of a "Better C", already covered too, by languages such as D? And I mean literally too, with their "-betterC".

There is arguably no place for Zig to go, along the "better C" route, that hasn't been covered by other languages. And languages like D and Object Pascal, are way up on the charts, so it will be a hard climb to reach them in popularity. Even the much older Nim language (which has been called a C alternative), still hasn't cracked the top 50, among language rankings. Debatably, a language's "claim to fame" (to escape niche status), can't just be about being a more complex C replacement. There has to be other things about it, that it can firmly stand on, for greater mass appeal.

Re:Some Thoughts on Zig by Schroedingersat on Saturday July 23, 2022 @09:13PM (#32209233)
D is garbage collected and requires a runtime.

Object pascal appears to be pascal with classes.

Both of them have exceptions and operator overloading.

Zig is smaller than object pascal or D, and closer to the metal than D, it's arguably simpler than C as it's more regular, has no hidden control flow and has no separate preprocessor.

The unique things it's attempting to do are manual memory management with handrails (not dissimilar to pascal, but maybe with a few more handrails like testing allocator), error handling with better erganomics than go, rust, or unchecked exceptions (but slightly less power), and comptime which afaik is unique and is where it provides much of the power to do things like generics, many kinds of composition, or even implement a borrow checker and lifetimes as user code (with terrible erganomics and poor performance, but it's illustrative of its power).

The thing it stands on for mass appeal is trivial interop with C and the zig compiler is a functional, statically linked C toolchain out of the box and is easier for a beginner to do things like cross compile on than gcc or clang msvcc or similar. Additionally it has the power and simplicity of C with fewer footguns (ie. it attempts to cover some of pascal's strengths, but on the balance I like pascal more as a teaching language). It also shares some of nim's goals like fast compilation and hot code reloading.

The thing I'm personally interested in is the type system and semantics of comptime seem to have all the ingredients required for formal verification with no additions to the language, translations to a prover, or annotations (but it would require extending the compiler's abilities considerably). This is unique if true (but I will lack the skills to find out if it is true for a while yet, and the people closer to it are busy with other things).

Whether it will succeed, or the comptime concept is fatally flawed in some way is as yet unknown, but it does fill out a currently empty niche as a simpler C, it is not trying to be yet another more complex C.

I think its main competitor if it is successful will be go. Go is slightly higher level, arguably marginally simpler and considerably less powerful out of the box (at least last time I looked before the latest generics stuff). The last two are arguably in its favour for being a better C.

Re:Some Thoughts on Zig by Tozen on Tuesday July 26, 2022 @03:06AM (#32235274)
The languages which are closer to Go, are Vlang and Odin. With Vlang also having a focus on C interop as well (importing C libraries, C2V transpiler, and ability to be compiled to human-readable C). These debatably have a much better shot, with being viable alternatives to Go, in terms of use cases.

Zig is arguably in a different category and appears targeted to a more limited or lower space, in terms of general programming, which brings it more in the orbit of Rust. It's made clear that Zig's features and size will be purposely kept to the minimum, though its syntax is significantly complex. Which may hamper it, as it appears such complexity does to Rust, in terms of being picked up for general usage.

It's hard to say how attempting to be a "better Rust" or attempts at taking on C, will work out. But, it does seem clear it's not going for more general and wide usage that languages such as Object Pascal, D, or Go cover.

By the way, Object Pascal can be written where it comes quite close to classical Pascal, as classes are extensions of the language and more optional than required (see also advanced Records and/or Objects). Object Pascal is used in embedded systems, along with various dialects of Pascal, specific for the purpose. There are also various variants of Go, aimed directly at embedded development (TinyGo). So variants of Go and Pascal can more directly compete with Zig, and they are already well established (like Rust is). Now how this will all turn out, in say 5 years from now, anybody's guess. But, it will be an uphill climb for Zig, as for any new language trying to displace established ones.

Vlang documentation (https://github.com/vlang/v/blob/master/doc/docs.md)

Odin documentation (https://odin-lang.org/docs/overview/).

Golang by example (https://golangbyexample.com/)

Re:Some Thoughts on Zig by Schroedingersat on Wednesday July 27, 2022 @11:50AM (#32251970)
> Zig is arguably in a different category and appears targeted to a more limited or lower space, in terms of general programming, which brings it more in the orbit of Rust. It's made clear that Zig's features and size will be purposely kept to the minimum, though its syntax is significantly complex. Which may hamper it, as it appears such complexity does to Rust, in terms of being picked up for general usage.

I don't understand the basis on which you're saying this. From where I'm standing only odin looks simpler, the other two just make different tradeoffs -- namely trading reduced metaprogramming facilities (and the danger of complexity that entails on user code) for more language features.

> It's hard to say how attempting to be a "better Rust" or attempts at taking on C, will work out. But, it does seem clear it's not going for more general and wide usage that languages such as Object Pascal, D, or Go cover.

D and Go are closer to rust than zig is, and object pascal is also just another OO language. Of those, only Go is even attempting to be a better C, but requiring a runtime prevents it from being a true competitor.

Re:Some Thoughts on Zig by pjmlp on Friday July 22, 2022 @09:20AM (#32191614)
They could have used Object Pascal or Modula-2, and would get the same memory safety guarantees that Zig offers.
Re:Some Thoughts on Zig by Schroedingersat on Friday July 22, 2022 @09:57AM (#32191901)
And it makes no pretense otherwise.

Why bring this up?

Re:Some Thoughts on Zig by pjmlp on Friday July 22, 2022 @10:42AM (#32192351)
One would thought a C replacement in 2022 would have better hindsight regarding memory safety than what was already possible in 1978, which was actually even available in JOVIAL (1958), or ESPOL/NEWP (1961), among many other possible examples.
Re:Some Thoughts on Zig by Schroedingersat on Saturday July 23, 2022 @03:40AM (#32201260)
Guarantee =/= capability.

There are plenty of tools in the language and ecosystem for dealing with memory errors and reducing their frequency, but you're clearly not interested in a rational discussion.

Re:Some Thoughts on Zig by nemothekid on Thursday July 21, 2022 @11:57PM (#32188344)
I don't think pcwalton is a stranger to Zig; I'd be surprised if he hasn't played with it yet. Regardless my experience with Zig, is, as far as memory management was concerned, it was less powerful than C++. A UAF is still trivial in the language (the stdlib makes exploiting one pretty difficult); and the lack of smart pointers still seemed like a strange decision in our multi-threaded, multi-core world.

This makes something like the use of sanitizers required for large projects in Zig; It's a race against time until your project gets big enough that someone mistakenly frees something that gets used 200 lines later.

Re:Some Thoughts on Zig by throwaway17_17 on Friday July 22, 2022 @12:17AM (#32188477)
Slightly off topic, but I would prefer to never see smart pointers in Zig. Smart pointers are an inefficient and semantically-unenforceable ‘solution’ for the wrong problem. Smart pointers are supposed to be a way to handle references to mutable data in concurrent/multi-threaded/distributed computation, however I don’t think they form a satisfactory basis to solve the problem they think they solve. Now, I’m not a fan of Rust’s approach in general or it’s approach to this problem, but at least it is a semantically motivated solution.

If a Zig user wants to restrict usage of pointers in certain ways let them make their own data structures, but unless Zig is willing to alter its semantics including a ‘standard’ smart pointer is putting icing on a cardboard box.

/edited to fix a typo/

Re:Some Thoughts on Zig by thehaxerdude on Thursday July 21, 2022 @11:48PM (#32188300)
Rust is a terribly complex language, there are people at FAANG companies threatening to fork the language when attempts to simplify it are proposed. The amount of half-baked features present in the language are footguns in their own right.

I love Rust, but I think Zig solves 70% of the memory safety problem at the benefit of a much simpler language.

Re:Some Thoughts on Zig by pcwalton on Thursday July 21, 2022 @11:51PM (#32188312)
> Rust is a terribly complex language, there are people at FAANG companies threatening to fork the language when attempts to simplify it are proposed.

Ironically, the person I think you're referring to is actually me, and that's a strong mischaracterization of what happened. Nobody was ever "threatening to fork the language". Besides, that's irrelevant to this discussion, because none of the complexity of Rust compromises memory safety.

> I love Rust, but I think Zig solves 70% of the memory safety problem at the benefit of a much simpler language.

I don't believe the 70% number without evidence. Again, the problem that we really need to solve is complex UAF caused by unexpected interactions between components of large software systems, and I see nothing in Zig to solve that.

Re:Some Thoughts on Zig by kaba0 on Friday July 22, 2022 @02:59AM (#32189315)
The problem is that solving 70% of memory safety is arguably the same as solving 0% of it.
Re:Some Thoughts on Zig by flohofwoe on Friday July 22, 2022 @03:33AM (#32189505)
So basically, using Rust is also entirely pointless as soon as it needs to call into non-Rust code (like operating system shared libraries)? ;)

On a more serious note: IME most memory corruption issues in C and C++ are 'side effects' of the sloppiness of those languages (like not catching overflow, missing range checks, problems arising from implicit type conversions, sometimes just not compiling with warnings enabled, etc...).

Zig enforces much more correctness (to a point where it is sometimes annoying to a C coder, but one quickly gets used to it), and it even does this when building C/C++ code (just by using much more 'picky' default compilation options than C/C++ compilers).

Re:Some Thoughts on Zig by Tozen on Friday July 22, 2022 @12:44PM (#32194090)
But, is Zig really all that much simpler a language, from the viewpoint of a C alternative? And is that simplicity partially due to lack of features and youth, so that the complexity can be expected to pile on later. We are talking about a language that hasn't hit 1.0 yet, nor is not due to for some years to come.
Re:Some Thoughts on Zig by dralley on Friday July 22, 2022 @09:15PM (#32199212)
Andrew Kelley has been pretty explicit that he's going to hold a hard line on not adding a surplus of features to the language.
Re:Some Thoughts on Zig by sph on Friday July 22, 2022 @02:47AM (#32189255)
> More concerning is that un-named parameters are a compiler error

I've been ranting about it many times. Most recently: https://news.ycombinator.com/item?id=32145520

There is an upstream issue about it where we should probably get our voices heard, it's encouraging to see many people in that thread that are opposed to that change, but it doesn't seem the core devs are open to considering any alternative, rather they just hope we can learn to live with it.

https://github.com/ziglang/zig/issues/335

This is by far my biggest annoyance with Go, more so than writing if err != nil.

I feel I write code in a different way than other people. Some are very slow, thoughtful and methodical in writing code, while I tend to experiment, throwing stuff at the wall and iterating quickly, and only after going back to polish and refactor. Stopping every iteration during my experimentation phase to lint my code because the compiler is pedantic and stubborn just slows me down immensely and turns me off a language.

EDIT: went and shared my criticism upstream, in a constructive and reasoned manner, I hope. My quixotic fight against unused variables being errors carries on.

Re:Some Thoughts on Zig by LAC-Tech on Friday July 22, 2022 @03:42AM (#32189559)
I don't think there's any point addressing this through the Zig community. It's self-selecting for people who think this is perfectly fine.

Public discussion of it is much more effective.

Best case scenario, they take notice at the wider world meme'ing Zig for such irritating quirks and realise how many potential users they're losing. An OK case scenario is someone maintains a fork. Worst case scenario is they don't change, no one bothers with a fork, and very few people bother with Zig and general.

Ball is in their court. I would hate for them to cut their nose to spite their face, but I suspect they will unfortunately.

Re:Some Thoughts on Zig by markisus on Friday July 22, 2022 @01:22AM (#32188801)
I agree. Zig is great in many ways but I am also so frustrated by some of the obviously horrible unergonomic decisions the core team makes. To this day, you are forced to declare loop index outside the loop body, despite many requests from the community.

https://github.com/ziglang/zig/issues/358#issuecomment-49100...

Re:Some Thoughts on Zig by abainbridge on Friday July 22, 2022 @03:05AM (#32189348)
I don't understand why fixing issue 358 in the Zig compiler isn't trivial. I don't understand the reluctance. I don't understand why:

    for (var i: usize = a; i < b; i += 1) { foo(); }
    for (var i: usize = a; i < b; i += 1) { bar(); }
Is worse than:

    var i: usize = a;
    while (i < b) : (i += 1) { foo(); }
    while (i < b) : (i += 1) { bar(); }
Which is what happens without the for-loop scoping of the variable. (ie when I cut and pasted, I introduced a logic bug where I forgot to initialize i for the second loop).
Re:Some Thoughts on Zig by abainbridge on Friday July 22, 2022 @03:09AM (#32189383)
Maybe there is a good reason. I'd just like to know what it is. My guess is that fixing issue 358 with syntactic sugar would mean that the debug info would show scope blocks that don't correspond to {} in the source, and that is annoying.
Re:Some Thoughts on Zig by abainbridge on Saturday July 23, 2022 @02:14AM (#32200853)
I also see that this is a bike-shed issue [1]. It probably isn't as important as some other language issues that are complicated enough that I don't even know about them. However, people's first impression of the language matter, so fixing the bike shed matters.

[1] https://en.wikipedia.org/wiki/Law_of_triviality

Re:Some Thoughts on Zig by Aransentin on Friday July 22, 2022 @05:18AM (#32190119)
There is an accepted (but as of yet unimplemented) proposal to fix just that:

https://github.com/ziglang/zig/issues/7257#issuecomment-1030...

Re:Some Thoughts on Zig by markisus on Friday July 22, 2022 @01:59PM (#32195091)
Wow I did not know this! This is really great news.
Re:Some Thoughts on Zig by peteradio on Thursday July 21, 2022 @11:23PM (#32188131)
> More concerning is that un-named parameters are a compiler error and this cannot be adjusted.

Would you be willing to expand this a bit? This would save you from giving a dummy name in a function?

Re:Some Thoughts on Zig by neandrake on Thursday July 21, 2022 @11:29PM (#32188168)
I think it’s a typo and they meant unused parameters.
Re:Some Thoughts on Zig by LAC-Tech on Thursday July 21, 2022 @11:43PM (#32188256)
Yes, I meant unused parameters. Makes it very hard to do TDD.
Re:Some Thoughts on Zig by kuon on Thursday July 21, 2022 @11:56PM (#32188339)
I have an editor macro that inserts

_ = myvar; // TODO remove

easily for all function arguments when prototyping.

I agree it can be cumbersome, but I am not sure it is a bad thing.

Re:Some Thoughts on Zig by 41b696ef1113 on Friday July 22, 2022 @12:15AM (#32188463)
Not that I write Go anymore, but this was definitely one of those things that regularly made my blood boil. Any kind of experimentation/swapping around data structures always invoked the wrath of the compiler when I am just trying to quickly iterate on an idea. Always felt like it disincentivized improvements.

Should have been a warning. Baked in from day-one, easily makes that a CI check without hampering development.

Re:Some Thoughts on Zig by kaba0 on Friday July 22, 2022 @03:01AM (#32189336)
TS with this lint enabled as well.

When these languages tout fast iteration cycles, yet for a single-line code you have to do a recursive, n-step compilation of everything that might also get unused is literally insane and indefensible position.