Skip to content

Mismatched lifetime on trait impl shows a misleading error #80701

Open
@Earlz

Description

@Earlz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions