-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
This code does not work:
use std::borrow::Borrow;
struct S;
trait T {}
impl T for S {}
impl Borrow<T> for S {
fn borrow(&self) -> &T {
self as &T
}
}
fn main() {
let s = S;
let _t: &T = s.borrow();
}
The error message I get is:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in generic type due to conflicting requirements
--> <anon>:8:5
|
8 | fn borrow(&self) -> &T {
| _____^ starting here...
9 | | self as &T
10 | | }
| |_____^ ...ending here
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 8:27...
--> <anon>:8:28
|
8 | fn borrow(&self) -> &T {
| ____________________________^ starting here...
9 | | self as &T
10 | | }
| |_____^ ...ending here
note: ...so that method type is compatible with trait (expected fn(&S) -> &T + 'static, found fn(&S) -> &T)
--> <anon>:8:5
|
8 | fn borrow(&self) -> &T {
| _____^ starting here...
9 | | self as &T
10 | | }
| |_____^ ...ending here
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that method type is compatible with trait (expected fn(&S) -> &T + 'static, found fn(&S) -> &T)
--> <anon>:8:5
|
8 | fn borrow(&self) -> &T {
| _____^ starting here...
9 | | self as &T
10 | | }
| |_____^ ...ending here
error: aborting due to previous error
This code does work:
use std::borrow::Borrow;
struct S;
trait T {}
impl T for S {}
impl<'a> Borrow<T + 'a> for S {
fn borrow(&self) -> &(T + 'a) {
self as &T
}
}
fn main() {
let s = S;
let _t: &T = s.borrow();
}
The only clue I got was this note:
note: ...so that method type is compatible with trait (expected fn(&S) -> &T + 'static, found fn(&S) -> &T)
which even then I wasn't clear if my change would make a difference or not. Is there some change we could make to the output that would make the solution clearer?
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.