// RUN_PIPELINE_TILL: FRONTEND
// LANGUAGE: +UnrestrictedBuilderInference
// DIAGNOSTICS: -DEPRECATION -UNCHECKED_CAST -OPT_IN_IS_NOT_ENABLED
// WITH_STDLIB
// ISSUE: KT-63996

// FILE: main.kt

fun <R> build(block: TestInterface<R>.() -> Unit): R = TODO()

interface TestInterface<R> {
    fun emit(r: R)
    fun getOut(): Inv<out R>
}

class Inv<T> {
}

fun <K> capture(x: Inv<K>): K = null as K
fun <I> id(x: I): I = null as I

fun main() {
    build {
        emit("")
        // K is fixed into CapturedType(out NotFixed: TypeVariable(R)
        <!CANNOT_INFER_PARAMETER_TYPE!>capture<!>(<!ARGUMENT_TYPE_MISMATCH!>id(getOut())<!>) // unexpected TYPE_MISMATCH (KT-63996)
        // capture(getOut()) // OK!!!
        Unit
    }
    build<String> {
        emit("")
        // K is fixed into CapturedType(out NotFixed: TypeVariable(R)
        capture(id(getOut())) // OK!!!
        // capture(getOut()) // OK!!!
        Unit
    }
}

/* GENERATED_FIR_TAGS: asExpression, capturedType, classDeclaration, functionDeclaration, functionalType,
interfaceDeclaration, lambdaLiteral, nullableType, outProjection, stringLiteral, typeParameter, typeWithExtension */
