Fix RefUnwindSafe & UnwinsSafe impls for lazy::SyncLazy#74814
Fix RefUnwindSafe & UnwinsSafe impls for lazy::SyncLazy#74814bors merged 1 commit intorust-lang:masterfrom
Conversation
|
r? @kennytm (rust_highfive has picked a reviewer for you, use r? to override) |
|
cc tracking issue #74465 |
|
r? @KodrAus |
|
Hmm, I think we need the closure to be unwind safe, because users can observe panics through borrowed arguments. Right now, this won’t compile: let not_unwind_safe = std::cell::RefCell::new(42);
let l = std::lazy::SyncLazy::new(|| {
let mut a = not_unwind_safe.borrow_mut();
*a = 43;
panic!("lol");
*a
});
std::panic::catch_unwind(|| {
let a = *l;
});But if we implement They’re pretty niche traits 🙂 I use this blanket impl to try remember how they interact: impl<T: RefUnwindSafe + ?Sized> UnwindSafe for &T {} // and Rc<T> and Arc<T> etc
fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> { .. }So Looking at them now, should the impl be: impl<T: RefUnwindSafe, F: UnwindSafe> RefUnwindSafe for SyncLazy<T, F> {}because we move out of It might also be worth moving these impls into the |
|
Yup, I agree with your reasoning, I think this is rougthly the same justification we use for |
|
☔ The latest upstream changes (presumably #73265) made this pull request unmergeable. Please resolve the merge conflicts. |
The logic here is the same as for Send&Sync impls.
|
Rebased onto master. I decided not to move stuff over to |
|
Looks good to me! @bors r+ |
|
📌 Commit ed1439c has been approved by |
Rollup of 8 pull requests Successful merges: - rust-lang#74266 (Clean up E0720 explanation) - rust-lang#74671 (add const generics array coercion test) - rust-lang#74707 (Add str::[r]split_once) - rust-lang#74814 (Fix RefUnwindSafe & UnwinsSafe impls for lazy::SyncLazy) - rust-lang#74859 (Update outdated readme) - rust-lang#74864 (ayu theme: Change doccomment color to `#a1ac88`) - rust-lang#74872 (Enable to ping RISC-V group via triagebot) - rust-lang#74891 (handle ConstEquate in rustdoc) Failed merges: r? @ghost
I think we should implement those unconditionally with respect to
F.The user code can't observe the closure in any way, and we poison lazy if the closure itself panics.
But I've never fully wrapped my head around
UnwindSafetraits, so 🤷♂️