Skip to content

Commit 022cb6d

Browse files
authored
Auto merge of #35915 - llogiq:rfc-1623, r=nikomatsakis
implementing RFC 1623. This fixes #35897. This is a work in progress. In particular, I want to add more tests, especially the compile-fail test is very bare-bones.
2 parents 8aeb15a + a87b4d8 commit 022cb6d

File tree

6 files changed

+362
-16
lines changed

6 files changed

+362
-16
lines changed

src/librustc_typeck/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,7 @@ fn type_of_def_id<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
15551555
NodeItem(item) => {
15561556
match item.node {
15571557
ItemStatic(ref t, _, _) | ItemConst(ref t, _) => {
1558-
ccx.icx(&()).to_ty(&ExplicitRscope, &t)
1558+
ccx.icx(&()).to_ty(&ElidableRscope::new(ty::ReStatic), &t)
15591559
}
15601560
ItemFn(ref decl, unsafety, _, abi, ref generics, _) => {
15611561
let tofd = AstConv::ty_of_bare_fn(&ccx.icx(generics), unsafety, abi, &decl,

src/test/compile-fail/regions-in-consts.rs

-15
This file was deleted.

src/test/compile-fail/rfc1623.rs

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
fn non_elidable<'a, 'b>(a: &'a u8, b: &'b u8) -> &'a u8 {
14+
a
15+
}
16+
17+
// the boundaries of elision
18+
static NON_ELIDABLE_FN: &fn(&u8, &u8) -> &u8 = &(non_elidable as fn(&u8, &u8) -> &u8);
19+
//~^ ERROR missing lifetime specifier [E0106]
20+
21+
struct SomeStruct<'x, 'y, 'z: 'x> {
22+
foo: &'x Foo<'z>,
23+
bar: &'x Bar<'z>,
24+
f: &'y for<'a, 'b: 'a> Fn(&'a Foo<'b>) -> &'a Bar<'b>,
25+
}
26+
27+
fn id<T>(t: T) -> T {
28+
t
29+
}
30+
31+
static SOME_STRUCT: &SomeStruct = SomeStruct {
32+
foo: &Foo { bools: &[false, true] },
33+
bar: &Bar { bools: &[true, true] },
34+
f: &id,
35+
};
36+
37+
// very simple test for a 'static static with default lifetime
38+
static STATIC_STR: &'static str = "&'static str";
39+
const CONST_STR: &'static str = "&'static str";
40+
41+
// this should be the same as without default:
42+
static EXPLICIT_STATIC_STR: &'static str = "&'static str";
43+
const EXPLICIT_CONST_STR: &'static str = "&'static str";
44+
45+
// a function that elides to an unbound lifetime for both in- and output
46+
fn id_u8_slice(arg: &[u8]) -> &[u8] {
47+
arg
48+
}
49+
50+
// one with a function, argument elided
51+
static STATIC_SIMPLE_FN: &'static fn(&[u8]) -> &[u8] = &(id_u8_slice as fn(&[u8]) -> &[u8]);
52+
const CONST_SIMPLE_FN: &'static fn(&[u8]) -> &[u8] = &(id_u8_slice as fn(&[u8]) -> &[u8]);
53+
54+
// this should be the same as without elision
55+
static STATIC_NON_ELIDED_fN: &'static for<'a> fn(&'a [u8]) -> &'a [u8] =
56+
&(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
57+
const CONST_NON_ELIDED_fN: &'static for<'a> fn(&'a [u8]) -> &'a [u8] =
58+
&(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
59+
60+
// another function that elides, each to a different unbound lifetime
61+
fn multi_args(a: &u8, b: &u8, c: &u8) {}
62+
63+
static STATIC_MULTI_FN: &'static fn(&u8, &u8, &u8) = &(multi_args as fn(&u8, &u8, &u8));
64+
const CONST_MULTI_FN: &'static fn(&u8, &u8, &u8) = &(multi_args as fn(&u8, &u8, &u8));
65+
66+
struct Foo<'a> {
67+
bools: &'a [bool],
68+
}
69+
70+
static STATIC_FOO: Foo<'static> = Foo { bools: &[true, false] };
71+
const CONST_FOO: Foo<'static> = Foo { bools: &[true, false] };
72+
73+
type Bar<'a> = Foo<'a>;
74+
75+
static STATIC_BAR: Bar<'static> = Bar { bools: &[true, false] };
76+
const CONST_BAR: Bar<'static> = Bar { bools: &[true, false] };
77+
78+
type Baz<'a> = fn(&'a [u8]) -> Option<u8>;
79+
80+
fn baz(e: &[u8]) -> Option<u8> {
81+
e.first().map(|x| *x)
82+
}
83+
84+
static STATIC_BAZ: &'static Baz<'static> = &(baz as Baz);
85+
const CONST_BAZ: &'static Baz<'static> = &(baz as Baz);
86+
87+
static BYTES: &'static [u8] = &[1, 2, 3];
88+
89+
fn main() {
90+
let x = &[1u8, 2, 3];
91+
let y = x;
92+
93+
// this works, so lifetime < `'static` is valid
94+
assert_eq!(Some(1), STATIC_BAZ(y));
95+
assert_eq!(Some(1), CONST_BAZ(y));
96+
97+
let y = &[1u8, 2, 3];
98+
99+
STATIC_BAZ(BYTES); // BYTES has static lifetime
100+
CONST_BAZ(y); // interestingly this does not get reported
101+
}

src/test/compile-fail/rfc1623.rs.bk

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
fn non_elidable<'a, 'b>(a: &'a u8, b: &'b u8) -> &'a u8 { a }
14+
15+
// the boundaries of elision
16+
static NON_ELIDABLE_FN : &fn(&u8, &u8) -> &u8 =
17+
//~^ ERROR: missing lifetime specifier
18+
&(non_elidable as fn(&u8, &u8) -> &u8);
19+
20+
struct SomeStruct<'x, 'y, 'z: 'x> {
21+
foo: &'x Foo<'z>,
22+
bar: &'x Bar<'z>,
23+
f: &'y for<'a, 'b: 'a> Fn(&'a Foo<'b>) -> &'a Bar<'b>,
24+
}
25+
26+
fn id<T>(t: T) -> T { t }
27+
28+
static SOME_STRUCT : &SomeStruct = SomeStruct {
29+
foo: &Foo { bools: &[false, true] },
30+
bar: &Bar { bools: &[true, true] },
31+
f: &id,
32+
};
33+
34+
// very simple test for a 'static static with default lifetime
35+
static STATIC_STR : &'static str = "&'static str";
36+
const CONST_STR : &'static str = "&'static str";
37+
38+
// this should be the same as without default:
39+
static EXPLICIT_STATIC_STR : &'static str = "&'static str";
40+
const EXPLICIT_CONST_STR : &'static str = "&'static str";
41+
42+
// a function that elides to an unbound lifetime for both in- and output
43+
fn id_u8_slice(arg: &[u8]) -> &[u8] { arg }
44+
45+
// one with a function, argument elided
46+
static STATIC_SIMPLE_FN : &'static fn(&[u8]) -> &[u8] =
47+
&(id_u8_slice as fn(&[u8]) -> &[u8]);
48+
const CONST_SIMPLE_FN : &'static fn(&[u8]) -> &[u8] =
49+
&(id_u8_slice as fn(&[u8]) -> &[u8]);
50+
51+
// this should be the same as without elision
52+
static STATIC_NON_ELIDED_fN : &'static for<'a> fn(&'a [u8]) -> &'a [u8] =
53+
&(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
54+
const CONST_NON_ELIDED_fN : &'static for<'a> fn(&'a [u8]) -> &'a [u8] =
55+
&(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
56+
57+
// another function that elides, each to a different unbound lifetime
58+
fn multi_args(a: &u8, b: &u8, c: &u8) { }
59+
60+
static STATIC_MULTI_FN : &'static fn(&u8, &u8, &u8) =
61+
&(multi_args as fn(&u8, &u8, &u8));
62+
const CONST_MULTI_FN : &'static fn(&u8, &u8, &u8) =
63+
&(multi_args as fn(&u8, &u8, &u8));
64+
65+
struct Foo<'a> {
66+
bools: &'a [bool]
67+
}
68+
69+
static STATIC_FOO : Foo<'static> = Foo { bools: &[true, false] };
70+
const CONST_FOO : Foo<'static> = Foo { bools: &[true, false] };
71+
72+
type Bar<'a> = Foo<'a>;
73+
74+
static STATIC_BAR : Bar<'static> = Bar { bools: &[true, false] };
75+
const CONST_BAR : Bar<'static> = Bar { bools: &[true, false] };
76+
77+
type Baz<'a> = fn(&'a [u8]) -> Option<u8>;
78+
79+
fn baz(e: &[u8]) -> Option<u8> { e.first().map(|x| *x) }
80+
81+
static STATIC_BAZ : &'static Baz<'static> = &(baz as Baz);
82+
const CONST_BAZ : &'static Baz<'static> = &(baz as Baz);
83+
84+
static BYTES : &'static [u8] = &[1, 2, 3];
85+
86+
fn main() {
87+
let x = &[1u8, 2, 3];
88+
let y = x;
89+
90+
//this works, so lifetime < `'static` is valid
91+
assert_eq!(Some(1), STATIC_BAZ(y));
92+
assert_eq!(Some(1), CONST_BAZ(y));
93+
94+
let y = &[1u8, 2, 3];
95+
//^~ ERROR: borrowed values does not live long enough
96+
STATIC_BAZ(BYTES); // BYTES has static lifetime
97+
CONST_BAZ(y); // This forces static lifetime, which y has not
98+
}

src/test/run-pass/rfc1623.rs

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}

src/test/run-pass/rfc1623.rs.bk

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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] { arg }
23+
24+
// one with a function, argument elided
25+
static STATIC_SIMPLE_FN : &fn(&[u8]) -> &[u8] =
26+
&(id_u8_slice as fn(&[u8]) -> &[u8]);
27+
const CONST_SIMPLE_FN : &fn(&[u8]) -> &[u8] =
28+
&(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) =
40+
&(multi_args as fn(&u8, &u8, &u8));
41+
const CONST_MULTI_FN : &fn(&u8, &u8, &u8) =
42+
&(multi_args as fn(&u8, &u8, &u8));
43+
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];
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+
}

0 commit comments

Comments
 (0)