Skip to content

Rustc confuses lifetimes of different unrelated variables of type &'static str when implementing a trait for &'a str #26448

Description

@daboross

Example 1:

pub trait Foo<T> {
    fn foo(self) -> T;
}

impl<'a, T> Foo<T> for &'a str where &'a str: Into<T> {
    fn foo(self) -> T {
        panic!();
    }
}

Results in:

<std macros>:1:23: 1:39 error: cannot infer an appropriate lifetime due to conflicting requirements
<std macros>:1 (  ) => ( { panic ! ( "explicit panic" ) } ) ; ( $ msg : expr ) => (
                                     ^~~~~~~~~~~~~~~~
...

Playground: https://play.rust-lang.org/?gist=22052e30a6d8e19c053e&version=stable.


Example 2:

pub struct Bar<T> {
    items: Vec<&'static str>,
    inner: T,
}

pub trait IntoBar<T> {
    fn into_bar(self) -> Bar<T>;
}

impl<'a, T> IntoBar<T> for &'a str where &'a str: Into<T> {
    fn into_bar(self) -> Bar<T> {
        Bar {
            items: Vec::new(),
            inner: self.into(),
        }
    }
}

Results in:

<anon>:16:20: 16:30 error: cannot infer an appropriate lifetime due to conflicting requirements
<anon>:16             items: Vec::new(),
...

Playground: https://play.rust-lang.org/?gist=1bffa565c5fd39f942d1&version=nightly


Example 3:

pub struct Item {
    _inner: &'static str,
}

pub struct Bar<T> {
    items: Vec<Item>,
    inner: T,
}

pub trait IntoBar<T> {
    fn into_bar(self) -> Bar<T>;
}

impl<'a, T> IntoBar<T> for &'a str where &'a str: Into<T> {
    fn into_bar(self) -> Bar<T> {
        Bar {
            items: Vec::new(),
            inner: self.into(),
        }
    }
}

Results in:

<anon>:17:20: 17:28 error: mismatched types:
 expected `core::marker::Sized`,
    found `core::marker::Sized`
(lifetime mismatch) [E0308]
<anon>:17             items: Vec::new(),
                             ^~~~~~~~
...

Playground: https://play.rust-lang.org/?gist=fd54c0069ab0f38079b7&version=stable


The first two examples occur on rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14), rustc 1.1.0-beta.3 (6aa2d5078 2015-06-12) and rustc 1.2.0-nightly (2f5683913 2015-06-18).

The third example occurs only on rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14) and rustc 1.1.0-beta.3 (6aa2d5078 2015-06-12), and appears to be fixed in nightly. I've included it to contrast with the first and second examples, which are still broken in the nightly.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemC-bugCategory: This is a bug.E-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