about summary refs log tree commit diff
path: root/library/coretests/tests/pin_macro.rs
blob: bfbfa8d280fa1e06f12fe1313f6f935c3f11b614 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// edition:2021

use core::marker::PhantomPinned;
use core::mem::{drop as stuff, transmute};
use core::pin::{Pin, pin};

#[test]
fn basic() {
    let it: Pin<&mut PhantomPinned> = pin!(PhantomPinned);
    stuff(it);
}

#[test]
fn extension_works_through_block() {
    let it: Pin<&mut PhantomPinned> = { pin!(PhantomPinned) };
    stuff(it);
}

#[test]
fn extension_works_through_unsafe_block() {
    // "retro-type-inference" works as well.
    let it: Pin<&mut PhantomPinned> = unsafe { pin!(transmute(())) };
    stuff(it);
}

#[test]
fn unsize_coercion() {
    let slice: Pin<&mut [PhantomPinned]> = pin!([PhantomPinned; 2]);
    stuff(slice);
    let dyn_obj: Pin<&mut dyn Send> = pin!([PhantomPinned; 2]);
    stuff(dyn_obj);
}

#[test]
fn rust_2024_expr() {
    // Check that we accept a Rust 2024 $expr.
    std::pin::pin!(const { 1 });
}

#[test]
fn temp_lifetime() {
    // Check that temporary lifetimes work as in Rust 2021.
    // Regression test for https://github.com/rust-lang/rust/issues/138596
    match std::pin::pin!(foo(&mut 0)) {
        _ => {}
    }
    async fn foo(_: &mut usize) {}
}

#[test]
fn transitive_extension() {
    async fn temporary() {}

    // `pin!` witnessed in the wild being used like this, even if it yields
    // a `Pin<&mut &mut impl Unpin>`; it does work because `pin!`
    // happens to transitively extend the lifespan of `temporary()`.
    let p = pin!(&mut temporary());
    let _use = p;
}