Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Arbitrary self type notes

Current behavior

Self Pattern Stable #61207 "Niko's rule" Playground
Foo fn(&🅰️ self) -> &u8 🅰️ 🅰️ 🅰️ playground
Foo fn(self: &🅰️ Foo) -> &u8 🅰️ 🅰️ ❌ playground
Foo fn(self: &🅰️ Self) -> &u8 🅰️ 🅰️ 🅰️ playground
Foo<'a> fn(&🅰️ self) -> &u8 🅰️ 🅰️ 🅰️ playground
Foo<'a> fn(self: &🅰️ Foo) -> &u8 🅰️ 🅰️ ❌ playground
Foo<'a> fn(self: &🅰️ Self) -> &u8 🅰️ 🅰️ 🅰️ playground
Foo fn(self: Pin<&🅰️ mut Self>) -> &u8 ❌ ? 🅰️ playground
Foo<'_> fn(self: Foo<'_>) -> &u8 ❌ ? ❌ playground
Foo<'_> fn(self: Self) -> &u8 ❌ ? ❌ playground
Foo fn(self: Pin<&🅰️ Foo>, f: &🅱️ Foo) -> &Foo 🅱️ ❌ ❌ playground
Foo<'a> fn(self: Foo<'a>, x: &🅱️ u8) -> &u8 🅱️ 🅱️ ❌ playground

Key:

  • &* indicates the lifetime used for the return value
  • ✅ -- compiles
  • ❌ -- error
  • ⛔️ -- resolves to (arguably) wrong thing
  • 🆘 -- resolves to (arguably) wrong thing and is stable

Bug

#52675 noted that elision didn't work in some scenarios where it might be desirable.

The actual example in the bug involves a Deref impl and is a bit... unusual to my eyes:

impl Foo {
    fn c(self: Bar<'_>) -> Bar<'_> { // here `Foo` derefs to `Bar<'_>`.
        self
    }
}
@pnkfelix

This comment has been minimized.

Copy link

pnkfelix commented Jun 28, 2019

Link to referenced PR, for convenience: rust-lang/rust#61207

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.