| ▲ | adwn 18 hours ago | ||||||||||||||||
> The only thing unsafe does is let you have an unbounded lifetime. No, you're wrong: You can create any lifetime. Proof:
This will take a reference and return it with any lifetime specified by the caller.> You don't disable anything. I said "effectively disable". For example: fn trust_me_bro<'a>(x: mut u32) -> &'a mut u32 { unsafe { &mut x } }
After the call to trust_me_bro, two aliasing, mutable references exist simultaneously. This would usually be prevented by the borrow checker, but the unsafe code has effectively disabled it. | |||||||||||||||||
| ▲ | Ygg2 17 hours ago | parent [-] | ||||||||||||||||
> Proof: You just listed examples of unbounded lifetimes. > I said "effectively disable". For example: Not sure what you meant by this example since it doesn't compile. It seems the borrow checker caught your mischief. So much for effectively disabling stuff :P You haven't effectively disabled anything; you just (tried to) wrote unsound code that washes one mutable ref as another. This stuff is allowed provided shared refs are never accessed at the same time (for example, panicking upon reading reference_b). What you probably meant is https://play.rust-lang.org/?version=stable&mode=debug&editio... But you know what? If you're dabbling in unsafe, you have this big button called Tools in the playground. Choose Miri, then run your code; it will display large Undefined behavior. It even highlights the `trust_me_bro` function. Hell, run this with UBSAN, ASAN, and other C tools. They will probably catch any such behavior. | |||||||||||||||||
| |||||||||||||||||