It's always possible to use a safe class unsafely from an unsafe context. Same as in Rust. If you dereference a dangling pointer, borrow from that lvalue, and pass it to a safe function, that's an unsound use. The guarantee is that UB won't originate from safe code, not that safe code is impossible to use in an unsound manner.
Basically yes. Safe functions have defined behavior for all valid inputs. Mutable references that alias are not valid inputs. In a safe context, the compiler upholds that invariant. In an unsafe context it's up to the user not to break it with unsafe operations.
View all comments
Show parent comment
12
u/seanbaxter Oct 26 '24
It's always possible to use a safe class unsafely from an unsafe context. Same as in Rust. If you dereference a dangling pointer, borrow from that lvalue, and pass it to a safe function, that's an unsound use. The guarantee is that UB won't originate from safe code, not that safe code is impossible to use in an unsound manner.