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

FIR: Self types draft #4796

Draft
wants to merge 24 commits into
base: master
Choose a base branch
from

Conversation

maxim092001
Copy link

@maxim092001 maxim092001 commented Apr 20, 2022

@mglukhikh mglukhikh self-assigned this Apr 21, 2022
@mglukhikh mglukhikh added the FIR label Apr 21, 2022
@mglukhikh mglukhikh marked this pull request as draft April 21, 2022 06:03
@mglukhikh mglukhikh changed the title Self types FIR: Self types draft Oct 24, 2022
@maxim092001 maxim092001 force-pushed the self-types-thesis branch 4 times, most recently from 95d8ae9 to ab6c031 Compare January 7, 2023 19:31
Self type will be generated for classes/interfaces
annotated with `@Self`.
For FIR will be generated special type <Self> and transformed to $Self
during FIR2IR.

Self type is added as last type parameter.

Example:

```
@self
class Foo { // generated type: out Self: Foo<Self>
  fun foo(): Self = this as Self // without UNCHECKED_CAST warning
}
```
constructors of this class.

This allows to create Self classes.

Example:

```
@self
class Foo<T> { // generated type: out Self: Foo<T, Self>
  ...
}

Foo<Int>() // last type argument would be generated as Foo<Foo<Int, *>>
```
compiler using Self types.

Some parts were changed due to complexity of initial implementation.
* Usage of self types deriving sealed interface.
* Checker for @self classes with custom `Self` type parameter declared.
* Self class with `Self` function.
* Self class extending interface with `out` type parameter.
* Self class with qualified this.
@mglukhikh
Copy link
Contributor

mglukhikh commented Jan 20, 2023

Some more questionable examples:

  • @Self on a local class
  • @Self on an object (or anonymous object), on an annotation class, on an enum class, on an enum entry
  • @Self on an interface, or on a fun interface
  • different kinds of inheritance (class from self interface, class from self class, local class from self class, local class from self local class)
  • @Self around inner classes (inner class inside self class, self inner class inside self class)
  • @Self sealed class with inheritors

@@ -451,7 +451,7 @@ open class FirSupertypeResolverVisitor(
private fun addSelfToTypeParameters(firClass: FirClassLikeDeclaration, session: FirSession) {
val isSelf = firClass.getAnnotationByClassId(StandardClassIds.Annotations.Self, session) != null
val params = firClass.typeParameters
if (params is MutableList && isSelf) {
if (params is MutableList && isSelf && params.find { it.symbol.name == SpecialNames.SELF_TYPE } == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test confirming this check is needed

Add * to type parameters for classes with `@Self` annotation when class
used as type.

Add test for class with `@Self` annotation used as return type.
Add test for class with `@Self` annotation used as value parameter.
* Nested class extends outer self class
* Nested class extends nested self class
* Lazy container with Self type, list and set implementations
* Allows to use this as Self type without explicit cast
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants