From 944a9138fbb8dab6212b50f3dee00b28999818c7 Mon Sep 17 00:00:00 2001 From: "progressive.galib" Date: Fri, 14 Feb 2025 20:30:07 +0000 Subject: [PATCH] started work on https://github.com/rust-lang/rust/issues/136827 --- .../src/error_codes/E0495.md | 50 +++++++++---------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_error_codes/src/error_codes/E0495.md b/compiler/rustc_error_codes/src/error_codes/E0495.md index cd10e71931202..ffab2ac6208e9 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0495.md +++ b/compiler/rustc_error_codes/src/error_codes/E0495.md @@ -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 { + 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 } } ```