Skip to content

Commit 5b465e3

Browse files
committed
Auto merge of #52761 - toidiu:ak-static-infer-fg, r=nikomatsakis
static infer feature gate #44493 r? @nikomatsakis
2 parents cc22428 + 130e3ab commit 5b465e3

File tree

9 files changed

+199
-7
lines changed

9 files changed

+199
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# `infer_static_outlives_requirements`
2+
3+
The tracking issue for this feature is: [#44493]
4+
5+
[#44493]: https://github.com/rust-lang/rust/issues/44493
6+
7+
------------------------
8+
The `infer_static_outlives_requirements` feature indicates that certain
9+
`'static` outlives requirements can be infered by the compiler rather than
10+
stating them explicitly.
11+
12+
Note: It is an accompanying feature to `infer_outlives_requirements`,
13+
which must be enabled to infer outlives requirements.
14+
15+
For example, currently generic struct definitions that contain
16+
references, require where-clauses of the form T: 'static. By using
17+
this feature the outlives predicates will be infered, although
18+
they may still be written explicitly.
19+
20+
```rust,ignore (pseudo-Rust)
21+
struct Foo<U> where U: 'static { // <-- currently required
22+
bar: Bar<U>
23+
}
24+
struct Bar<T: 'static> {
25+
x: T,
26+
}
27+
```
28+
29+
30+
## Examples:
31+
32+
```rust,ignore (pseudo-Rust)
33+
#![feature(infer_outlives_requirements)]
34+
#![feature(infer_static_outlives_requirements)]
35+
36+
#[rustc_outlives]
37+
// Implicitly infer U: 'static
38+
struct Foo<U> {
39+
bar: Bar<U>
40+
}
41+
struct Bar<T: 'static> {
42+
x: T,
43+
}
44+
```
45+

src/librustc_typeck/outlives/utils.rs

+24-7
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

+9
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",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
// Needs an explicit where clause stating outlives condition. (RFC 2093)
12+
13+
// Type T needs to outlive lifetime 'static.
14+
struct Foo<U> {
15+
bar: Bar<U> //~ ERROR 15:5: 15:16: the parameter type `U` may not live long enough [E0310]
16+
}
17+
struct Bar<T: 'static> {
18+
x: T,
19+
}
20+
21+
22+
fn main() { }
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/feature-gate-infer_static_outlives_requirements.rs:15:5
3+
|
4+
LL | struct Foo<U> {
5+
| - help: consider adding an explicit lifetime bound `U: 'static`...
6+
LL | bar: Bar<U> //~ ERROR 15:5: 15: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/feature-gate-infer_static_outlives_requirements.rs:15:5
11+
|
12+
LL | bar: Bar<U> //~ ERROR 15:5: 15: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`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
/*
16+
* We don't infer `T: 'static` outlives relationships by default.
17+
* Instead an additional feature gate `infer_static_outlives_requirements`
18+
* is required.
19+
*/
20+
21+
struct Foo<U> {
22+
bar: Bar<U> //~ ERROR 22:5: 22:16: the parameter type `U` may not live long enough [E0310]
23+
}
24+
struct Bar<T: 'static> {
25+
x: T,
26+
}
27+
28+
fn main() {}
29+
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:22:5
3+
|
4+
LL | struct Foo<U> {
5+
| - help: consider adding an explicit lifetime bound `U: 'static`...
6+
LL | bar: Bar<U> //~ ERROR 22:5: 22: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:22:5
11+
|
12+
LL | bar: Bar<U> //~ ERROR 22:5: 22: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`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
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)