News for hackers. Stuff that matters.
Re:Stacked Borrows: An Aliasing Model for Rust by mjw1007 on Sunday February 09, 2020 @05:01AM (#22281333)
As I understand it, this is a proposal for formalising Rust's existing rules around memory safety and the borrow checker.
Re:Stacked Borrows: An Aliasing Model for Rust by kd5bjo on Sunday February 09, 2020 @06:46AM (#22281648)
Yes, and extending that model to unsafe code. It would make violations of the model have undefined results, which allows the compiler to make optimizations that assume the model is followed even in situations where that can’t be statically proved.
Re:Stacked Borrows: An Aliasing Model for Rust by acqq on Sunday February 09, 2020 @08:24AM (#22281972)
Hopefully the programmer will be informed whenever the compiler decides it can treat some code as "undefined"? Otherwise, it can be the source of bad results.
Re:Stacked Borrows: An Aliasing Model for Rust by cesarb on Sunday February 09, 2020 @09:22AM (#22282219)
> Hopefully the programmer will be informed whenever the compiler decides it can treat some code as "undefined"?

That's the wrong mental model. The compiler doesn't decide to treat some code as "undefined". Instead, the compiler assumes all code is not "undefined", and generates code according to that. The compiler might notice that the code is doing bad things, and warn the developer (or even return an error), but that's not guaranteed, since undefined behavior is generally things the compiler cannot easily check.

For an example, Rust guarantees that no two mutable references can reference the same place at the same time, even in unsafe code. If a function receives a pair of mutable references, the compiler assumes that they do not overlap. If they in fact overlap, the code generated by the compiler might do the wrong thing.

Re:Stacked Borrows: An Aliasing Model for Rust by acqq on Sunday February 09, 2020 @05:43PM (#22284844)
I'm still on Linus' side:

https://gcc.gnu.org/ml/gcc/2016-02/msg00381.html

"The fact is, undefined compiler behavior is never a good idea. Not for serious projects.

Performance doesn't come from occasional small and odd micro-optimizations. I care about performance a lot, and I actually look at generated code and do profiling etc. None of those three options have ever shown up as issues. But the incorrect code they generate? It has."

Re:Stacked Borrows: An Aliasing Model for Rust by kd5bjo on Sunday February 09, 2020 @09:07AM (#22282152)
In Rust, that’s more or less the meaning of the unsafe keyword. It marks code that does things the compiler can’t verify as correct, but it’s always been expected that unsafe code will maintain the invariants the Rust compiler uses. This is an attempt to formalize that general expectation.