News for hackers. Stuff that matters.
Re:Zig; what I think after months of using it by chrisco255 on Tuesday February 04, 2025 @11:45PM (#42943923)
Unsafe Rust is problematic: https://zackoverflow.dev/writing/unsafe-rust-vs-zig See also: https://github.com/roc-lang/roc/blob/main/www/content/faq.md...

Zig is not entirely unsafe. It provides quite a few compile time checks and primitives to catch memory leaks or prevent them altogether.

Re:Zig; what I think after months of using it by oneshtein on Wednesday February 05, 2025 @12:30AM (#42944233)
Yes, unsafe code is problematic in Rust, C, C++, etc. Is Zig different?
Re:Zig; what I think after months of using it by mk12 on Wednesday February 05, 2025 @12:52AM (#42944382)
It’s harder to write correct unsafe Rust than correct Zig because (1) Rust uses references all over the place, but when writing unsafe code you must scrupulously avoid “producing” an invalid reference (even if you never deference it), and (2) there’s lots of syntax noise which obscures what the code is doing (though &raw is a step in the right direction).
Re:Zig; what I think after months of using it by zozbot234 on Wednesday February 05, 2025 @02:53AM (#42945351)
> Rust uses references all over the place

This is mostly a concern with the Rust stdlib, though. And it's in principle fixable, by writing new varieties of those stdlib functions that take raw-pointer or &UnsafeCell<...> arguments, and delegating the "safe" varieties to those.

Re:Zig; what I think after months of using it by kprotty on Thursday February 06, 2025 @10:21AM (#42963199)
There's compiler-level traits like `Iterator` and `Future` which enforce references. If wanting to do intrusive pointers into them, one risks creating overlapping references: https://github.com/tokio-rs/tokio/issues/3399
Re:Zig; what I think after months of using it by zozbot234 on Thursday February 06, 2025 @05:02PM (#42966947)
References to UnsafeCell<> should still be safe, because the only thing you can do with an &UnsafeCell<T> is extract a possibly-mutating raw pointer to T. (UnsafeCell<> is also special in that, like Cell<>, you can mutate through it without incurring UB.)
Re:Zig; what I think after months of using it by oneshtein on Wednesday February 05, 2025 @06:28AM (#42947034)
For Rust references, rules[0] are not hard to follow:

The pointer must be properly aligned.

It must be non-null.

It must be “dereferenceable”: a pointer is dereferenceable if the memory range of the given size starting at the pointer is entirely contained within the bounds of that allocated object. Note that in Rust, every (stack-allocated) variable is considered a separate allocated object.

The pointer must point to a valid value of type T.

When creating a mutable reference, then while this reference exists, the memory it points to must not get accessed (read or written) through any other pointer or reference not derived from this reference.

When creating a shared reference, then while this reference exists, the memory it points to must not get mutated (except inside UnsafeCell).

[0]: https://doc.rust-lang.org/stable/core/ptr/index.html#pointer...

Re:Zig; what I think after months of using it by mk12 on Wednesday February 05, 2025 @09:56AM (#42949297)
One reason it’s harder to follow is you can’t have references to uninitialized memory. In Zig pointers to uninitialized memory are fine as long as you don’t dereference them. That’s also true of Rust’s raw pointers, but most Rust code uses references, so it can’t be reused in unsafe contexts.

As a concrete example, I previously used Vec in unsafe code that dealt with uninitialized memory. This should be fine because Vec is basically a raw pointer and two integers. But later they changed the docs to say that this was undefined behavior (essentially, Vec reserves the right to “produce” a reference whenever it wants, even if you avoid calling methods that would do that). So my code that previously followed the rules now appeared to violate them.

Re:Zig; what I think after months of using it by oneshtein on Wednesday February 05, 2025 @07:33PM (#42957448)
> you can’t have references to uninitialized memory.

The method is available[0] in nightly Rust

    pub const unsafe fn as_uninit_ref<'a>(self) -> Option<&'a MaybeUninit<T>>
    where
        T: Sized,
    {
        // SAFETY: the caller must guarantee that `self` meets all the
        // requirements for a reference.
        if self.is_null() { None } else { Some(unsafe { &*(self as *const MaybeUninit<T>) }) }
    }
[0]: https://doc.rust-lang.org/stable/core/primitive.pointer.html...
Re:Zig; what I think after months of using it by zamalek on Wednesday February 05, 2025 @12:30AM (#42944237)
This is precisely the myth that the article talks about. Miri finds significantly more UB in unsafe Rust than Zig's checks do.

Even if it weren't, this exaggeration is a complete theater. You aren't supposed to use unsafe Rust unless you really have to. I have been using Rust since 2020 and I've used it once, for 3 lines of code. The entirety of all Zig codebases is unsafe. That's fine if you are fine with unsafe code, but this myth is dishonest, and I take great issue with using a language where the founder is the primary source of the dishonesty - because what else is being swept under the rug?

Re:Zig; what I think after months of using it by chrisco255 on Wednesday February 05, 2025 @01:54AM (#42944855)
> Miri finds significantly more UB in unsafe Rust than Zig's checks do.

That's not a substantiated claim. Miri also runs very slowly.

> You aren't supposed to use unsafe Rust unless you really have to. I have been using Rust since 2020 and I've used it once, for 3 lines of code.

Cool, glad you haven't needed it. If you're ever writing interpreters or interfacing with external code, you'll need it.

> The entirety of all Zig codebases is unsafe

Zig is not 100% memory safe but it has compile-time safety features for vast majority of problems developers get themselves into with C/C++. Meanwhile, Rust's safety overhead has real trade-offs in terms of developer productivity, computational performance, compiler performance and binary size.

Re:Zig; what I think after months of using it by zamalek on Wednesday February 05, 2025 @02:03PM (#42953446)
> That's not a substantiated claim.

The article we are commenting on substantiates it with, several, actual examples.

> interpreters

In what world do interpreters require unsafe code? A naive interpreter that recursively descends an AST doesn't need it, and a bytecode interpreter doesn't need it either. You'll probably need it if you want to make a fast GC, but that does not mean your entire codebase has to be unsafe.

> interfacing with external code

This is one reason unsafe exists, yes. You are supposed to hide the unsafe parts behind a safe interface. For example, Rust unavoidably has to deal with external code to do I/O - yet, the exposed std::fs interface is safe. This is a well established doctrine in the Rust community, and at least one prominent project has received hot hell for ignoring it.

And, again, the portions of code that are unsafe in a Rust codebase - even when required - are supposed to be minimal, well contained, and well tested. Running a suite of tests to check a small amount of code under Miri is not prohibitive at all. If someone is going to insist on using unsafe across their codebase then, yes, they are far better served by using a language that is unsafe to begin with.

I have done embedded Rust, and even there I have largely avoided unsafe code (the 3 lines I was forced to write happened to be for embedded).

> Rust's safety overhead has real trade-offs [...]

I never claimed otherwise. Those trade-offs have a purpose: fewer degrees of freedom result in higher degrees of certainty. Even Rust has too many degrees of freedom[1], but we don't sweep that under the rug, deflect it, or outright lie about the situation.

The Rust zeitgeist largely agrees your opinion (or rather: Andrew's opinion) of unsafe Rust, in a very oblique way. It's shit, we don't like using it. It is certainly not an accurate summary of Rust as a whole.

Leveraging unsafe Rust against Rust as a whole is a dishonest line of thinking and I'm not going to engage with it further.

[1]: https://github.com/Speykious/cve-rs

Re:Zig; what I think after months of using it by throwawaymaths on Wednesday February 05, 2025 @12:47AM (#42944348)
yes but by the time you're using miri, why not just run zig with a separate static checker that does all the memory safety parts?

https://github.com/ityonemo/clr

Re:Zig; what I think after months of using it by pcwalton on Wednesday February 05, 2025 @01:14AM (#42944553)
For one, it doesn't do all the "memory safety parts", according to the readme. I'm very skeptical that Zig can be made memory safe with a checker while still remaining compatible with existing code. Certainly neither C nor C++ can, and Zig isn't meaningfully different in expressivity (if anything, it's more expressive, which is the opposite of what you want).
Re:Zig; what I think after months of using it by throwawaymaths on Wednesday February 05, 2025 @01:45AM (#42944783)
FTrepo:

Q: You didn't do X, so Zig will never be able to track X

A: Maybe. Only way to know for sure is to fork this (or, hopefully, a 'real' successor) and fail. However, consider that "trivially" it should be possible to externally annotate every zig file with lifetime/type annotations identical to that of Rust and run "exactly the same" analysis as Rust and get the same memory safety as Rust.

it appears the clr author anticipated you: you didnt fork it, try, and fail, so you have ceded the authority to credibly make your speculative complaint

> Zig isn't meaningfully different in expressivity

it is meaningfully different in expressivity at the AIR level. AIR looks nothing like c, c++, zig, or rust.

Re:Zig; what I think after months of using it by Ygg2 on Wednesday February 05, 2025 @03:38AM (#42945718)
> it appears the clr author anticipated you: you didnt fork it, try, and fail, so you have ceded the authority to credibly make your speculative complaint

That's not how proof works. But neither Zig nor Zig+Clr have really proven they are safe.

Re:Zig; what I think after months of using it by throwawaymaths on Wednesday February 05, 2025 @09:25AM (#42948864)
wow what are you afraid of. you're working really hard to tear down something that is an incomplete proof of concept.
Re:Zig; what I think after months of using it by zamalek on Wednesday February 05, 2025 @02:22PM (#42953708)
The original argument is that clr proves Zig is as safe as Rust, per your wording " all the memory safety parts."

Does the incompletely POC do all the parts, or doesn't it? That is no criticism against the project itself, striving to improve memory safety in any language is an honorable goal. The cinch is that an incomplete POC doesn't prove things one way or another, the POC needs to be completed (or at least completed far enough to prove your point). It either matches or exceeds Rust memory safety, or it only vastly improves Zig memory safety. Both are great outcomes, for what it's worth.

It shouldn't have been shown as an example of how Zig is just as/more safe than Rust if it is not. Mispresenting the project tears it down, not questioning its use an an example. Just like misrepresenting Zig's safety (and Rust's unsafety) tears Zig down.

Re:Zig; what I think after months of using it by throwawaymaths on Thursday February 06, 2025 @12:46PM (#42964680)
I guess the question is what level of satisfied are you.

Are you satisfied like "proof is left as an exercise for the reader" in a math textbook? Or is it like "I have proved this but it's too large to fit in the margins"?

It seems author couldn't be bothered to complete it since there's (minor) missing features in the zig compiler that need to be satisfied first to make it worthwhile?

Re:Zig; what I think after months of using it by zamalek on Thursday February 06, 2025 @02:57PM (#42965905)
> Are you satisfied like "proof is left as an exercise for the reader" in a math textbook? Or is it like "I have proved this but it's too large to fit in the margins"?

In the first example the proof would be available elsewhere. In the second example, no. My answer is "just the facts as they exist today."

A mathematical proof holds no inherent utility. The thing it proves is what is useful. What I find useful is not suffering memory safety woes. Can I run a tool, any tool, today that will give me the same assurances that the Rust compiler (and/or Miri) does? Conjecture about the future is pointless because someone could equally go and write some static analysis tool like clr for Rust to fix all the bad parts, or fix the compiler, or the parts of clr that aren't finished might be impossible (we don't know, nobody has tried), or the heat death of the universe could come early. All still irrelevant, because I'm a user and I care exclusively about what I can currently do.

Re:Zig; what I think after months of using it by Ygg2 on Wednesday February 05, 2025 @03:58PM (#42955021)
Nothing, it's the principle of the thing. I.e. when you make a challenging to evaluate statement, the burden of proof is on the one making the claim.

If I say "Moon is made of millennia old cheese", the burden of proof isn't on you to go create a rocket, fly to the moon, sample it and come with conclusions, but on me, making a difficult to verify statement.

Re:Zig; what I think after months of using it by throwawaymaths on Thursday February 06, 2025 @12:49PM (#42964711)
the author has put a lot of unpaid I'm guessing work into this repo -- seems reasonable for the author to demand a bit of work out of any asshole taking a potshot at it.
Re:Zig; what I think after months of using it by pcwalton on Wednesday February 05, 2025 @01:24AM (#42944623)
> It provides quite a few compile time checks and primitives to catch memory leaks or prevent them altogether.

From what I've seen, clang has all of these and more for C++. If your metric is "tooling to help you catch UB", C++ is significantly superior to Zig.

Re:Zig; what I think after months of using it by chrisco255 on Wednesday February 05, 2025 @01:35AM (#42944711)
Zig has a C and C++ compiler built into it and works seamlessly with it. Several C/C++ projects use Zig as a build tool. Zig makes different trade-offs with C++ from a language design standpoint. C++ has a lot more footguns to create UB in the first place.
Re:Zig; what I think after months of using it by pcwalton on Wednesday February 05, 2025 @01:37AM (#42944725)
> C++ has a lot more footguns to create UB in the first place.

I'd actually give the edge to C++ over Zig, because of smart pointers (not that I'm implying smart pointers are anywhere near sufficient).

Re:Zig; what I think after months of using it by brabel on Wednesday February 05, 2025 @01:58AM (#42944881)
WTH they were talking about Rust and Zig, they did not even mention C++ and you come with "if your metrics is blah then C++ is superior, checkmate!", completely ignoring C++ is a monster of complexity while Zig is basically simple as C.
Re:Zig; what I think after months of using it by Ygg2 on Wednesday February 05, 2025 @03:21AM (#42945591)
If you present an argument that you like X in a thing so you picked B, and A exist that's more X than B, it means your argument is partially (you like X and Z) or totally (you like Y) wrong.

In this context of, if you are using Zig for its safety via tooling, there is a much more mature candidate C++.

Re:Zig; what I think after months of using it by brabel on Wednesday February 05, 2025 @12:44PM (#42952241)
That's exactly what I was criticizing: you just make the unfounded assumption that "argument that you like X" is the only thing that matters, and the debate is not only about X in the context of languages A and B. Introducing C to the debate is stupid, as then the obvious answer is just "but C has Y which is horrile" and so on.
Re:Zig; what I think after months of using it by dolmen on Thursday February 06, 2025 @12:04PM (#42964296)
In related news, someone just published a PoC borrow checker for Zig:

https://news.ycombinator.com/item?id=42923829