News for hackers. Stuff that matters.
Re:Some Thoughts on Zig by Schroedingersat on Thursday July 21, 2022 @10:33PM (#32187810)
I think this is missing one major difference in the goals of Zig vs. something like rust.

Zig wants to make it possible (and as easy as is practical without ballooning the language) to make a 100% correct program. That is no panics, no UB, no failed allocation, 100% deterministic behavior.

Whereas Rust wants to make 70% or so of bugs that exist elsewhere impossible.

Re:Some Thoughts on Zig by pcwalton on Thursday July 21, 2022 @11:05PM (#32187995)
This is incorrect in two ways. First, Zig certainly has undefined behavior. Any non-memory-safe language trivially has UB.

Second, this implicitly assumes that terminating on OOM is not "correct" behavior, and handling OOM is the "correct" thing to do. But if there's nothing better that the app can do than to terminate on OOM, then terminating is the correct behavior. Many apps fall into this category. There's also the fact that OOM code paths tend not to be well-tested, so empirically aborting on OOM is a safer default.

Re:Some Thoughts on Zig by skybrian on Thursday July 21, 2022 @11:26PM (#32188144)
There's a difference between a language allowing undefined behavior and a particular program written in that language exhibiting undefined behavior.

The post you're replying to wasn't claiming that no Zig programs have undefined behavior, but rather that it should be be feasible to avoid it in the programs that you write.

(I don't know if that will actually be true, though.)

Re:Some Thoughts on Zig by pcwalton on Thursday July 21, 2022 @11:31PM (#32188186)
Well, that weaker sense of "correct programs have no UB" is true for Rust as well, then.
Re:Some Thoughts on Zig by Schroedingersat on Friday July 22, 2022 @03:37AM (#32189523)
Rust has hidden allocation, no planned static stack analysis or limits on recursion, little to no control over how allocation happens, no ability to guarantee a panic won't happen and thus no ability for the programmer to guarantee eg. essential external resources are cleaned up.

The guarantees that zig seeks to enable (this being the key phrase, it does not guarantee them just by its use, nor has the project solved all the problems this entails) the programmer to provide are non-goals of rust.

Ie. the projects have completely different goals. The things zig want to provide would make rust unusable if it tried to provide them and vice versa.

Re:Some Thoughts on Zig by zozbot234 on Friday July 22, 2022 @03:40AM (#32189543)
Rust has had to implement limits on recursion already as part of async. It seems feasible to extend this to other code. And local allocators are also being worked on.
Re:Some Thoughts on Zig by Schroedingersat on Friday July 22, 2022 @04:46AM (#32189939)
That's cool. Linky to discussions? I can't seem to search it.
Re:Some Thoughts on Zig by Schroedingersat on Thursday July 21, 2022 @11:50PM (#32188310)
You've missed the point in at least two ways.

The goal (not the present reality, although it's closer than most) is for the programmer to be able to get a particular program that is 100% defined behavior if they do everything right. Ie. it's a 'there exists' relationship. It's also an aspiration rather than an absolute (ie. make the tools progressively better for that purpose rather than making it an absolute constraint from day 0).

Memory safe languages forbid certain classes of behavior preventing some bugs altogether. Ie. a 'for all' relationship.

The correct behavior on OOM could also be defined as either checking whether allocation succeeded before moving on or as knowing how much memory you need and getting a guarantee it is available. You could be in a state where unpredicted termination has severe negative consequences (mission critical software, resources that need to be cleaned up). By pre-selecting a particular allocation strategy and response to failed allocation, and making it almost impossible to even know when allocation is attempted without reading every line of the massive stdlib you're making it impossible for the programmer to make that choice.

Re:Some Thoughts on Zig by chriskrycho on Thursday July 21, 2022 @10:37PM (#32187832)
That’s a good goal! A 100% correct program which cannot guarantee a lack of memory safety bugs is not a 100% correct program, though, right?
Re:Some Thoughts on Zig by Schroedingersat on Thursday July 21, 2022 @11:03PM (#32187981)
Rust still needs unsafe blocks for full generality. Additionally there are many sources of panics that are unavoidable. The memory safety guarantees it makes are also not all bugs. It is equally possible to write a memory safe program in both (although far easier in rust), but impossible to make a panic-safe program in rust without avoiding a large portion of the language.

A guarantee of the absence of certain classes of memory bugs is of no use to creating a 100% correct program if it also comes with a guarantee of panic on allocation failure and standard libraries that are full of panics.

They are for different things. Rust is for keeping you completely safe from the worst things while you build something big like a web browser where the program doing something you didn't want is the worst failure state, and crashing is a mere inconvenience. It is there to replace C++

Zig is for making it possible to make yourself safe from all bugs if you try hard enough. It is for making small things that you understand fully where crashing is as bad or worse than memory errors like the firmware for a space probe. With the addition of some static analysis tools it could maybe replace or be useful in addition to something like Ada one day, and definitely replace C and some hand coded assembly.

Re:Some Thoughts on Zig by pcwalton on Thursday July 21, 2022 @11:08PM (#32188018)
How does Zig protect against stack overflow? To make a "100% panic-safe program" you must disallow recursion.
Re:Some Thoughts on Zig by csande17 on Thursday July 21, 2022 @11:22PM (#32188116)
Zig basically does plan to disallow recursion: https://github.com/ziglang/zig/issues/1006#issuecomment-5346...

More specifically, it'll force you to allocate memory for your recursive functions on the heap (and thus handle the possible resulting OOM condition).

Re:Some Thoughts on Zig by pcwalton on Thursday July 21, 2022 @11:24PM (#32188133)
Do you disallow function pointers/closures/virtual methods too, since these can be used to smuggle recursion into the semantics without the compiler being able to statically detect it? You'd have to heap allocate every call to a function pointer, which would be very slow (see how much benefit apps get from PGO which for a long time mostly amounted to optimizing virtual calls).
Re:Some Thoughts on Zig by csande17 on Friday July 22, 2022 @12:03AM (#32188387)
> Do you disallow function pointers/closures/virtual methods too [...]

Maybe? Of these features, AFAIK Zig only has function pointers, and they already don't seem to interact very well with other parts of the language. I wouldn't be surprised if they restricted them to comptime-only usage or made "how much stack space does the function need" part of the pointer type or something.

Re:Some Thoughts on Zig by Schroedingersat on Thursday July 21, 2022 @11:30PM (#32188173)
Disallowing recursion is one possibility (IIRC misra guidelines forbid it).

In some circumstances, static proofs are also possible. IIRC it's still an open problem in zig as to what the possible language features will be while keeping this guarantee, but the goal is to be able to have recursion in some circumstances and still provide the possibility of stack-overflow-safety with a performance tradeoff, and to know when you've stepped out of the bounds where that is possible.

Here's a thread I'm at least a year out of date on https://github.com/ziglang/zig/issues/1006

The general philosophy is to give the programmer tools to know what the consequence of a choice will be as far as that doesn't hamstring them overly. Ie. the compiler can tell you when a stack overflow is possible, and it's up to you to decide what to do about it (or whether it is wrong because you know something it doesn't).

Re:Some Thoughts on Zig by pcwalton on Thursday July 21, 2022 @11:35PM (#32188198)
The trouble here is the same problem that affects tons of static analyses: what do you do about higher order functions. I'm pretty skeptical that heap allocating every call to a function pointer/closure/virtual method is going to be fast enough.
Re:Some Thoughts on Zig by fancl20 on Friday July 22, 2022 @12:38AM (#32188577)
What do you mean by saying heap allocation? Do you mean syscall or libc? Zig has a extremely explicit requirement for any allocation. You can have a preallocated region of memory in the allocator for recursion and only move the needle for each “heap allocation”.
Re:Some Thoughts on Zig by pcwalton on Friday July 22, 2022 @02:39AM (#32189210)
Ah, I see, it's a split stack. Well, we experimented with those in Rust for Rust->C calls and they were not fast enough (also, they cause problems with debuggers and tooling). Perhaps if virtual calls are uncommon, though.

At that point I have to wonder what the advantage is over just using explicit checks for overflow on the machine stack though.

Re:Some Thoughts on Zig by Schroedingersat on Saturday July 23, 2022 @03:47AM (#32201288)
You're still forcing a mindset onto the language that isn't present.

It's not 'a split stack' or any other particular solution. You pass in an allocator and it does it whichever way your chose allocator picks.

The goal is to explore sections of design space you're completely ruling out from before you even start.

Re:Some Thoughts on Zig by throwawaymaths on Friday July 22, 2022 @01:15AM (#32188773)
I'm pretty confident that zig will eventually get a static lifetime analysis tool.

Such a thing should be writeable, because a human can reason about code. We also know that for example SEL4 does "sort-of" static analysis on c and gives memory safety guarantees.

Re:Some Thoughts on Zig by suremarc on Thursday July 21, 2022 @10:46PM (#32187886)
The absence of a proof of correctness is not a proof of incorrectness, strictly speaking.