Skip to content

Conversation

@Godin
Copy link
Member

@Godin Godin commented Mar 14, 2021

For the following Example.kt

open class Example {
  fun f1(p: String = "p") {}
  open fun f2(p: String = "p") {}
}

fun main(args: Array<String>) {
  Example().f1()
  Example().f2()
}

Execution of

kotlin/bin/kotlinc Example.kt -d classes
javap -v -p classes/Example.class

using Kotlin compiler versions before 1.5-M1 (tested versions from 1.3.0 to 1.4.30) produces

  public static void f1$default(Example, java.lang.String, int, java.lang.Object);
    descriptor: (LExample;Ljava/lang/String;ILjava/lang/Object;)V
    flags: ACC_PUBLIC, ACC_STATIC, ACC_SYNTHETIC
    Code:
      stack=3, locals=4, args_size=4
         0: aload_3
         1: ifnull        14
         4: new           #22                 // class java/lang/UnsupportedOperationException
         7: dup
         8: ldc           #24                 // String Super calls with default arguments not supported in this target, function: f1
        10: invokespecial #27                 // Method java/lang/UnsupportedOperationException."<init>":(Ljava/lang/String;)V
        13: athrow
        14: iload_2
        15: iconst_1
        16: iand
        17: ifeq          23
        20: ldc           #9                  // String p
        22: astore_1
        23: aload_0
        24: aload_1
        25: invokevirtual #29                 // Method f1:(Ljava/lang/String;)V
        28: return
      LineNumberTable:
        line 2: 20

  public static void f2$default(Example, java.lang.String, int, java.lang.Object);
    descriptor: (LExample;Ljava/lang/String;ILjava/lang/Object;)V
    flags: ACC_PUBLIC, ACC_STATIC, ACC_SYNTHETIC
    Code:
      stack=3, locals=4, args_size=4
         0: aload_3
         1: ifnull        14
         4: new           #22                 // class java/lang/UnsupportedOperationException
         7: dup
         8: ldc           #33                 // String Super calls with default arguments not supported in this target, function: f2
        10: invokespecial #27                 // Method java/lang/UnsupportedOperationException."<init>":(Ljava/lang/String;)V
        13: athrow
        14: iload_2
        15: iconst_1
        16: iand
        17: ifeq          23
        20: ldc           #9                  // String p
        22: astore_1
        23: aload_0
        24: aload_1
        25: invokevirtual #35                 // Method f2:(Ljava/lang/String;)V
        28: return
      LineNumberTable:
        line 3: 20

whereas using Kotlin compiler version 1.5.0-M1 produces

  public static void f1$default(Example, java.lang.String, int, java.lang.Object);
    descriptor: (LExample;Ljava/lang/String;ILjava/lang/Object;)V
    flags: ACC_PUBLIC, ACC_STATIC, ACC_SYNTHETIC
    Code:
      stack=3, locals=4, args_size=4
         0: aload_3
         1: ifnull        14
         4: new           #26                 // class java/lang/UnsupportedOperationException
         7: dup
         8: ldc           #28                 // String Super calls with default arguments not supported in this target, function: f1
        10: invokespecial #30                 // Method java/lang/UnsupportedOperationException."<init>":(Ljava/lang/String;)V
        13: athrow
        14: iload_2
        15: iconst_1
        16: iand
        17: ifeq          23
        20: ldc           #15                 // String p
        22: astore_1
        23: aload_0
        24: aload_1
        25: invokevirtual #32                 // Method f1:(Ljava/lang/String;)V
        28: return
      LineNumberTable:
        line 2: 0

  public static void f2$default(Example, java.lang.String, int, java.lang.Object);
    descriptor: (LExample;Ljava/lang/String;ILjava/lang/Object;)V
    flags: ACC_PUBLIC, ACC_STATIC, ACC_SYNTHETIC
    Code:
      stack=3, locals=4, args_size=4
         0: aload_3
         1: ifnull        14
         4: new           #26                 // class java/lang/UnsupportedOperationException
         7: dup
         8: ldc           #36                 // String Super calls with default arguments not supported in this target, function: f2
        10: invokespecial #30                 // Method java/lang/UnsupportedOperationException."<init>":(Ljava/lang/String;)V
        13: athrow
        14: iload_2
        15: iconst_1
        16: iand
        17: ifeq          23
        20: ldc           #15                 // String p
        22: astore_1
        23: aload_0
        24: aload_1
        25: invokevirtual #38                 // Method f2:(Ljava/lang/String;)V
        28: return
      LineNumberTable:
        line 3: 0

where LineNumberTable is different

  • prior to 1.5-M1 line number is in the middle of the methods
  • with 1.5-M1 is at the beginning of the methods

KotlinDefaultArgumentsFilter currently doesn't handle last case, because of

and

So execution of

java \
  -javaagent:jacoco-0.8.6/lib/jacocoagent.jar \
  -cp kotlin/lib/kotlin-stdlib.jar:classes \
  ExampleKt

java \
  -jar jacoco-0.8.6/lib/jacococli.jar \
  report jacoco.exec \
  --classfiles classes \
  --sourcefiles . \
  --html report

produces following report

before

Report will be same after this change for Kotlin compiler version 1.5-M1 as for previous versions

after

@Godin Godin added this to the 0.8.7 milestone Mar 14, 2021
@Godin Godin self-assigned this Mar 14, 2021
@Godin Godin marked this pull request as ready for review March 14, 2021 20:44
@Godin Godin requested a review from marchof March 14, 2021 20:44
@marchof
Copy link
Member

marchof commented Mar 14, 2021

@Godin I wonder whether you test scenario is already covered by the existing validation target KotlinDefaultArgumentsTarget? Will the existing target fail when we upgrade to Kotlin 1.5?

@Godin
Copy link
Member Author

Godin commented Mar 15, 2021

I wonder whether you test scenario is already covered by the existing validation target KotlinDefaultArgumentsTarget? Will the existing target fail when we upgrade to Kotlin 1.5?

@marchof yes it is, before this change when executing

mvn clean package -Dkotlin.version=1.5.0-M1

we'll get

Failed tests:
  execute_assertions_in_comments(org.jacoco.core.test.validation.kotlin.KotlinDefaultArgumentsTest): Branches (KotlinDefaultArgumentsTarget.kt:20) expected:<Counter[0/0]> but was:<Counter[0/4]>

among other differences that I currently investigate, and this test will pass after this change.

@marchof marchof merged commit d9296f7 into master Mar 15, 2021
@marchof marchof deleted the KotlinDefaultArgumentsFilter branch March 15, 2021 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project
Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants