Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 23 additions & 27 deletions compiler/rustc_error_codes/src/error_codes/E0495.md
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`.

Check failure on line 1 in compiler/rustc_error_codes/src/error_codes/E0495.md

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

line longer than 80 chars
Copy link
Member

@Noratrieb Noratrieb Feb 16, 2025

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

fn report_inference_failure(&self, var_origin: RegionVariableOrigin) -> Diag<'_> {
)


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`:

Check failure on line 19 in compiler/rustc_error_codes/src/error_codes/E0495.md

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

line longer than 80 chars

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
}
}
```
Loading