Closed
Description
openedon Mar 20, 2016
Please consider the following code:
use std::marker::PhantomData;
use std::ops::Index;
pub trait Context: Clone {
type Container: ?Sized;
fn foobar_1( container: &Self::Container ) -> &str;
fn foobar_2( container: &Self::Container ) -> &str;
fn foobar_3( container: &Self::Container ) -> &str;
}
#[derive(Clone)]
struct Foobar<'a> {
phantom: PhantomData<&'a ()>
}
impl<'a> Context for Foobar<'a> {
type Container = [&'a str];
fn foobar_1<'r>( container: &'r [&'a str] ) -> &'r str {
// This compiles fine.
container[0]
}
fn foobar_2<'r>( container: &'r Self::Container ) -> &'r str {
// This also compiles fine.
container.index( 0 )
}
fn foobar_3<'r>( container: &'r Self::Container ) -> &'r str {
// error: `*container[..]` does not live long enough
container[0]
}
}
fn main() {}
which fails with the following error:
test.rs:31:9: 31:21 error: `*container[..]` does not live long enough
test.rs:31 container[0]
^~~~~~~~~~~~
test.rs:29:66: 32:6 note: reference must be valid for the lifetime 'r as defined on the block at 29:65...
test.rs:29 fn foobar_3<'r>( container: &'r Self::Container ) -> &'r str {
test.rs:30 // error: `*container[..]` does not live long enough
test.rs:31 container[0]
test.rs:32 }
test.rs:29:66: 32:6 note: ...but borrowed value is only valid for the lifetime 'a as defined on the block at 29:65
test.rs:29 fn foobar_3<'r>( container: &'r Self::Container ) -> &'r str {
test.rs:30 // error: `*container[..]` does not live long enough
test.rs:31 container[0]
test.rs:32 }
error: aborting due to previous error
So if I use .index()
and use an associated type it compiles fine. If I use []
and replace the associated type with the underlying type it also compiles fine. Logic would have it that if I use []
and I'll leave the associated type as-is it should also compile fine, but alas, I get an error, which doesn't really make any sense.
Rust version: rustc 1.9.0-nightly (b12b4e4 2016-03-17)
This is a regression; this code used to compile on at least 1.4, 1.5 and 1.6; it broke at 1.7.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Metadata
Assignees
Labels
Area: Lifetimes / regionsArea: The type systemCategory: This is a bug.Bugs fixed, but only when NLL is enabled.Medium priorityRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the language team, which will review and decide on the PR/issue.Performance or correctness regression from one stable version to another.