diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index 26fecc4b0cb..fdab662b4f1 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -14220,9 +14220,21 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag } @Test - @TestMetadata("kt47986.kt") - public void testKt47986() throws Exception { - runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986.kt"); + @TestMetadata("kt47986Default.kt") + public void testKt47986Default() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986Default.kt"); + } + + @Test + @TestMetadata("kt47986Disabled.kt") + public void testKt47986Disabled() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.kt"); + } + + @Test + @TestMetadata("kt47986Enabled.kt") + public void testKt47986Enabled() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986Enabled.kt"); } @Test diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 2de5921e43a..aaa45f61a06 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -14220,9 +14220,21 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti } @Test - @TestMetadata("kt47986.kt") - public void testKt47986() throws Exception { - runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986.kt"); + @TestMetadata("kt47986Default.kt") + public void testKt47986Default() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986Default.kt"); + } + + @Test + @TestMetadata("kt47986Disabled.kt") + public void testKt47986Disabled() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.kt"); + } + + @Test + @TestMetadata("kt47986Enabled.kt") + public void testKt47986Enabled() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986Enabled.kt"); } @Test diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index 417263fc525..dc473f2d5ac 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -14220,9 +14220,21 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac } @Test - @TestMetadata("kt47986.kt") - public void testKt47986() throws Exception { - runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986.kt"); + @TestMetadata("kt47986Default.kt") + public void testKt47986Default() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986Default.kt"); + } + + @Test + @TestMetadata("kt47986Disabled.kt") + public void testKt47986Disabled() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.kt"); + } + + @Test + @TestMetadata("kt47986Enabled.kt") + public void testKt47986Enabled() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986Enabled.kt"); } @Test diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index fc000e85849..ad493c62bc3 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -890,6 +890,7 @@ public interface Errors { DiagnosticFactory1 TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER = DiagnosticFactory1.create(ERROR); DiagnosticFactory1 NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER = DiagnosticFactory1.create(ERROR); + DiagnosticFactory1 INFERRED_INTO_DECLARED_UPPER_BOUNDS = DiagnosticFactory1.create(WARNING); DiagnosticFactory1 COULD_BE_INFERRED_ONLY_WITH_UNRESTRICTED_BUILDER_INFERENCE = DiagnosticFactory1.create(ERROR); DiagnosticFactory1 TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS = DiagnosticFactory1.create(ERROR); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java index 20cfb2a9388..5f445f0b28b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -959,6 +959,8 @@ public class DefaultErrorMessages { MAP.put(TYPE_INFERENCE_CANNOT_CAPTURE_TYPES, "Type inference failed: {0}", TYPE_INFERENCE_CANNOT_CAPTURE_TYPES_RENDERER); MAP.put(TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER, "Type inference failed: {0}", TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER_RENDERER); MAP.put(NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER, "Not enough information to infer type variable {0}", STRING); + MAP.put(INFERRED_INTO_DECLARED_UPPER_BOUNDS, "Type parameter for a type argument {0} can''t be inferred into declared upper bounds. " + + "Please provide any use-site type information. It will become an error in future releases.", STRING); MAP.put(COULD_BE_INFERRED_ONLY_WITH_UNRESTRICTED_BUILDER_INFERENCE, "Builder inference lambda contains inapplicable calls so {0} can''t be inferred. It could be resolved only with unrestricted builder inference. Please use -Xunrestricted-builder-inference compiler flag to enable it.", STRING); MAP.put(TYPE_INFERENCE_PARAMETER_CONSTRAINT_ERROR, "Type inference failed: {0}", TYPE_INFERENCE_PARAMETER_CONSTRAINT_ERROR_RENDERER); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt index a875767ab4a..33eee647e12 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt @@ -541,6 +541,22 @@ class DiagnosticReporterByTrackingStrategy( } } + InferredIntoDeclaredUpperBounds::class.java -> { + error as InferredIntoDeclaredUpperBounds + + val psiCall = psiKotlinCall.psiCall + val expression = if (psiCall is CallTransformer.CallForImplicitInvoke) { + psiCall.outerCall.calleeExpression + } else { + psiCall.calleeExpression + } ?: return + val typeVariable = error.typeVariable as? TypeVariableFromCallableDescriptor ?: return + + trace.reportDiagnosticOnce( + INFERRED_INTO_DECLARED_UPPER_BOUNDS.on(expression, typeVariable.originalTypeParameter.name.asString()) + ) + } + NotEnoughInformationForTypeParameterImpl::class.java -> { error as NotEnoughInformationForTypeParameterImpl diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceSession.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceSession.kt index fc285ce22ea..c73481754b0 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceSession.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceSession.kt @@ -6,6 +6,7 @@ package org.jetbrains.kotlin.resolve.calls.inference import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.ModuleDescriptor import org.jetbrains.kotlin.descriptors.impl.AnonymousFunctionDescriptor @@ -34,7 +35,6 @@ import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.checker.NewCapturedType import org.jetbrains.kotlin.types.expressions.DoubleColonExpressionResolver import org.jetbrains.kotlin.types.expressions.ExpressionTypingServices -import org.jetbrains.kotlin.types.model.freshTypeConstructor import org.jetbrains.kotlin.types.model.safeSubstitute import org.jetbrains.kotlin.types.typeUtil.asTypeProjection import org.jetbrains.kotlin.types.typeUtil.contains @@ -589,10 +589,15 @@ class BuilderInferenceSession( if (lowerSubstituted == a && upperSubstituted == b) return this - val resultingPosition = if (upperSubstituted == b && position is DeclaredUpperBoundConstraintPosition<*>) { + val isInferringIntoUpperBoundsForbidden = expressionTypingServices.languageVersionSettings.supportsFeature( + LanguageFeature.ForbidInferringPostponedTypeVariableIntoDeclaredUpperBound + ) + val isFromNotSubstitutedDeclaredUpperBound = upperSubstituted == b && position is DeclaredUpperBoundConstraintPosition<*> + + val resultingPosition = if (isFromNotSubstitutedDeclaredUpperBound && isInferringIntoUpperBoundsForbidden) { position } else { - BuilderInferenceSubstitutionConstraintPositionImpl(lambdaArgument, this) + BuilderInferenceSubstitutionConstraintPositionImpl(lambdaArgument, this, isFromNotSubstitutedDeclaredUpperBound) } return InitialConstraint( diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt index 7a8e61d2813..48546e47f7a 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt @@ -24,8 +24,11 @@ abstract class InjectedAnotherStubTypeConstraintPosition(private val builderI override fun toString(): String = "Injected from $builderInferenceLambdaOfInjectedStubType builder inference call" } -abstract class BuilderInferenceSubstitutionConstraintPosition(private val builderInferenceLambda: L, val initialConstraint: I) : - ConstraintPosition(), OnlyInputTypeConstraintPosition { +abstract class BuilderInferenceSubstitutionConstraintPosition( + private val builderInferenceLambda: L, + val initialConstraint: I, + val isFromNotSubstitutedDeclaredUpperBound: Boolean = false +) : ConstraintPosition(), OnlyInputTypeConstraintPosition { override fun toString(): String = "Incorporated builder inference constraint $initialConstraint " + "into $builderInferenceLambda call" } @@ -128,6 +131,8 @@ open class NotEnoughInformationForTypeParameter( val couldBeResolvedWithUnrestrictedBuilderInference: Boolean ) : ConstraintSystemError(INAPPLICABLE) +class InferredIntoDeclaredUpperBounds(val typeVariable: TypeVariableMarker) : ConstraintSystemError(RESOLVED) + class ConstrainingTypeIsError( val typeVariable: TypeVariableMarker, val constraintType: KotlinTypeMarker, diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt index 82af42268a3..55a36da9635 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt @@ -150,8 +150,11 @@ class KotlinConstraintSystemCompleter( continue // Stage 6: fix next ready type variable with proper constraints - if (fixNextReadyVariable(completionMode, topLevelAtoms, topLevelType, collectVariablesFromContext, postponedArguments)) - continue + if ( + fixNextReadyVariable( + completionMode, topLevelAtoms, topLevelType, collectVariablesFromContext, postponedArguments, diagnosticsHolder + ) + ) continue // Stage 7: try to complete call with the builder inference if there are uninferred type variables val areThereAppearedProperConstraintsForSomeVariable = tryToCompleteWithBuilderInference( @@ -272,6 +275,7 @@ class KotlinConstraintSystemCompleter( topLevelType: UnwrappedType, collectVariablesFromContext: Boolean, postponedArguments: List, + diagnosticsHolder: KotlinDiagnosticsHolder ): Boolean { val variableForFixation = variableFixationFinder.findFirstVariableForFixation( this, @@ -283,7 +287,7 @@ class KotlinConstraintSystemCompleter( if (!variableForFixation.hasProperConstraint) return false - fixVariable(this, notFixedTypeVariables.getValue(variableForFixation.variable), topLevelAtoms) + fixVariable(this, notFixedTypeVariables.getValue(variableForFixation.variable), topLevelAtoms, diagnosticsHolder) return true } @@ -405,20 +409,41 @@ class KotlinConstraintSystemCompleter( private fun fixVariable( c: ConstraintSystemCompletionContext, variableWithConstraints: VariableWithConstraints, - topLevelAtoms: List + topLevelAtoms: List, + diagnosticsHolder: KotlinDiagnosticsHolder ) { - fixVariable(c, variableWithConstraints, TypeVariableDirectionCalculator.ResolveDirection.UNKNOWN, topLevelAtoms) + fixVariable(c, variableWithConstraints, TypeVariableDirectionCalculator.ResolveDirection.UNKNOWN, topLevelAtoms, diagnosticsHolder) + } + + private fun reportWarningIfFixedIntoDeclaredUpperBounds( + diagnosticsHolder: KotlinDiagnosticsHolder, + variableWithConstraints: VariableWithConstraints + ) { + val areAllConstraintFromDeclaredUpperBounds = variableWithConstraints.constraints.all { + val position = it.position.from + position is BuilderInferenceSubstitutionConstraintPosition<*, *> && position.isFromNotSubstitutedDeclaredUpperBound + } + + if (areAllConstraintFromDeclaredUpperBounds) { + diagnosticsHolder.addDiagnostic( + KotlinConstraintSystemDiagnostic(InferredIntoDeclaredUpperBounds(variableWithConstraints.typeVariable)) + ) + } } private fun fixVariable( c: ConstraintSystemCompletionContext, variableWithConstraints: VariableWithConstraints, direction: TypeVariableDirectionCalculator.ResolveDirection, - topLevelAtoms: List + topLevelAtoms: List, + diagnosticsHolder: KotlinDiagnosticsHolder ) { val resultType = resultTypeResolver.findResultType(c, variableWithConstraints, direction) val variable = variableWithConstraints.typeVariable val resolvedAtom = findResolvedAtomBy(variable, topLevelAtoms) ?: topLevelAtoms.firstOrNull() + + reportWarningIfFixedIntoDeclaredUpperBounds(diagnosticsHolder, variableWithConstraints) + c.fixVariable(variable, resultType, FixVariableConstraintPositionImpl(variable, resolvedAtom)) } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrorsImpl.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrorsImpl.kt index 8cb7b3ea56c..ec4d70d3898 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrorsImpl.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrorsImpl.kt @@ -7,7 +7,6 @@ package org.jetbrains.kotlin.resolve.calls.inference.model import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.resolve.calls.model.* -import org.jetbrains.kotlin.resolve.scopes.receivers.DetailedReceiver import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.model.TypeVariableMarker @@ -19,9 +18,11 @@ class InjectedAnotherStubTypeConstraintPositionImpl(builderInferenceLambdaOfInje InjectedAnotherStubTypeConstraintPosition(builderInferenceLambdaOfInjectedStubType) class BuilderInferenceSubstitutionConstraintPositionImpl( - builderInferenceLambda: LambdaKotlinCallArgument, initialConstraint: InitialConstraint + builderInferenceLambda: LambdaKotlinCallArgument, + initialConstraint: InitialConstraint, + isFromNotSubstitutedDeclaredUpperBound: Boolean = false ) : BuilderInferenceSubstitutionConstraintPosition( - builderInferenceLambda, initialConstraint + builderInferenceLambda, initialConstraint, isFromNotSubstitutedDeclaredUpperBound ) class ExpectedTypeConstraintPositionImpl(topLevelCall: KotlinCall) : ExpectedTypeConstraintPosition(topLevelCall) diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Default.diag.txt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Default.diag.txt new file mode 100644 index 00000000000..b8ba78cd71b --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Default.diag.txt @@ -0,0 +1,12 @@ +/kt47986Default.kt:4:18: warning: parameter 'builderAction' is never used +fun buildFoo(builderAction: Foo.() -> Unit): Foo = Foo() + ^ +/kt47986Default.kt:6:20: warning: parameter 'x' is never used +fun Foo.bar(x: Int = 1) {} + ^ +/kt47986Default.kt:9:9: warning: variable 'x' is never used + val x = buildFoo { + ^ +/kt47986Default.kt:9:13: warning: type parameter for a type argument K can't be inferred into declared upper bounds. Please provide any use-site type information. It will become an error in future releases. + val x = buildFoo { + ^ diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986.kt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Default.fir.kt similarity index 86% rename from compiler/testData/diagnostics/tests/inference/builderInference/kt47986.kt rename to compiler/testData/diagnostics/tests/inference/builderInference/kt47986Default.fir.kt index 5544b6bfa77..8c003d5c18c 100644 --- a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986.kt +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Default.fir.kt @@ -1,4 +1,4 @@ -// FIR_IDENTICAL +// !RENDER_DIAGNOSTICS_FULL_TEXT class Foo fun buildFoo(builderAction: Foo.() -> Unit): Foo = Foo() @@ -9,4 +9,4 @@ fun main() { val x = buildFoo { bar() } -} +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Default.kt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Default.kt new file mode 100644 index 00000000000..267a0a94885 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Default.kt @@ -0,0 +1,12 @@ +// !RENDER_DIAGNOSTICS_FULL_TEXT +class Foo + +fun buildFoo(builderAction: Foo.() -> Unit): Foo = Foo() + +fun Foo.bar(x: Int = 1) {} + +fun main() { + val x = buildFoo { + bar() + } +} diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986.txt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Default.txt similarity index 100% rename from compiler/testData/diagnostics/tests/inference/builderInference/kt47986.txt rename to compiler/testData/diagnostics/tests/inference/builderInference/kt47986Default.txt diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.diag.txt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.diag.txt new file mode 100644 index 00000000000..026f85a9c3d --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.diag.txt @@ -0,0 +1,12 @@ +/kt47986Disabled.kt:5:18: warning: parameter 'builderAction' is never used +fun buildFoo(builderAction: Foo.() -> Unit): Foo = Foo() + ^ +/kt47986Disabled.kt:7:20: warning: parameter 'x' is never used +fun Foo.bar(x: Int = 1) {} + ^ +/kt47986Disabled.kt:10:9: warning: variable 'x' is never used + val x = buildFoo { + ^ +/kt47986Disabled.kt:10:13: warning: type parameter for a type argument K can't be inferred into declared upper bounds. Please provide any use-site type information. It will become an error in future releases. + val x = buildFoo { + ^ diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.fir.kt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.fir.kt new file mode 100644 index 00000000000..d895b2cd999 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.fir.kt @@ -0,0 +1,13 @@ +// !RENDER_DIAGNOSTICS_FULL_TEXT +// !LANGUAGE: -ForbidInferringPostponedTypeVariableIntoDeclaredUpperBound +class Foo + +fun buildFoo(builderAction: Foo.() -> Unit): Foo = Foo() + +fun Foo.bar(x: Int = 1) {} + +fun main() { + val x = buildFoo { + bar() + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.kt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.kt new file mode 100644 index 00000000000..82f812ee532 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.kt @@ -0,0 +1,13 @@ +// !RENDER_DIAGNOSTICS_FULL_TEXT +// !LANGUAGE: -ForbidInferringPostponedTypeVariableIntoDeclaredUpperBound +class Foo + +fun buildFoo(builderAction: Foo.() -> Unit): Foo = Foo() + +fun Foo.bar(x: Int = 1) {} + +fun main() { + val x = buildFoo { + bar() + } +} diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.txt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.txt new file mode 100644 index 00000000000..e524080b34f --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.txt @@ -0,0 +1,12 @@ +package + +public fun buildFoo(/*0*/ builderAction: Foo.() -> kotlin.Unit): Foo +public fun main(): kotlin.Unit +public fun Foo.bar(/*0*/ x: kotlin.Int = ...): kotlin.Unit + +public final class Foo { + public constructor Foo() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Enabled.kt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Enabled.kt new file mode 100644 index 00000000000..f83115cf0f8 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Enabled.kt @@ -0,0 +1,13 @@ +// FIR_IDENTICAL +// !LANGUAGE: +ForbidInferringPostponedTypeVariableIntoDeclaredUpperBound +class Foo + +fun buildFoo(builderAction: Foo.() -> Unit): Foo = Foo() + +fun Foo.bar(x: Int = 1) {} + +fun main() { + val x = buildFoo { + bar() + } +} diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Enabled.txt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Enabled.txt new file mode 100644 index 00000000000..e524080b34f --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986Enabled.txt @@ -0,0 +1,12 @@ +package + +public fun buildFoo(/*0*/ builderAction: Foo.() -> kotlin.Unit): Foo +public fun main(): kotlin.Unit +public fun Foo.bar(/*0*/ x: kotlin.Int = ...): kotlin.Unit + +public final class Foo { + public constructor Foo() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_2.diag.txt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_2.diag.txt new file mode 100644 index 00000000000..d2339039b6b --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_2.diag.txt @@ -0,0 +1,12 @@ +/kt47986_2.kt:4:18: warning: parameter 'builderAction' is never used +fun buildFoo(builderAction: Foo.() -> Unit): Foo = Foo() + ^ +/kt47986_2.kt:11:9: warning: variable 'x' is never used + val x = buildFoo { // can't infer + ^ +/kt47986_2.kt:11:13: warning: type parameter for a type argument K can't be inferred into declared upper bounds. Please provide any use-site type information. It will become an error in future releases. + val x = buildFoo { // can't infer + ^ +/kt47986_2.kt:12:13: warning: variable 'y' is never used + val y = id(::bar) + ^ diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_2.fir.kt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_2.fir.kt new file mode 100644 index 00000000000..c0ab24adb83 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_2.fir.kt @@ -0,0 +1,14 @@ +// !RENDER_DIAGNOSTICS_FULL_TEXT +class Foo + +fun buildFoo(builderAction: Foo.() -> Unit): Foo = Foo() + +fun Foo.bar() {} + +fun id(x: K) = x + +fun main() { + val x = buildFoo { // can't infer + val y = id(::bar) + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_2.kt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_2.kt index a8a7f7f46d1..882956d5593 100644 --- a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_2.kt +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_2.kt @@ -1,4 +1,4 @@ -// FIR_IDENTICAL +// !RENDER_DIAGNOSTICS_FULL_TEXT class Foo fun buildFoo(builderAction: Foo.() -> Unit): Foo = Foo() @@ -8,7 +8,7 @@ fun Foo.bar() {} fun id(x: K) = x fun main() { - val x = buildFoo { // can't infer + val x = buildFoo { // can't infer val y = id(::bar) } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_3.diag.txt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_3.diag.txt new file mode 100644 index 00000000000..5eefeaec1f0 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_3.diag.txt @@ -0,0 +1,12 @@ +/kt47986_3.kt:4:18: warning: parameter 'builderAction' is never used +fun buildFoo(builderAction: Foo.() -> Unit): Foo = Foo() + ^ +/kt47986_3.kt:6:26: warning: parameter 'x' is never used +fun Foo.bar(x: Int = 1) {} + ^ +/kt47986_3.kt:9:9: warning: variable 'x' is never used + val x = buildFoo { + ^ +/kt47986_3.kt:9:13: warning: type parameter for a type argument K can't be inferred into declared upper bounds. Please provide any use-site type information. It will become an error in future releases. + val x = buildFoo { + ^ diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_3.fir.kt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_3.fir.kt new file mode 100644 index 00000000000..2250acf7373 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_3.fir.kt @@ -0,0 +1,12 @@ +// !RENDER_DIAGNOSTICS_FULL_TEXT +class Foo + +fun buildFoo(builderAction: Foo.() -> Unit): Foo = Foo() + +fun Foo.bar(x: Int = 1) {} + +fun main() { + val x = buildFoo { + bar() + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_3.kt b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_3.kt index e096bb11f3e..7a422bd3566 100644 --- a/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_3.kt +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt47986_3.kt @@ -1,4 +1,4 @@ -// FIR_IDENTICAL +// !RENDER_DIAGNOSTICS_FULL_TEXT class Foo fun buildFoo(builderAction: Foo.() -> Unit): Foo = Foo() @@ -6,7 +6,7 @@ fun buildFoo(builderAction: Foo.() -> Unit): Foo = Foo() fun Foo.bar(x: Int = 1) {} fun main() { - val x = buildFoo { + val x = buildFoo { bar() } } diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt51464.diag.txt b/compiler/testData/diagnostics/tests/inference/builderInference/kt51464.diag.txt new file mode 100644 index 00000000000..73d249e66cd --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt51464.diag.txt @@ -0,0 +1,12 @@ +/kt51464.kt:2:16: warning: parameter 'value' is never used +fun flowOf(value: T): Flow = TODO() + ^ +/kt51464.kt:8:30: warning: parameter 'transform' is never used +fun Flow.transform(transform: FlowCollector.(T) -> Unit): Flow = TODO() + ^ +/kt51464.kt:11:20: warning: parameter 'collector' is never used + fun doEmit(collector: FlowCollector) {} + ^ +/kt51464.kt:12:15: warning: type parameter for a type argument R can't be inferred into declared upper bounds. Please provide any use-site type information. It will become an error in future releases. + flowOf(1).transform { doEmit(this) } + ^ diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt51464.fir.kt b/compiler/testData/diagnostics/tests/inference/builderInference/kt51464.fir.kt index 47d5840a1e8..82f0c40f1ea 100644 --- a/compiler/testData/diagnostics/tests/inference/builderInference/kt51464.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt51464.fir.kt @@ -1,5 +1,4 @@ -import kotlinx.coroutines.flow.transform - +// !RENDER_DIAGNOSTICS_FULL_TEXT fun flowOf(value: T): Flow = TODO() interface FlowCollector {} diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/kt51464.kt b/compiler/testData/diagnostics/tests/inference/builderInference/kt51464.kt index 50ede69999f..924100af61b 100644 --- a/compiler/testData/diagnostics/tests/inference/builderInference/kt51464.kt +++ b/compiler/testData/diagnostics/tests/inference/builderInference/kt51464.kt @@ -1,3 +1,4 @@ +// !RENDER_DIAGNOSTICS_FULL_TEXT fun flowOf(value: T): Flow = TODO() interface FlowCollector {} @@ -8,5 +9,5 @@ fun Flow.transform(transform: FlowCollector.(T) -> Unit): Flow = fun f() { fun doEmit(collector: FlowCollector) {} - flowOf(1).transform { doEmit(this) } + flowOf(1).transform { doEmit(this) } } \ No newline at end of file diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 9338cdc879e..4752f899a6e 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -14226,9 +14226,21 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { } @Test - @TestMetadata("kt47986.kt") - public void testKt47986() throws Exception { - runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986.kt"); + @TestMetadata("kt47986Default.kt") + public void testKt47986Default() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986Default.kt"); + } + + @Test + @TestMetadata("kt47986Disabled.kt") + public void testKt47986Disabled() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986Disabled.kt"); + } + + @Test + @TestMetadata("kt47986Enabled.kt") + public void testKt47986Enabled() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47986Enabled.kt"); } @Test diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 2578df1f24c..d5aa6be386c 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -278,6 +278,7 @@ enum class LanguageFeature( ReportErrorsOnRecursiveTypeInsidePlusAssignment(KOTLIN_1_9, kind = BUG_FIX), // KT-48546 ForbidInferringTypeVariablesIntoEmptyIntersection(KOTLIN_1_9, kind = BUG_FIX), // KT-51221 ForbidExtensionCallsOnInlineFunctionalParameters(KOTLIN_1_9, kind = BUG_FIX), // KT-52502 + ForbidInferringPostponedTypeVariableIntoDeclaredUpperBound(KOTLIN_1_9, kind = BUG_FIX), // KT-47986 // Disabled for indefinite time. See KT-48535 and related discussion ApproximateIntegerLiteralTypesInReceiverPosition(sinceVersion = null),