// RUN_PIPELINE_TILL: BACKEND
// DIAGNOSTICS: -UNCHECKED_CAST

// CHECK_TYPE_WITH_EXACT

// ISSUE: KT-59551
// (also see an analogous codegen test)

class Buildee<CT> {
    fun yield(arg: CT) {}
    fun materialize(): CT = UserKlass() as CT
}

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

class UserKlass

// test 1: PTV is in consuming position (yield-case)
fun testYield() {
    val arg: UserKlass = UserKlass()
    val buildee = build(fun(it) {
        it.yield(arg)
    })
    checkExactType<Buildee<UserKlass>>(buildee)
}

// test 2: PTV is in producing position (materialize-case)
fun testMaterialize() {
    fun consume(arg: UserKlass) {}
    val buildee = build(fun(it) {
        consume(it.materialize())
    })
    checkExactType<Buildee<UserKlass>>(buildee)
}

/* GENERATED_FIR_TAGS: anonymousFunction, asExpression, classDeclaration, functionDeclaration, functionalType,
localFunction, localProperty, nullableType, propertyDeclaration, stringLiteral, typeParameter, typeWithExtension */
