Skip to content

Commit 41d1cd7

Browse files
committed
add static_in_const feature gate
also updates tests and deletes the spurious .bk files I inadvertently added last time.
1 parent cf0cdc4 commit 41d1cd7

File tree

9 files changed

+48
-182
lines changed

9 files changed

+48
-182
lines changed

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+
Ok(vec![ty::ReStatic; count])
236+
}
237+
238+
fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
239+
Some(self.base_object_lifetime_default(span))
240+
}
241+
242+
fn base_object_lifetime_default(&self, span: Span) -> ty::Region {
243+
if !self.tcx.sess.features.borrow().static_in_const {
244+
self.tcx
245+
.sess
246+
.struct_span_warn(span,
247+
"This needs a `'static` lifetime or the \
248+
`static_in_const` feature, see #35897")
249+
.emit();
250+
}
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! (

src/test/compile-fail/const-unsized.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const CONST_0: Debug+Sync = *(&0 as &(Debug+Sync));
1515
//~| NOTE `std::fmt::Debug + Sync + 'static: std::marker::Sized` not satisfied
1616
//~| NOTE does not have a constant size known at compile-time
1717
//~| NOTE constant expressions must have a statically known size
18+
//~| WARNING This needs a `'static` lifetime or the `static_in_const` feature
1819

1920
const CONST_FOO: str = *"foo";
2021
//~^ ERROR `str: std::marker::Sized` is not satisfied
@@ -27,6 +28,7 @@ static STATIC_1: Debug+Sync = *(&1 as &(Debug+Sync));
2728
//~| NOTE `std::fmt::Debug + Sync + 'static: std::marker::Sized` not satisfied
2829
//~| NOTE does not have a constant size known at compile-time
2930
//~| NOTE constant expressions must have a statically known size
31+
//~| WARNING This needs a `'static` lifetime or the `static_in_const` feature
3032

3133
static STATIC_BAR: str = *"bar";
3234
//~^ ERROR `str: std::marker::Sized` is not satisfied

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ 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-
15+
//~| WARNING: This needs a `'static` lifetime or the `static_in_const` feature
1616
0
1717
};
1818
}

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)