Skip to content

Impossible to infer the lifetime parameter of a function parameter when going through a trait #38673

Closed
@tomaka

Description

@tomaka

This code:

fn foo<F, P>(f: F)
    where F: Fn(P),
          P: for<'a> From<&'a str>
{
    f(From::from("test"))
}

fn main() {
    foo(|a: &str| {
        // ...
    })
}

Leads to:

error[E0277]: the trait bound `for<'a> &str: std::convert::From<&'a str>` is not satisfied
  --> <anon>:12:5
   |
12 |     foo(|a: &str| {
   |     ^^^ the trait `for<'a> std::convert::From<&'a str>` is not implemented for `&str`
   |
   = note: required by `foo`

In case you're wondering, there is a impl<T> From<T> for T in stdlib, so &'a str does implement From<&'a str>. To make sure I used a custom trait instead of From and it didn't fix the problem.

Chances are that this is a duplicate of an existing issue, but it's hard for someone not familiar with the internals of rustc like me to know if that's the case.

(Also, any idea for a work-around would be greatly appreciated)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions