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

Very unhelpful error message cannot infer an appropriate lifetime for autoref #22537

Closed
mvdnes opened this issue Feb 19, 2015 · 2 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system

Comments

@mvdnes
Copy link
Contributor

mvdnes commented Feb 19, 2015

When compiling the following code:

use std::cell::{RefCell, RefMut};

trait FooTrait {
    fn footrait(&mut self);
}

struct Foo;
impl FooTrait for Foo {
    fn footrait(&mut self) { () }
}

impl<'a> FooTrait for RefMut<'a, Foo> {
    fn footrait(&mut self) { () }
}

struct Bar {
    bar: RefCell<Foo>,
}
impl Bar {
    fn foobox(&self) -> Box<FooTrait> {
        let inner = self.bar.borrow_mut();
        Box::new(inner)
    }
}

I get the following (confusing) error message:

<anon>:21:30: 21:42 error: cannot infer an appropriate lifetime for autoref due to conflicting requirements
<anon>:21         let inner = self.bar.borrow_mut();
                                       ^~~~~~~~~~~~
<anon>:21:21: 21:29 note: first, the lifetime cannot outlive the expression at 21:20...
<anon>:21         let inner = self.bar.borrow_mut();
                              ^~~~~~~~
<anon>:21:21: 21:29 note: ...so that auto-reference is valid at the time of borrow
<anon>:21         let inner = self.bar.borrow_mut();
                              ^~~~~~~~
<anon>:21:21: 21:42 note: but, the lifetime must be valid for the method call at 21:20...
<anon>:21         let inner = self.bar.borrow_mut();
                              ^~~~~~~~~~~~~~~~~~~~~
<anon>:21:21: 21:29 note: ...so that method receiver is valid for the method call
<anon>:21         let inner = self.bar.borrow_mut();
                              ^~~~~~~~

After a lot of trial-and-error I noticed I had to do the following change:

// old:
     fn foobox(&self) -> Box<FooTrait> {
// new:
     fn foobox<'a>(&'a self) -> Box<FooTrait + 'a> {
@kmcallister kmcallister added A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system labels Feb 20, 2015
@ticki
Copy link
Contributor

ticki commented Feb 3, 2016

This question is something people constantly ask on Reddit and IRC, which indicates that this error message could be improved.

@arielb1
Copy link
Contributor

arielb1 commented Feb 24, 2017

Duplicate of #33539

@arielb1 arielb1 closed this as completed Feb 24, 2017
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-trait-system Area: Trait system
Projects
None yet
Development

No branches or pull requests

4 participants