File: issue-105084.rs

package info (click to toggle)
rustc-web 1.78.0%2Bdfsg1-2~deb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,245,420 kB
  • sloc: xml: 147,985; javascript: 18,022; sh: 11,083; python: 10,265; ansic: 6,172; cpp: 5,023; asm: 4,390; makefile: 4,269
file content (42 lines) | stat: -rw-r--r-- 1,180 bytes parent folder | download | duplicates (2)
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
#![feature(coroutines)]
#![feature(coroutine_clone)]
#![feature(coroutine_trait)]
#![feature(rustc_attrs, stmt_expr_attributes)]

use std::ops::Coroutine;
use std::pin::Pin;

fn copy<T: Copy>(x: T) -> T {
    x
}

fn main() {
    let mut g = || {
        // This is desuraged as 4 stages:
        // - allocate a `*mut u8` with `exchange_malloc`;
        // - create a Box that is ignored for trait computations;
        // - compute fields (and yields);
        // - assign to `t`.
        let t = #[rustc_box]
        Box::new((5, yield));
        drop(t);
    };

    // Allocate the temporary box.
    Pin::new(&mut g).resume(());

    // The temporary box is in coroutine locals.
    // As it is not taken into account for trait computation,
    // the coroutine is `Copy`.
    let mut h = copy(g);
    //~^ ERROR the trait bound `Box<(i32, ())>: Copy` is not satisfied in

    // We now have 2 boxes with the same backing allocation:
    // one inside `g` and one inside `h`.
    // Proceed and drop `t` in `g`.
    Pin::new(&mut g).resume(());
    //~^ ERROR borrow of moved value: `g`

    // Proceed and drop `t` in `h` -> double free!
    Pin::new(&mut h).resume(());
}