-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
As per issue #136827 submitting updates to E0495 documentation #137042
Open
progressive-galib
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
progressive-galib:E0495-documentation-fix-#136827
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+23
−27
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,36 @@ | ||
#### Note: this error code is no longer emitted by the compiler. | ||
A trait implementation returns a reference without an explicit lifetime linking it to `self`. | ||
|
||
A lifetime cannot be determined in the given situation. | ||
```compile_fail,E0495 | ||
trait Tr<X> { | ||
fn f(&self) -> X; | ||
} | ||
|
||
Erroneous code example: | ||
struct Wr<'b> { | ||
ri: &'b i32, | ||
f: f32, | ||
} | ||
|
||
```compile_fail | ||
fn transmute_lifetime<'a, 'b, T>(t: &'a (T,)) -> &'b T { | ||
match (&t,) { // error! | ||
((u,),) => u, | ||
impl<'b> Tr<&f32> for Wr<'b> { | ||
fn f(&self) -> &f32 { // error: missing lifetime linking &f32 to self | ||
&self.f | ||
} | ||
} | ||
|
||
let y = Box::new((42,)); | ||
let x = transmute_lifetime(&y); | ||
``` | ||
Specify an explicit lifetime in the trait to ensure that the reference returned is valid for at least as long as `self`: | ||
|
||
In this code, you have two ways to solve this issue: | ||
1. Enforce that `'a` lives at least as long as `'b`. | ||
2. Use the same lifetime requirement for both input and output values. | ||
|
||
So for the first solution, you can do it by replacing `'a` with `'a: 'b`: | ||
|
||
``` | ||
fn transmute_lifetime<'a: 'b, 'b, T>(t: &'a (T,)) -> &'b T { | ||
match (&t,) { // ok! | ||
((u,),) => u, | ||
} | ||
```rust | ||
trait Tr<'a, X> { | ||
fn f(&'a self) -> X; | ||
} | ||
``` | ||
|
||
In the second you can do it by simply removing `'b` so they both use `'a`: | ||
struct Wr<'b> { | ||
ri: &'b i32, | ||
f: f32, | ||
} | ||
|
||
``` | ||
fn transmute_lifetime<'a, T>(t: &'a (T,)) -> &'a T { | ||
match (&t,) { // ok! | ||
((u,),) => u, | ||
impl<'a, 'b: 'a> Tr<'a, &'a f32> for Wr<'b> { | ||
fn f(&'a self) -> &'a f32 { // ok! | ||
&self.f | ||
} | ||
} | ||
``` |
Oops, something went wrong.
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.
I don't think this is what this error stands for, I think this is a much more generic lifetime error from searching for the code in
./tests
.I would probably just keep the old documentation, it's not amazing but I don't think it's possible to write documentation with great examples for this error code, it just seems like this is a very generic error code emitted in all sorts of cases.
@compiler-errors maybe you know more about this error code and whether what I said is correct. (emitted here
rust/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs
Line 994 in c3fe9e7