Skip to content

lifetime constraints cross-interfere between method definitions #18163

Closed
@dwrensha

Description

@dwrensha
$ rustc --version
rustc 0.13.0-nightly (c46812f65 2014-10-19 00:47:18 +0000)

I expect this program to compile successfully:

// lifetimes.rs

pub struct ListBuilder<'a> {
    data : &'a [u8]
}

pub struct StructBuilder<'a> {
    data : &'a [u8]
}

impl <'a> ListBuilder <'a> {
    pub fn get_struct_element(&self, index : uint) -> StructBuilder<'a> {
        StructBuilder { data: self.data.slice(index, index + 4) }
    }
}

pub trait FromStructBuilder<'a> {
    fn new(structBuilder : StructBuilder<'a>) -> Self;
}

pub struct ConcreteListBuilder<'a, T> {
    pub builder : ListBuilder<'a>
}

impl <'a, T : FromStructBuilder<'a>> ConcreteListBuilder<'a, T> {
    pub fn get(&self, index : uint) -> T {
        FromStructBuilder::new(self.builder.get_struct_element(index))
    }
}

// Compilation succeeds if I comment out this impl.
impl <'a, T : FromStructBuilder<'a>> ConcreteListBuilder<'a, T> {
    pub fn get2(&self, index : uint) -> T {
        FromStructBuilder::new(self.builder.get_struct_element(index))
    }
}

pub fn main () {}

Instead, it gives me this error:

$ rustc lifetimes.rs 
lifetimes.rs:34:32: 34:44 error: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
lifetimes.rs:34         FromStructBuilder::new(self.builder.get_struct_element(index))
                                               ^~~~~~~~~~~~
lifetimes.rs:33:43: 35:6 note: first, the lifetime cannot outlive the lifetime 'a as defined on the block at 33:42...
lifetimes.rs:33     pub fn get2(&self, index : uint) -> T {
lifetimes.rs:34         FromStructBuilder::new(self.builder.get_struct_element(index))
lifetimes.rs:35     }
lifetimes.rs:34:32: 34:44 note: ...so that types are compatible (expected `&ListBuilder<'_>`, found `&ListBuilder<'a>`)
lifetimes.rs:34         FromStructBuilder::new(self.builder.get_struct_element(index))
                                               ^~~~~~~~~~~~
lifetimes.rs:26:42: 28:6 note: but, the lifetime must be valid for the lifetime 'a as defined on the block at 26:41...
lifetimes.rs:26     pub fn get(&self, index : uint) -> T {
lifetimes.rs:27         FromStructBuilder::new(self.builder.get_struct_element(index))
lifetimes.rs:28     }
lifetimes.rs:34:9: 34:31 note: ...so that trait type parameters matches those specified on the impl (expected `FromStructBuilder<'_>`, found `FromStructBuilder<'a>`)
lifetimes.rs:34         FromStructBuilder::new(self.builder.get_struct_element(index))
                        ^~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

The program successfully compiles if I comment out the definition of the get2() method.

This kind of thing used to work; e.g. the program compiles successfully at the commit 9b80efd.

cc @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions