News for hackers. Stuff that matters.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by n_plus_1_acc on Sunday May 05, 2024 @06:50AM (#40263831)
std::optional<T> is fundamentally broken because it has an imolicit conversion (or operator* or something) to T. If you forget to check if it's empty you get UB.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by lorenzhs on Sunday May 05, 2024 @07:20AM (#40263996)
There is no implicit conversion (except to bool, but that tells you whether the optional contains a value), and operator* / operator-> throw std::bad_optional_access if it’s empty. See https://en.cppreference.com/w/cpp/utility/optional
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by tialaramex on Sunday May 05, 2024 @07:30AM (#40264037)
You're describing what it would do in a sane world where WG21 cared about safety.

In this world, as the document you've linked says: "The behavior is undefined if *this does not contain a value."

The operators for such access are actually `noexcept` - the exception you're apparently relying on would be illegal.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by kzrdude on Sunday May 05, 2024 @07:40AM (#40264094)
Can we salvage this by forbidding * on optional with compiler warnings (as errors)?
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by lorenzhs on Monday May 06, 2024 @07:06AM (#40273265)
clang-tidy has a check for this -- it's not a compiler check but with clangd and LSP, almost every code editor can show an inline warning: https://clang.llvm.org/extra/clang-tidy/checks/bugprone/unch...
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by lorenzhs on Sunday May 05, 2024 @03:18PM (#40267395)
Should’ve checked my own link instead of relying on memory — I might have some code to revisit on Monday. That’s insane, thanks for correcting me!
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by tialaramex on Sunday May 05, 2024 @09:02PM (#40270133)
No problem, if I caused you to fix a bug before it happened that's great. Yes, I find that reading sources I'm about to cite is often eye-opening. Our memories are not as good as we think they are, and our condensed understanding of a complex situation may have ignored something which is now crucial.

Once in a while I go down a rabbit hole, but hey, it's not as though HN isn't a rabbit hole anyway.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by masklinn on Sunday May 05, 2024 @07:25AM (#40264013)
> and operator* / operator-> throw std::bad_optional_access if it’s empty.

Of course not, they’re literally `noexcept`, what they do is UB if empty.

value() will throw.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by formerly_proven on Sunday May 05, 2024 @08:30AM (#40264347)
Step 1 of API design: Always make the easiest and shortest way the wrong way.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by steveklabnik on Sunday May 05, 2024 @10:00AM (#40264956)
They were really in a pickle here. It’s easy to be snarky, but both options (no pun intended) have downsides. In short, do you choose consistency by default, or safety by default?

This feels like an easy choice in isolation, but at the time this was being developed (and arguably even now), there’s no definitive plan to holistically move C++ code to being safe by default. So whenever that happens, a ton of things will need to be dealt with, and there’s always the possibility that being an odd API here makes that overall move harder not easier. And C++ is regularly criticized for being inconsistent. Do you deepen those criticisms just so that one tiny corner of an API is better?

If I’m honest with myself, I probably would have made the same choices they did in this situation.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by tialaramex on Sunday May 05, 2024 @10:26AM (#40265138)
> If I’m honest with myself, I probably would have made the same choices they did in this situation.

Some of the more modern proposals (std::optional is quite old) actually make an explicit appeal to WG21 not to choose consistency at the price of safety because it just needlessly makes the language worse. "But we made the language worse before" is more like a plea for help than an excuse.

Barry Revzin did this in his "do expressions" which are an attempt to kludge compound expressions into C++ which really wants them to be compound statements instead. For consistency, all the obvious mistakes you'll make in do expressions could introduce UB like they would in equivalent C++ core features, but Barry argues they should be Ill-Formed instead - resulting in your mistakes not compiling rather than having undefined behaviour.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by masklinn on Sunday May 05, 2024 @09:00AM (#40264569)
C++ APIs follow the principle of most astonishement.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by blegr on Sunday May 05, 2024 @08:38AM (#40264407)
It sucks but it's easy to review and avoid, probably could be checked statically by linters too.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by lorenzhs on Monday May 06, 2024 @07:07AM (#40273269)
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by SilasX on Sunday May 05, 2024 @07:36AM (#40264075)
Love the typo, and it's fitting here. I'm going to use it for any time the implicit behavior risks burning (immolating) you, as the sibling comments note applies std::optional.

Implicit conversion that immolates: imolicit conversion.