-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Suggest using precise capturing for hidden type that captures region #127619
Merged
bors
merged 2 commits into
rust-lang:master
from
compiler-errors:precise-capturing-better-sugg
Jul 13, 2024
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,10 @@ LL | fn lifetime_in_hidden<'a>(x: &'a ()) -> impl Sized + use<> { x } | |
| | opaque type defined here | ||
| hidden type `&'a ()` captures the lifetime `'a` as defined here | ||
| | ||
help: to declare that `impl Sized` captures `'a`, you can add an explicit `'a` lifetime bound | ||
help: add `'a` to the `use<...>` bound to explicitly capture it | ||
| | ||
LL | fn lifetime_in_hidden<'a>(x: &'a ()) -> impl Sized + use<> + 'a { x } | ||
| ++++ | ||
LL | fn lifetime_in_hidden<'a>(x: &'a ()) -> impl Sized + use<'a> { x } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ yay |
||
| ++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
|
30 changes: 30 additions & 0 deletions
30
tests/ui/impl-trait/precise-capturing/hidden-type-suggestion.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,30 @@ | ||
#![feature(precise_capturing)] | ||
|
||
fn lifetime<'a, 'b>(x: &'a ()) -> impl Sized + use<'b> { | ||
//~^ HELP add `'a` to the `use<...>` bound | ||
x | ||
//~^ ERROR hidden type for | ||
} | ||
|
||
fn param<'a, T>(x: &'a ()) -> impl Sized + use<T> { | ||
//~^ HELP add `'a` to the `use<...>` bound | ||
x | ||
//~^ ERROR hidden type for | ||
} | ||
|
||
fn empty<'a>(x: &'a ()) -> impl Sized + use<> { | ||
//~^ HELP add `'a` to the `use<...>` bound | ||
x | ||
//~^ ERROR hidden type for | ||
} | ||
|
||
trait Captures<'a> {} | ||
impl<T> Captures<'_> for T {} | ||
|
||
fn missing<'a, 'captured, 'not_captured, Captured>(x: &'a ()) -> impl Captures<'captured> { | ||
//~^ HELP add a `use<...>` bound | ||
x | ||
//~^ ERROR hidden type for | ||
} | ||
|
||
fn main() {} |
67 changes: 67 additions & 0 deletions
67
tests/ui/impl-trait/precise-capturing/hidden-type-suggestion.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,67 @@ | ||
error[E0700]: hidden type for `impl Sized` captures lifetime that does not appear in bounds | ||
--> $DIR/hidden-type-suggestion.rs:5:5 | ||
| | ||
LL | fn lifetime<'a, 'b>(x: &'a ()) -> impl Sized + use<'b> { | ||
| -- -------------------- opaque type defined here | ||
| | | ||
| hidden type `&'a ()` captures the lifetime `'a` as defined here | ||
LL | | ||
LL | x | ||
| ^ | ||
| | ||
help: add `'a` to the `use<...>` bound to explicitly capture it | ||
| | ||
LL | fn lifetime<'a, 'b>(x: &'a ()) -> impl Sized + use<'b, 'a> { | ||
| ++++ | ||
|
||
error[E0700]: hidden type for `impl Sized` captures lifetime that does not appear in bounds | ||
--> $DIR/hidden-type-suggestion.rs:11:5 | ||
| | ||
LL | fn param<'a, T>(x: &'a ()) -> impl Sized + use<T> { | ||
| -- ------------------- opaque type defined here | ||
| | | ||
| hidden type `&'a ()` captures the lifetime `'a` as defined here | ||
LL | | ||
LL | x | ||
| ^ | ||
| | ||
help: add `'a` to the `use<...>` bound to explicitly capture it | ||
| | ||
LL | fn param<'a, T>(x: &'a ()) -> impl Sized + use<'a, T> { | ||
| +++ | ||
|
||
error[E0700]: hidden type for `impl Sized` captures lifetime that does not appear in bounds | ||
--> $DIR/hidden-type-suggestion.rs:17:5 | ||
| | ||
LL | fn empty<'a>(x: &'a ()) -> impl Sized + use<> { | ||
| -- ------------------ opaque type defined here | ||
| | | ||
| hidden type `&'a ()` captures the lifetime `'a` as defined here | ||
LL | | ||
LL | x | ||
| ^ | ||
| | ||
help: add `'a` to the `use<...>` bound to explicitly capture it | ||
| | ||
LL | fn empty<'a>(x: &'a ()) -> impl Sized + use<'a> { | ||
| ++ | ||
|
||
error[E0700]: hidden type for `impl Captures<'captured>` captures lifetime that does not appear in bounds | ||
--> $DIR/hidden-type-suggestion.rs:26:5 | ||
| | ||
LL | fn missing<'a, 'captured, 'not_captured, Captured>(x: &'a ()) -> impl Captures<'captured> { | ||
| -- ------------------------ opaque type defined here | ||
| | | ||
| hidden type `&'a ()` captures the lifetime `'a` as defined here | ||
LL | | ||
LL | x | ||
| ^ | ||
| | ||
help: add a `use<...>` bound to explicitly capture `'a` | ||
| | ||
LL | fn missing<'a, 'captured, 'not_captured, Captured>(x: &'a ()) -> impl Captures<'captured> + use<'captured, 'a, Captured> { | ||
| ++++++++++++++++++++++++++++++ | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0700`. |
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.
Are there other cases where we suggest adding a feature if using nightly? If so, that might be better!
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.
Yeah, that could be done, though I think we plan on stabilizing precise capturing soon, so not totally certain about the value. Also, having things gated on "is this a nightly compiler" adds one more weird case that's impossible to test 😅