Skip to content

Investigate disappearance of SMAP in Kotlin 2.0 #1840

@Godin

Description

@Godin

For the following src/a/utils.kt

package a

public inline fun <reified T> example(): T? {
    nop {
        "" is T
    }
    return null
}

fun nop(x: Runnable) = x.run()

and src/Example.kt

import a.example

fun main() {
  example<String>()
}

execution of

kotlin/bin/kotlinc src -d classes

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

java -jar jacoco-0.8.13-SNAPSHOT/lib/jacococli.jar \
    report jacoco.exec \
    --classfiles classes \
    --sourcefiles src \
    --html report

using Kotlin compiler version 1.9.25 produces

Image a classes utils.kt Image Image

whereas using Kotlin compiler versions 2.0.0 , 2.0.21 and 2.1.0 produces

Image a classes a sources utils.kt Image Image Image

which is caused by the following the only differences in class files

javap -v -p classes/a/UtilsKt\$example\$1.class
 SourceFile: "utils.kt"
-SourceDebugExtension:
-  SMAP
-  utils.kt
-  Kotlin
-  *S Kotlin
-  *F
-  + 1 utils.kt
-  a/UtilsKt$example$1
-  *L
-  1#1,11:1
-  *E
 RuntimeVisibleAnnotations:
-  0: #29(#30=[I#31,I#32,I#33],#34=I#35,#36=I#37,#38=[s#39],#40=[s#41,s#14,s#16,s#13])
+  0: #29(#30=[I#31,I#32,I#32],#33=I#34,#35=I#36)
     kotlin.Metadata(
-      mv=[1,9,0]
+      mv=[2,0,0]
       k=3
       xi=176
-      d1=["\u0000\n\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0002\u0010\u0000\u001a\u00020\u0001\"\u0006\b\u0000\u0010\u0002\u0018\u0001H\n¢\u0006\u0002\b\u0003"]
-      d2=["<anonymous>","","T","run"]
-    )
-RuntimeInvisibleAnnotations:
-  0: #48(#49=[s#50])
-    kotlin.jvm.internal.SourceDebugExtension(
-      value=["SMAP\nutils.kt\nKotlin\n*S Kotlin\n*F\n+ 1 utils.kt\na/UtilsKt$example$1\n*L\n1#1,11:1\n*E\n"]
     )
javap -v -p classes/ExampleKt\$main\$\$inlined\$example\$1.class
 SourceFile: "utils.kt"
-SourceDebugExtension:
-  SMAP
-  utils.kt
-  Kotlin
-  *S Kotlin
-  *F
-  + 1 utils.kt
-  a/UtilsKt$example$1
-  *L
-  1#1,11:1
-  *E
 RuntimeVisibleAnnotations:
-  0: #24(#25=[I#26,I#27,I#28],#29=I#30,#31=I#32,#33=[s#34],#35=[s#36,s#37,s#38,s#19,s#39])
+  0: #24(#25=[I#26,I#27,I#27],#28=I#29,#30=I#31)
     kotlin.Metadata(
-      mv=[1,9,0]
+      mv=[2,0,0]
       k=3
       xi=48
-      d1=["\u0000\n\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0003\u0010\u0000\u001a\u00020\u0001\"\u0006\b\u0000\u0010\u0002\u0018\u0001H\n¢\u0006\u0002\b\u0003¨\u0006\u0004"]
-      d2=["<anonymous>","","T","run","a/UtilsKt$example$1"]
-    )
-RuntimeInvisibleAnnotations:
-  0: #45(#46=[s#47])
-    kotlin.jvm.internal.SourceDebugExtension(
-      value=["SMAP\nutils.kt\nKotlin\n*S Kotlin\n*F\n+ 1 utils.kt\na/UtilsKt$example$1\n*L\n1#1,11:1\n*E\n"]
     )

which seem to be related to JetBrains/kotlin@e5ad035

https://github.com/JetBrains/kotlin/blob/v2.1.0/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/SMAP.kt#L133-L134

considers change of SourceFileName and full mapping onto the other fully qualified class name by SMAP as "trivial" so that

https://github.com/JetBrains/kotlin/blob/v2.1.0/compiler/backend/src/org/jetbrains/kotlin/codegen/AbstractClassBuilder.java#L151

does not write SMAP to class file.
To confirm - I tried to do the following modification in Kotlin compiler

index aa71688ea8d6..777f92572dd5 100644
--- i/compiler/backend/src/org/jetbrains/kotlin/codegen/AbstractClassBuilder.java
+++ w/compiler/backend/src/org/jetbrains/kotlin/codegen/AbstractClassBuilder.java
@@ -148,7 +148,7 @@ public abstract class AbstractClassBuilder implements ClassBuilder {

     @Override
     public void visitSMAP(@NotNull SourceMapper smap, boolean backwardsCompatibleSyntax) {
-        if (GENERATE_SMAP && !smap.isTrivial()) {
+        if (GENERATE_SMAP) {
             List<FileMapping> fileMappings = smap.getResultMappings();
             visitSource(fileMappings.get(0).getName(), SMAPBuilder.INSTANCE.build(fileMappings, backwardsCompatibleSyntax));
         } else {

and it restored generation of SMAP and coverage results.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions