diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java index 194aa8abe5f..0afb36cb513 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java @@ -445,6 +445,10 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { if (parent instanceof KtBinaryExpression || parent instanceof KtUnaryExpression) { return true; } + // Previously we've checked that there is no expected type, therefore cast in property has an effect on inference + if (parent instanceof KtProperty || parent instanceof KtPropertyAccessor) { + return true; + } return false; } diff --git a/compiler/testData/diagnostics/tests/Casts.kt b/compiler/testData/diagnostics/tests/Casts.kt index 7e320605db1..ce45e53f0ce 100644 --- a/compiler/testData/diagnostics/tests/Casts.kt +++ b/compiler/testData/diagnostics/tests/Casts.kt @@ -15,7 +15,7 @@ fun test() : Unit { checkSubtype(x as? Int?) checkSubtype(y as? Int?) - val s = "" as Any + val s = "" as Any ("" as String?)?.length (data@("" as String?))?.length (@MustBeDocumented()( "" as String?))?.length diff --git a/compiler/testData/diagnostics/tests/cast/AsInPropertyAndPropertyAccessor.kt b/compiler/testData/diagnostics/tests/cast/AsInPropertyAndPropertyAccessor.kt new file mode 100644 index 00000000000..426da743e89 --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/AsInPropertyAndPropertyAccessor.kt @@ -0,0 +1,17 @@ +// !DIAGNOSTICS: -UNUSED_VARIABLE + +fun test() { + val a = 1 as Any? + val b: Number = 1 as Number + val c = null as String? + val d: Number = 1 as Int +} + +val c1 get() = 1 as Number +val c2: Number get() = 1 as Number + +val d: Number + get() { + 1 as Number + return 1 as Number + } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/cast/AsInPropertyAndPropertyAccessor.txt b/compiler/testData/diagnostics/tests/cast/AsInPropertyAndPropertyAccessor.txt new file mode 100644 index 00000000000..1b5a3b0878f --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/AsInPropertyAndPropertyAccessor.txt @@ -0,0 +1,6 @@ +package + +public val c1: kotlin.Number +public val c2: kotlin.Number +public val d: kotlin.Number +public fun test(): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/cast/AsTypeAlias.kt b/compiler/testData/diagnostics/tests/cast/AsTypeAlias.kt index 321302d7f8d..6ae90b849fb 100644 --- a/compiler/testData/diagnostics/tests/cast/AsTypeAlias.kt +++ b/compiler/testData/diagnostics/tests/cast/AsTypeAlias.kt @@ -1,7 +1,7 @@ typealias MyString = String val x: MyString = "" -val y = x as Any +val y = x as Any interface Base class Derived : Base diff --git a/compiler/testData/diagnostics/tests/cast/bare/FromErrorType.kt b/compiler/testData/diagnostics/tests/cast/bare/FromErrorType.kt index 0a76ce0445c..edb31389f1b 100644 --- a/compiler/testData/diagnostics/tests/cast/bare/FromErrorType.kt +++ b/compiler/testData/diagnostics/tests/cast/bare/FromErrorType.kt @@ -3,6 +3,6 @@ class G fun foo(p: P) { - val v = p as G? + val v = p as G? checkSubtype>(v!!) } \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index d6e998c330b..976929a4ad5 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -2686,6 +2686,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("AsInPropertyAndPropertyAccessor.kt") + public void testAsInPropertyAndPropertyAccessor() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/AsInPropertyAndPropertyAccessor.kt"); + doTest(fileName); + } + @TestMetadata("AsNothing.kt") public void testAsNothing() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/AsNothing.kt");