Skip to content
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

Mismatched lifetime on trait impl shows a misleading error #80701

Open
Earlz opened this issue Jan 4, 2021 · 3 comments · May be fixed by #121274
Open

Mismatched lifetime on trait impl shows a misleading error #80701

Earlz opened this issue Jan 4, 2021 · 3 comments · May be fixed by #121274
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related A-traits Area: Trait system C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Earlz
Copy link

Earlz commented Jan 4, 2021

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

@Earlz Earlz added the C-bug Category: This is a bug. label Jan 4, 2021
@Earlz
Copy link
Author

Earlz commented Jan 4, 2021

For reference, this error message is resolved by changing

impl BaseSetter for dyn FooSetter

To this explicit lifetime scoped version:

impl <'a>BaseSetter for (dyn FooSetter + 'a)

@ThePuzzlemaker
Copy link
Contributor

@rustbot modify labels: A-diagnostics A-traits C-bug D-confusing D-papercut T-compiler A-lifetimes

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related A-traits Area: Trait system D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 7, 2021
@Dylan-DPC
Copy link
Member

Current error:

error[[E0521]](https://doc.rust-lang.org/stable/error_codes/E0521.html): borrowed data escapes outside of associated function
  --> src/main.rs:33:8
   |
31 | impl <'a>TestHolder<'a>{
   |       -- lifetime `'a` defined here
32 |     pub fn test_foo(&self){
   |                     ----- `self` is a reference that is only valid in the associated function body
33 |        self.holder.as_ref().unwrap().borrow_mut().set(20); 
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |        |
   |        `self` escapes the associated function body here
   |        argument requires that `'a` must outlive `'static`
   |
   = note: requirement occurs because of a mutable reference to `dyn FooSetter`
   = note: mutable references are invariant over their type parameter
   = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related A-traits Area: Trait system C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants