Skip to content

Commit 9b70cfd

Browse files
committed
no-send-res-ports.rs
1 parent 000d5ae commit 9b70cfd

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

tests/ui/threads-sendsync/rc-is-not-send.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
use std::thread;
1+
//! Test that `Rc<T>` cannot be sent between threads.
2+
//!
3+
//! `Rc<T>` uses non-atomic reference counting, so it's not safe to send between
4+
//! threads. This test makes sure the compiler catches attempts to move `Rc<T>`
5+
//! into a spawned thread and produces a helpful error message.
6+
27
use std::rc::Rc;
8+
use std::thread;
39

410
#[derive(Debug)]
511
struct Port<T>(Rc<T>);
612

7-
fn main() {
8-
#[derive(Debug)]
9-
struct Foo {
10-
_x: Port<()>,
11-
}
13+
#[derive(Debug)]
14+
struct Foo {
15+
_x: Port<()>,
16+
}
1217

13-
impl Drop for Foo {
14-
fn drop(&mut self) {}
15-
}
18+
impl Drop for Foo {
19+
fn drop(&mut self) {}
20+
}
1621

17-
fn foo(x: Port<()>) -> Foo {
18-
Foo {
19-
_x: x
20-
}
21-
}
22+
fn foo(x: Port<()>) -> Foo {
23+
Foo { _x: x }
24+
}
2225

26+
fn main() {
2327
let x = foo(Port(Rc::new(())));
2428

25-
thread::spawn(move|| {
29+
thread::spawn(move || {
2630
//~^ ERROR `Rc<()>` cannot be sent between threads safely
2731
let y = x;
2832
println!("{:?}", y);

tests/ui/threads-sendsync/rc-is-not-send.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0277]: `Rc<()>` cannot be sent between threads safely
2-
--> $DIR/no-send-res-ports.rs:25:19
2+
--> $DIR/rc-is-not-send.rs:29:19
33
|
4-
LL | thread::spawn(move|| {
5-
| ------------- ^-----
4+
LL | thread::spawn(move || {
5+
| ------------- ^------
66
| | |
7-
| _____|_____________within this `{closure@$DIR/no-send-res-ports.rs:25:19: 25:25}`
7+
| _____|_____________within this `{closure@$DIR/rc-is-not-send.rs:29:19: 29:26}`
88
| | |
99
| | required by a bound introduced by this call
1010
LL | |
@@ -13,22 +13,22 @@ LL | | println!("{:?}", y);
1313
LL | | });
1414
| |_____^ `Rc<()>` cannot be sent between threads safely
1515
|
16-
= help: within `{closure@$DIR/no-send-res-ports.rs:25:19: 25:25}`, the trait `Send` is not implemented for `Rc<()>`
16+
= help: within `{closure@$DIR/rc-is-not-send.rs:29:19: 29:26}`, the trait `Send` is not implemented for `Rc<()>`
1717
note: required because it appears within the type `Port<()>`
18-
--> $DIR/no-send-res-ports.rs:5:8
18+
--> $DIR/rc-is-not-send.rs:11:8
1919
|
2020
LL | struct Port<T>(Rc<T>);
2121
| ^^^^
2222
note: required because it appears within the type `Foo`
23-
--> $DIR/no-send-res-ports.rs:9:12
23+
--> $DIR/rc-is-not-send.rs:14:8
2424
|
25-
LL | struct Foo {
26-
| ^^^
25+
LL | struct Foo {
26+
| ^^^
2727
note: required because it's used within this closure
28-
--> $DIR/no-send-res-ports.rs:25:19
28+
--> $DIR/rc-is-not-send.rs:29:19
2929
|
30-
LL | thread::spawn(move|| {
31-
| ^^^^^^
30+
LL | thread::spawn(move || {
31+
| ^^^^^^^
3232
note: required by a bound in `spawn`
3333
--> $SRC_DIR/std/src/thread/mod.rs:LL:COL
3434

0 commit comments

Comments
 (0)