Skip to content

Commit 42d69e2

Browse files
Write {ui,} tests for pin_macro and pin!
1 parent ee9cd7b commit 42d69e2

6 files changed

+119
-0
lines changed

library/core/tests/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#![feature(inline_const)]
4646
#![feature(is_sorted)]
4747
#![feature(pattern)]
48+
#![feature(pin_macro)]
4849
#![feature(sort_internals)]
4950
#![feature(slice_take)]
5051
#![feature(maybe_uninit_uninit_array)]
@@ -122,6 +123,7 @@ mod ops;
122123
mod option;
123124
mod pattern;
124125
mod pin;
126+
mod pin_macro;
125127
mod ptr;
126128
mod result;
127129
mod simd;

library/core/tests/pin_macro.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// edition:2021
2+
use core::{
3+
marker::PhantomPinned,
4+
mem::{drop as stuff, transmute},
5+
pin::{pin, Pin},
6+
};
7+
8+
#[test]
9+
fn basic() {
10+
let it: Pin<&mut PhantomPinned> = pin!(PhantomPinned);
11+
stuff(it);
12+
}
13+
14+
#[test]
15+
fn extension_works_through_block() {
16+
let it: Pin<&mut PhantomPinned> = { pin!(PhantomPinned) };
17+
stuff(it);
18+
}
19+
20+
#[test]
21+
fn extension_works_through_unsafe_block() {
22+
// "retro-type-inference" works as well.
23+
let it: Pin<&mut PhantomPinned> = unsafe { pin!(transmute(())) };
24+
stuff(it);
25+
}
26+
27+
#[test]
28+
fn unsize_coercion() {
29+
let slice: Pin<&mut [PhantomPinned]> = pin!([PhantomPinned; 2]);
30+
stuff(slice);
31+
let dyn_obj: Pin<&mut dyn Send> = pin!([PhantomPinned; 2]);
32+
stuff(dyn_obj);
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// edition:2018
2+
#![feature(pin_macro)]
3+
4+
use core::{
5+
marker::PhantomPinned,
6+
mem,
7+
pin::{pin, Pin},
8+
};
9+
10+
fn main() {
11+
let mut phantom_pinned = pin!(PhantomPinned);
12+
mem::take(phantom_pinned.pointer); //~ ERROR use of unstable library feature 'unsafe_pin_internals'
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0658]: use of unstable library feature 'unsafe_pin_internals'
2+
--> $DIR/cant_access_internals.rs:12:15
3+
|
4+
LL | mem::take(phantom_pinned.pointer);
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: add `#![feature(unsafe_pin_internals)]` to the crate attributes to enable
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// edition:2018
2+
#![feature(pin_macro)]
3+
4+
use core::{
5+
convert::identity,
6+
marker::PhantomPinned,
7+
mem::drop as stuff,
8+
pin::pin,
9+
};
10+
11+
fn function_call_stops_borrow_extension() {
12+
let phantom_pinned = identity(pin!(PhantomPinned));
13+
//~^ ERROR temporary value dropped while borrowed
14+
stuff(phantom_pinned)
15+
}
16+
17+
fn promotion_only_works_for_the_innermost_block() {
18+
let phantom_pinned = {
19+
let phantom_pinned = pin!(PhantomPinned);
20+
//~^ ERROR temporary value dropped while borrowed
21+
phantom_pinned
22+
};
23+
stuff(phantom_pinned)
24+
}
25+
26+
fn main() {
27+
function_call_stops_borrow_extension();
28+
promotion_only_works_for_the_innermost_block();
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error[E0716]: temporary value dropped while borrowed
2+
--> $DIR/lifetime_errors_on_promotion_misusage.rs:12:35
3+
|
4+
LL | let phantom_pinned = identity(pin!(PhantomPinned));
5+
| ^^^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
6+
| |
7+
| creates a temporary which is freed while still in use
8+
LL |
9+
LL | stuff(phantom_pinned)
10+
| -------------- borrow later used here
11+
|
12+
= note: consider using a `let` binding to create a longer lived value
13+
= note: this error originates in the macro `pin` (in Nightly builds, run with -Z macro-backtrace for more info)
14+
15+
error[E0716]: temporary value dropped while borrowed
16+
--> $DIR/lifetime_errors_on_promotion_misusage.rs:19:30
17+
|
18+
LL | let phantom_pinned = {
19+
| -------------- borrow later stored here
20+
LL | let phantom_pinned = pin!(PhantomPinned);
21+
| ^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
22+
...
23+
LL | };
24+
| - temporary value is freed at the end of this statement
25+
|
26+
= note: consider using a `let` binding to create a longer lived value
27+
= note: this error originates in the macro `pin` (in Nightly builds, run with -Z macro-backtrace for more info)
28+
29+
error: aborting due to 2 previous errors
30+
31+
For more information about this error, try `rustc --explain E0716`.

0 commit comments

Comments
 (0)