A deeper look into the GCC Rust front-end
A deeper look into the GCC Rust front-end
Posted Oct 10, 2022 19:47 UTC (Mon) by mb (subscriber, #50428)In reply to: A deeper look into the GCC Rust front-end by JoeBuck
Parent article: A deeper look into the GCC Rust front-end
Saying that unsafe disables checks is misleading.
All safety checks are still upheld. (Like borrow checking, for example)
It just adds *more* ways to manipulate data:
https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html#u...
Saying that the unsafe keyword disables checks leads to nowcomers thinking that just adding unsafe disables basic safety guarantees. Which is not the case. If you write code that only accesses safe functions and features in an unsafe block, then all safety checks are still upheld.
Posted Oct 10, 2022 21:54 UTC (Mon)
by JoeBuck (subscriber, #2330)
[Link] (11 responses)
Posted Oct 11, 2022 8:19 UTC (Tue)
by mb (subscriber, #50428)
[Link] (8 responses)
And the unsafe keyword doesn't disable the borrow checker.
Posted Oct 11, 2022 16:21 UTC (Tue)
by JoeBuck (subscriber, #2330)
[Link] (7 responses)
Posted Oct 11, 2022 17:08 UTC (Tue)
by mb (subscriber, #50428)
[Link]
> It sounds like you are refusing to believe the Rust project's own book.
Ehm, wat?
> Go read it, it explains the issue.
I read it a long time ago.
If you write safe code that doesn't pass a Rust safety check, then merely adding `unsafe` to your code will never result in a running program. It will throw the exactly same errors as before.
Posted Oct 11, 2022 18:02 UTC (Tue)
by steveklabnik (guest, #114343)
[Link] (4 responses)
https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html
> It’s important to understand that unsafe doesn’t turn off the borrow checker or disable any other of Rust’s safety checks: if you use a reference in unsafe code, it will still be checked. The unsafe keyword only gives you access to these five features that are then not checked by the compiler for memory safety. You’ll still get some degree of safety inside of an unsafe block.
Posted Oct 11, 2022 18:26 UTC (Tue)
by mb (subscriber, #50428)
[Link] (3 responses)
Thanks for the book. It's one of the best technical books that I ever read.
Posted Oct 11, 2022 22:23 UTC (Tue)
by steveklabnik (guest, #114343)
[Link] (2 responses)
Posted Oct 12, 2022 4:54 UTC (Wed)
by buck (subscriber, #55985)
[Link] (1 responses)
As long as you are coming out of the woodwork here, let me also pile on:
Yes, a very good book, and one for which I and, I'm sure, many (thousands of) others are extremely grateful and obliged to you and Ms. Nichols. It's also one of the pillars on which the success of the language is founded/building, I have to believe, as I would find it hard to fathom there are many (any?) who have bypassed your book when they set out to do Rust, unless they want to miss out on the most thorough and comprehensive explanation of the language one can get, at its length, and, obviously, at its extreme of affordability and accessibility, and with its didactic rock-solidness and sweep, from people just kicking the tires on Rust to those who want to grasp the language implicitly. It is, in a word, a gateway drug.
Well, maybe I'm overstating some of that, and I'm not exactly a Rust developer myself, so maybe I don't have the read on it exactly right, but I'm guessing it is no exaggeration to say that the reason so many are undeterred by the knock on Rust that it's hard is because your book is right there to give them a pretty thorough understanding of what exactly Rust is, why the hard parts are the way they are and what you gain by way of recompense, and is welcoming and instructive in places where the compiler alone is a little less friendly a learning companion. At least it made me want to go program something up. (Alas, the only thing I've been able to find are the exercises in the Command-line Rust book, which were thoroughly engaging, but no particular itch of my own to scratch.)
Posted Oct 12, 2022 18:50 UTC (Wed)
by mathstuf (subscriber, #69389)
[Link]
Posted Oct 12, 2022 9:32 UTC (Wed)
by farnz (subscriber, #17727)
[Link]
The book does not say that the safety checks are "disabled" (your assertion). It says that in an unsafe block, you can use functionality that has weaker checks applied to it than safe Rust does - but if you don't use that extra functionality, you get exactly the same checking as any other Rust code.
And the reason for having some functionality gated behind "unsafe" is also explained; the Rust developers intend that the checks that apply to "safe" Rust guarantee that the code has no Undefined Behaviour, whereas no such guarantee is made for "unsafe" Rust.
It sounds like you need to read the Rust book again, before accusing people of refusing to believe it.
Posted Oct 11, 2022 8:23 UTC (Tue)
by farnz (subscriber, #17727)
[Link] (1 responses)
The borrow checker is still running in the example code, though, and still says that all the properties the borrow checker enforces are correct in the code given in the example. It's just that you use functionality (ptr::add, slice::from_raw_parts_mut) that isn't available in Safe Rust, and that does things whose properties are not checked by the compiler.
I know this feels a lot like nit-picking, but the thing about Unsafe Rust is not that safety checks in Safe Rust get turned off - it's that when you use unsafe, you get access to extra features of Rust that are not safety checked. You still have all the same safety checks as any other Rust code - but the operations that require unsafe do so because some of the burden of checking safety is pushed onto the user.
Posted Oct 22, 2022 10:32 UTC (Sat)
by ssokolow (guest, #94568)
[Link]
Inside or outside an unsafe block, the invariants of the safe Rust constructs must be upheld.
A deeper look into the GCC Rust front-end
A deeper look into the GCC Rust front-end
split_at_mut is a safe method. It doesn't require you to use an unsafe block.
A deeper look into the GCC Rust front-end
A deeper look into the GCC Rust front-end
But that block doesn't disable any safety check.
That's why I showed you the part which explains that unsafe doesn't disable any check and merely adds a handful of additional unsafe features.
A deeper look into the GCC Rust front-end
A deeper look into the GCC Rust front-end
A deeper look into the GCC Rust front-end
Re: Rust-book kudos
Re: Rust-book kudos
A deeper look into the GCC Rust front-end
A deeper look into the GCC Rust front-end
A deeper look into the GCC Rust front-end