Skip to content

Commit

Permalink
Added some more compile tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DelSkayn committed Jun 2, 2023
1 parent a1fc41e commit e1a33f0
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 26 deletions.
2 changes: 2 additions & 0 deletions core/tests/async_compile_fail/async_nested_contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub async fn test() {
let ctx_2 = AsyncContext::full(&rt).await.unwrap();
async_with!(ctx_1 => |ctx_1|{
async_with!(ctx_2 => |ctx_2|{
// It is disallowed to use multiple ctx object together from different with closures.
// Lifetime on ctx should be unique.
ctx_1.globals().set("t", ctx_2.globals());
}).await
})
Expand Down
63 changes: 37 additions & 26 deletions core/tests/async_compile_fail/async_nested_contexts.stderr
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
error[E0521]: borrowed data escapes outside of closure
--> tests/async_compile_fail/async_nested_contexts.rs:9:38
error[E0277]: `NonNull<JSContext>` cannot be shared between threads safely
--> tests/async_compile_fail/async_nested_contexts.rs:8:9
|
7 | / async_with!(ctx_1 => |ctx_1|{
8 | |/ async_with!(ctx_2 => |ctx_2|{
9 | || ctx_1.globals().set("t", ctx_2.globals());
| || ^^^^^^^^^^^^^^^ `ctx_2` escapes the closure body here
10 | || }).await
| ||__________- `ctx_2` is a reference that is only valid in the closure body
11 | | })
| |_______- `ctx_1` declared here, outside of the closure body

error[E0521]: borrowed data escapes outside of closure
--> tests/async_compile_fail/async_nested_contexts.rs:9:13
8 | / async_with!(ctx_2 => |ctx_2|{
9 | | // It is disallowed to use multiple ctx object together from different with closures.
10 | | // Lifetime on ctx should be unique.
11 | | ctx_1.globals().set("t", ctx_2.globals());
12 | | }).await
| | ^
| | |
| |__________`NonNull<JSContext>` cannot be shared between threads safely
| required by a bound introduced by this call
|
7 | / async_with!(ctx_1 => |ctx_1|{
8 | | async_with!(ctx_2 => |ctx_2|{
9 | | ctx_1.globals().set("t", ctx_2.globals());
| | ^^^^^^^^^^^^^^^
| | |
| | `ctx_1` escapes the closure body here
| | argument requires that `'1` must outlive `'static`
10 | | }).await
11 | | })
| | -
| | |
| |______`ctx_1` is a reference that is only valid in the closure body
| has type `Ctx<'1>`
= help: within `Ctx<'_>`, the trait `Sync` is not implemented for `NonNull<JSContext>`
note: required because it appears within the type `Ctx<'_>`
--> src/context/ctx.rs
|
| pub struct Ctx<'js> {
| ^^^
= note: required for `&Ctx<'_>` to implement `Send`
note: required because it's used within this closure
--> tests/async_compile_fail/async_nested_contexts.rs:8:9
|
8 | / async_with!(ctx_2 => |ctx_2|{
9 | | // It is disallowed to use multiple ctx object together from different with closures.
10 | | // Lifetime on ctx should be unique.
11 | | ctx_1.globals().set("t", ctx_2.globals());
12 | | }).await
| |__________^
= note: required for `[closure@$DIR/src/context/async.rs:106:52: 106:58]` to implement `ParallelSend`
note: required by a bound in `AsyncContext::async_with`
--> src/context/async.rs
|
| pub async fn async_with<'a, F, R: 'a>(&'a self, f: F) -> R
| ---------- required by a bound in this associated function
...
| + ParallelSend,
| ^^^^^^^^^^^^ required by this bound in `AsyncContext::async_with`
= note: this error originates in the macro `async_with` (in Nightly builds, run with -Z macro-backtrace for more info)
20 changes: 20 additions & 0 deletions core/tests/async_parallel_compile_fail/capture_rc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::rc::Rc;

use rquickjs::{async_with, AsyncContext, AsyncRuntime};

pub async fn test() {
let rt = AsyncRuntime::new().unwrap();
let ctx = AsyncContext::full(&rt).await.unwrap();

let fut = {
let mut var = Rc::new(1);
let var_c = var.clone();
async_with!(ctx => |_ctx|{
// you should not be able to move non send types into the closure.
assert_eq!(*var_c,1);
})
};
fut.await;
}

fn main() {}
32 changes: 32 additions & 0 deletions core/tests/async_parallel_compile_fail/capture_rc.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
error[E0277]: `Rc<i32>` cannot be sent between threads safely
--> tests/async_parallel_compile_fail/capture_rc.rs:12:9
|
12 | / async_with!(ctx => |_ctx|{
13 | | // you should not be able to move non send types into the closure.
14 | | assert_eq!(*var_c,1);
15 | | })
| | ^
| | |
| | `Rc<i32>` cannot be sent between threads safely
| |__________within this `[closure@$DIR/src/context/async.rs:106:52: 106:58]`
| required by a bound introduced by this call
|
= help: within `[closure@$DIR/src/context/async.rs:106:52: 106:58]`, the trait `Send` is not implemented for `Rc<i32>`
note: required because it's used within this closure
--> tests/async_parallel_compile_fail/capture_rc.rs:12:9
|
12 | / async_with!(ctx => |_ctx|{
13 | | // you should not be able to move non send types into the closure.
14 | | assert_eq!(*var_c,1);
15 | | })
| |__________^
= note: required for `[closure@$DIR/src/context/async.rs:106:52: 106:58]` to implement `ParallelSend`
note: required by a bound in `AsyncContext::async_with`
--> src/context/async.rs
|
| pub async fn async_with<'a, F, R: 'a>(&'a self, f: F) -> R
| ---------- required by a bound in this associated function
...
| + ParallelSend,
| ^^^^^^^^^^^^ required by this bound in `AsyncContext::async_with`
= note: this error originates in the macro `async_with` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 2 additions & 0 deletions core/tests/compile_fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ fn compile_test() {
t.compile_fail("tests/compile_fail/*.rs");
#[cfg(feature = "futures")]
t.compile_fail("tests/async_compile_fail/*.rs");
#[cfg(all(feature = "futures", feature = "parallel"))]
t.compile_fail("tests/async_parallel_compile_fail/*.rs");
}

0 comments on commit e1a33f0

Please sign in to comment.