]> git.lizzy.rs Git - rust.git/blob - src/test/ui/generator/yield-while-iterating.rs
Auto merge of #44696 - michaelwoerister:fingerprints-in-dep-graph-3, r=nikomatsakis
[rust.git] / src / test / ui / generator / yield-while-iterating.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(generators, generator_trait)]
12
13 use std::ops::{GeneratorState, Generator};
14 use std::cell::Cell;
15
16 fn yield_during_iter_owned_data(x: Vec<i32>) {
17     // The generator owns `x`, so we error out when yielding with a
18     // reference to it.  This winds up becoming a rather confusing
19     // regionck error -- in particular, we would freeze with the
20     // reference in scope, and it doesn't live long enough.
21     let _b = move || {
22         for p in &x { //~ ERROR
23             yield();
24         }
25     };
26 }
27
28 fn yield_during_iter_borrowed_slice(x: &[i32]) {
29     let _b = move || {
30         for p in x {
31             yield();
32         }
33     };
34 }
35
36 fn yield_during_iter_borrowed_slice_2() {
37     let mut x = vec![22_i32];
38     let _b = || {
39         for p in &x {
40             yield();
41         }
42     };
43     println!("{:?}", x);
44 }
45
46 fn yield_during_iter_borrowed_slice_3() {
47     // OK to take a mutable ref to `x` and yield
48     // up pointers from it:
49     let mut x = vec![22_i32];
50     let mut b = || {
51         for p in &mut x {
52             yield p;
53         }
54     };
55     b.resume();
56 }
57
58 fn yield_during_iter_borrowed_slice_4() {
59     // ...but not OK to do that while reading
60     // from `x` too
61     let mut x = vec![22_i32];
62     let mut b = || {
63         for p in &mut x {
64             yield p;
65         }
66     };
67     println!("{}", x[0]); //~ ERROR
68     b.resume();
69 }
70
71 fn yield_during_range_iter() {
72     // Should be OK.
73     let mut b = || {
74         let v = vec![1,2,3];
75         let len = v.len();
76         for i in 0..len {
77             let x = v[i];
78             yield x;
79         }
80     };
81     b.resume();
82 }
83
84 fn main() { }