Repository: JetBrains/kotlin
File: core/reflection.jvm/src/kotlin/reflect/jvm/internal/calls/InlineClassAwareCaller.kt (Kotlin)

Code before:
            }

            else -> 0
        }

        val kotlinParameterTypes: List<KotlinType> = ArrayList<KotlinType>().also { kotlinParameterTypes ->
            val extensionReceiverType = descriptor.extensionReceiverParameter?.type
            if (extensionReceiverType != null) {
                kotlinParameterTypes.add(extensionReceiverType)
            } else if (descriptor is ConstructorDescriptor) {
                val constructedClass = descriptor.constructedClass
                if (constructedClass.isInner) {
                    kotlinParameterTypes.add((constructedClass.containingDeclaration as ClassDescriptor).defaultType)
                }
            } else {
                val containingDeclaration = descriptor.containingDeclaration
                if (containingDeclaration is ClassDescriptor && containingDeclaration.isInlineClass()) {
                    kotlinParameterTypes.add(containingDeclaration.defaultType)
                }
            }

            descriptor.valueParameters.mapTo(kotlinParameterTypes, ValueParameterDescriptor::getType)
        }

        // If the default argument is set, ((paramSize / Int.SIZE_BITS) + 1) masks and one marker are added to the end of the argument.
        val extraArgumentsTail = (if (isDefault) (kotlinParameterTypes.size / Int.SIZE_BITS) + 2 else 0) +
                (if (descriptor is FunctionDescriptor && descriptor.isSuspend) 1 else 0)
        val expectedArgsSize = kotlinParameterTypes.size + shift + extraArgumentsTail
        if (arity != expectedArgsSize) {
            throw KotlinReflectionInternalError(
                "Inconsistent number of parameters in the descriptor and Java reflection object: $arity != $expectedArgsSize\n" +
                        "Calling: $descriptor\n" +
                        "Parameter types: ${this.parameterTypes})\n" +
                        "Default: $isDefault"
            )
        }

        // maxOf is needed because in case of a bound top level extension, shift can be -1 (see above). But in that case, we need not unbox
        // the extension receiver argument, since it has already been unboxed at compile time and generated into the reference
        val argumentRange = maxOf(shift, 0) until (kotlinParameterTypes.size + shift)

        val unbox = Array(expectedArgsSize) { i ->
            if (i in argumentRange) {
                kotlinParameterTypes[i - shift].toInlineClass()?.getUnboxMethod(descriptor)
            } else null
        }

        BoxUnboxData(argumentRange, unbox, box)
    }

    override fun call(args: Array<*>): Any? {

Review comment (bug):
The correct formula is `(kotlinParameterTypes.size + Int.SIZE_BITS - 1) / Int.SIZE_BITS` which means to round the division result up to the nearest integer:
https://github.com/JetBrains/kotlin/blob/d61e7db937499b0a26efacb8c10c5f92ec512aa2/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KDeclarationContainerImpl.kt#L236

With the current changes, `callBy` should fail for functions that take 32 * N parameters, for integer N. Please add a function with exactly 32 parameters to the test case.

Code after:
            }

            else -> 0
        }

        val kotlinParameterTypes: List<KotlinType> = ArrayList<KotlinType>().also { kotlinParameterTypes ->
            val extensionReceiverType = descriptor.extensionReceiverParameter?.type
            if (extensionReceiverType != null) {
                kotlinParameterTypes.add(extensionReceiverType)
            } else if (descriptor is ConstructorDescriptor) {
                val constructedClass = descriptor.constructedClass
                if (constructedClass.isInner) {
                    kotlinParameterTypes.add((constructedClass.containingDeclaration as ClassDescriptor).defaultType)
                }
            } else {
                val containingDeclaration = descriptor.containingDeclaration
                if (containingDeclaration is ClassDescriptor && containingDeclaration.isInlineClass()) {
                    kotlinParameterTypes.add(containingDeclaration.defaultType)
                }
            }

            descriptor.valueParameters.mapTo(kotlinParameterTypes, ValueParameterDescriptor::getType)
        }

        // If the default argument is set,
        // (kotlinParameterTypes.size + Integer.SIZE - 1) / Integer.SIZE masks and one marker are added to the end of the argument.
        val extraArgumentsTail = (if (isDefault) ((kotlinParameterTypes.size + Integer.SIZE - 1) / Integer.SIZE) + 1 else 0) +
                (if (descriptor is FunctionDescriptor && descriptor.isSuspend) 1 else 0)
        val expectedArgsSize = kotlinParameterTypes.size + shift + extraArgumentsTail
        if (arity != expectedArgsSize) {
            throw KotlinReflectionInternalError(
                "Inconsistent number of parameters in the descriptor and Java reflection object: $arity != $expectedArgsSize\n" +
                        "Calling: $descriptor\n" +
                        "Parameter types: ${this.parameterTypes})\n" +
                        "Default: $isDefault"
            )
        }

        // maxOf is needed because in case of a bound top level extension, shift can be -1 (see above). But in that case, we need not unbox
        // the extension receiver argument, since it has already been unboxed at compile time and generated into the reference
        val argumentRange = maxOf(shift, 0) until (kotlinParameterTypes.size + shift)

        val unbox = Array(expectedArgsSize) { i ->
            if (i in argumentRange) {
                kotlinParameterTypes[i - shift].toInlineClass()?.getUnboxMethod(descriptor)
            } else null
        }

        BoxUnboxData(argumentRange, unbox, box)
    }