K1: report swallowed diagnostic about receiver type mismatch

#KT-55056 Fixed
This commit is contained in:
Mikhail Glukhikh
2023-02-14 16:27:25 +01:00
committed by Space Team
parent 176325eaa7
commit bd27ec840c
21 changed files with 173 additions and 19 deletions
@@ -36604,6 +36604,18 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/completeIrrelevantCalls.kt");
}
@Test
@TestMetadata("inconsistentTypeInference.kt")
public void testInconsistentTypeInference() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference.kt");
}
@Test
@TestMetadata("inconsistentTypeInference2.kt")
public void testInconsistentTypeInference2() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference2.kt");
}
@Test
@TestMetadata("incorrectCalls.kt")
public void testIncorrectCalls() throws Exception {
@@ -36604,6 +36604,18 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/completeIrrelevantCalls.kt");
}
@Test
@TestMetadata("inconsistentTypeInference.kt")
public void testInconsistentTypeInference() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference.kt");
}
@Test
@TestMetadata("inconsistentTypeInference2.kt")
public void testInconsistentTypeInference2() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference2.kt");
}
@Test
@TestMetadata("incorrectCalls.kt")
public void testIncorrectCalls() throws Exception {
@@ -36604,6 +36604,18 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/completeIrrelevantCalls.kt");
}
@Test
@TestMetadata("inconsistentTypeInference.kt")
public void testInconsistentTypeInference() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference.kt");
}
@Test
@TestMetadata("inconsistentTypeInference2.kt")
public void testInconsistentTypeInference2() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference2.kt");
}
@Test
@TestMetadata("incorrectCalls.kt")
public void testIncorrectCalls() throws Exception {
@@ -36700,6 +36700,18 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/completeIrrelevantCalls.kt");
}
@Test
@TestMetadata("inconsistentTypeInference.kt")
public void testInconsistentTypeInference() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference.kt");
}
@Test
@TestMetadata("inconsistentTypeInference2.kt")
public void testInconsistentTypeInference2() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference2.kt");
}
@Test
@TestMetadata("incorrectCalls.kt")
public void testIncorrectCalls() throws Exception {
@@ -915,6 +915,7 @@ public interface Errors {
DiagnosticFactory2<PsiElement, KotlinType, KotlinType> TYPE_MISMATCH_WARNING_FOR_INCORRECT_CAPTURE_APPROXIMATION = DiagnosticFactory2.create(WARNING);
DiagnosticFactory2<PsiElement, KotlinType, KotlinType> RECEIVER_TYPE_MISMATCH_WARNING_FOR_INCORRECT_CAPTURE_APPROXIMATION = DiagnosticFactory2.create(WARNING);
DiagnosticFactory2<PsiElement, KotlinType, KotlinType> RECEIVER_TYPE_MISMATCH = DiagnosticFactory2.create(ERROR);
// Type inference
@@ -519,6 +519,11 @@ public class DefaultErrorMessages {
"Extension receiver type mismatch: inferred type is {1} but {0} was expected. This warning will be an error soon. See https://youtrack.jetbrains.com/issue/KT-49404 for details",
RENDER_TYPE, RENDER_TYPE
);
MAP.put(
RECEIVER_TYPE_MISMATCH,
"Constraint error in receiver type argument: inferred type is {1} but {0} was expected",
RENDER_TYPE, RENDER_TYPE
);
MAP.put(LOCAL_EXTENSION_PROPERTY, "Local extension properties are not allowed");
MAP.put(LOCAL_VARIABLE_WITH_GETTER, "Local variables are not allowed to have getters");
MAP.put(LOCAL_VARIABLE_WITH_SETTER, "Local variables are not allowed to have setters");
@@ -459,19 +459,22 @@ class DiagnosticReporterByTrackingStrategy(
is ArgumentConstraintPosition<*> -> {
reportArgumentConstraintErrorByPosition(
error, position.argument as KotlinCallArgument,
isWarning, typeMismatchDiagnostic, report
isWarning, typeMismatchDiagnostic,
kotlinCall = null, report
)
}
is ReceiverConstraintPosition<*> -> {
reportArgumentConstraintErrorByPosition(
error, position.argument as KotlinCallArgument,
isWarning, typeMismatchDiagnostic, report
isWarning, typeMismatchDiagnostic,
kotlinCall = (position as ReceiverConstraintPositionImpl).kotlinCall, report
)
}
is LambdaArgumentConstraintPosition<*> -> {
reportArgumentConstraintErrorByPosition(
error, (position.lambda as ResolvedLambdaAtom).atom,
isWarning, typeMismatchDiagnostic, report
isWarning, typeMismatchDiagnostic,
kotlinCall = null, report
)
}
is BuilderInferenceExpectedTypeConstraintPosition -> {
@@ -545,6 +548,7 @@ class DiagnosticReporterByTrackingStrategy(
argument: KotlinCallArgument,
isWarning: Boolean,
typeMismatchDiagnostic: DiagnosticFactory2<KtExpression, KotlinType, KotlinType>,
kotlinCall: KotlinCall?,
report: (Diagnostic) -> Unit
) {
if (argument is LambdaKotlinCallArgument) {
@@ -562,8 +566,15 @@ class DiagnosticReporterByTrackingStrategy(
}
}
// TODO: FIXME (KT-55056)
val expression = argument.psiExpression ?: return
val expression = argument.psiExpression ?: run {
val psiCall = (kotlinCall as? PSIKotlinCall)?.psiCall ?: psiKotlinCall.psiCall
report(
RECEIVER_TYPE_MISMATCH.on(
psiCall.calleeExpression ?: psiCall.callElement, error.upperKotlinType, error.lowerKotlinType
)
)
return
}
val deparenthesized = KtPsiUtil.safeDeparenthesize(expression)
if (reportConstantTypeMismatch(error, deparenthesized)) return
@@ -34,10 +34,11 @@ fun resolveKtPrimitive(
diagnosticsHolder: KotlinDiagnosticsHolder,
receiverInfo: ReceiverInfo,
convertedType: UnwrappedType?,
inferenceSession: InferenceSession?
inferenceSession: InferenceSession?,
kotlinCall: KotlinCall? = null,
): ResolvedAtom = when (argument) {
is SimpleKotlinCallArgument ->
checkSimpleArgument(csBuilder, argument, expectedType, diagnosticsHolder, receiverInfo, convertedType, inferenceSession)
checkSimpleArgument(csBuilder, argument, expectedType, diagnosticsHolder, receiverInfo, convertedType, inferenceSession, kotlinCall)
is LambdaKotlinCallArgument ->
preprocessLambdaArgument(csBuilder, argument, expectedType, diagnosticsHolder)
@@ -533,7 +533,8 @@ private fun ResolutionCandidate.resolveKotlinArgument(
this,
receiverInfo,
convertedArgument?.unknownIntegerType?.unwrap(),
inferenceSession
inferenceSession,
receiverInfo.selectorCall
)
addResolvedKtPrimitive(resolvedAtom)
@@ -647,7 +648,7 @@ private fun ResolutionCandidate.getReceiverArgumentWithConstraintIfCompatible(
val expectedTypeUnprepared = argument.getExpectedType(parameter, callComponents.languageVersionSettings)
val expectedType = prepareExpectedType(expectedTypeUnprepared)
val argumentType = captureFromTypeParameterUpperBoundIfNeeded(argument.receiver.stableType, expectedType)
val position = ReceiverConstraintPositionImpl(argument)
val position = ReceiverConstraintPositionImpl(argument, resolvedCall.atom)
return if (csBuilder.isSubtypeConstraintCompatible(argumentType, expectedType, position))
ApplicableContextReceiverArgumentWithConstraint(argument, argumentType, expectedType, position)
else null
@@ -732,7 +733,8 @@ internal object CheckReceivers : ResolutionPart() {
val receiverInfo = ReceiverInfo(
isReceiver = true,
shouldReportUnsafeCall = implicitInvokeState != ImplicitInvokeCheckStatus.UNSAFE_INVOKE_REPORTED,
reportUnsafeCallAsUnsafeImplicitInvoke = implicitInvokeState == ImplicitInvokeCheckStatus.INVOKE_ON_NOT_NULL_VARIABLE
reportUnsafeCallAsUnsafeImplicitInvoke = implicitInvokeState == ImplicitInvokeCheckStatus.INVOKE_ON_NOT_NULL_VARIABLE,
selectorCall = resolvedCall.atom
)
resolveKotlinArgument(receiverArgument, receiverParameter, receiverInfo)
@@ -35,6 +35,7 @@ class ReceiverInfo(
val isReceiver: Boolean,
val shouldReportUnsafeCall: Boolean, // should not report if unsafe implicit invoke has been reported already
val reportUnsafeCallAsUnsafeImplicitInvoke: Boolean,
val selectorCall: KotlinCall? = null,
) {
init {
assert(!reportUnsafeCallAsUnsafeImplicitInvoke || shouldReportUnsafeCall) { "Inconsistent receiver info" }
@@ -52,11 +53,15 @@ fun checkSimpleArgument(
diagnosticsHolder: KotlinDiagnosticsHolder,
receiverInfo: ReceiverInfo,
convertedType: UnwrappedType?,
inferenceSession: InferenceSession?
inferenceSession: InferenceSession?,
kotlinCall: KotlinCall?
): ResolvedAtom = when (argument) {
is ExpressionKotlinCallArgument -> checkExpressionArgument(csBuilder, argument, expectedType, diagnosticsHolder, receiverInfo.isReceiver, convertedType)
is SubKotlinCallArgument -> checkSubCallArgument(csBuilder, argument, expectedType, diagnosticsHolder, receiverInfo, inferenceSession)
else -> unexpectedArgument(argument)
is ExpressionKotlinCallArgument ->
checkExpressionArgument(csBuilder, argument, expectedType, diagnosticsHolder, receiverInfo.isReceiver, convertedType, kotlinCall)
is SubKotlinCallArgument ->
checkSubCallArgument(csBuilder, argument, expectedType, diagnosticsHolder, receiverInfo, inferenceSession)
else ->
unexpectedArgument(argument)
}
private fun checkExpressionArgument(
@@ -65,7 +70,8 @@ private fun checkExpressionArgument(
expectedType: UnwrappedType?,
diagnosticsHolder: KotlinDiagnosticsHolder,
isReceiver: Boolean,
convertedType: UnwrappedType?
convertedType: UnwrappedType?,
kotlinCall: KotlinCall?
): ResolvedAtom {
val resolvedExpression = ResolvedExpressionAtom(expressionArgument)
if (expectedType == null) return resolvedExpression
@@ -93,7 +99,9 @@ private fun checkExpressionArgument(
return null
}
val position = if (isReceiver) ReceiverConstraintPositionImpl(expressionArgument) else ArgumentConstraintPositionImpl(expressionArgument)
val position =
if (isReceiver) ReceiverConstraintPositionImpl(expressionArgument, kotlinCall)
else ArgumentConstraintPositionImpl(expressionArgument)
// Used only for arguments with @NotNull annotation
if (expectedType is NotNullTypeParameter && argumentType.isMarkedNullable) {
@@ -188,7 +196,9 @@ private fun checkSubCallArgument(
if (expectedType == null) return subCallResult
val expectedNullableType = expectedType.makeNullableAsSpecified(true)
val position = if (receiverInfo.isReceiver) ReceiverConstraintPositionImpl(subCallArgument) else ArgumentConstraintPositionImpl(subCallArgument)
val position =
if (receiverInfo.isReceiver) ReceiverConstraintPositionImpl(subCallArgument, subCallArgument.callResult.resultCallAtom.atom)
else ArgumentConstraintPositionImpl(subCallArgument)
// subArgument cannot has stable smartcast
// return type can contains fixed type variables
@@ -39,7 +39,10 @@ class ArgumentConstraintPositionImpl(argument: KotlinCallArgument) : ArgumentCon
class CallableReferenceConstraintPositionImpl(val callableReferenceCall: CallableReferenceKotlinCall) :
CallableReferenceConstraintPosition<CallableReferenceResolutionAtom>(callableReferenceCall)
class ReceiverConstraintPositionImpl(argument: KotlinCallArgument) : ReceiverConstraintPosition<KotlinCallArgument>(argument)
class ReceiverConstraintPositionImpl(
argument: KotlinCallArgument,
val kotlinCall: KotlinCall?
) : ReceiverConstraintPosition<KotlinCallArgument>(argument)
class FixVariableConstraintPositionImpl(
variable: TypeVariableMarker,
@@ -0,0 +1,4 @@
/inconsistentTypeInference.kt:7:17: error: constraint error in receiver type argument: inferred type is String but Int was expected
println(plus(1)[0])
^
@@ -0,0 +1,10 @@
// !RENDER_DIAGNOSTICS_FULL_TEXT
// FIR_DUMP
fun foo() {
buildList {
add("Boom")
println(plus(1)[0])
}
}
@@ -0,0 +1,8 @@
FILE: inconsistentTypeInference.fir.kt
public final fun foo(): R|kotlin/Unit| {
R|kotlin/collections/buildList|<R|kotlin/String|>(<L> = buildList@fun R|kotlin/collections/MutableList<kotlin/String>|.<anonymous>(): R|kotlin/Unit| <inline=Inline, kind=EXACTLY_ONCE> {
this@R|special/anonymous|.R|SubstitutionOverride<kotlin/collections/MutableList.add: R|kotlin/Boolean|>|(String(Boom))
R|kotlin/io/println|(this@R|special/anonymous|.R|kotlin/collections/plus|<R|kotlin/Int|>(Int(1)).R|SubstitutionOverride<kotlin/collections/List.get: R|kotlin/Int|>|(Int(0)))
}
)
}
@@ -0,0 +1,10 @@
// !RENDER_DIAGNOSTICS_FULL_TEXT
// FIR_DUMP
fun foo() {
buildList {
add("Boom")
println(<!RECEIVER_TYPE_MISMATCH!>plus<!>(1)[0])
}
}
@@ -0,0 +1,3 @@
/inconsistentTypeInference2.kt:7:17: error: type mismatch: inferred type is String but Int was expected
println(this.plus(1)[0])
^
@@ -0,0 +1,9 @@
// !RENDER_DIAGNOSTICS_FULL_TEXT
// FIR_DUMP
fun bar() {
buildList {
add("Boom")
println(this.plus(1)[0])
}
}
@@ -0,0 +1,8 @@
FILE: inconsistentTypeInference2.fir.kt
public final fun bar(): R|kotlin/Unit| {
R|kotlin/collections/buildList|<R|kotlin/String|>(<L> = buildList@fun R|kotlin/collections/MutableList<kotlin/String>|.<anonymous>(): R|kotlin/Unit| <inline=Inline, kind=EXACTLY_ONCE> {
this@R|special/anonymous|.R|SubstitutionOverride<kotlin/collections/MutableList.add: R|kotlin/Boolean|>|(String(Boom))
R|kotlin/io/println|(this@R|special/anonymous|.R|kotlin/collections/plus|<R|kotlin/Int|>(Int(1)).R|SubstitutionOverride<kotlin/collections/List.get: R|kotlin/Int|>|(Int(0)))
}
)
}
@@ -0,0 +1,9 @@
// !RENDER_DIAGNOSTICS_FULL_TEXT
// FIR_DUMP
fun bar() {
buildList {
add("Boom")
println(<!TYPE_MISMATCH!>this<!>.plus(1)[0])
}
}
@@ -52,7 +52,7 @@ val test6 = generate {
val test7 = generate {
yield(<!TYPE_MISMATCH!>"baz"<!>)
genericExtension<Int>()
<!RECEIVER_TYPE_MISMATCH!>genericExtension<!><Int>()
}
val test8 = generate {
@@ -37448,6 +37448,18 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/completeIrrelevantCalls.kt");
}
@Test
@TestMetadata("inconsistentTypeInference.kt")
public void testInconsistentTypeInference() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference.kt");
}
@Test
@TestMetadata("inconsistentTypeInference2.kt")
public void testInconsistentTypeInference2() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference2.kt");
}
@Test
@TestMetadata("incorrectCalls.kt")
public void testIncorrectCalls() throws Exception {