]> git.lizzy.rs Git - rust.git/blob - src/test/ui/static/static-reference-to-fn-2.rs
Rollup merge of #105872 - chenyukang:yukang/fix-105494-remove-method-call, r=eholk
[rust.git] / src / test / ui / static / static-reference-to-fn-2.rs
1 fn id<T>(x: T) -> T { x }
2
3 struct StateMachineIter<'a> {
4     statefn: &'a StateMachineFunc<'a>
5 }
6
7 type StateMachineFunc<'a> = fn(&mut StateMachineIter<'a>) -> Option<&'static str>;
8
9 impl<'a> Iterator for StateMachineIter<'a> {
10     type Item = &'static str;
11
12     fn next(&mut self) -> Option<&'static str> {
13         return  (*self.statefn)(self);
14     }
15 }
16
17 fn state1(self_: &mut StateMachineIter) -> Option<&'static str> {
18     self_.statefn = &id(state2 as StateMachineFunc);
19     //~^ ERROR temporary value dropped while borrowed
20     return Some("state1");
21 }
22
23 fn state2(self_: &mut StateMachineIter) -> Option<(&'static str)> {
24     self_.statefn = &id(state3 as StateMachineFunc);
25     //~^ ERROR temporary value dropped while borrowed
26     return Some("state2");
27 }
28
29 fn state3(self_: &mut StateMachineIter) -> Option<(&'static str)> {
30     self_.statefn = &id(finished as StateMachineFunc);
31     //~^ ERROR temporary value dropped while borrowed
32     return Some("state3");
33 }
34
35 fn finished(_: &mut StateMachineIter) -> Option<(&'static str)> {
36     return None;
37 }
38
39 fn state_iter() -> StateMachineIter<'static> {
40     StateMachineIter {
41     //~^ ERROR cannot return value referencing temporary value
42         statefn: &id(state1 as StateMachineFunc)
43     }
44 }
45
46
47 fn main() {
48     let mut it = state_iter();
49     println!("{:?}",it.next());
50     println!("{:?}",it.next());
51     println!("{:?}",it.next());
52     println!("{:?}",it.next());
53     println!("{:?}",it.next());
54 }