Skip to content

Commit

Permalink
Add ui test of sync bound
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 10, 2021
1 parent e1e347d commit ea173f9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/ui/non-sync.rs
@@ -0,0 +1,21 @@
use std::rc::Rc;
use std::thread;

struct Thing(Rc<i32>);

inventory::collect!(Thing);

fn clone_all() {
for thing in inventory::iter::<Thing> {
let _ = Rc::clone(&thing.0);
}
}

fn main() {
// It would be bad if this were allowed. These threads would race on the
// nonatomic reference counts.
let thread1 = thread::spawn(clone_all);
let thread2 = thread::spawn(clone_all);
thread1.join().unwrap();
thread2.join().unwrap();
}
18 changes: 18 additions & 0 deletions tests/ui/non-sync.stderr
@@ -0,0 +1,18 @@
error[E0277]: `Rc<i32>` cannot be shared between threads safely
--> tests/ui/non-sync.rs:6:1
|
6 | inventory::collect!(Thing);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rc<i32>` cannot be shared between threads safely
|
= help: within `Thing`, the trait `Sync` is not implemented for `Rc<i32>`
note: required because it appears within the type `Thing`
--> tests/ui/non-sync.rs:4:8
|
4 | struct Thing(Rc<i32>);
| ^^^^^
note: required by a bound in `Collect`
--> src/lib.rs
|
| pub trait Collect: Sync + Sized + 'static {
| ^^^^ required by this bound in `Collect`
= note: this error originates in the macro `inventory::collect` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit ea173f9

Please sign in to comment.