Skip to content

Commit cfce351

Browse files
committed
first attempt at 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.
1 parent 3c5a0fa commit cfce351

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

src/librustc_typeck/collect.rs

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

src/test/compile-fail/rfc1623.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 = non_elidable;
17+
//~^ERROR: missing lifetime specifier
18+
19+
fn main() {
20+
// nothing to do here
21+
}

src/test/run-pass/rfc1623.rs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 SOME_STATIC_STR : &str = "&'static str";
15+
const SOME_CONST_STR : &str = "&'static str";
16+
17+
// 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";
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 SOME_STATIC_SIMPLE_FN : &fn(&[u8]) -> &[u8] = id_u8_slice;
26+
const SOME_CONST_SIMPLE_FN : &fn(&[u8]) -> &[u8] = id_u8_slice;
27+
28+
// this should be the same as without elision
29+
static SOME_STATIC_NON_ELIDED_fN : &fn<'a>(&'a [u8]) -> &'a [u8] = id_u8_slice;
30+
const SOME_CONST_NON_ELIDED_fN : &fn<'a>(&'a [u8]) -> &'a [u8] = id_u8_slice;
31+
32+
// another function that elides, each to a different unbound lifetime
33+
fn multi_args(a: &u8, b: &u8, c: &u8) { }
34+
35+
static SOME_STATIC_MULTI_FN : &fn(&u8, &u8, &u8) = multi_args;
36+
const SOME_CONST_MULTI_FN : &fn(&u8, &u8, &u8) = multi_args;
37+
38+
39+
fn main() {
40+
// make sure that the lifetime is actually elided (and not defaulted)
41+
let x = &[1u8, 2, 3];
42+
SOME_STATIC_SIMPLE_FN(x);
43+
SOME_CONST_SIMPLE_FN(x);
44+
45+
// make sure this works with different lifetimes
46+
let a = &1;
47+
{
48+
let b = &2;
49+
let c = &3;
50+
SOME_CONST_MULTI_FN(a, b, c);
51+
}
52+
}

0 commit comments

Comments
 (0)