Skip to content

Cannot declare a generic fn with a constraint containing a region  #8579

Closed
@erickt

Description

@erickt

We cannot declare a generic function where one of the tyvars is constrained by a type that contains a region. Here's an example:

trait Trait<'self> {
    fn foo(&self) -> &'self str;
}

fn foo<'a, T: Trait<'a>>(_x: T) {}

fn main() {}

This errors with:

a.rs:5:22: 5:24 error: Illegal lifetime 'a: only 'self is allowed as part of a type declaration
a.rs:5 fn foo<'a, T: Trait<'a>>(_x: T) {}

There is a workaround though. Instead we can use a vtable, which compiles fine.

trait Trait<'self> {
    fn foo(&self) -> &'self str;
}

fn foo<'a>(_x: &Trait<'a>) {}

fn main() {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions