Repository navigation
Mismatched lifetime on trait impl shows a misleading error #80701
Copy link
Copy link
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-dyn-traitArea: trait objects, vtable layoutArea: trait objects, vtable layoutA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsA-trait-systemArea: Trait systemArea: Trait systemD-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.Diagnostics: An error or lint that doesn't give enough information about the problem at hand.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
The rust compiler gives a very poor error message when there is a lifetime expectation mismatch on trait usage.
I used this code: (playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=581fa159f8ff99f2df447308fccd4bbc )
use std::cell::*;
#[derive(Default)]
struct Test {
pub foo: u32,
}
trait FooSetter {
fn set_foo(&mut self, value: u32);
}
impl FooSetter for Test {
fn set_foo(&mut self, value: u32) {
self.foo = value;
}
}
trait BaseSetter{
fn set(&mut self, value: u32);
}
impl BaseSetter for dyn FooSetter{
fn set(&mut self, value: u32){
self.set_foo(value);
}
}
struct TestHolder<'a> {
pub holder: Option<RefCell<&'a mut dyn FooSetter>>,
}
impl <'a>TestHolder<'a>{
pub fn test_foo(&self){
self.holder.as_ref().unwrap().borrow_mut().set(20);
}
}
fn main() {
let mut test = Test::default();
test.foo = 10;
{
let holder = TestHolder { holder: Some(RefCell::from(&mut test))};
holder.test_foo();
}
test.foo = 30;
}I expected to see a useful error message, such as "expected trait BaseSetter<'a>, found trait: BaseSetter<'static>"
Instead, I was greeted with this almost useless error message: "expected trait BaseSetter, found trait BaseSetter"
The same error occurs on stable and nightly compilers
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-dyn-traitArea: trait objects, vtable layoutArea: trait objects, vtable layoutA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsA-trait-systemArea: Trait systemArea: Trait systemD-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.Diagnostics: An error or lint that doesn't give enough information about the problem at hand.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.
Activity
Earlz commentedon Jan 4, 2021
For reference, this error message is resolved by changing
To this explicit lifetime scoped version:
ThePuzzlemaker commentedon Jan 7, 2021
@rustbot modify labels: A-diagnostics A-traits C-bug D-confusing D-papercut T-compiler A-lifetimes
Dylan-DPC commentedon Apr 18, 2023
Current error: