-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Regression in helpful compiler error message when working with lifetimes #66406
Comments
Happens on beta too |
For what is worth, the correct code would be closer to: pub trait HaveRelationship<'a, To: 'a> {
fn get_relation(&'a self) -> To;
}
impl<'a> HaveRelationship<'a, &'a ProofReader> for Article {
fn get_relation(&'a self) -> &'a ProofReader {
&self.proof_reader
}
} The error is not incorrect, as there is a discrepancy between the |
This is caused by #65068. I believe this is now a duplicate of #41343. CC @nikomatsakis do you have any ideas of what the appropriate output should be for these cases? |
triage: P-medium, removing nomination |
I don't think it's necessarily possible to tell what the correct code is here, but I would guess that pub trait HaveRelationship<To> {
fn get_relation(&self) -> &To;
}
impl HaveRelationship<ProofReader> for Article {
fn get_relation(&self) -> &ProofReader {
&self.proof_reader
}
} is closer to the mark. i.e., move the references into the trait signature. |
Another good version: struct Article {
proof_reader: ProofReader,
}
struct ProofReader {
name: String,
}
pub trait HaveRelationship<To> {
fn get_relation(self) -> To;
}
impl<'a> HaveRelationship<&'a ProofReader> for &'a Article {
fn get_relation(self) -> &'a ProofReader {
&self.proof_reader
}
}
fn main() {
println!("Hello, world!");
} |
In both cases, the principle is the same: move the |
This version of the code also works, but it seems more complex than both of the above versions and it doesn't allow anything in particular that they don't, as far as I know. It still doesn't require the struct Article {
proof_reader: ProofReader,
}
struct ProofReader {
name: String,
}
pub trait HaveRelationship<'a, To> {
fn get_relation(&'a self) -> To;
}
impl<'a> HaveRelationship<'a, &'a ProofReader> for Article {
fn get_relation(&'a self) -> &'a ProofReader {
&self.proof_reader
}
}
fn main() {
println!("Hello, world!");
} |
I feel that the first of the offered suggestions makes the most sense for the general case, changing the signature consume |
I would rather not provide a structured suggestion for this case. What prose would you recommend here @nikomatsakis that would be suitable for a label on |
What do you think about this output?
I spent some time trying to actually verify if a type param has been replaced with a borrow, but it seems to be more involved than I think is worth for this case. |
@estebank sorry for lack of replies, i've enqueued the PR back into my task queue...my first thought when reading the comments above are that it seems pretty complex, but I don't have any constructive ideas how to improve yet :) |
@nikomatsakis fair. Keep in mind that the case above is exercising a more complex case than the more likely one:
It is also difficult because anything I remove I feel like I'm hiding useful information. |
@estebank I think this comment in particular
might be confusing to folks. Can we maybe just say "you might want to change this return type to make it match the |
Tweak impl signature mismatch errors involving `RegionKind::ReVar` lifetimes Fix rust-lang#66406, fix rust-lang#72106. ``` error: `impl` item signature doesn't match `trait` item signature --> $DIR/trait-param-without-lifetime-constraint.rs:14:5 | LL | fn get_relation(&self) -> To; | ----------------------------- expected `fn(&Article) -> &ProofReader` ... LL | fn get_relation(&self) -> &ProofReader { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&Article) -> &ProofReader` | = note: expected `fn(&Article) -> &ProofReader` found `fn(&Article) -> &ProofReader` help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait` --> $DIR/trait-param-without-lifetime-constraint.rs:10:31 | LL | fn get_relation(&self) -> To; | ^^ consider borrowing this type parameter in the trait ``` r? @nikomatsakis
Tweak impl signature mismatch errors involving `RegionKind::ReVar` lifetimes Fix rust-lang#66406, fix rust-lang#72106. ``` error: `impl` item signature doesn't match `trait` item signature --> $DIR/trait-param-without-lifetime-constraint.rs:14:5 | LL | fn get_relation(&self) -> To; | ----------------------------- expected `fn(&Article) -> &ProofReader` ... LL | fn get_relation(&self) -> &ProofReader { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&Article) -> &ProofReader` | = note: expected `fn(&Article) -> &ProofReader` found `fn(&Article) -> &ProofReader` help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait` --> $DIR/trait-param-without-lifetime-constraint.rs:10:31 | LL | fn get_relation(&self) -> To; | ^^ consider borrowing this type parameter in the trait ``` r? @nikomatsakis
Tweak impl signature mismatch errors involving `RegionKind::ReVar` lifetimes Fix rust-lang#66406, fix rust-lang#72106. ``` error: `impl` item signature doesn't match `trait` item signature --> $DIR/trait-param-without-lifetime-constraint.rs:14:5 | LL | fn get_relation(&self) -> To; | ----------------------------- expected `fn(&Article) -> &ProofReader` ... LL | fn get_relation(&self) -> &ProofReader { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&Article) -> &ProofReader` | = note: expected `fn(&Article) -> &ProofReader` found `fn(&Article) -> &ProofReader` help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait` --> $DIR/trait-param-without-lifetime-constraint.rs:10:31 | LL | fn get_relation(&self) -> To; | ^^ consider borrowing this type parameter in the trait ``` r? @nikomatsakis
Tweak impl signature mismatch errors involving `RegionKind::ReVar` lifetimes Fix rust-lang#66406, fix rust-lang#72106. ``` error: `impl` item signature doesn't match `trait` item signature --> $DIR/trait-param-without-lifetime-constraint.rs:14:5 | LL | fn get_relation(&self) -> To; | ----------------------------- expected `fn(&Article) -> &ProofReader` ... LL | fn get_relation(&self) -> &ProofReader { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&Article) -> &ProofReader` | = note: expected `fn(&Article) -> &ProofReader` found `fn(&Article) -> &ProofReader` help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait` --> $DIR/trait-param-without-lifetime-constraint.rs:10:31 | LL | fn get_relation(&self) -> To; | ^^ consider borrowing this type parameter in the trait ``` r? @nikomatsakis
So I have the following code:
Which on the stable compiler gives this helpful error message:
Yet on the nightly compiler the compiler error does not inform me about lifetimes, but tells me the signature needs to be what it already is:
playground stable version
playground nightly version
The text was updated successfully, but these errors were encountered: