kotlin icon indicating copy to clipboard operation
kotlin copied to clipboard

Implement sealed inline classes feature

Open ilmirus opened this issue 3 years ago • 2 comments

KEEP: https://github.com/Kotlin/KEEP/pull/312

ilmirus avatar Jul 20 '22 11:07 ilmirus

There's two more comments that I couldn't add because they were from earlier commits:


I don't think the code for type parameters in coerceInlineClasses is entirely correct. The unsafeCoerce intrinsic looks like this:

fun <A, B> unsafeCoerce(x: A): B

while the generated call is

unsafeCoerce<From, UnderlyingType>(argument as UnderlyingType)

I think it's probably for the best to avoid folding any additional casts into this intrinsic in any case, since it really should only be used for boxing/unboxing inline classes. Casts from/to upper bounds can always be added around it.


The workaround for nullable receivers in JvmInlineClassLowering.visitGetField won't work for nullable type parameters, e.g., if we have

value class A(val x: String)

and now, e.g., the debugger is asked to evaluate an expression a?.x_field in a context where a : T and T : A? then the code will fail to produce a non-nullable receiver, since makeNotNull doesn't handle type parameters. It's probably best to add an explicit cast to the inline class type instead.

In fact, this should probably be done when we generate code for checked calls in general, since an IrGetField (IrCall) needs a non-nullable (dispatch) receiver.

sfs avatar Aug 09 '22 11:08 sfs

@sfs, @udalov Thanks a lot for your comments. I reorganized the code, so it is hopefully easier to review, since there were a lot of obsolete code, due to design change midway. Sorry I kept you waiting - I was waiting for value classes, which required some refactoring, so, I would have to adopt the changes anyway.

ilmirus avatar Oct 20 '22 02:10 ilmirus