Skip to content

Commit f508ddc

Browse files
authored
Auto merge of #36332 - llogiq:static_consts_feature, r=nikomatsakis
add static_in_const feature gate also updates tests and deletes the spurious .bk files I inadvertently added last time. r? @nikomatsakis
2 parents 1284081 + 76a2f9f commit f508ddc

File tree

10 files changed

+62
-182
lines changed

10 files changed

+62
-182
lines changed

src/doc/reference.md

+3
Original file line numberDiff line numberDiff line change
@@ -2441,6 +2441,9 @@ The currently implemented features of the reference compiler are:
24412441
into a Rust program. This capability, especially the signature for the
24422442
annotated function, is subject to change.
24432443

2444+
* `static_in_const` - Enables lifetime elision with a `'static` default for
2445+
`const` and `static` item declarations.
2446+
24442447
* `thread_local` - The usage of the `#[thread_local]` attribute is experimental
24452448
and should be seen as unstable. This attribute is used to
24462449
declare a `static` as being unique per-thread leveraging

src/librustc_typeck/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,7 @@ fn type_of_def_id<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
15671567
NodeItem(item) => {
15681568
match item.node {
15691569
ItemStatic(ref t, ..) | ItemConst(ref t, _) => {
1570-
ccx.icx(&()).to_ty(&ElidableRscope::new(ty::ReStatic), &t)
1570+
ccx.icx(&()).to_ty(&StaticRscope::new(&ccx.tcx), &t)
15711571
}
15721572
ItemFn(ref decl, unsafety, _, abi, ref generics, _) => {
15731573
let tofd = AstConv::ty_of_bare_fn(&ccx.icx(generics), unsafety, abi, &decl,

src/librustc_typeck/rscope.rs

+39
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,45 @@ impl RegionScope for ElidableRscope {
213213
}
214214
}
215215

216+
/// A scope that behaves as an ElidabeRscope with a `'static` default region
217+
/// that should also warn if the `static_in_const` feature is unset.
218+
#[derive(Copy, Clone)]
219+
pub struct StaticRscope<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
220+
tcx: &'a ty::TyCtxt<'a, 'gcx, 'tcx>,
221+
}
222+
223+
impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> StaticRscope<'a, 'gcx, 'tcx> {
224+
/// create a new StaticRscope from a reference to the `TyCtxt`
225+
pub fn new(tcx: &'a ty::TyCtxt<'a, 'gcx, 'tcx>) -> Self {
226+
StaticRscope { tcx: tcx }
227+
}
228+
}
229+
230+
impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> RegionScope for StaticRscope<'a, 'gcx, 'tcx> {
231+
fn anon_regions(&self,
232+
span: Span,
233+
count: usize)
234+
-> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
235+
if !self.tcx.sess.features.borrow().static_in_const {
236+
self.tcx
237+
.sess
238+
.struct_span_err(span,
239+
"this needs a `'static` lifetime or the \
240+
`static_in_const` feature, see #35897")
241+
.emit();
242+
}
243+
Ok(vec![ty::ReStatic; count])
244+
}
245+
246+
fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
247+
Some(self.base_object_lifetime_default(span))
248+
}
249+
250+
fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
251+
ty::ReStatic
252+
}
253+
}
254+
216255
/// A scope in which we generate anonymous, late-bound regions for
217256
/// omitted regions. This occurs in function signatures.
218257
pub struct BindingRscope {

src/libsyntax/feature_gate.rs

+3
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ declare_features! (
295295

296296
// Allows untagged unions `union U { ... }`
297297
(active, untagged_unions, "1.13.0", Some(32836)),
298+
299+
// elide `'static` lifetimes in `static`s and `const`s
300+
(active, static_in_const, "1.13.0", Some(35897)),
298301
);
299302

300303
declare_features! (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2015 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+
static FOO: &str = "this will work once static_in_const is stable";
12+
//~^ ERROR: this needs a `'static` lifetime or the `static_in_const` feature
13+
14+
fn main() {}

src/test/compile-fail/issue-24446.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ fn main() {
1212
static foo: Fn() -> u32 = || -> u32 {
1313
//~^ ERROR: mismatched types
1414
//~| ERROR: `std::ops::Fn() -> u32 + 'static: std::marker::Sized` is not satisfied
15-
1615
0
1716
};
1817
}

src/test/compile-fail/rfc1623.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
10-
10+
#![feature(static_in_const)]
1111
#![allow(dead_code)]
1212

1313
fn non_elidable<'a, 'b>(a: &'a u8, b: &'b u8) -> &'a u8 {

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

-98
This file was deleted.

src/test/run-pass/rfc1623.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(static_in_const)]
1112
#![allow(dead_code)]
1213

1314
// very simple test for a 'static static with default lifetime

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

-81
This file was deleted.

0 commit comments

Comments
 (0)