Repository: JetBrains/kotlin
File: compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/JvmLateinitLowering.kt (Kotlin)

Code before:

        override fun visitGetValue(expression: IrGetValue): IrExpression {
            val irValue = expression.symbol.owner
            if (irValue !is IrVariable || !irValue.isLateinit) {
                return expression
            }

            return backendContext.createIrBuilder(
                (irValue.parent as IrSymbolOwner).symbol,
                expression.startOffset,
                expression.endOffset
            ).run {
                irIfThenElse(
                    expression.type,
                    irEqualsNull(irGet(irValue)),
                    backendContext.throwUninitializedPropertyAccessException(this, irValue.name.asString()),
                    irGet(irValue)
                )
            }
        }

        override fun visitGetField(expression: IrGetField): IrExpression {
            expression.transformChildrenVoid(this)
            val irField = expression.symbol.owner
            if (irField.isLateinitBackingField()) {
                irField.type = irField.type.makeNullable()
            }
            return expression
        }

        private fun IrField.isLateinitBackingField(): Boolean {
            val property = this.correspondingPropertySymbol?.owner
            return property != null && property.isRealLateinit()
        }

        private fun IrProperty.isRealLateinit() =
            isLateinit && !isFakeOverride

        override fun visitCall(expression: IrCall): IrExpression {
            expression.transformChildrenVoid(this)

            if (!Symbols.isLateinitIsInitializedPropertyGetter(expression.symbol)) return expression

            return expression.extensionReceiver!!.replaceTailExpression {
                val irPropertyRef = it as? IrPropertyReference
                    ?: throw AssertionError("Property reference expected: ${it.render()}")
                val property = irPropertyRef.getter?.owner?.resolveFakeOverride()?.correspondingPropertySymbol?.owner
                    ?: throw AssertionError("isInitialized cannot be invoked on ${it.render()}")
                require(property.isLateinit) {
                    "isInitialized invoked on non-lateinit property ${property.render()}"
                }

Review comment (suggestion):
I guess we need to change the type of `expression` here, not `irField`

Code after:
        }

        override fun visitGetValue(expression: IrGetValue): IrExpression {
            val irValue = expression.symbol.owner
            if (irValue !is IrVariable || !irValue.isLateinit) {
                return expression
            }

            return backendContext.createIrBuilder(
                (irValue.parent as IrSymbolOwner).symbol,
                expression.startOffset,
                expression.endOffset
            ).run {
                irIfThenElse(
                    expression.type,
                    irEqualsNull(irGet(irValue)),
                    backendContext.throwUninitializedPropertyAccessException(this, irValue.name.asString()),
                    irGet(irValue)
                )
            }
        }

        override fun visitGetField(expression: IrGetField): IrExpression {
            expression.transformChildrenVoid(this)
            val irField = expression.symbol.owner
            if (irField.isLateinitBackingField()) {
                expression.type = expression.type.makeNullable()
            }
            return expression
        }

        private fun IrField.isLateinitBackingField(): Boolean {
            val property = this.correspondingPropertySymbol?.owner
            return property != null && property.isRealLateinit()
        }

        private fun IrProperty.isRealLateinit() =
            isLateinit && !isFakeOverride

        override fun visitCall(expression: IrCall): IrExpression {
            expression.transformChildrenVoid(this)

            if (!Symbols.isLateinitIsInitializedPropertyGetter(expression.symbol)) return expression

            return expression.extensionReceiver!!.replaceTailExpression {
                val irPropertyRef = it as? IrPropertyReference
                    ?: throw AssertionError("Property reference expected: ${it.render()}")
                val property = irPropertyRef.getter?.owner?.resolveFakeOverride()?.correspondingPropertySymbol?.owner
                    ?: throw AssertionError("isInitialized cannot be invoked on ${it.render()}")
                require(property.isLateinit) {
                    "isInitialized invoked on non-lateinit property ${property.render()}"