Skip to content

Borrowck oddness w/ several trait object refs in a struct #14821

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

Closed
XMPPwocky opened this issue Jun 11, 2014 · 3 comments · Fixed by #27117
Closed

Borrowck oddness w/ several trait object refs in a struct #14821

XMPPwocky opened this issue Jun 11, 2014 · 3 comments · Fixed by #27117
Labels
A-type-system Area: Type system E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@XMPPwocky
Copy link
Contributor

For some reason, trying to place two trait objects with the same templated lifetime into a struct confuses borrowck:

[wocky@XMPPwocky ~]$ cat foo.rs
trait SomeTrait{}
struct Meow;
impl SomeTrait for Meow{}

struct Foo<'a> {
  x: &'a SomeTrait,
  y: &'a SomeTrait
}

impl<'a> Foo<'a> {
  pub fn new<'b>(x:&'b SomeTrait, y: &'b SomeTrait) -> Foo<'b> {
    Foo { x: x, y: y }
  }
}
fn main() {
  let r = Meow;
  let s = Meow;
  let q = Foo::new(& r as & SomeTrait, & s as & SomeTrait);
}
[wocky@XMPPwocky ~]$ rustc foo.rs
foo.rs:11:5: 11:23 error: cannot infer an appropriate lifetime for lifetime parameter `a due to conflicting requirements
foo.rs:11     Foo { x: x, y: y }
              ^~~~~~~~~~~~~~~~~~
foo.rs:11:14: 11:15 note: first, the lifetime must be contained by the expression at 11:13...
foo.rs:11     Foo { x: x, y: y }
                       ^
foo.rs:11:14: 11:15 note: ...so that automatically reference is valid at the time of borrow
foo.rs:11     Foo { x: x, y: y }
                       ^
foo.rs:11:20: 11:21 note: but, the lifetime must also be contained by the expression at 11:19...
foo.rs:11     Foo { x: x, y: y }
                             ^
foo.rs:11:20: 11:21 note: ...so that automatically reference is valid at the time of borrow
foo.rs:11     Foo { x: x, y: y }

This does NOT occur with references to primitive types (tested w/ int). It also does NOT occur if there is only one trait object reference.

This is currently holding up progress on red (my text editor in Rust), so a rapid fix would be very helpful.

Thanks in advance.

@XMPPwocky
Copy link
Contributor Author

In an absolutely baffling turn of events, using tuple-like structs works fine.

The below compiles happily...

trait SomeTrait{}
struct Meow;
impl SomeTrait for Meow{}

struct Foo<'a> (
  &'a SomeTrait,
  &'a SomeTrait
);
impl<'a> Foo<'a> {
  pub fn new<'b>(x:&'b SomeTrait, y: &'b SomeTrait) -> Foo<'b> {
    Foo ( x, y )
  }
}
fn main() {
  let r = Meow;
  let s = Meow;
  let q = Foo::new(&r as &SomeTrait, &s as &SomeTrait);
}

This is a usable workaround for the time being, but really only raises more questions as to what exactly is going on in the borrow checker.

@pnkfelix
Copy link
Member

cc me

@steveklabnik steveklabnik added the A-type-system Area: Type system label Jan 23, 2015
@apasel422
Copy link
Contributor

This compiles as of rustc 1.3.0-nightly (e4e93196e 2015-07-14).

@Gankra Gankra added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Jul 17, 2015
bors added a commit that referenced this issue Jul 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants