Skip to content

Commit

Permalink
Test(ui): Extend block tests for with_encoding
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Mabileau <paul.mabileau@harfanglab.fr>
  • Loading branch information
PaulDance committed Sep 16, 2024
1 parent 8793842 commit 6911801
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 3 deletions.
50 changes: 49 additions & 1 deletion crates/test-ui/ui/block_lifetimes_independent.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Test that lifetimes in blocks are not bound to each other.
//!
//! These tests will succeed if there are `'a: 'b`-like bounds on the closure.
use block2::RcBlock;
use block2::{ManualBlockEncoding, RcBlock};
use std::ffi::CStr;
use std::marker::PhantomData;

fn args<'a, 'b>(
f: impl Fn(&'a i32, &'b i32) + 'static,
Expand All @@ -23,4 +25,50 @@ fn return_entire<'a, 'b>(f: impl Fn() -> &'a i32 + 'b) -> RcBlock<dyn Fn() -> &'
RcBlock::new(f)
}

fn args_with_encoding<'a, 'b>(
f: impl Fn(&'a i32, &'b i32) + 'static,
) -> RcBlock<dyn Fn(&'b i32, &'a i32) + 'static> {
struct Enc<'a, 'b>(PhantomData<&'a i32>, PhantomData<&'b i32>);
unsafe impl<'a, 'b> ManualBlockEncoding for Enc<'a, 'b> {
type Arguments = (&'a i32, &'b i32);
type Return = ();
const ENCODING_CSTR: &'static CStr = c"v24@?0^i8^i16";
}
unsafe { RcBlock::with_encoding::<_, _, _, Enc<'a, 'b>>(f) }
}

fn args_return_with_encoding<'a, 'b>(
f: impl Fn(&'a i32) -> &'b i32 + 'static,
) -> RcBlock<dyn Fn(&'b i32) -> &'a i32 + 'static> {
struct Enc<'a, 'b>(PhantomData<&'a i32>, PhantomData<&'b i32>);
unsafe impl<'a, 'b> ManualBlockEncoding for Enc<'a, 'b> {
type Arguments = (&'a i32,);
type Return = &'b i32;
const ENCODING_CSTR: &'static CStr = c"^i816@?0^i8";
}
unsafe { RcBlock::with_encoding::<_, _, _, Enc<'a, 'b>>(f) }
}

fn args_entire_with_encoding<'a, 'b>(f: impl Fn(&'a i32) + 'b) -> RcBlock<dyn Fn(&'b i32) + 'a> {
struct Enc<'a>(PhantomData<&'a i32>);
unsafe impl<'a> ManualBlockEncoding for Enc<'a> {
type Arguments = (&'a i32,);
type Return = ();
const ENCODING_CSTR: &'static CStr = c"v16@?0^i8";
}
unsafe { RcBlock::with_encoding::<_, _, _, Enc<'a>>(f) }
}

fn return_entire_with_encoding<'a, 'b>(
f: impl Fn() -> &'a i32 + 'b,
) -> RcBlock<dyn Fn() -> &'b i32 + 'a> {
struct Enc<'a>(PhantomData<&'a i32>);
unsafe impl<'a> ManualBlockEncoding for Enc<'a> {
type Arguments = ();
type Return = &'a i32;
const ENCODING_CSTR: &'static CStr = c"^i8@?0";
}
unsafe { RcBlock::with_encoding::<_, _, _, Enc<'a>>(f) }
}

fn main() {}
104 changes: 104 additions & 0 deletions crates/test-ui/ui/block_lifetimes_independent.stderr

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 29 additions & 1 deletion crates/test-ui/ui/lifetime_of_closure_tied_to_block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
use block2::{RcBlock, StackBlock};
use block2::{ManualBlockEncoding, RcBlock, StackBlock};
use std::ffi::CStr;

struct VoidToI32;
unsafe impl ManualBlockEncoding for VoidToI32 {
type Arguments = ();
type Return = i32;
const ENCODING_CSTR: &'static CStr = c"i8@?0";
}

fn main() {
let _ = {
Expand All @@ -11,6 +19,16 @@ fn main() {
RcBlock::new(|| x + 2).clone()
};

let _ = {
let x = 2;
unsafe { RcBlock::with_encoding::<_, _, _, VoidToI32>(|| x + 2) }
};

let _ = {
let x = 2;
unsafe { RcBlock::with_encoding::<_, _, _, VoidToI32>(|| x + 2).clone() }
};

let _ = {
let x = 2;
StackBlock::new(|| x + 2)
Expand All @@ -20,4 +38,14 @@ fn main() {
let x = 2;
StackBlock::new(|| x + 2).copy()
};

let _ = {
let x = 2;
unsafe { StackBlock::with_encoding::<VoidToI32>(|| x + 2) }
};

let _ = {
let x = 2;
unsafe { StackBlock::with_encoding::<VoidToI32>(|| x + 2).copy() }
};
}
59 changes: 58 additions & 1 deletion crates/test-ui/ui/lifetime_of_closure_tied_to_block.stderr

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions crates/test-ui/ui/stack_block_with_encoding_requires_clone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use block2::{ManualBlockEncoding, StackBlock};
use std::ffi::CStr;

struct Foo;

fn main() {
struct FooBlockEncoding;
unsafe impl ManualBlockEncoding for FooBlockEncoding {
type Arguments = ();
type Return = ();
const ENCODING_CSTR: &'static CStr = c"v8@?0";
}

let foo = Foo;
let _ = unsafe {
StackBlock::with_encoding::<FooBlockEncoding>(move || {
let _ = &foo;
})
};
}
31 changes: 31 additions & 0 deletions crates/test-ui/ui/stack_block_with_encoding_requires_clone.stderr

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6911801

Please sign in to comment.