summaryrefslogtreecommitdiffstats
path: root/tests/ui/polymorphization/generators.rs
blob: 779bac0ace29bd0e23729904766afa4b9d181890 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// build-fail
// compile-flags:-Zpolymorphize=on -Zinline-mir=off
#![feature(generic_const_exprs, generators, generator_trait, rustc_attrs)]
//~^ WARN the feature `generic_const_exprs` is incomplete

use std::marker::Unpin;
use std::ops::{Generator, GeneratorState};
use std::pin::Pin;

enum YieldOrReturn<Y, R> {
    Yield(Y),
    Return(R),
}

fn finish<T, Y, R>(mut t: T) -> Vec<YieldOrReturn<Y, R>>
where
    T: Generator<(), Yield = Y, Return = R> + Unpin,
{
    let mut results = Vec::new();
    loop {
        match Pin::new(&mut t).resume(()) {
            GeneratorState::Yielded(yielded) => results.push(YieldOrReturn::Yield(yielded)),
            GeneratorState::Complete(returned) => {
                results.push(YieldOrReturn::Return(returned));
                return results;
            }
        }
    }
}

// This test checks that the polymorphization analysis functions on generators.

#[rustc_polymorphize_error]
pub fn unused_type<T>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin {
    || {
        //~^ ERROR item has unused generic parameters
        yield 1;
        2
    }
}

#[rustc_polymorphize_error]
pub fn used_type_in_yield<Y: Default>() -> impl Generator<(), Yield = Y, Return = u32> + Unpin {
    || {
        yield Y::default();
        2
    }
}

#[rustc_polymorphize_error]
pub fn used_type_in_return<R: Default>() -> impl Generator<(), Yield = u32, Return = R> + Unpin {
    || {
        yield 3;
        R::default()
    }
}

#[rustc_polymorphize_error]
pub fn unused_const<const T: u32>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin {
    || {
        //~^ ERROR item has unused generic parameters
        yield 1;
        2
    }
}

#[rustc_polymorphize_error]
pub fn used_const_in_yield<const Y: u32>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin
{
    || {
        yield Y;
        2
    }
}

#[rustc_polymorphize_error]
pub fn used_const_in_return<const R: u32>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin
{
    || {
        yield 4;
        R
    }
}

fn main() {
    finish(unused_type::<u32>());
    finish(used_type_in_yield::<u32>());
    finish(used_type_in_return::<u32>());
    finish(unused_const::<1u32>());
    finish(used_const_in_yield::<1u32>());
    finish(used_const_in_return::<1u32>());
}