]> git.proxmox.com Git - rustc.git/blame - src/test/compile-fail/static-reference-to-fn-2.rs
New upstream version 1.21.0+dfsg1
[rustc.git] / src / test / compile-fail / static-reference-to-fn-2.rs
CommitLineData
1a4d82fc
JJ
1// Copyright 2014 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
3b2f2976
XL
11fn id<T>(x: T) -> T { x }
12
1a4d82fc
JJ
13struct StateMachineIter<'a> {
14 statefn: &'a StateMachineFunc<'a>
15}
16
17type StateMachineFunc<'a> = fn(&mut StateMachineIter<'a>) -> Option<&'static str>;
18
19impl<'a> Iterator for StateMachineIter<'a> {
20 type Item = &'static str;
21
22 fn next(&mut self) -> Option<&'static str> {
23 return (*self.statefn)(self);
24 }
25}
26
27fn state1(self_: &mut StateMachineIter) -> Option<&'static str> {
3b2f2976 28 self_.statefn = &id(state2 as StateMachineFunc);
1a4d82fc
JJ
29 //~^ ERROR borrowed value does not live long enough
30 return Some("state1");
31}
32
33fn state2(self_: &mut StateMachineIter) -> Option<(&'static str)> {
3b2f2976 34 self_.statefn = &id(state3 as StateMachineFunc);
1a4d82fc
JJ
35 //~^ ERROR borrowed value does not live long enough
36 return Some("state2");
37}
38
39fn state3(self_: &mut StateMachineIter) -> Option<(&'static str)> {
3b2f2976 40 self_.statefn = &id(finished as StateMachineFunc);
1a4d82fc
JJ
41 //~^ ERROR borrowed value does not live long enough
42 return Some("state3");
43}
44
45fn finished(_: &mut StateMachineIter) -> Option<(&'static str)> {
46 return None;
47}
48
49fn state_iter() -> StateMachineIter<'static> {
50 StateMachineIter {
3b2f2976
XL
51 statefn: &id(state1 as StateMachineFunc)
52 //~^ ERROR borrowed value does not live long enough
1a4d82fc
JJ
53 }
54}
55
56
57fn main() {
58 let mut it = state_iter();
59 println!("{:?}",it.next());
60 println!("{:?}",it.next());
61 println!("{:?}",it.next());
62 println!("{:?}",it.next());
63 println!("{:?}",it.next());
64}