Closed
Description
#75648 added #[may_dangle]
to T
in the Drop
implementation of SyncOnceCell
. This is correct for simple types like T = &str
, but when T
's Drop
implementation accesses borrowed data, this might lead to accessing already dropped data:
#![feature(once_cell)]
use std::lazy::SyncOnceCell;
struct A<'a>(&'a str);
impl<'a> Drop for A<'a> {
fn drop(&mut self) {
dbg!(self.0);
}
}
fn main() {
let cell = SyncOnceCell::new();
{
let s = String::from("hello world");
let _ = cell.set(A(&s));
}
}
[src/main.rs:9] self.0 = "\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{10}thread 'main' panicked at 'byte index 9 is not a char boundary; it is inside '\u{10}' (bytes 8..9) of `À`', library/core/src/fmt/mod.rs:2043:22
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace