Skip to content

Associated type with a 'static bound still captures lifetimes when using impl Trait. #106684

Open
@kepexx

Description

@kepexx

Using a returned impl Trait for a trait with an associated type and not specifying the associated type leads to the following error:

error[E0700]: hidden type for `impl Trait + 'static` captures lifetime that does not appear in bounds

..even when + 'static is specified, meaning no lifetimes should be captured. Adding #![feature(associated_type_bounds)] and instead using impl Trait<AssocType: 'static> as a return type solves the issue. I'm not sure if this is intended behavior - it seems pretty strange since AssocType is defined with a 'static bound. See the code below for a reproducible example.

#![feature(associated_type_bounds)]

trait Anything {}
impl<T: ?Sized> Anything for T {}

trait Thing {
    type Output: 'static;

    fn calc(&mut self) -> Self::Output;
}

impl<'a> Thing for &'a u8 {
    type Output = u8;
    
    fn calc(&mut self) -> u8 { **self }
}

// uncomment the bound to make it work
fn get_thing(x: &u8) -> impl Thing /* <Output: 'static> */ + '_ {
    x
}

fn f(n: &u8) -> impl Anything + 'static {
    let mut t = get_thing(n);
    t.calc()
}

Meta

rustc --version --verbose:

rustc 1.67.0-nightly (c090c6880 2022-12-01)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-lifetimesArea: Lifetimes / regionsC-discussionCategory: Discussion or questions that doesn't represent real issues.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions