-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
static infer feature gate #52761
Merged
Merged
static infer feature gate #52761
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
44df365
feature gate for inferring 'static lifetimes
toidiu 429ae93
some comment about not infering static lifetimes test
toidiu 0fd7fee
fix test and add feature gate test
toidiu 3bc6bec
fix tidy
toidiu 130e3ab
add section in book
toidiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/doc/unstable-book/src/language-features/infer-static-outlives-requirements.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# `infer_static_outlives_requirements` | ||
|
||
The tracking issue for this feature is: [#44493] | ||
|
||
[#44493]: /~https://github.com/rust-lang/rust/issues/44493 | ||
|
||
------------------------ | ||
The `infer_static_outlives_requirements` feature indicates that certain | ||
`'static` outlives requirements can be infered by the compiler rather than | ||
stating them explicitly. | ||
|
||
Note: It is an accompanying feature to `infer_outlives_requirements`, | ||
which must be enabled to infer outlives requirements. | ||
|
||
For example, currently generic struct definitions that contain | ||
references, require where-clauses of the form T: 'static. By using | ||
this feature the outlives predicates will be infered, although | ||
they may still be written explicitly. | ||
|
||
```rust,ignore (pseudo-Rust) | ||
struct Foo<U> where U: 'static { // <-- currently required | ||
bar: Bar<U> | ||
} | ||
struct Bar<T: 'static> { | ||
x: T, | ||
} | ||
``` | ||
|
||
|
||
## Examples: | ||
|
||
```rust,ignore (pseudo-Rust) | ||
#![feature(infer_outlives_requirements)] | ||
#![feature(infer_static_outlives_requirements)] | ||
|
||
#[rustc_outlives] | ||
// Implicitly infer U: 'static | ||
struct Foo<U> { | ||
bar: Bar<U> | ||
} | ||
struct Bar<T: 'static> { | ||
x: T, | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/test/ui/feature-gate-infer_static_outlives_requirements.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// Needs an explicit where clause stating outlives condition. (RFC 2093) | ||
|
||
// Type T needs to outlive lifetime 'static. | ||
struct Foo<U> { | ||
bar: Bar<U> //~ ERROR 15:5: 15:16: the parameter type `U` may not live long enough [E0310] | ||
} | ||
struct Bar<T: 'static> { | ||
x: T, | ||
} | ||
|
||
|
||
fn main() { } |
17 changes: 17 additions & 0 deletions
17
src/test/ui/feature-gate-infer_static_outlives_requirements.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error[E0310]: the parameter type `U` may not live long enough | ||
--> $DIR/feature-gate-infer_static_outlives_requirements.rs:15:5 | ||
| | ||
LL | struct Foo<U> { | ||
| - help: consider adding an explicit lifetime bound `U: 'static`... | ||
LL | bar: Bar<U> //~ ERROR 15:5: 15:16: the parameter type `U` may not live long enough [E0310] | ||
| ^^^^^^^^^^^ | ||
| | ||
note: ...so that the type `U` will meet its required lifetime bounds | ||
--> $DIR/feature-gate-infer_static_outlives_requirements.rs:15:5 | ||
| | ||
LL | bar: Bar<U> //~ ERROR 15:5: 15:16: the parameter type `U` may not live long enough [E0310] | ||
| ^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0310`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// ignore-tidy-linelength | ||
|
||
#![feature(infer_outlives_requirements)] | ||
|
||
/* | ||
* We don't infer `T: 'static` outlives relationships by default. | ||
* Instead an additional feature gate `infer_static_outlives_requirements` | ||
* is required. | ||
*/ | ||
|
||
struct Foo<U> { | ||
bar: Bar<U> //~ ERROR 22:5: 22:16: the parameter type `U` may not live long enough [E0310] | ||
} | ||
struct Bar<T: 'static> { | ||
x: T, | ||
} | ||
|
||
fn main() {} | ||
|
17 changes: 17 additions & 0 deletions
17
src/test/ui/rfc-2093-infer-outlives/dont-infer-static.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error[E0310]: the parameter type `U` may not live long enough | ||
--> $DIR/dont-infer-static.rs:22:5 | ||
| | ||
LL | struct Foo<U> { | ||
| - help: consider adding an explicit lifetime bound `U: 'static`... | ||
LL | bar: Bar<U> //~ ERROR 22:5: 22:16: the parameter type `U` may not live long enough [E0310] | ||
| ^^^^^^^^^^^ | ||
| | ||
note: ...so that the type `U` will meet its required lifetime bounds | ||
--> $DIR/dont-infer-static.rs:22:5 | ||
| | ||
LL | bar: Bar<U> //~ ERROR 22:5: 22:16: the parameter type `U` may not live long enough [E0310] | ||
| ^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0310`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(rustc_attrs)] | ||
#![feature(infer_outlives_requirements)] | ||
#![feature(infer_static_outlives_requirements)] | ||
|
||
#[rustc_outlives] | ||
struct Foo<U> { //~ ERROR 16:1: 18:2: rustc_outlives | ||
bar: Bar<U> | ||
} | ||
struct Bar<T: 'static> { | ||
x: T, | ||
} | ||
|
||
fn main() {} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error: rustc_outlives | ||
--> $DIR/infer-static.rs:16:1 | ||
| | ||
LL | / struct Foo<U> { //~ ERROR 16:1: 18:2: rustc_outlives | ||
LL | | bar: Bar<U> | ||
LL | | } | ||
| |_^ | ||
| | ||
= note: U : 'static | ||
|
||
error: aborting due to previous error | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use a different comment than the previous entry, to distinguish it.
Infer 'static outlives requirements
, perhaps?