diff --git a/compiler/fir/analysis-tests/testData/resolve/intersectionTypes.txt b/compiler/fir/analysis-tests/testData/resolve/intersectionTypes.txt index c1b24d926d3..cebe390f34a 100644 --- a/compiler/fir/analysis-tests/testData/resolve/intersectionTypes.txt +++ b/compiler/fir/analysis-tests/testData/resolve/intersectionTypes.txt @@ -16,12 +16,12 @@ FILE: intersectionTypes.kt } public final fun select(x: R|K|, y: R|K|): R|K| - public final fun test(): R|it(A & B)| { + public final fun test(): R|kotlin/Any| { ^test R|/select|(R|/Clazz1.Clazz1|(), R|/Clazz2.Clazz2|()) } public final fun makeNull(x: R|T|): R|T?| { ^makeNull Null(null) } - public final fun testNull(): R|it(A? & B?)| { + public final fun testNull(): R|kotlin/Any?| { ^testNull R|/makeNull|(R|/select|(R|/Clazz1.Clazz1|(), R|/Clazz2.Clazz2|())) } diff --git a/compiler/fir/analysis-tests/testData/resolve/whenElse.txt b/compiler/fir/analysis-tests/testData/resolve/whenElse.txt index f9154d38bee..a19e9f31eea 100644 --- a/compiler/fir/analysis-tests/testData/resolve/whenElse.txt +++ b/compiler/fir/analysis-tests/testData/resolve/whenElse.txt @@ -28,7 +28,7 @@ FILE: whenElse.kt public get(): R|B| } - public final fun get(f: R|kotlin/Boolean|): R|it(kotlin/Comparable & java/io/Serializable)| { + public final fun get(f: R|kotlin/Boolean|): R|kotlin/Any| { ^get when () { R|/f| -> { Q|A|.R|/A.A1| diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt index 8be6341dd05..c974a22b92e 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt @@ -7,6 +7,10 @@ package org.jetbrains.kotlin.fir.resolve.transformers.body.resolve import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.fir.* +import org.jetbrains.kotlin.fir.Visibilities.Internal +import org.jetbrains.kotlin.fir.Visibilities.Private +import org.jetbrains.kotlin.fir.Visibilities.Protected +import org.jetbrains.kotlin.fir.Visibilities.Public import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.builder.buildValueParameter import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyAccessor @@ -28,6 +32,7 @@ import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor import org.jetbrains.kotlin.fir.resolve.transformers.* import org.jetbrains.kotlin.fir.scopes.impl.FirLocalScope import org.jetbrains.kotlin.fir.scopes.impl.FirMemberTypeParameterScope +import org.jetbrains.kotlin.fir.scopes.impl.withReplacedConeType import org.jetbrains.kotlin.fir.symbols.constructStarProjectedType import org.jetbrains.kotlin.fir.symbols.impl.ConeClassLookupTagWithFixedSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol @@ -37,6 +42,7 @@ import org.jetbrains.kotlin.fir.types.builder.buildImplicitTypeRef import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef import org.jetbrains.kotlin.fir.visitors.* import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.types.TypeApproximatorConfiguration import org.jetbrains.kotlin.utils.addToStdlib.safeAs open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransformer) : FirPartialBodyResolveTransformer(transformer) { @@ -503,7 +509,7 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor result.transformReturnTypeRef( transformer, withExpectedType( - body.resultType.hideLocalTypeIfNeeded(simpleFunction?.visibility, simpleFunction?.isInline == true) + body.resultType.approximateTypeIfNeeded(simpleFunction?.visibility, simpleFunction?.isInline == true) ) ) } else { @@ -911,7 +917,7 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor } variable.transformReturnTypeRef( transformer, - withExpectedType(expectedType.hideLocalTypeIfNeeded((variable as? FirProperty)?.visibility)) + withExpectedType(expectedType.approximateTypeIfNeeded((variable as? FirProperty)?.visibility)) ) } variable.getter != null && variable.getter !is FirDefaultPropertyAccessor -> { @@ -933,7 +939,7 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor } variable.transformReturnTypeRef( transformer, - withExpectedType(expectedType?.hideLocalTypeIfNeeded((variable as? FirProperty)?.visibility)) + withExpectedType(expectedType?.approximateTypeIfNeeded((variable as? FirProperty)?.visibility)) ) } else -> { @@ -956,6 +962,34 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor } } + private fun FirTypeRef.approximateTypeIfNeeded( + containingCallableVisibility: Visibility?, + isInlineFunction: Boolean = false + ): FirTypeRef { + val approximatedType = if (this is FirResolvedTypeRef && + (containingCallableVisibility == Public || containingCallableVisibility == Protected) + ) { + when (this.type) { + is ConeIntegerLiteralType, + is ConeCapturedType, + is ConeDefinitelyNotNullType, + is ConeIntersectionType -> { + this.withReplacedConeType( + inferenceComponents.approximator.approximateToSuperType( + this.type, TypeApproximatorConfiguration.PublicDeclaration + ) as ConeKotlinType + ) + } + else -> { + this + } + } + } else { + this + } + return approximatedType.hideLocalTypeIfNeeded(containingCallableVisibility, isInlineFunction) + } + /* * Suppose a function without an explicit return type just returns an anonymous object: * @@ -976,10 +1010,10 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor } // Approximate types for non-private (all but package private or private) members. // Also private inline functions, as per KT-33917. - if (containingCallableVisibility == Visibilities.Public || - containingCallableVisibility == Visibilities.Protected || - containingCallableVisibility == Visibilities.Internal || - (containingCallableVisibility == Visibilities.Private && isInlineFunction) + if (containingCallableVisibility == Public || + containingCallableVisibility == Protected || + containingCallableVisibility == Internal || + (containingCallableVisibility == Private && isInlineFunction) ) { val firClass = (((this as? FirResolvedTypeRef) diff --git a/compiler/testData/codegen/box/javaInterop/notNullAssertions/enhancedNullability/inFunctionWithExpressionBodyWithJavaGeneric.kt b/compiler/testData/codegen/box/javaInterop/notNullAssertions/enhancedNullability/inFunctionWithExpressionBodyWithJavaGeneric.kt index 6a1a76f985f..e85957c8e19 100644 --- a/compiler/testData/codegen/box/javaInterop/notNullAssertions/enhancedNullability/inFunctionWithExpressionBodyWithJavaGeneric.kt +++ b/compiler/testData/codegen/box/javaInterop/notNullAssertions/enhancedNullability/inFunctionWithExpressionBodyWithJavaGeneric.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // !LANGUAGE: +StrictJavaNullabilityAssertions // TARGET_BACKEND: JVM // SKIP_JDK6 diff --git a/compiler/testData/codegen/box/lazyCodegen/ifElse.kt b/compiler/testData/codegen/box/lazyCodegen/ifElse.kt index c1c12b79d10..0f683860b24 100644 --- a/compiler/testData/codegen/box/lazyCodegen/ifElse.kt +++ b/compiler/testData/codegen/box/lazyCodegen/ifElse.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR class A (val p: String, p1: String, p2: String) { var cond1 :String = "" diff --git a/compiler/testData/diagnostics/tests/inference/publicApproximation/chainedLambdas.fir.kt b/compiler/testData/diagnostics/tests/inference/publicApproximation/chainedLambdas.fir.kt index 0059860e2bb..ee27bff3f91 100644 --- a/compiler/testData/diagnostics/tests/inference/publicApproximation/chainedLambdas.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/publicApproximation/chainedLambdas.fir.kt @@ -29,6 +29,6 @@ fun chained2(arg: First) = run { } fun test(arg: First) { - chained1(arg).first() - chained2(arg).first() + chained1(arg).first() + chained2(arg).first() } diff --git a/compiler/testData/diagnostics/tests/inference/publicApproximation/intersectionAfterSmartCastInLambdaReturn.fir.kt b/compiler/testData/diagnostics/tests/inference/publicApproximation/intersectionAfterSmartCastInLambdaReturn.fir.kt index 23cdcc38c3f..65380c4fab8 100644 --- a/compiler/testData/diagnostics/tests/inference/publicApproximation/intersectionAfterSmartCastInLambdaReturn.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/publicApproximation/intersectionAfterSmartCastInLambdaReturn.fir.kt @@ -33,6 +33,6 @@ fun intersectArgWithSmartCastFromLambda(arg: One, arg2: Base) = argOrFn(arg) { } fun test() { - intersectAfterSmartCast(O1, O2).base() - intersectArgWithSmartCastFromLambda(O1, O2).base() + intersectAfterSmartCast(O1, O2).base() + intersectArgWithSmartCastFromLambda(O1, O2).base() } diff --git a/compiler/testData/diagnostics/tests/inference/publicApproximation/smartCastInLambdaReturnAfterIntersection.fir.kt b/compiler/testData/diagnostics/tests/inference/publicApproximation/smartCastInLambdaReturnAfterIntersection.fir.kt index e03f91c740c..87713bd85f0 100644 --- a/compiler/testData/diagnostics/tests/inference/publicApproximation/smartCastInLambdaReturnAfterIntersection.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/publicApproximation/smartCastInLambdaReturnAfterIntersection.fir.kt @@ -18,5 +18,5 @@ fun smartCastAfterIntersection(a: One, b: Two) = run { } fun test(one: One, two: Two) { - smartCastAfterIntersection(one, two)?.base() + smartCastAfterIntersection(one, two)?.base() } diff --git a/compiler/testData/diagnostics/tests/inference/publicApproximation/twoIntersections.fir.kt b/compiler/testData/diagnostics/tests/inference/publicApproximation/twoIntersections.fir.kt index 61df54f8734..f67d9d8df08 100644 --- a/compiler/testData/diagnostics/tests/inference/publicApproximation/twoIntersections.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/publicApproximation/twoIntersections.fir.kt @@ -21,5 +21,5 @@ fun intersectNoBound(vararg elements: S): S = TODO() fun some(a: One, b: Two, c: Three) = intersectNoBound(intersect(a, b), c) fun test(arg: Base, arg2: Base) { - some(O1, O2, O3).base() + some(O1, O2, O3).base() } diff --git a/compiler/testData/ir/irText/types/intersectionType2_NI.fir.txt b/compiler/testData/ir/irText/types/intersectionType2_NI.fir.txt index 3af549d7502..cbd7ebc35fc 100644 --- a/compiler/testData/ir/irText/types/intersectionType2_NI.fir.txt +++ b/compiler/testData/ir/irText/types/intersectionType2_NI.fir.txt @@ -75,9 +75,9 @@ FILE fqName: fileName:/intersectionType2_NI.kt RETURN type=kotlin.Nothing from='public final fun run (fn: kotlin.Function0.run>): T of .run declared in ' CALL 'public abstract fun invoke (): R of kotlin.Function0 [operator] declared in kotlin.Function0' type=T of .run origin=INVOKE $this: GET_VAR 'fn: kotlin.Function0.run> declared in .run' type=kotlin.Function0.run> origin=null - FUN name:foo visibility:public modality:FINAL <> () returnType:.Foo + FUN name:foo visibility:public modality:FINAL <> () returnType:kotlin.Any BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun foo (): .Foo declared in ' + RETURN type=kotlin.Nothing from='public final fun foo (): kotlin.Any declared in ' CALL 'public final fun run (fn: kotlin.Function0.run>): T of .run declared in ' type=.Foo origin=null : .Foo fn: FUN_EXPR type=kotlin.Function0<.Foo> origin=LAMBDA diff --git a/compiler/testData/ir/irText/types/intersectionType2_OI.fir.txt b/compiler/testData/ir/irText/types/intersectionType2_OI.fir.txt index 1fb39afeaeb..e5b607616b1 100644 --- a/compiler/testData/ir/irText/types/intersectionType2_OI.fir.txt +++ b/compiler/testData/ir/irText/types/intersectionType2_OI.fir.txt @@ -75,9 +75,9 @@ FILE fqName: fileName:/intersectionType2_OI.kt RETURN type=kotlin.Nothing from='public final fun run (fn: kotlin.Function0.run>): T of .run declared in ' CALL 'public abstract fun invoke (): R of kotlin.Function0 [operator] declared in kotlin.Function0' type=T of .run origin=INVOKE $this: GET_VAR 'fn: kotlin.Function0.run> declared in .run' type=kotlin.Function0.run> origin=null - FUN name:foo visibility:public modality:FINAL <> () returnType:.Foo + FUN name:foo visibility:public modality:FINAL <> () returnType:kotlin.Any BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun foo (): .Foo declared in ' + RETURN type=kotlin.Nothing from='public final fun foo (): kotlin.Any declared in ' CALL 'public final fun run (fn: kotlin.Function0.run>): T of .run declared in ' type=.Foo origin=null : .Foo fn: FUN_EXPR type=kotlin.Function0<.Foo> origin=LAMBDA