summary refs log blame commit diff stats
path: root/tests/ui/no_uaf_1.rs
blob: e4d7ac4638b00fe3c9977c11b6e206649340b111 (plain) (tree)











































                                                                
use core::cell::Cell;

use selfref::Holder;
use selfref::NewWith;
use selfref::opaque;

mod bad {
    use super::*;
    use std::rc::Rc;
    struct Foo<'a> {
        x: String,
        y: Rc<Cell<&'a str>>,
    }
    struct FooOpaque;
    opaque! {
        impl Opaque for FooOpaque {
            type Kind<'a> = Foo<'a>;
        }
    }
    pub fn test() {
        let rc = Rc::new(Cell::new(""));
        struct FooBuilder<'b>(Rc<Cell<&'b str>>);
        impl<'k, 'b> NewWith<'k, FooOpaque> for FooBuilder<'b> {
            fn new_with<'a>(self) -> Foo<'a> where 'k: 'a {
                Foo {
                    x: "Hello".to_owned(),
                    y: self.0,
                }
            }
        }
        let x = Holder::new_with(FooBuilder(rc.clone()));
        let x = Box::pin(x);
        x.as_ref().operate_in(|foo| {
            let foo = foo.get_ref();
            foo.y.set(&foo.x);
        });
        dbg!(&rc);
        drop(x);
        dbg!(&rc);
    }
}
fn main() {
    bad::test();
}