4 ms·
The exception handling thing is a showstopper. Stroustrup is resolute that C++ style exception handling is the right error model. So even if a big fraction of C
by tialaramex 5y ago
The exception handling thing is a showstopper. Stroustrup is resolute that C++ style exception handling is the right error model. So even if a big fraction of C++ programmers disagree, the ISO C++ language standard, and the guidance for new developers, is going to keep saying Exceptions are the way forward for the foreseeable future.
Linus didn't like the panic-on-fail memory allocation handling from userspace Rust for the same reason and so the kernel Rust people had to be very clear early on that was temporary and wouldn't survive from a prototype into the actual kernel.
There are some edge cases where this is surprising and I expect Linus will ask for further work. e.g.
let x = "Clowns".to_owned();
let mut y = x + " are not welcome here";
y += " any more.";
The first variable ends up with a growable String, containing the word "Clowns" so that's an allocation and it might fail, in userspace it panics, in kernel Rust should this not compile? If it needs to "return an error" how?
The second line needs a concatenation operation, it won't end up with a new String, the old one gets moved instead (in Rust this means if you try to refer to x after this that won't compile, it was moved and the compiler knows that) but it also gets a concatenation to add this extra string to the end, probably allocating again. Thus it too might fail, in userspace that would panic, but in the kernel what happens this time? Does this Add operator have a Result type now?
The third line concatenates again, but this time the operation doesn't even have a return type, it's obliged to mutate y, so there is just no possible way it can result in an error. Either this mustn't happen in the Linux kernel, or it panics when it can't allocate.
Whereas these are all fine, they don't allocate and aren't writeable so they won't bother Linus:
let x = "Clowns"; // A str, definitely UTF-8 encoded
let y = b"\xf7\xf7\xf7"; // not Unicode, just some bytes
let z = r#"No need to "escape" the double quotes now."#;
- forrestthewoods 5y ago> Does this Add operator have a Result type now? I suspect the answer is: those traits not implemented for String in no-panic mode. And instead you have to use fallible “try_extend” methods that return a result? I’m just guessing though.
- db48x 5y agoThis is usually the right way to go. If you cannot implement the exact trait then you make a trait of your own with similar semantics instead. This one is a language item though, which complicates things.
- Ericson2314 5y agoYeah, first we just nuke a bunch of impls, then we figure out whether we need new infra like a TryClone or whatever.
- Measter 5y agoTo clear something up here, while the Add trait is a language item, String is not. The String type is just a normal type implemented in liballoc[0]. As far as the language is concerned, there's nothing separating it from any other random type a user might write[1]. If you're in a no_std environment, String doesn't even exist unless you depend on liballoc. The reason you can use String with the + and += operators is because it implements the Add and AddAssign traits[2], and it looks like the implementations are already feature gated for OOM handling so might not be implemented for String. [0] https://github.com/rust-lang/rust/blob/master/library/alloc/src/string.rs#L289-L294 https://github.com/rust-lang/rust/blob/master/library/alloc/... [1] Note that the compiler knows about it to provide better diagnostics to the user, but this has no impact on the language itself. [2] https://github.com/rust-lang/rust/blob/master/library/alloc/src/string.rs#L2175-L2185 https://github.com/rust-lang/rust/blob/master/library/alloc/...
- db48x 5y agoYes, that’s a good point. I suppose the compiler can parse «foo + "bar"» regardless of whether the Add language item is implemented for the types involved. It’ll only fail during type checking, which is a later step. Not so complex after all. Oh, and good point about the existing feature gate; I debated going and looking to see if one existed yet or not, but I was hungry so I didn’t. Glad you were around to point it out.
- kortex 5y agoCan't we just write some --hella-strict flag and refuse to compile any of these corner cases? > Does this Add operator have a Result type now? Essentially yes. There could be other APIs which accomplish the same results but have different constraints, such as not being able to mutate strings without also creating a Result to be handled. Yeah you lose some ergonomics but even that feels more ergonomic than C. I think Rust is in a unique position in that zero / extremely few panics is actually achievable, and easier to achieve, with less manual discipline, than C.
- tialaramex 5y ago> Essentially yes. Maybe you understood this as purely a rhetorical question, rather than I'm lazy and didn't take time to go read the source code yet when I wrote it. I apologise. impl Add<&str> for String { type Output = String; #[inline] fn add(mut self, other: &str) -> String { self.push_str(other); self } } So, the implementation of Add for String in the kernel when it exists gives a String, it does not give a Result. What is unclear to me still is the significance of no_global_oom_handling. If this is the intended final state of Rust for Linux, then in fact all these traits just go away, so, you can't concatenate Strings with the + operator, and in fact most ways to do stuff with Strings in Rust go away too†. Which is one way to approach it, it's not as though people were dying to do string manipulation inside the Linux kernel anyway. † In Rust the String type is a growable heap allocated data structure you can change while str (mostly seen as the reference &str) is just a "slice", a length, plus a memory location, plus a promise that the series of bytes in the slice form valid UTF-8 encoded Unicode, but with no way to grow or change it.
- dodobirdlord 5y agoRust's String type is in the standard library [1], rather than in the language core where things like str live [2]. There's a lot of stuff in the standard library that assumes that it can allocate, and will panic on failure. The solution for now is simply to use Rust's `no_std` support [3] and not import the standard library into kernel code, since it's unsuitable for writing kernel code. In the longer term, Rust is investigating adding fallible variants for all of the standard library traits, so that kernel code could use the standard library with a compiler flag enabled that bans usage of any of the infallible standard library traits. There was a lot of discussion about this a few months ago in https://news.ycombinator.com/item?id=26812047 https://news.ycombinator.com/item?id=26812047. [1] https://doc.rust-lang.org/std/string/struct.String.html https://doc.rust-lang.org/std/string/struct.String.html [2] https://doc.rust-lang.org/core/str/index.html https://doc.rust-lang.org/core/str/index.html [3] https://docs.rust-embedded.org/book/intro/no-std.html https://docs.rust-embedded.org/book/intro/no-std.html [4] https://github.com/rust-lang/rfcs/blob/master/text/2116-alloc-me-maybe.md https://github.com/rust-lang/rfcs/blob/master/text/2116-allo...
- Ericson2314 5y agoWe've cfg'd out all global OOM handling from alloc, so I think this example must be out of date. In the latest version you indeed would have to write it s different way.
- jcelerier 5y ago> So even if a big fraction of C++ programmers disagree, The percentage of c++ programmers using exceptions seems to increase in jetbrains survey - for the last one it's at 80%, it used to be close to 60% iirc. I wouldn't call 20% a big fraction. There are no other ways to prevent an invalid object representation to exist unless one is willing to wrap literally everything in optionals, basically reimplementing an exception unwind stack by hand.
- drran 5y agoYour examples will not compile without alloc. IMHO, kernel needs another kind of String type anyway, e.g. https://github.com/paulocsanz/arraystring https://github.com/paulocsanz/arraystring .
- 3836293648 5y ago> If it needs to "return an error" how? Because this is all just library features from a library that isn't being used. They could use core::alloc and have panic on allocation failure, or they could implement their own library and that library presumably wouldn't have the explicit panics that core::alloc has. Kernel rust isn't a matter of a variation of the language, it's just about not using the standard libraries