summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/no_uaf_1.rs18
-rw-r--r--tests/ui/no_uaf_1.stderr47
-rw-r--r--tests/ui/no_uaf_2.rs4
3 files changed, 22 insertions, 47 deletions
diff --git a/tests/ui/no_uaf_1.rs b/tests/ui/no_uaf_1.rs
index e4d7ac4..1d9411e 100644
--- a/tests/ui/no_uaf_1.rs
+++ b/tests/ui/no_uaf_1.rs
@@ -1,7 +1,6 @@
 use core::cell::Cell;
 
 use selfref::Holder;
-use selfref::NewWith;
 use selfref::opaque;
 
 mod bad {
@@ -19,16 +18,13 @@ mod bad {
     }
     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 rc_clone = rc.clone();
+        let x = Holder::<'_, FooOpaque>::new_with(move |builder| {
+            builder.build(Foo {
+                x: "Hello".to_owned(),
+                y: rc_clone,
+            });
+        });
         let x = Box::pin(x);
         x.as_ref().operate_in(|foo| {
             let foo = foo.get_ref();
diff --git a/tests/ui/no_uaf_1.stderr b/tests/ui/no_uaf_1.stderr
index 6db82ed..af0d3e6 100644
--- a/tests/ui/no_uaf_1.stderr
+++ b/tests/ui/no_uaf_1.stderr
@@ -1,37 +1,16 @@
-error: lifetime may not live long enough
-  --> tests/ui/no_uaf_1.rs:25:17
+error[E0521]: borrowed data escapes outside of closure
+  --> tests/ui/no_uaf_1.rs:23:13
    |
-23 |           impl<'k, 'b> NewWith<'k, FooOpaque> for FooBuilder<'b> {
-   |                    -- lifetime `'b` defined here
-24 |               fn new_with<'a>(self) -> Foo<'a> where 'k: 'a {
-   |                           -- lifetime `'a` defined here
-25 | /                 Foo {
-26 | |                     x: "Hello".to_owned(),
-27 | |                     y: self.0,
-28 | |                 }
-   | |_________________^ associated function was supposed to return data with lifetime `'a` but it is returning data with lifetime `'b`
+21 |           let rc_clone = rc.clone();
+   |               -------- `rc_clone` declared here, outside of the closure body
+22 |           let x = Holder::<'_, FooOpaque>::new_with(move |builder| {
+   |                                                           ------- `builder` is a reference that is only valid in the closure body
+23 | /             builder.build(Foo {
+24 | |                 x: "Hello".to_owned(),
+25 | |                 y: rc_clone,
+26 | |             });
+   | |______________^ `builder` escapes the closure body here
    |
-   = help: consider adding the following bound: `'b: 'a`
-   = note: requirement occurs because of the type `Foo<'_>`, which makes the generic argument `'_` invariant
-   = note: the struct `Foo<'a>` is invariant over the parameter `'a`
+   = note: requirement occurs because of a mutable reference to `selfref::Builder<'_, FooOpaque>`
+   = note: mutable references are invariant over their type parameter
    = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
-
-error: lifetime may not live long enough
-  --> tests/ui/no_uaf_1.rs:25:17
-   |
-23 |           impl<'k, 'b> NewWith<'k, FooOpaque> for FooBuilder<'b> {
-   |                    -- lifetime `'b` defined here
-24 |               fn new_with<'a>(self) -> Foo<'a> where 'k: 'a {
-   |                           -- lifetime `'a` defined here
-25 | /                 Foo {
-26 | |                     x: "Hello".to_owned(),
-27 | |                     y: self.0,
-28 | |                 }
-   | |_________________^ associated function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a`
-   |
-   = help: consider adding the following bound: `'a: 'b`
-   = note: requirement occurs because of the type `Foo<'_>`, which makes the generic argument `'_` invariant
-   = note: the struct `Foo<'a>` is invariant over the parameter `'a`
-   = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
-
-help: `'b` and `'a` must be the same: replace one with the other
diff --git a/tests/ui/no_uaf_2.rs b/tests/ui/no_uaf_2.rs
index eb6b7d3..49694b2 100644
--- a/tests/ui/no_uaf_2.rs
+++ b/tests/ui/no_uaf_2.rs
@@ -6,7 +6,7 @@ use std::cell::Cell;
 use std::pin::pin;
 
 use selfref::opaque;
-use selfref::{new_with_closure, Holder};
+use selfref::Holder;
 
 struct MyStruct<'this> {
     cell: Cell<&'this str>,
@@ -22,7 +22,7 @@ opaque! {
 
 fn main() {
     let s =
-        Holder::<'_, MyStructKey>::new_with(new_with_closure::<MyStructKey, _>(|[]| MyStruct {
+        Holder::<'_, MyStructKey>::new_with(|builder| builder.build(MyStruct {
             cell: Default::default(),
         }));
     let s = pin!(s);