Skip to content

Commit 44df365

Browse files
committed
feature gate for inferring 'static lifetimes
1 parent 45b48b9 commit 44df365

File tree

6 files changed

+110
-7
lines changed

6 files changed

+110
-7
lines changed

src/librustc_typeck/outlives/utils.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn insert_outlives_predicate<'tcx>(
2727
) {
2828
// If the `'a` region is bound within the field type itself, we
2929
// don't want to propagate this constraint to the header.
30-
if !is_free_region(outlived_region) {
30+
if !is_free_region(tcx, outlived_region) {
3131
return;
3232
}
3333

@@ -120,27 +120,44 @@ pub fn insert_outlives_predicate<'tcx>(
120120
}
121121

122122
UnpackedKind::Lifetime(r) => {
123-
if !is_free_region(r) {
123+
if !is_free_region(tcx, r) {
124124
return;
125125
}
126126
required_predicates.insert(ty::OutlivesPredicate(kind, outlived_region));
127127
}
128128
}
129129
}
130130

131-
fn is_free_region(region: Region<'_>) -> bool {
131+
fn is_free_region<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>, region: Region<'_>) -> bool {
132132
// First, screen for regions that might appear in a type header.
133133
match region {
134-
// *These* correspond to `T: 'a` relationships where `'a` is
135-
// either declared on the type or `'static`:
134+
// These correspond to `T: 'a` relationships:
136135
//
137136
// struct Foo<'a, T> {
138137
// field: &'a T, // this would generate a ReEarlyBound referencing `'a`
139-
// field2: &'static T, // this would generate a ReStatic
140138
// }
141139
//
142140
// We care about these, so fall through.
143-
RegionKind::ReStatic | RegionKind::ReEarlyBound(_) => true,
141+
RegionKind::ReEarlyBound(_) => true,
142+
143+
// These correspond to `T: 'static` relationships which can be
144+
// rather surprising. We are therefore putting this behind a
145+
// feature flag:
146+
//
147+
// struct Foo<'a, T> {
148+
// field: &'static T, // this would generate a ReStatic
149+
// }
150+
RegionKind::ReStatic => {
151+
if tcx
152+
.sess
153+
.features_untracked()
154+
.infer_static_outlives_requirements
155+
{
156+
true
157+
} else {
158+
false
159+
}
160+
}
144161

145162
// Late-bound regions can appear in `fn` types:
146163
//

src/libsyntax/feature_gate.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ declare_features! (
396396
// Infer outlives requirements; RFC 2093
397397
(active, infer_outlives_requirements, "1.26.0", Some(44493), None),
398398

399+
// Infer outlives requirements; RFC 2093
400+
(active, infer_static_outlives_requirements, "1.26.0", Some(44493), None),
401+
399402
// Multiple patterns with `|` in `if let` and `while let`
400403
(active, if_while_or_patterns, "1.26.0", Some(48215), None),
401404

@@ -1057,6 +1060,12 @@ pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeG
10571060
"infer outlives requirements is an experimental feature",
10581061
cfg_fn!(infer_outlives_requirements))),
10591062

1063+
// RFC #2093
1064+
("infer_static_outlives_requirements", Normal, Gated(Stability::Unstable,
1065+
"infer_static_outlives_requirements",
1066+
"infer 'static lifetime requirements",
1067+
cfg_fn!(infer_static_outlives_requirements))),
1068+
10601069
// RFC 2070
10611070
("panic_implementation", Normal, Gated(Stability::Unstable,
10621071
"panic_implementation",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
// ignore-tidy-linelength
12+
13+
#![feature(infer_outlives_requirements)]
14+
15+
struct Foo<U> {
16+
bar: Bar<U> //~ ERROR 16:5: 16:16: the parameter type `U` may not live long enough [E0310]
17+
}
18+
struct Bar<T: 'static> {
19+
x: T,
20+
}
21+
22+
fn main() {}
23+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0310]: the parameter type `U` may not live long enough
2+
--> $DIR/dont-infer-static.rs:16:5
3+
|
4+
LL | struct Foo<U> {
5+
| - help: consider adding an explicit lifetime bound `U: 'static`...
6+
LL | bar: Bar<U> //~ ERROR 16:5: 16:16: the parameter type `U` may not live long enough [E0310]
7+
| ^^^^^^^^^^^
8+
|
9+
note: ...so that the type `U` will meet its required lifetime bounds
10+
--> $DIR/dont-infer-static.rs:16:5
11+
|
12+
LL | bar: Bar<U> //~ ERROR 16:5: 16:16: the parameter type `U` may not live long enough [E0310]
13+
| ^^^^^^^^^^^
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0310`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
#![feature(rustc_attrs)]
12+
#![feature(infer_outlives_requirements)]
13+
#![feature(infer_static_outlives_requirements)]
14+
15+
#[rustc_outlives]
16+
struct Foo<U> { //~ ERROR 16:1: 18:2: rustc_outlives
17+
bar: Bar<U>
18+
}
19+
struct Bar<T: 'static> {
20+
x: T,
21+
}
22+
23+
fn main() {}
24+
25+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: rustc_outlives
2+
--> $DIR/infer-static.rs:16:1
3+
|
4+
LL | / struct Foo<U> { //~ ERROR 16:1: 18:2: rustc_outlives
5+
LL | | bar: Bar<U>
6+
LL | | }
7+
| |_^
8+
|
9+
= note: U : 'static
10+
11+
error: aborting due to previous error
12+

0 commit comments

Comments
 (0)