Skip to content

Fix lifetimes usage in delegate types (#6)

50U10FCA7 requested to merge 50U10FCA7/enum_delegate:6-fix-type-lifetimes into main

Part of #6

Synopsis

Lifetimes in delegate types seems to be broken.

Example:

#[delegate]
trait Named {
  fn name(&self) -> &str;
}

struct User {
  name: String
}

impl Named for User {
  fn name(&self) -> &str {
    &self.name
  }
}

impl Named for &'_ User {
  fn name(&self) -> &str {
    &self.name
  }
}

#[delegate(derive(Named))] // ERROR: `'borrowed` should outlive `'static`
enum CowUser<'borrowed> {
    Borrowed(&'borrowed User),
    Owned(User),
}

Also, there is a problem with Self bounds on methods, because of incorrect macro expansion:

#[delegate]
trait Named {
  fn name<'b>(&'b self) -> &'b str
  where
    Self: 'b;
}

struct User {
  name: String
}

impl Named for User {
  fn name<'b>(&'b self) -> &'b str
  where
    Self: 'b
  {
    &self.name
  }
}

impl Named for &'_ User {
  fn name<'b>(&'b self) -> &'b str
  where
    Self: 'b
  {
    &self.name
  }
}

#[delegate(derive(Named))] // ERROR: `Self::BindType0` should outlive `'b` (but we bound the `Self`, right?)
enum CowUser<'borrowed> {
    Borrowed(&'borrowed User),
    Owned(User),
}

Solution

  • Fix lifetimes usage in delegate types;
  • Fix incorrect types scope bounds generation in macro expansion.

Merge request reports