Skip to content

Existential types have issues with lifetimes #62988

Closed
@jethrogb

Description

@jethrogb
#![feature(existential_type)]

trait MyTrait {
    type AssocType: Send;
    fn ret(&self) -> Self::AssocType;
}

impl MyTrait for () {
    existential type AssocType: Send;
    fn ret(&self) -> Self::AssocType {
        ()
    }
}

impl<'a> MyTrait for &'a () {
    existential type AssocType: Send;
    fn ret(&self) -> Self::AssocType {
        ()
    }
}

trait MyLifetimeTrait<'a> {
    type AssocType: Send + 'a;
    fn ret(&self) -> Self::AssocType;
}

impl<'a> MyLifetimeTrait<'a> for &'a () {
    existential type AssocType: Send + 'a;
    fn ret(&self) -> Self::AssocType {
        *self
    }
}
error: could not find defining uses
  --> src/lib.rs:16:5
   |
16 |     existential type AssocType: Send;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
  --> src/lib.rs:28:5
   |
28 |     existential type AssocType: Send + 'a;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: hidden type `&'a ()` captures the lifetime 'a as defined on the impl at 27:6
  --> src/lib.rs:27:6
   |
27 | impl<'a> MyLifetimeTrait<'a> for &'a () {
   |      ^^

error: could not find defining uses
  --> src/lib.rs:28:5
   |
28 |     existential type AssocType: Send + 'a;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

cc @cramertj

(original report at #34511 (comment))

@rustbot modify labels: A-impl-trait C-bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-bugCategory: This is a bug.F-type_alias_impl_trait`#[feature(type_alias_impl_trait)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions