]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/regions-name-undeclared.rs
Do not use entropy during gen_weighted_bool(1)
[rust.git] / src / test / compile-fail / regions-name-undeclared.rs
1 // Copyright 2012-2013 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 // Check that lifetime resolver enforces the lifetime name scoping
12 // rules correctly in various scenarios.
13
14 struct Foo<'a> {
15     x: &'a int
16 }
17
18 impl<'a> Foo<'a> {
19     // &'a is inherited:
20     fn m1(&self, arg: &'a int) { }
21     fn m2(&'a self) { }
22     fn m3(&self, arg: Foo<'a>) { }
23
24     // &'b is not:
25     fn m4(&self, arg: &'b int) { } //~ ERROR undeclared lifetime
26     fn m5(&'b self) { } //~ ERROR undeclared lifetime
27     fn m6(&self, arg: Foo<'b>) { } //~ ERROR undeclared lifetime
28 }
29
30 fn bar<'a>(x: &'a int) {
31     // &'a is visible to code:
32     let y: &'a int = x;
33
34     // &'a is not visible to *items*:
35     type X = Option<&'a int>; //~ ERROR undeclared lifetime
36     enum E {
37         E1(&'a int) //~ ERROR undeclared lifetime
38     }
39     struct S {
40         f: &'a int //~ ERROR undeclared lifetime
41     }
42     fn f(a: &'a int) { } //~ ERROR undeclared lifetime
43
44     // &'a CAN be declared on functions and used then:
45     fn g<'a>(a: &'a int) { } // OK
46     fn h(a: for<'a>|&'a int|) { } // OK
47
48     // But not in the bound of a closure, it's not in scope *there*
49     fn i(a: for<'a>|&int|:'a) { } //~ ERROR undeclared lifetime
50 }
51
52 // Test nesting of lifetimes in fn type declarations
53 fn fn_types(a: &'a int, //~ ERROR undeclared lifetime
54             b: for<'a>|a: &'a int,
55                        b: &'b int, //~ ERROR undeclared lifetime
56                        c: for<'b>|a: &'a int,
57                                   b: &'b int|,
58                        d: &'b int|, //~ ERROR undeclared lifetime
59             c: &'a int) //~ ERROR undeclared lifetime
60 {
61 }
62
63 pub fn main() {}