diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt index 340b9122ad8..ad2274e51e0 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt @@ -309,7 +309,13 @@ open class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransform TransformData.Data(value) } block.transformOtherChildren(transformer, data) - block.writeResultType(session) + if (data is ResolutionMode.WithExpectedType && data.expectedTypeRef is FirResolvedTypeRef) { + // Top-down propagation: from the explicit type of the enclosing declaration to the block type + block.resultType = data.expectedTypeRef + } else { + // Bottom-up propagation: from the return type of the last expression in the block to the block type + block.writeResultType(session) + } dataFlowAnalyzer.exitBlock(block) } diff --git a/compiler/testData/ir/irText/expressions/coercionToUnit.fir.kt.txt b/compiler/testData/ir/irText/expressions/coercionToUnit.fir.kt.txt index ab06fa52ba3..2d878235e87 100644 --- a/compiler/testData/ir/irText/expressions/coercionToUnit.fir.kt.txt +++ b/compiler/testData/ir/irText/expressions/coercionToUnit.fir.kt.txt @@ -1,6 +1,6 @@ val test1: Function0 field = local fun (): Int { - return 42 + 42 /*~> Unit */ } get diff --git a/compiler/testData/ir/irText/expressions/coercionToUnit.fir.txt b/compiler/testData/ir/irText/expressions/coercionToUnit.fir.txt index 89ac7828f47..18e3854f53c 100644 --- a/compiler/testData/ir/irText/expressions/coercionToUnit.fir.txt +++ b/compiler/testData/ir/irText/expressions/coercionToUnit.fir.txt @@ -5,7 +5,7 @@ FILE fqName: fileName:/coercionToUnit.kt FUN_EXPR type=kotlin.Function0 origin=LAMBDA FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL <> () returnType:kotlin.Int BLOCK_BODY - RETURN type=kotlin.Nothing from='local final fun (): kotlin.Int declared in .test1' + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit CONST Int type=kotlin.Int value=42 FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> () returnType:kotlin.Function0 correspondingProperty: PROPERTY name:test1 visibility:public modality:FINAL [val] diff --git a/compiler/testData/ir/irText/firProblems/typeVariableAfterBuildMap.fir.kt.txt b/compiler/testData/ir/irText/firProblems/typeVariableAfterBuildMap.fir.kt.txt index cecf37e0998..208bad8f60e 100644 --- a/compiler/testData/ir/irText/firProblems/typeVariableAfterBuildMap.fir.kt.txt +++ b/compiler/testData/ir/irText/firProblems/typeVariableAfterBuildMap.fir.kt.txt @@ -167,7 +167,7 @@ object Visibilities { .put(key = Private, value = 0) /*~> Unit */ .put(key = Internal, value = 1) /*~> Unit */ .put(key = Protected, value = 1) /*~> Unit */ - return .put(key = Public, value = 2) /*~> Unit */ + .put(key = Public, value = 2) /*~> Unit */ } ) private get diff --git a/compiler/testData/ir/irText/firProblems/typeVariableAfterBuildMap.fir.txt b/compiler/testData/ir/irText/firProblems/typeVariableAfterBuildMap.fir.txt index 102f6689c32..c4ed0731a15 100644 --- a/compiler/testData/ir/irText/firProblems/typeVariableAfterBuildMap.fir.txt +++ b/compiler/testData/ir/irText/firProblems/typeVariableAfterBuildMap.fir.txt @@ -572,12 +572,11 @@ FILE fqName: fileName:/typeVariableAfterBuildMap.kt $this: GET_VAR ': kotlin.collections.MutableMap<.Visibility, kotlin.Int> declared in .Visibilities.ORDERED_VISIBILITIES.' type=kotlin.collections.MutableMap<.Visibility, kotlin.Int> origin=null key: GET_OBJECT 'CLASS OBJECT name:Protected modality:FINAL visibility:public superTypes:[.Visibility]' type=.Visibilities.Protected value: CONST Int type=kotlin.Int value=1 - RETURN type=kotlin.Nothing from='local final fun (): kotlin.Unit declared in .Visibilities.ORDERED_VISIBILITIES' - TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit - CALL 'public abstract fun put (key: K of kotlin.collections.MutableMap, value: V of kotlin.collections.MutableMap): V of kotlin.collections.MutableMap? declared in kotlin.collections.MutableMap' type=kotlin.Int? origin=null - $this: GET_VAR ': kotlin.collections.MutableMap<.Visibility, kotlin.Int> declared in .Visibilities.ORDERED_VISIBILITIES.' type=kotlin.collections.MutableMap<.Visibility, kotlin.Int> origin=null - key: GET_OBJECT 'CLASS OBJECT name:Public modality:FINAL visibility:public superTypes:[.Visibility]' type=.Visibilities.Public - value: CONST Int type=kotlin.Int value=2 + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + CALL 'public abstract fun put (key: K of kotlin.collections.MutableMap, value: V of kotlin.collections.MutableMap): V of kotlin.collections.MutableMap? declared in kotlin.collections.MutableMap' type=kotlin.Int? origin=null + $this: GET_VAR ': kotlin.collections.MutableMap<.Visibility, kotlin.Int> declared in .Visibilities.ORDERED_VISIBILITIES.' type=kotlin.collections.MutableMap<.Visibility, kotlin.Int> origin=null + key: GET_OBJECT 'CLASS OBJECT name:Public modality:FINAL visibility:public superTypes:[.Visibility]' type=.Visibilities.Public + value: CONST Int type=kotlin.Int value=2 FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:private modality:FINAL <> ($this:.Visibilities) returnType:kotlin.collections.Map<.Visibility, kotlin.Int> correspondingProperty: PROPERTY name:ORDERED_VISIBILITIES visibility:private modality:FINAL [val] $this: VALUE_PARAMETER name: type:.Visibilities diff --git a/compiler/testData/ir/irText/stubs/builtinMap.fir.kt.txt b/compiler/testData/ir/irText/stubs/builtinMap.fir.kt.txt index 98510fbd27f..1a2febaf1d0 100644 --- a/compiler/testData/ir/irText/stubs/builtinMap.fir.kt.txt +++ b/compiler/testData/ir/irText/stubs/builtinMap.fir.kt.txt @@ -2,7 +2,7 @@ fun Map.plus(pair: Pair): Map return when { .isEmpty() -> mapOf(pair = pair) else -> LinkedHashMap(p0 = ).apply>(block = local fun LinkedHashMap.() { - return .put(p0 = pair.(), p1 = pair.()) /*~> Unit */ + .put(p0 = pair.(), p1 = pair.()) /*~> Unit */ } ) } diff --git a/compiler/testData/ir/irText/stubs/builtinMap.fir.txt b/compiler/testData/ir/irText/stubs/builtinMap.fir.txt index 0216d629a41..2df3708de14 100644 --- a/compiler/testData/ir/irText/stubs/builtinMap.fir.txt +++ b/compiler/testData/ir/irText/stubs/builtinMap.fir.txt @@ -26,11 +26,10 @@ FILE fqName: fileName:/builtinMap.kt FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL <> ($receiver:java.util.LinkedHashMap.plus?, V1 of .plus?>) returnType:kotlin.Unit $receiver: VALUE_PARAMETER name: type:java.util.LinkedHashMap.plus?, V1 of .plus?> BLOCK_BODY - RETURN type=kotlin.Nothing from='local final fun (): kotlin.Unit declared in .plus' - TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit - CALL 'public open fun put (p0: @[FlexibleNullability] K of java.util.LinkedHashMap?, p1: @[FlexibleNullability] V of java.util.LinkedHashMap?): @[FlexibleNullability] V of java.util.LinkedHashMap? declared in java.util.LinkedHashMap' type=V1 of .plus? origin=null - $this: GET_VAR ': java.util.LinkedHashMap.plus?, V1 of .plus?> declared in .plus.' type=java.util.LinkedHashMap.plus?, V1 of .plus?> origin=null - p0: CALL 'public final fun (): A of kotlin.Pair declared in kotlin.Pair' type=K1 of .plus origin=GET_PROPERTY - $this: GET_VAR 'pair: kotlin.Pair.plus, V1 of .plus> declared in .plus' type=kotlin.Pair.plus, V1 of .plus> origin=null - p1: CALL 'public final fun (): B of kotlin.Pair declared in kotlin.Pair' type=V1 of .plus origin=GET_PROPERTY - $this: GET_VAR 'pair: kotlin.Pair.plus, V1 of .plus> declared in .plus' type=kotlin.Pair.plus, V1 of .plus> origin=null + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + CALL 'public open fun put (p0: @[FlexibleNullability] K of java.util.LinkedHashMap?, p1: @[FlexibleNullability] V of java.util.LinkedHashMap?): @[FlexibleNullability] V of java.util.LinkedHashMap? declared in java.util.LinkedHashMap' type=V1 of .plus? origin=null + $this: GET_VAR ': java.util.LinkedHashMap.plus?, V1 of .plus?> declared in .plus.' type=java.util.LinkedHashMap.plus?, V1 of .plus?> origin=null + p0: CALL 'public final fun (): A of kotlin.Pair declared in kotlin.Pair' type=K1 of .plus origin=GET_PROPERTY + $this: GET_VAR 'pair: kotlin.Pair.plus, V1 of .plus> declared in .plus' type=kotlin.Pair.plus, V1 of .plus> origin=null + p1: CALL 'public final fun (): B of kotlin.Pair declared in kotlin.Pair' type=V1 of .plus origin=GET_PROPERTY + $this: GET_VAR 'pair: kotlin.Pair.plus, V1 of .plus> declared in .plus' type=kotlin.Pair.plus, V1 of .plus> origin=null