Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GAT] Associated type lifetime on regular type #87735

Closed
c410-f3r opened this issue Aug 3, 2021 · 3 comments
Closed

[GAT] Associated type lifetime on regular type #87735

c410-f3r opened this issue Aug 3, 2021 · 3 comments
Labels
A-GATs Area: Generic associated types (GATs) F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs GATs-triaged Issues using the `generic_associated_types` feature that have been triaged S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test.

Comments

@c410-f3r
Copy link
Contributor

c410-f3r commented Aug 3, 2021

After playing with Generic Associated Types, the following snippet was successfully created:

#![feature(generic_associated_types)]

pub trait AsRef2 {
  type Output<'a> where Self: 'a;

  fn as_ref2<'a>(&'a self) -> Self::Output<'a>;
}

impl<T> AsRef2 for Vec<T> {
  type Output<'a> where Self: 'a = &'a [T];

  fn as_ref2<'a>(&'a self) -> Self::Output<'a> {
    &self[..]
  }
}

#[derive(Debug)]
struct Foo<T>(T);
#[derive(Debug)]
struct FooRef<'a, U>(&'a [U]);

impl<U> AsRef2 for Foo<Vec<U>> {
  type Output<'a> where Self: 'a = FooRef<'a, U>;

  fn as_ref2<'a>(&'a self) -> Self::Output<'a> {
    FooRef(self.0.as_ref2())
  }
}

fn main() {
    let foo = Foo(vec![1, 2, 3]);
    dbg!(foo.as_ref2());
}

But then I was wondering if it is possible to generalize the above statement for every Foo<T> with T: AsRef2. Here is what was accomplished so far:

#![feature(generic_associated_types)]

pub trait AsRef2 {
  type Output<'a> where Self: 'a;

  fn as_ref2<'a>(&'a self) -> Self::Output<'a>;
}

impl<T> AsRef2 for Vec<T> {
  type Output<'a> where Self: 'a = &'a [T];

  fn as_ref2<'a>(&'a self) -> Self::Output<'a> {
    &self[..]
  }
}

#[derive(Debug)]
struct Foo<T>(T);
#[derive(Debug)]
struct FooRef<'a, U>(&'a [U]);

impl<'b, T, U> AsRef2 for Foo<T>
where
    // * `for<'b, 'c> T: AsRef2<Output<'b> = &'c [U]>>` does not work
    //
    // * `U` is unconstrained but should be allowed in this context because `Output` is
    // an associated type
    T: AsRef2<Output<'b> = &'b [U]>,
    U: 'b
{
  type Output<'a> where Self: 'a = FooRef<'a, U>;

  fn as_ref2<'a>(&'a self) -> Self::Output<'a> {
    FooRef(self.0.as_ref2())
  }
}

fn main() {
    let foo = Foo(vec![1, 2, 3]);
    dbg!(foo.as_ref2());
}

Any clues? Feel free to close this issue if duplicated.

@jackh726 jackh726 added the F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs label Aug 3, 2021
@jackh726
Copy link
Member

Short blurb here while I'm looking at this and remember the problem: in the GATs case, 'b is not considered constrained inT: AsRef2<Output<'b> = &'b [U]>, U isn't constrained.

@jackh726
Copy link
Member

GATs issue triage: not blocking. @nikomatsakis is supposed to post a comment at some point with some more details, but the general gist is to be able to do something like for<'b> T: AsRef2<Output<'b> = &'b [U]>, we need some sort of implied bounds or additional reasoning. Not going to really be possible for initial GATs release; kind of blocked on Chalk.

@jackh726 jackh726 added the GATs-triaged Issues using the `generic_associated_types` feature that have been triaged label Oct 20, 2021
@jackh726 jackh726 added the S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. label Mar 12, 2022
@c410-f3r
Copy link
Contributor Author

Closing in favor of #88526

@fmease fmease added the A-GATs Area: Generic associated types (GATs) label Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-GATs Area: Generic associated types (GATs) F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs GATs-triaged Issues using the `generic_associated_types` feature that have been triaged S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test.
Projects
None yet
Development

No branches or pull requests

3 participants