// ISSUE: KT-63840
// WITH_STDLIB

// IGNORE_BACKEND_K1: ANY
// REASON: red code (see corresponding diagnostic test)

fun box(): String {
    build {
        select(
            replaceTypeVariable(TargetType()),
            DifferentType()
        )
    }
    return "OK"
}




fun <T> select(vararg values: T): T = values.first()

class TargetType
class DifferentType

class Buildee<TV> {
    fun replaceTypeVariable(value: TV): TV { val temp = storage; storage = value; return temp }
    private var storage: TV = TargetType() as TV
}

fun <PTV> build(instructions: Buildee<PTV>.() -> Unit): Buildee<PTV> {
    return Buildee<PTV>().apply(instructions)
}
