Skip to content

Commit d22554a

Browse files
committed
fix: Alloc new errorcode E0803 for E0495
Signed-off-by: xizheyin <[email protected]>
1 parent d88ffcd commit d22554a

File tree

11 files changed

+65
-18
lines changed

11 files changed

+65
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
A trait implementation returns a reference without an
2+
explicit lifetime linking it to `self`.
3+
It commonly arises in generic trait implementations
4+
requiring explicit lifetime bounds.
5+
6+
Erroneous code example:
7+
8+
```compile_fail,E0803
9+
trait DataAccess<T> {
10+
fn get_ref(&self) -> T;
11+
}
12+
13+
struct Container<'a> {
14+
value: &'a f64,
15+
}
16+
17+
// Attempting to implement reference return
18+
impl<'a> DataAccess<&f64> for Container<'a> {
19+
fn get_ref(&self) -> &f64 { // Error: Lifetime mismatch
20+
self.value
21+
}
22+
}
23+
```
24+
25+
The trait method returns &f64 requiring an independent lifetime
26+
The struct Container<'a> carries lifetime parameter 'a
27+
The compiler cannot verify if the returned reference satisfies 'a constraints
28+
Solution
29+
Explicitly bind lifetimes to clarify constraints:
30+
```
31+
// Modified trait with explicit lifetime binding
32+
trait DataAccess<'a, T> {
33+
fn get_ref(&'a self) -> T;
34+
}
35+
36+
struct Container<'a> {
37+
value: &'a f64,
38+
}
39+
40+
// Correct implementation (bound lifetimes)
41+
impl<'a> DataAccess<'a, &'a f64> for Container<'a> {
42+
fn get_ref(&'a self) -> &'a f64 {
43+
self.value
44+
}
45+
}
46+
```

compiler/rustc_error_codes/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ E0799: 0799,
546546
E0800: 0800,
547547
E0801: 0801,
548548
E0802: 0802,
549+
E0803: 0803,
549550
);
550551
)
551552
}

compiler/rustc_trait_selection/src/error_reporting/infer/region.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::iter;
22

33
use rustc_data_structures::fx::FxIndexSet;
44
use rustc_errors::{
5-
Applicability, Diag, E0309, E0310, E0311, E0495, Subdiagnostic, struct_span_code_err,
5+
Applicability, Diag, E0309, E0310, E0311, E0803, Subdiagnostic, struct_span_code_err,
66
};
77
use rustc_hir::def::DefKind;
88
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -1032,7 +1032,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
10321032
struct_span_code_err!(
10331033
self.dcx(),
10341034
var_origin.span(),
1035-
E0495,
1035+
E0803,
10361036
"cannot infer an appropriate lifetime{} due to conflicting requirements",
10371037
var_description
10381038
)

tests/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter 's in generic type due to conflicting requirements
1+
error[E0803]: cannot infer an appropriate lifetime for lifetime parameter 's in generic type due to conflicting requirements
22
--> $DIR/impl-implied-bounds-compatibility-unnormalized.rs:11:5
33
|
44
LL | fn get<'s>(s: &'s str, _: <&'static &'s () as Project>::Ty) -> &'static str {
@@ -25,4 +25,4 @@ LL | fn get<'s>(s: &'s str, _: <&'static &'s () as Project>::Ty) -> &'static
2525

2626
error: aborting due to 1 previous error
2727

28-
For more information about this error, try `rustc --explain E0495`.
28+
For more information about this error, try `rustc --explain E0803`.

tests/ui/issues/issue-20831-debruijn.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
1+
error[E0803]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
22
--> $DIR/issue-20831-debruijn.rs:28:33
33
|
44
LL | fn subscribe(&mut self, t : Box<dyn Subscriber<Input=<Self as Publisher>::Output> + 'a>) {
@@ -48,4 +48,4 @@ LL | fn subscribe(&mut self, t : Box<dyn Subscriber<Input=<Self as Publisher
4848

4949
error: aborting due to 3 previous errors
5050

51-
For more information about this error, try `rustc --explain E0495`.
51+
For more information about this error, try `rustc --explain E0803`.

tests/ui/nll/normalization-bounds-error.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'d` due to conflicting requirements
1+
error[E0803]: cannot infer an appropriate lifetime for lifetime parameter `'d` due to conflicting requirements
22
--> $DIR/normalization-bounds-error.rs:12:31
33
|
44
LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
@@ -36,4 +36,4 @@ LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
3636

3737
error: aborting due to 2 previous errors
3838

39-
For more information about this error, try `rustc --explain E0495`.
39+
For more information about this error, try `rustc --explain E0803`.

tests/ui/regions/regions-normalize-in-where-clause-list.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
1+
error[E0803]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
22
--> $DIR/regions-normalize-in-where-clause-list.rs:24:4
33
|
44
LL | fn bar<'a, 'b>()
@@ -24,4 +24,4 @@ LL | fn bar<'a, 'b>()
2424

2525
error: aborting due to 1 previous error
2626

27-
For more information about this error, try `rustc --explain E0495`.
27+
For more information about this error, try `rustc --explain E0803`.

tests/ui/regions/resolve-re-error-ice.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter '_ in generic type due to conflicting requirements
1+
error[E0803]: cannot infer an appropriate lifetime for lifetime parameter '_ in generic type due to conflicting requirements
22
--> $DIR/resolve-re-error-ice.rs:12:5
33
|
44
LL | fn key_set(&self) -> Subject<'a, Keys<K, V>, (), R> {
@@ -34,4 +34,4 @@ LL | struct Subject<'a, T, V, R>(PhantomData<(&'a T, V, R)>);
3434

3535
error: aborting due to 1 previous error
3636

37-
For more information about this error, try `rustc --explain E0495`.
37+
For more information about this error, try `rustc --explain E0803`.

tests/ui/static/static-lifetime.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | impl<'a, A: Clone> Arbitrary for ::std::borrow::Cow<'a, A> {}
1111
| ^^
1212
= note: but lifetime parameter must outlive the static lifetime
1313

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

3939
error: aborting due to 2 previous errors
4040

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

tests/ui/traits/impl-of-supertrait-has-wrong-lifetime-parameters.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements
1+
error[E0803]: cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements
22
--> $DIR/impl-of-supertrait-has-wrong-lifetime-parameters.rs:24:28
33
|
44
LL | impl<'a,'b> T2<'a, 'b> for S<'a, 'b> {
@@ -24,4 +24,4 @@ LL | impl<'a,'b> T2<'a, 'b> for S<'a, 'b> {
2424

2525
error: aborting due to 1 previous error
2626

27-
For more information about this error, try `rustc --explain E0495`.
27+
For more information about this error, try `rustc --explain E0803`.

tests/ui/type-alias-impl-trait/closure_wf_outlives.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ note: but lifetime parameter must outlive the lifetime `'b` as defined here
1515
LL | type Opaque<'a, 'b> = impl Sized + 'a + 'b;
1616
| ^^
1717

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

6464
error: aborting due to 3 previous errors
6565

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

0 commit comments

Comments
 (0)