Skip to content

Commit e0eb1ba

Browse files
committed
fixed and extended tests, however...
...there is still one confusing thing – see the _BAZ functions, which appear to be elided in the `compile-fail` test and defaulted in the ´run-pass` test (if you uncomment line 73).
1 parent a6b9fea commit e0eb1ba

File tree

2 files changed

+51
-16
lines changed

2 files changed

+51
-16
lines changed

src/test/compile-fail/rfc1623.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,21 @@
1313
fn non_elidable<'a, 'b>(a: &'a u8, b: &'b u8) -> &'a u8 { a }
1414

1515
// the boundaries of elision
16-
static NON_ELIDABLE_FN : &fn(&u8, &u8) -> &u8 =
17-
//~^ERROR: missing lifetime specifier
16+
static NON_ELIDABLE_FN : &fn(&u8, &u8) -> &u8 =
17+
//^ERROR: missing lifetime specifier
1818
&(non_elidable as fn(&u8, &u8) -> &u8);
1919

20+
type Baz<'a> = fn(&'a [u8]) -> Option<u8>;
21+
22+
fn baz(e: &[u8]) -> Option<u8> { e.first().map(|x| *x) }
23+
24+
static STATIC_BAZ : &Baz<'static> = &(baz as Baz);
25+
const CONST_BAZ : &Baz<'static> = &(baz as Baz);
26+
2027
fn main() {
21-
// nothing to do here
28+
let y = [1u8, 2, 3];
29+
30+
//surprisingly this appears to work, so lifetime < `'static` is valid
31+
assert_eq!(Some(1), STATIC_BAZ(y));
32+
assert_eq!(Some(1), CONST_BAZ(y));
2233
}

src/test/run-pass/rfc1623.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,72 @@
1111
#![allow(dead_code)]
1212

1313
// very simple test for a 'static static with default lifetime
14-
static SOME_STATIC_STR : &str = "&'static str";
15-
const SOME_CONST_STR : &str = "&'static str";
14+
static STATIC_STR : &str = "&'static str";
15+
const CONST_STR : &str = "&'static str";
1616

1717
// this should be the same as without default:
18-
static SOME_EXPLICIT_STATIC_STR : &'static str = "&'static str";
19-
const SOME_EXPLICIT_CONST_STR : &'static str = "&'static str";
18+
static EXPLICIT_STATIC_STR : &'static str = "&'static str";
19+
const EXPLICIT_CONST_STR : &'static str = "&'static str";
2020

2121
// a function that elides to an unbound lifetime for both in- and output
2222
fn id_u8_slice(arg: &[u8]) -> &[u8] { arg }
2323

2424
// one with a function, argument elided
25-
static SOME_STATIC_SIMPLE_FN : &fn(&[u8]) -> &[u8] =
25+
static STATIC_SIMPLE_FN : &fn(&[u8]) -> &[u8] =
2626
&(id_u8_slice as fn(&[u8]) -> &[u8]);
27-
const SOME_CONST_SIMPLE_FN : &fn(&[u8]) -> &[u8] =
27+
const CONST_SIMPLE_FN : &fn(&[u8]) -> &[u8] =
2828
&(id_u8_slice as fn(&[u8]) -> &[u8]);
2929

3030
// this should be the same as without elision
31-
static SOME_STATIC_NON_ELIDED_fN : &for<'a> fn(&'a [u8]) -> &'a [u8] =
31+
static STATIC_NON_ELIDED_fN : &for<'a> fn(&'a [u8]) -> &'a [u8] =
3232
&(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
33-
const SOME_CONST_NON_ELIDED_fN : &for<'a> fn(&'a [u8]) -> &'a [u8] =
33+
const CONST_NON_ELIDED_fN : &for<'a> fn(&'a [u8]) -> &'a [u8] =
3434
&(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
3535

3636
// another function that elides, each to a different unbound lifetime
3737
fn multi_args(a: &u8, b: &u8, c: &u8) { }
3838

39-
static SOME_STATIC_MULTI_FN : &fn(&u8, &u8, &u8) =
39+
static STATIC_MULTI_FN : &fn(&u8, &u8, &u8) =
4040
&(multi_args as fn(&u8, &u8, &u8));
41-
const SOME_CONST_MULTI_FN : &fn(&u8, &u8, &u8) =
41+
const CONST_MULTI_FN : &fn(&u8, &u8, &u8) =
4242
&(multi_args as fn(&u8, &u8, &u8));
4343

44+
struct Foo<'a> {
45+
bools: &'a [bool]
46+
}
47+
48+
static STATIC_FOO : Foo = Foo { bools: &[true, false] };
49+
const CONST_FOO : Foo = Foo { bools: &[true, false] };
50+
51+
type Bar<'a> = Foo<'a>;
52+
53+
static STATIC_BAR : Bar = Bar { bools: &[true, false] };
54+
const CONST_BAR : Bar = Bar { bools: &[true, false] };
55+
56+
type Baz<'a> = fn(&'a [u8]) -> Option<u8>;
57+
58+
fn baz(e: &[u8]) -> Option<u8> { e.first().map(|x| *x) }
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];
4464

4565
fn main() {
4666
// make sure that the lifetime is actually elided (and not defaulted)
4767
let x = &[1u8, 2, 3];
48-
SOME_STATIC_SIMPLE_FN(x);
49-
SOME_CONST_SIMPLE_FN(x);
68+
STATIC_SIMPLE_FN(x);
69+
CONST_SIMPLE_FN(x);
70+
71+
let y = &[1u8, 2, 3];
72+
STATIC_BAZ(BYTES);
73+
//CONST_BAZ(y); // strangely enough, this fails
5074

5175
// make sure this works with different lifetimes
5276
let a = &1;
5377
{
5478
let b = &2;
5579
let c = &3;
56-
SOME_CONST_MULTI_FN(a, b, c);
80+
CONST_MULTI_FN(a, b, c);
5781
}
5882
}

0 commit comments

Comments
 (0)