Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Godin
Copy link
Member

@Godin Godin commented Nov 8, 2024

For

fun example1(s: String?) = when (s) {
    "a" -> "case a"
    "b" -> "case b"
    "c" -> "case c"
    else -> "else"
}

fun example2(s: String?) = when (s) {
    "a" -> "case a"
    "b" -> "case c"
    "c" -> "case c"
    null -> "null"
    else -> "else"
}

Kotlin compiler version 2.0.21 generates

  public static final java.lang.String example1(java.lang.String);
    descriptor: (Ljava/lang/String;)Ljava/lang/String;
    flags: (0x0019) ACC_PUBLIC, ACC_STATIC, ACC_FINAL
    Code:
      stack=2, locals=2, args_size=1
         0: aload_0
         1: astore_1
         2: aload_1
         3: ifnull        87
         6: aload_1
         7: invokevirtual #14                 // Method java/lang/String.hashCode:()I
        10: tableswitch   { // 97 to 99
                      97: 36
                      98: 48
                      99: 60
                 default: 87
            }
        36: aload_1
        37: ldc           #16                 // String a
        39: invokevirtual #20                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
        42: ifne          72
        45: goto          87
        48: aload_1
        49: ldc           #22                 // String b
        51: invokevirtual #20                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
        54: ifne          77
        57: goto          87
        60: aload_1
        61: ldc           #24                 // String c
        63: invokevirtual #20                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
        66: ifne          82
        69: goto          87
        72: ldc           #26                 // String case a
        74: goto          89
        77: ldc           #28                 // String case b
        79: goto          89
        82: ldc           #30                 // String case c
        84: goto          89
        87: ldc           #32                 // String else
        89: areturn
      LineNumberTable:
        line 1: 0
        line 2: 72
        line 3: 77
        line 4: 82
        line 5: 87
        line 6: 89

  public static final java.lang.String example2(java.lang.String);
    descriptor: (Ljava/lang/String;)Ljava/lang/String;
    flags: (0x0019) ACC_PUBLIC, ACC_STATIC, ACC_FINAL
    Code:
      stack=2, locals=2, args_size=1
         0: aload_0
         1: astore_1
         2: aload_1
         3: dup
         4: ifnull        87
         7: invokevirtual #14                 // Method java/lang/String.hashCode:()I
        10: tableswitch   { // 97 to 99
                      97: 36
                      98: 48
                      99: 60
                 default: 93
            }
        36: aload_1
        37: ldc           #16                 // String a
        39: invokevirtual #20                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
        42: ifne          72
        45: goto          93
        48: aload_1
        49: ldc           #22                 // String b
        51: invokevirtual #20                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
        54: ifne          77
        57: goto          93
        60: aload_1
        61: ldc           #24                 // String c
        63: invokevirtual #20                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
        66: ifne          82
        69: goto          93
        72: ldc           #26                 // String case a
        74: goto          95
        77: ldc           #28                 // String case b
        79: goto          95
        82: ldc           #30                 // String case c
        84: goto          95
        87: pop
        88: ldc           #37                 // String null
        90: goto          95
        93: ldc           #32                 // String else
        95: return
      LineNumberTable:
        line 8: 0
        line 9: 72
        line 10: 77
        line 11: 82
        line 12: 87
        line 13: 93
        line 14: 95

Fixes #1749

@Godin Godin self-assigned this Nov 8, 2024
@Godin Godin force-pushed the kotlin_when_expression_nullable_string branch 2 times, most recently from 48197ef to 54d284f Compare November 8, 2024 19:27
@Godin Godin force-pushed the kotlin_when_expression_nullable_string branch from 54d284f to f6fddc5 Compare November 15, 2024 22:04
@Godin Godin force-pushed the kotlin_when_expression_nullable_string branch from f6fddc5 to f88a5c6 Compare November 18, 2024 20:47
@Godin Godin added this to the 0.8.13 milestone Nov 24, 2024
@Godin Godin force-pushed the kotlin_when_expression_nullable_string branch from 4a02adb to 2895551 Compare December 18, 2024 14:28
@Godin Godin force-pushed the kotlin_when_expression_nullable_string branch from 2895551 to 3ae248c Compare December 18, 2024 14:36
@Godin Godin changed the title StringSwitchFilter should handle Kotlin nullable string StringSwitchFilter should handle Kotlin nullable String Dec 18, 2024
@Godin Godin marked this pull request as ready for review December 18, 2024 14:55
@Godin Godin requested a review from leveretka December 18, 2024 14:55
Copy link
Collaborator

@leveretka leveretka left a comment

Choose a reason for hiding this comment

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

Looks good!

@Godin Godin enabled auto-merge (squash) December 21, 2024 01:15
@Godin Godin merged commit e21a0b1 into jacoco:master Dec 21, 2024
34 checks passed
@Godin Godin deleted the kotlin_when_expression_nullable_string branch December 21, 2024 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Partial coverage in when expression for nullable String in Kotlin

2 participants