openrewrite / rewrite-kotlin

Work-in-progress implementation of Kotlin language support for OpenRewrite.
Apache License 2.0
43 stars 12 forks source link

Info to extend TypeValidation for Kotlin #482

Closed traceyyoshima closed 9 months ago

traceyyoshima commented 10 months ago

Annotation use-site

Code

@file:Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "unused")

Info on use-site name

A use-site annotation is a keyword that informs the compiler where to apply annotation. The use-site annotation does not resolve to an annotation with a type, which means the type is null. FindMissingTypes does not consider this case as a valid null type, which leads to an error.

Aliases

Code

import java.util.regex.Pattern.CASE_INSENSITIVE as i
class A {
    val f = arrayOf(i)
}

Info

The variable type of a type aliases identifier is constructed from the aliased type. So, the aliased type and the J.Identifier's name do not match. The identifier i has a type of java.util.regex.Pattern{name=CASE_INSENSITIVE,type=kotlin.Int}

MethodInvocation syntax sugar

Code

val a = mapOf ( 1 to "one" , 2 to "two" )
val b = a [ 1 ]

Info on get and set

Kotlin recommends using map[key] instead of map.get(key). The type associated with the get is a MethodType with the name get, which does not match the name of the method invocation created a. A similar issue happens with set.

Code

              abstract class Test ( arg : Test  .   ( )  ->   Unit    =  { } ) {
                  init {
                      arg ( )
                  }
              }

Info on invoke

A similar issue exists for implicit invoke calls.

Code

val positive = fun(i: Int) = i > 0 /* C */

Info on anonymous functions

Anonymous functions have the name anonymous, which does not match the identifier created with an empty name.

NamedVariable

Code

              operator fun <T> MutableCollection<T>.divAssign(elements: Collection<T>) {
                  this.retainAll(elements)
              }

Info on receivers

An extension receiver has an unused name, <receiverType>, which does not match the variable type name.

Code

              import kotlin.text.Regex

              fun foo(choices: List<Pair<String, String>>, peekedHeader: Regex) {
                  for ((_, adapter) in choices) {
                      if (adapter.matches(peekedHeader)) {
                          print("1")
                      }
                  }
              }

Info on destructuring declarations

Similar to receivers, a destruct is created with a name <destruct>

MemberReference

Null types on class literal expressions Type::class. Type validation fails because the types are intentionally set to null here.

traceyyoshima commented 10 months ago

@knutwannheden @kunli2 this is a list of conditions that differ from Java to augment the type validation for Kotlin. I'll be adding to the list as other issues are identified. Please feel free to add/remove/edit as you see fit.

traceyyoshima commented 10 months ago

@knutwannheden @kunli2 is a list of errors thrown after type validation is added to Assertions#kotlin, which I think should be added as acceptable types.

Do you think there are any cases, which should not be considered acceptable?

kunli2 commented 9 months ago

I think so too

@knutwannheden @kunli2 is a list of errors thrown after type validation is added to Assertions#kotlin, which I think should be added as acceptable types.

knutwannheden commented 9 months ago

I am not sure I understand the question correctly.

Please also note that the TypeAlias example is an import alias and not a type alias. Of course the import alias can also be used to alias type names, but this is still distict from typealias.