Skip to content

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

Closed
@XMPPwocky

Description

@XMPPwocky

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions