]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/rfc1623.rs
New upstream version 1.17.0+dfsg1
[rustc.git] / src / test / run-pass / rfc1623.rs
1 // Copyright 2012 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 #![allow(dead_code)]
12
13 // very simple test for a 'static static with default lifetime
14 static STATIC_STR: &str = "&'static str";
15 const CONST_STR: &str = "&'static str";
16
17 // this should be the same as without default:
18 static EXPLICIT_STATIC_STR: &'static str = "&'static str";
19 const EXPLICIT_CONST_STR: &'static str = "&'static str";
20
21 // a function that elides to an unbound lifetime for both in- and output
22 fn id_u8_slice(arg: &[u8]) -> &[u8] {
23 arg
24 }
25
26 // one with a function, argument elided
27 static STATIC_SIMPLE_FN: &fn(&[u8]) -> &[u8] = &(id_u8_slice as fn(&[u8]) -> &[u8]);
28 const CONST_SIMPLE_FN: &fn(&[u8]) -> &[u8] = &(id_u8_slice as fn(&[u8]) -> &[u8]);
29
30 // this should be the same as without elision
31 static STATIC_NON_ELIDED_fN: &for<'a> fn(&'a [u8]) -> &'a [u8] =
32 &(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
33 const CONST_NON_ELIDED_fN: &for<'a> fn(&'a [u8]) -> &'a [u8] =
34 &(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
35
36 // another function that elides, each to a different unbound lifetime
37 fn multi_args(a: &u8, b: &u8, c: &u8) {}
38
39 static STATIC_MULTI_FN: &fn(&u8, &u8, &u8) = &(multi_args as fn(&u8, &u8, &u8));
40 const CONST_MULTI_FN: &fn(&u8, &u8, &u8) = &(multi_args as fn(&u8, &u8, &u8));
41
42 struct Foo<'a> {
43 bools: &'a [bool],
44 }
45
46 static STATIC_FOO: Foo = Foo { bools: &[true, false] };
47 const CONST_FOO: Foo = Foo { bools: &[true, false] };
48
49 type Bar<'a> = Foo<'a>;
50
51 static STATIC_BAR: Bar = Bar { bools: &[true, false] };
52 const CONST_BAR: Bar = Bar { bools: &[true, false] };
53
54 type Baz<'a> = fn(&'a [u8]) -> Option<u8>;
55
56 fn baz(e: &[u8]) -> Option<u8> {
57 e.first().map(|x| *x)
58 }
59
60 static STATIC_BAZ: &Baz = &(baz as Baz);
61 const CONST_BAZ: &Baz = &(baz as Baz);
62
63 static BYTES: &[u8] = &[1, 2, 3];
64
65 fn main() {
66 // make sure that the lifetime is actually elided (and not defaulted)
67 let x = &[1u8, 2, 3];
68 STATIC_SIMPLE_FN(x);
69 CONST_SIMPLE_FN(x);
70
71 STATIC_BAZ(BYTES); // neees static lifetime
72 CONST_BAZ(BYTES);
73
74 // make sure this works with different lifetimes
75 let a = &1;
76 {
77 let b = &2;
78 let c = &3;
79 CONST_MULTI_FN(a, b, c);
80 }
81 }