4 ms·
> Was Pin SUPPOSED to be resilient to unsafe code? Remember, unsafe does not mean "I can do whatever I want," it means "I am promising to uphold some guarantee
by steveklabnik 7y ago
> Was Pin SUPPOSED to be resilient to unsafe code?
Remember, unsafe does not mean "I can do whatever I want," it means "I am promising to uphold some guarantee on my own." That is, let's assume we have a function:
/// Makes a new Foo.
///
/// # Safety
///
/// x must never be greater than 5
unsafe fn new(x: i32) -> Foo {
and you write
let x = 6;
unsafe {
new(x);
}
You have a bug, even though new is marked unsafe.
In my understanding of the unsafe examples above, the unsafe code was upholding the invariants that it was required to uphold, and so would be more like having written `let x = 2;` in the above example, if that caused an issue, clearly there's a bug.
- eximius 7y agoI think this is a little too dumbed down for me to see the relevance to the actual problem. Also, I believ 1&2 both require unsafe code to implement the trait that allows the unsoundness. For what &T can I safely implement DerefMut? Is it the fault of Pin that a type has DerefMut implemented that the unsafe block of that implementation doesn't uphold safety?
- steveklabnik 7y agoThe relevance to the problem is that even if these examples needed unsafe code, that doesn't mean that there's no problem. "resilient to unsafe code" is not the right way to think about unsafe. > For what &T can I safely implement DerefMut? Is it the fault of Pin that a type has DerefMut implemented that the unsafe block of that implementation doesn't uphold safety? It is the fault of Pin's safety guidelines, which suggest a contract that is not actually enough to uphold safety. That's why this is considered an unsoundness in Pin, and not a problem with the code written with unsafe. That is: > that the unsafe block of that implementation doesn't uphold safety? is not correct, the unsafe block does everything it's supposed to to uphold safety.
- eximius 7y agoI can see the argument that Pin should be safe if you meet the contract it claims to need. The requirements are not sufficiently tight. Fair enough.