Skip to content

Removal of default parameter is binary compatible but breaks runtime #216

@osipxd

Description

@osipxd

If we remove default value for one of default parameters in function, this change doesn't affect API dump, however it can lead to unexpected behaviour or NPE in runtime (at least on JVM).

Is it the intended behaviour that default parameters are not marked somewhat in API dump? Or is it out of scope of BCV responsibility as soon as binary compatibility is not broken?

Example:

// for this declaration
fun foo(intParam: Int = -1,  stringParam: String = "")

// and these calls
foo(intParam = 42)
foo(stringParam = "value")
// will be generated this synthetic method
public static void foo$default(Foo var0, int var1, String var2, int var3, Object var4) {
    if ((var3 & 1) != 0) {
        var1 = -1;
    }

    if ((var3 & 2) != 0) {
        var2 = "";
    }

    var0.foo(var1, var2);
}

// and the calls will be
foo$default(this, 42, (String)null, 2, (Object)null);
foo$default(this, 0, "value", 1, (Object)null);

So if we remove default value for intParam, function call foo$default(this, 0, "value", 1, (Object)null); will still be valid, however it will lead to unexpected behaviour. intParam will be 0 instead of -1.
In case we remove default value for stringParam, function call foo$default(this, 42, (String)null, 2, (Object)null); will still be valid, however it will lead to NPE due to intrinsic non-null check failure on stringParam.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions