Skip to content

Commit

Permalink
Rollup merge of #137059 - xizheyin:issue-136827, r=davidtwco
Browse files Browse the repository at this point in the history
fix: Alloc new errorcode E0803 for E0495

As discussion in #136827, I alloc a new errorcode.
  • Loading branch information
matthiaskrgr authored Feb 19, 2025
2 parents 38fba8c + d22554a commit 960b122
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 18 deletions.
46 changes: 46 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0803.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
A trait implementation returns a reference without an
explicit lifetime linking it to `self`.
It commonly arises in generic trait implementations
requiring explicit lifetime bounds.

Erroneous code example:

```compile_fail,E0803
trait DataAccess<T> {
fn get_ref(&self) -> T;
}
struct Container<'a> {
value: &'a f64,
}
// Attempting to implement reference return
impl<'a> DataAccess<&f64> for Container<'a> {
fn get_ref(&self) -> &f64 { // Error: Lifetime mismatch
self.value
}
}
```

The trait method returns &f64 requiring an independent lifetime
The struct Container<'a> carries lifetime parameter 'a
The compiler cannot verify if the returned reference satisfies 'a constraints
Solution
Explicitly bind lifetimes to clarify constraints:
```
// Modified trait with explicit lifetime binding
trait DataAccess<'a, T> {
fn get_ref(&'a self) -> T;
}
struct Container<'a> {
value: &'a f64,
}
// Correct implementation (bound lifetimes)
impl<'a> DataAccess<'a, &'a f64> for Container<'a> {
fn get_ref(&'a self) -> &'a f64 {
self.value
}
}
```
1 change: 1 addition & 0 deletions compiler/rustc_error_codes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ E0799: 0799,
E0800: 0800,
E0801: 0801,
E0802: 0802,
E0803: 0803,
);
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::iter;

use rustc_data_structures::fx::FxIndexSet;
use rustc_errors::{
Applicability, Diag, E0309, E0310, E0311, E0495, Subdiagnostic, struct_span_code_err,
Applicability, Diag, E0309, E0310, E0311, E0803, Subdiagnostic, struct_span_code_err,
};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId};
Expand Down Expand Up @@ -1032,7 +1032,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
struct_span_code_err!(
self.dcx(),
var_origin.span(),
E0495,
E0803,
"cannot infer an appropriate lifetime{} due to conflicting requirements",
var_description
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter 's in generic type due to conflicting requirements
error[E0803]: cannot infer an appropriate lifetime for lifetime parameter 's in generic type due to conflicting requirements
--> $DIR/impl-implied-bounds-compatibility-unnormalized.rs:11:5
|
LL | fn get<'s>(s: &'s str, _: <&'static &'s () as Project>::Ty) -> &'static str {
Expand All @@ -25,4 +25,4 @@ LL | fn get<'s>(s: &'s str, _: <&'static &'s () as Project>::Ty) -> &'static

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0495`.
For more information about this error, try `rustc --explain E0803`.
4 changes: 2 additions & 2 deletions tests/ui/issues/issue-20831-debruijn.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
error[E0803]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> $DIR/issue-20831-debruijn.rs:28:33
|
LL | fn subscribe(&mut self, t : Box<dyn Subscriber<Input=<Self as Publisher>::Output> + 'a>) {
Expand Down Expand Up @@ -48,4 +48,4 @@ LL | fn subscribe(&mut self, t : Box<dyn Subscriber<Input=<Self as Publisher

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0495`.
For more information about this error, try `rustc --explain E0803`.
4 changes: 2 additions & 2 deletions tests/ui/nll/normalization-bounds-error.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'d` due to conflicting requirements
error[E0803]: cannot infer an appropriate lifetime for lifetime parameter `'d` due to conflicting requirements
--> $DIR/normalization-bounds-error.rs:12:31
|
LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
Expand Down Expand Up @@ -36,4 +36,4 @@ LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0495`.
For more information about this error, try `rustc --explain E0803`.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
error[E0803]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> $DIR/regions-normalize-in-where-clause-list.rs:24:4
|
LL | fn bar<'a, 'b>()
Expand All @@ -24,4 +24,4 @@ LL | fn bar<'a, 'b>()

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0495`.
For more information about this error, try `rustc --explain E0803`.
4 changes: 2 additions & 2 deletions tests/ui/regions/resolve-re-error-ice.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter '_ in generic type due to conflicting requirements
error[E0803]: cannot infer an appropriate lifetime for lifetime parameter '_ in generic type due to conflicting requirements
--> $DIR/resolve-re-error-ice.rs:12:5
|
LL | fn key_set(&self) -> Subject<'a, Keys<K, V>, (), R> {
Expand Down Expand Up @@ -34,4 +34,4 @@ LL | struct Subject<'a, T, V, R>(PhantomData<(&'a T, V, R)>);

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0495`.
For more information about this error, try `rustc --explain E0803`.
4 changes: 2 additions & 2 deletions tests/ui/static/static-lifetime.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LL | impl<'a, A: Clone> Arbitrary for ::std::borrow::Cow<'a, A> {}
| ^^
= note: but lifetime parameter must outlive the static lifetime

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
error[E0803]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> $DIR/static-lifetime.rs:3:34
|
LL | impl<'a, A: Clone> Arbitrary for ::std::borrow::Cow<'a, A> {}
Expand All @@ -38,5 +38,5 @@ LL | impl<'a, A: Clone> Arbitrary for ::std::borrow::Cow<'a, A> {}

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0478, E0495.
Some errors have detailed explanations: E0478, E0803.
For more information about an error, try `rustc --explain E0478`.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements
error[E0803]: cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements
--> $DIR/impl-of-supertrait-has-wrong-lifetime-parameters.rs:24:28
|
LL | impl<'a,'b> T2<'a, 'b> for S<'a, 'b> {
Expand All @@ -24,4 +24,4 @@ LL | impl<'a,'b> T2<'a, 'b> for S<'a, 'b> {

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0495`.
For more information about this error, try `rustc --explain E0803`.
4 changes: 2 additions & 2 deletions tests/ui/type-alias-impl-trait/closure_wf_outlives.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ note: but lifetime parameter must outlive the lifetime `'b` as defined here
LL | type Opaque<'a, 'b> = impl Sized + 'a + 'b;
| ^^

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
error[E0803]: cannot infer an appropriate lifetime due to conflicting requirements
--> $DIR/closure_wf_outlives.rs:34:9
|
LL | || {}
Expand Down Expand Up @@ -63,5 +63,5 @@ LL | type Opaque<T: 'static> = impl Sized;

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0310, E0478, E0495.
Some errors have detailed explanations: E0310, E0478, E0803.
For more information about an error, try `rustc --explain E0310`.

0 comments on commit 960b122

Please sign in to comment.