[FE 1.0] Don't report CAST_NEVER_SUCCEEDS on stub types

^KT-49829 Fixed
This commit is contained in:
Victor Petukhov
2021-12-17 10:29:14 +03:00
committed by teamcity
parent 37d163d417
commit 8e2b90b5db
10 changed files with 71 additions and 11 deletions
@@ -13944,6 +13944,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47744.kt");
}
@Test
@TestMetadata("kt49829.kt")
public void testKt49829() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt49829.kt");
}
@Test
@TestMetadata("labaledCall.kt")
public void testLabaledCall() throws Exception {
@@ -13944,6 +13944,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47744.kt");
}
@Test
@TestMetadata("kt49829.kt")
public void testKt49829() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt49829.kt");
}
@Test
@TestMetadata("labaledCall.kt")
public void testLabaledCall() throws Exception {
@@ -13944,6 +13944,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47744.kt");
}
@Test
@TestMetadata("kt49829.kt")
public void testKt49829() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt49829.kt");
}
@Test
@TestMetadata("labaledCall.kt")
public void testLabaledCall() throws Exception {
@@ -76,6 +76,7 @@ import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
import org.jetbrains.kotlin.types.expressions.ControlStructureTypingUtils.ResolveConstruct;
import org.jetbrains.kotlin.types.expressions.typeInfoFactory.TypeInfoFactoryKt;
import org.jetbrains.kotlin.types.expressions.unqualifiedSuper.UnqualifiedSuperKt;
import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt;
import org.jetbrains.kotlin.util.OperatorNameConventions;
import java.util.Collection;
@@ -372,7 +373,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
return;
}
if (!CastDiagnosticsUtil.isCastPossible(actualType, targetType, components.platformToKotlinClassMapper)) {
if (!CastDiagnosticsUtil.isCastPossible(actualType, targetType, components.platformToKotlinClassMapper) && !TypeUtilsKt.isStubType(actualType)) {
context.trace.report(CAST_NEVER_SUCCEEDS.on(expression.getOperationReference()));
return;
}
@@ -0,0 +1,12 @@
// WITH_STDLIB
// For FIR: see KT-50293
fun main() {
val list = buildList {
add("one")
add("two")
val secondParameter = get(1)
println(secondParameter <!USELESS_CAST!>as String<!>) // WARNING: [CAST_NEVER_SUCCEEDS] This cast can never succeed
}
}
@@ -0,0 +1,12 @@
// WITH_STDLIB
// For FIR: see KT-50293
fun main() {
val list = buildList {
add("one")
add("two")
val secondParameter = get(1)
println(secondParameter as String) // WARNING: [CAST_NEVER_SUCCEEDS] This cast can never succeed
}
}
@@ -0,0 +1,3 @@
package
public fun main(): kotlin.Unit
@@ -13950,6 +13950,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt47744.kt");
}
@Test
@TestMetadata("kt49829.kt")
public void testKt49829() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/builderInference/kt49829.kt");
}
@Test
@TestMetadata("labaledCall.kt")
public void testLabaledCall() throws Exception {
@@ -24,10 +24,8 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.inference.isCaptured
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.checker.NewCapturedType
import org.jetbrains.kotlin.types.checker.NewCapturedTypeConstructor
import org.jetbrains.kotlin.types.checker.intersectTypes
import org.jetbrains.kotlin.types.checker.*
import org.jetbrains.kotlin.types.model.SimpleTypeMarker
import org.jetbrains.kotlin.types.model.TypeArgumentMarker
import org.jetbrains.kotlin.types.model.TypeVariableTypeConstructorMarker
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@@ -391,3 +389,13 @@ private fun NewCapturedType.unCaptureTopLevelType(): UnwrappedType {
fun KotlinType?.shouldBeUpdated() =
this == null || contains { it is StubTypeForBuilderInference || it.constructor is TypeVariableTypeConstructorMarker || it.isError }
fun KotlinType.isStubType() = this is AbstractStubType || isDefNotNullStubType<AbstractStubType>()
fun KotlinType.isStubTypeForVariableInSubtyping(): Boolean =
this is StubTypeForTypeVariablesInSubtyping || isDefNotNullStubType<StubTypeForTypeVariablesInSubtyping>()
fun KotlinType.isStubTypeForBuilderInference(): Boolean =
this is StubTypeForBuilderInference || isDefNotNullStubType<StubTypeForBuilderInference>()
private inline fun <reified S : AbstractStubType> KotlinType.isDefNotNullStubType() = this is DefinitelyNotNullType && this.original is S
@@ -30,6 +30,9 @@ import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.kotlin.types.typeUtil.isSignedOrUnsignedNumberType as classicIsSignedOrUnsignedNumberType
import org.jetbrains.kotlin.types.typeUtil.isStubTypeForVariableInSubtyping as isSimpleTypeStubTypeForVariableInSubtyping
import org.jetbrains.kotlin.types.typeUtil.isStubTypeForBuilderInference as isSimpleTypeStubTypeForBuilderInference
import org.jetbrains.kotlin.types.typeUtil.isStubType as isSimpleTypeStubType
interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSystemCommonBackendContext {
override fun TypeConstructorMarker.isDenotable(): Boolean {
@@ -86,20 +89,17 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy
override fun SimpleTypeMarker.isStubType(): Boolean {
require(this is SimpleType, this::errorMessage)
return this is AbstractStubType || isDefNotNullStubType<AbstractStubType>()
return this.isSimpleTypeStubType()
}
private inline fun <reified S : AbstractStubType> SimpleTypeMarker.isDefNotNullStubType() =
this is DefinitelyNotNullType && this.original is S
override fun SimpleTypeMarker.isStubTypeForVariableInSubtyping(): Boolean {
require(this is SimpleType, this::errorMessage)
return this is StubTypeForTypeVariablesInSubtyping || isDefNotNullStubType<StubTypeForTypeVariablesInSubtyping>()
return this.isSimpleTypeStubTypeForVariableInSubtyping()
}
override fun SimpleTypeMarker.isStubTypeForBuilderInference(): Boolean {
require(this is SimpleType, this::errorMessage)
return this is StubTypeForBuilderInference || isDefNotNullStubType<StubTypeForBuilderInference>()
return this.isSimpleTypeStubTypeForBuilderInference()
}
override fun TypeConstructorMarker.unwrapStubTypeVariableConstructor(): TypeConstructorMarker {