Fix type inference issues for 'if' and 'when'.
Use 'expectedType' (when present) as an explicit type argument for a special construct call. Unfortunately, this approach can't be used for elvis due to other elvis-related inference hacks. Fixes KT-10807, KT-10811. This also affects KT-6189: now we can infer proper type for 'if'. If type inference for special call failed, and we found no type errors in sub-expressions, report TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT error. This (and the hack above) fixes KT-10809: code no longer compiles.
This commit is contained in:
@@ -528,6 +528,8 @@ public interface Errors {
|
||||
DiagnosticFactory1<PsiElement, InferenceErrorData> TYPE_INFERENCE_UPPER_BOUND_VIOLATED = DiagnosticFactory1.create(ERROR);
|
||||
DiagnosticFactory2<KtElement, KotlinType, KotlinType> TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH = DiagnosticFactory2.create(ERROR);
|
||||
|
||||
DiagnosticFactory0<KtExpression> TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT = DiagnosticFactory0.create(ERROR, SPECIAL_CONSTRUCT_TOKEN);
|
||||
|
||||
// Reflection
|
||||
|
||||
DiagnosticFactory1<KtExpression, CallableMemberDescriptor> EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED = DiagnosticFactory1.create(ERROR);
|
||||
|
||||
@@ -339,6 +339,16 @@ object PositioningStrategies {
|
||||
}
|
||||
}
|
||||
|
||||
@JvmField val SPECIAL_CONSTRUCT_TOKEN: PositioningStrategy<KtExpression> = object : PositioningStrategy<KtExpression>() {
|
||||
override fun mark(element: KtExpression): List<TextRange> =
|
||||
when (element) {
|
||||
is KtWhenExpression -> markElement(element.whenKeyword)
|
||||
is KtIfExpression -> markElement(element.ifKeyword)
|
||||
is KtOperationExpression -> markElement(element.operationReference)
|
||||
else -> error("Expression is not an if, when or operation expression: ${element.getElementTextWithContext()}")
|
||||
}
|
||||
}
|
||||
|
||||
@JvmField val NULLABLE_TYPE: PositioningStrategy<KtNullableType> = object : PositioningStrategy<KtNullableType>() {
|
||||
override fun mark(element: KtNullableType): List<TextRange> {
|
||||
return markNode(element.getQuestionMarkNode())
|
||||
|
||||
+2
@@ -658,6 +658,8 @@ public class DefaultErrorMessages {
|
||||
MAP.put(TYPE_INFERENCE_UPPER_BOUND_VIOLATED, "{0}", TYPE_INFERENCE_UPPER_BOUND_VIOLATED_RENDERER);
|
||||
MAP.put(TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH, "Type inference failed. Expected type mismatch: inferred type is {1} but {0} was expected", RENDER_TYPE, RENDER_TYPE);
|
||||
|
||||
MAP.put(TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT, "Type inference for control flow expression failed. Please specify its type explicitly.");
|
||||
|
||||
MAP.put(WRONG_NUMBER_OF_TYPE_ARGUMENTS, "{0,choice,0#No type arguments|1#Type argument|1<{0,number,integer} type arguments} expected", (Renderer) null);
|
||||
MAP.put(NO_TYPE_ARGUMENTS_ON_RHS, "{0,choice,0#No type arguments|1#Type argument|1<{0,number,integer} type arguments} expected. " +
|
||||
"Use ''{1}'' if you don''t want to pass type arguments", null, STRING);
|
||||
|
||||
+8
@@ -51,6 +51,14 @@ public class ResolutionCandidate<D extends CallableDescriptor> {
|
||||
return new ResolutionCandidate<D>(call, descriptor, null, null, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER, null);
|
||||
}
|
||||
|
||||
public static <D extends CallableDescriptor> ResolutionCandidate<D> create(
|
||||
@NotNull Call call, @NotNull D descriptor, @Nullable TypeSubstitutor knownTypeParametersResultingSubstitutor
|
||||
) {
|
||||
return new ResolutionCandidate<D>(call, descriptor,
|
||||
null, null, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER,
|
||||
knownTypeParametersResultingSubstitutor);
|
||||
}
|
||||
|
||||
public static <D extends CallableDescriptor> ResolutionCandidate<D> create(
|
||||
@NotNull Call call, @NotNull D descriptor, @Nullable ReceiverValue dispatchReceiver,
|
||||
@Nullable Receiver receiverArgument, @NotNull ExplicitReceiverKind explicitReceiverKind,
|
||||
|
||||
+35
-5
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.types.expressions;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.intellij.lang.ASTNode;
|
||||
@@ -24,6 +25,7 @@ import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
|
||||
import org.jetbrains.kotlin.descriptors.*;
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
|
||||
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl;
|
||||
@@ -50,13 +52,12 @@ import org.jetbrains.kotlin.resolve.calls.tasks.TracingStrategy;
|
||||
import org.jetbrains.kotlin.resolve.calls.util.CallMaker;
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.AnnotationsForResolveKt;
|
||||
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
|
||||
import org.jetbrains.kotlin.types.KotlinType;
|
||||
import org.jetbrains.kotlin.types.TypeUtils;
|
||||
import org.jetbrains.kotlin.types.Variance;
|
||||
import org.jetbrains.kotlin.types.*;
|
||||
import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.jetbrains.kotlin.diagnostics.Errors.TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT;
|
||||
import static org.jetbrains.kotlin.resolve.BindingContext.CALL;
|
||||
import static org.jetbrains.kotlin.resolve.BindingContext.RESOLVED_CALL;
|
||||
import static org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind.EXPECTED_TYPE_POSITION;
|
||||
@@ -103,13 +104,34 @@ public class ControlStructureTypingUtils {
|
||||
SimpleFunctionDescriptorImpl function = createFunctionDescriptorForSpecialConstruction(
|
||||
construct, argumentNames, isArgumentNullable);
|
||||
TracingStrategy tracing = createTracingForSpecialConstruction(call, construct.getName(), context);
|
||||
ResolutionCandidate<CallableDescriptor> resolutionCandidate = ResolutionCandidate.<CallableDescriptor>create(call, function);
|
||||
TypeSubstitutor knownTypeParameterSubstitutor = createKnownTypeParameterSubstitutorForSpecialCall(construct, function, context.expectedType);
|
||||
ResolutionCandidate<CallableDescriptor> resolutionCandidate =
|
||||
ResolutionCandidate.<CallableDescriptor>create(call, function, knownTypeParameterSubstitutor);
|
||||
OverloadResolutionResults<FunctionDescriptor> results = callResolver.resolveCallWithKnownCandidate(
|
||||
call, tracing, context, resolutionCandidate, dataFlowInfoForArguments);
|
||||
assert results.isSingleResult() : "Not single result after resolving one known candidate";
|
||||
return results.getResultingCall();
|
||||
}
|
||||
|
||||
private static @Nullable TypeSubstitutor createKnownTypeParameterSubstitutorForSpecialCall(
|
||||
@NotNull ResolveConstruct construct,
|
||||
@NotNull SimpleFunctionDescriptorImpl function,
|
||||
@NotNull KotlinType expectedType
|
||||
) {
|
||||
if (construct == ResolveConstruct.ELVIS
|
||||
|| TypeUtils.noExpectedType(expectedType)
|
||||
|| TypeUtils.isDontCarePlaceholder(expectedType)
|
||||
|| KotlinBuiltIns.isUnitOrNullableUnit(expectedType)
|
||||
|| KotlinBuiltIns.isAnyOrNullableAny(expectedType)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
TypeConstructor typeParameterConstructor = function.getTypeParameters().get(0).getTypeConstructor();
|
||||
TypeProjection typeProjection = new TypeProjectionImpl(expectedType);
|
||||
return TypeSubstitutor.create(ImmutableMap.of(typeParameterConstructor, typeProjection));
|
||||
}
|
||||
|
||||
private SimpleFunctionDescriptorImpl createFunctionDescriptorForSpecialConstruction(
|
||||
@NotNull ResolveConstruct construct,
|
||||
@NotNull List<String> argumentNames,
|
||||
@@ -426,7 +448,15 @@ public class ControlStructureTypingUtils {
|
||||
KtExpression expression = (KtExpression) call.getCallElement();
|
||||
if (status.hasOnlyErrorsDerivedFrom(EXPECTED_TYPE_POSITION) || status.hasConflictingConstraints()
|
||||
|| status.hasTypeInferenceIncorporationError()) { // todo after KT-... remove this line
|
||||
expression.accept(checkTypeVisitor, new CheckTypeContext(context.trace, data.expectedType));
|
||||
if (Boolean.TRUE != expression.accept(checkTypeVisitor, new CheckTypeContext(context.trace, data.expectedType))) {
|
||||
KtExpression calleeExpression = call.getCalleeExpression();
|
||||
if (calleeExpression instanceof KtWhenExpression || calleeExpression instanceof KtIfExpression) {
|
||||
if (status.hasConflictingConstraints() || status.hasTypeInferenceIncorporationError()) {
|
||||
// TODO provide comprehensible error report for hasConflictingConstraints() case (if possible)
|
||||
context.trace.report(TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT.on(expression));
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
KtDeclaration parentDeclaration = PsiTreeUtil.getParentOfType(expression, KtNamedDeclaration.class);
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
// FILE: J.java
|
||||
import java.util.*;
|
||||
|
||||
public class J {
|
||||
public static String s = null;
|
||||
public static Map<String, String> m = null;
|
||||
}
|
||||
|
||||
// FILE: k.kt
|
||||
|
||||
val testImplicitExclExcl1: String = J.s
|
||||
val testImplicitExclExcl2: String? = J.s
|
||||
|
||||
val testImplicitExclExcl3: String = <!TYPE_MISMATCH!>J.m[""]<!>
|
||||
val testImplicitExclExcl4: String? = J.m[""]
|
||||
|
||||
val testExclExcl1: String = J.s!!
|
||||
val testExclExcl2: String? = J.s!!
|
||||
|
||||
val testExclExcl3: String = J.m[""]!!
|
||||
val testExclExcl4: String? = J.m[""]!!
|
||||
|
||||
val testSafeCall1: String = <!TYPE_MISMATCH!>J.s?.let { it }<!>
|
||||
val testSafeCall2: String? = J.s?.let { it }
|
||||
|
||||
val testSafeCall3: String = <!TYPE_MISMATCH!>J.m[""]?.let { it }<!>
|
||||
val testSafeCall4: String? = J.m[""]?.let { it.toString() }
|
||||
|
||||
val testIf1: String = if (true) J.s else J.s
|
||||
val testIf2: String? = if (true) J.s else J.s
|
||||
|
||||
val testIf3: String = if (true) <!TYPE_MISMATCH!>J.m[""]<!> else <!TYPE_MISMATCH!>J.m[""]<!>
|
||||
val testIf4: String? = if (true) J.m[""] else J.m[""]
|
||||
|
||||
val testWhen1: String = when { else -> J.s }
|
||||
val testWhen2: String? = when { else -> J.s }
|
||||
|
||||
val testWhen3: String = when { else -> <!TYPE_MISMATCH!>J.m[""]<!> }
|
||||
val testWhen4: String? = when { else -> J.m[""] }
|
||||
Vendored
+33
@@ -0,0 +1,33 @@
|
||||
package
|
||||
|
||||
public val testExclExcl1: kotlin.String
|
||||
public val testExclExcl2: kotlin.String?
|
||||
public val testExclExcl3: kotlin.String
|
||||
public val testExclExcl4: kotlin.String?
|
||||
public val testIf1: kotlin.String
|
||||
public val testIf2: kotlin.String?
|
||||
public val testIf3: kotlin.String
|
||||
public val testIf4: kotlin.String?
|
||||
public val testImplicitExclExcl1: kotlin.String
|
||||
public val testImplicitExclExcl2: kotlin.String?
|
||||
public val testImplicitExclExcl3: kotlin.String
|
||||
public val testImplicitExclExcl4: kotlin.String?
|
||||
public val testSafeCall1: kotlin.String
|
||||
public val testSafeCall2: kotlin.String?
|
||||
public val testSafeCall3: kotlin.String
|
||||
public val testSafeCall4: kotlin.String?
|
||||
public val testWhen1: kotlin.String
|
||||
public val testWhen2: kotlin.String?
|
||||
public val testWhen3: kotlin.String
|
||||
public val testWhen4: kotlin.String?
|
||||
|
||||
public open class J {
|
||||
public constructor J()
|
||||
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
|
||||
|
||||
// Static members
|
||||
public final var m: kotlin.collections.(Mutable)Map<kotlin.String!, kotlin.String!>!
|
||||
public final var s: kotlin.String!
|
||||
}
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
val ns: String? = null
|
||||
|
||||
val testElvis1: String? = ns ?: ""
|
||||
val testElvis2: String = run { ns ?: "" }
|
||||
val testElvis3: String? = run { ns ?: "" }
|
||||
|
||||
val testIf1: String? = if (true) "" else ""
|
||||
val testIf2: String? = run { if (true) "" else "" }
|
||||
val testIf3: String? = if (true) run { "" } else ""
|
||||
val testIf4: String? = run { run { if (true) "" else "" } }
|
||||
val testIf5: String? = run { if (true) run { "" } else "" }
|
||||
|
||||
val testWhen1: String? = when { else -> "" }
|
||||
val testWhen2: String? = run { when { else -> "" } }
|
||||
val testWhen3: String? = when { else -> run { "" } }
|
||||
val testWhen4: String? = run { run { when { else -> "" } } }
|
||||
val testWhen5: String? = run { when { else -> run { "" } } }
|
||||
|
||||
val testExcl1: String? = run { ns!! }
|
||||
val testExcl2: String? = run { run { ns!! } }
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
package
|
||||
|
||||
public val ns: kotlin.String? = null
|
||||
public val testElvis1: kotlin.String?
|
||||
public val testElvis2: kotlin.String
|
||||
public val testElvis3: kotlin.String?
|
||||
public val testExcl1: kotlin.String?
|
||||
public val testExcl2: kotlin.String?
|
||||
public val testIf1: kotlin.String?
|
||||
public val testIf2: kotlin.String?
|
||||
public val testIf3: kotlin.String?
|
||||
public val testIf4: kotlin.String?
|
||||
public val testIf5: kotlin.String?
|
||||
public val testWhen1: kotlin.String?
|
||||
public val testWhen2: kotlin.String?
|
||||
public val testWhen3: kotlin.String?
|
||||
public val testWhen4: kotlin.String?
|
||||
public val testWhen5: kotlin.String?
|
||||
+2
-2
@@ -181,8 +181,8 @@ fun returnFunctionLiteral(a: Any?): Function0<Int> {
|
||||
else return { -> 1 }
|
||||
}
|
||||
|
||||
fun returnFunctionLiteralDoesntWork(a: Any?): Function0<Int> =
|
||||
if (a is Int) <!TYPE_MISMATCH!>{ -> a }<!>
|
||||
fun returnFunctionLiteralExpressionBody(a: Any?): Function0<Int> =
|
||||
if (a is Int) { -> <!DEBUG_INFO_SMARTCAST!>a<!> }
|
||||
else { -> 1 }
|
||||
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ public fun illegalWhenBody(/*0*/ a: kotlin.Any): kotlin.Int
|
||||
public fun mergeSmartCasts(/*0*/ a: kotlin.Any?): kotlin.Unit
|
||||
public fun returnFunctionLiteral(/*0*/ a: kotlin.Any?): () -> kotlin.Int
|
||||
public fun returnFunctionLiteralBlock(/*0*/ a: kotlin.Any?): () -> kotlin.Int
|
||||
public fun returnFunctionLiteralDoesntWork(/*0*/ a: kotlin.Any?): () -> kotlin.Int
|
||||
public fun returnFunctionLiteralExpressionBody(/*0*/ a: kotlin.Any?): () -> kotlin.Int
|
||||
public fun toInt(/*0*/ i: kotlin.Int?): kotlin.Int
|
||||
public fun vars(/*0*/ a: kotlin.Any?): kotlin.Unit
|
||||
|
||||
|
||||
+15
-3
@@ -8,9 +8,21 @@ interface D: A, B
|
||||
interface E: A, B
|
||||
|
||||
fun foo(c: C?, d: D?, e: E?) {
|
||||
val a: A? = <!TYPE_MISMATCH!>c ?: d<!> ?: e
|
||||
val test1: A? = <!TYPE_MISMATCH!>c ?: d<!> ?: e
|
||||
|
||||
val b: B? = if (false) <!TYPE_MISMATCH!>if (true) c else d<!> else e
|
||||
val test2: B? = if (false) if (true) c else d else e
|
||||
|
||||
//outer elvis operator and if-expression have error types
|
||||
val test3: A? = when {
|
||||
true -> c
|
||||
else -> when {
|
||||
true -> d
|
||||
else -> e
|
||||
}
|
||||
}
|
||||
|
||||
val test4: B? = when (1) {
|
||||
1 -> c
|
||||
2 -> d
|
||||
else -> e
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER -DEBUG_INFO_SMARTCAST
|
||||
|
||||
interface Data
|
||||
interface Item
|
||||
class FlagData(val value: Boolean) : Data
|
||||
class ListData<T : Item>(val list: List<T>) : Data
|
||||
|
||||
fun <T> listOf(vararg items: T): List<T> = null!!
|
||||
|
||||
fun test1(o: Any) = <!TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT!>when<!> (o) {
|
||||
is List<*> ->
|
||||
ListData(listOf())
|
||||
is Int -> when {
|
||||
o < 0 ->
|
||||
FlagData(true)
|
||||
else ->
|
||||
null
|
||||
}
|
||||
else ->
|
||||
null
|
||||
}
|
||||
|
||||
fun test1x(o: Any): Data? = when (o) {
|
||||
is List<*> ->
|
||||
ListData(listOf())
|
||||
is Int -> when {
|
||||
o < 0 ->
|
||||
FlagData(true)
|
||||
else ->
|
||||
null
|
||||
}
|
||||
else ->
|
||||
null
|
||||
}
|
||||
|
||||
fun test2() =
|
||||
<!TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT!>if<!> (true)
|
||||
ListData(listOf())
|
||||
else
|
||||
FlagData(true)
|
||||
|
||||
fun test2x(): Data =
|
||||
if (true) ListData(listOf()) else FlagData(true)
|
||||
|
||||
fun test2y(): Any =
|
||||
if (true) ListData(listOf()) else FlagData(true)
|
||||
|
||||
fun test2z(): Any =
|
||||
run { if (true) ListData(listOf()) else FlagData(true) }
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package
|
||||
|
||||
public fun </*0*/ T> listOf(/*0*/ vararg items: T /*kotlin.Array<out T>*/): kotlin.collections.List<T>
|
||||
public fun test1(/*0*/ o: kotlin.Any): ???
|
||||
public fun test1x(/*0*/ o: kotlin.Any): Data?
|
||||
public fun test2(): ???
|
||||
public fun test2x(): Data
|
||||
public fun test2y(): kotlin.Any
|
||||
public fun test2z(): kotlin.Any
|
||||
|
||||
public interface Data {
|
||||
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
|
||||
}
|
||||
|
||||
public final class FlagData : Data {
|
||||
public constructor FlagData(/*0*/ value: kotlin.Boolean)
|
||||
public final val value: kotlin.Boolean
|
||||
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
|
||||
}
|
||||
|
||||
public interface Item {
|
||||
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
|
||||
}
|
||||
|
||||
public final class ListData</*0*/ T : Item> : Data {
|
||||
public constructor ListData</*0*/ T : Item>(/*0*/ list: kotlin.collections.List<T>)
|
||||
public final val list: kotlin.collections.List<T>
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
interface Maybe<T>
|
||||
class Some<T>(val value: T) : Maybe<T>
|
||||
class None<T> : Maybe<T>
|
||||
|
||||
fun <T> none() : None<T> = TODO()
|
||||
|
||||
fun test1() : Maybe<String?> = if (true) none() else Some("")
|
||||
|
||||
fun test2() : Maybe<String?> = when {
|
||||
true -> none()
|
||||
else -> Some("")
|
||||
}
|
||||
|
||||
fun test3() : Maybe<String?> = when {
|
||||
true -> none()
|
||||
else -> Some<String?>("")
|
||||
}
|
||||
|
||||
fun test4() : Maybe<String?> {
|
||||
when ("") {
|
||||
"a" -> return none()
|
||||
else -> return Some<String?>("")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package
|
||||
|
||||
public fun </*0*/ T> none(): None<T>
|
||||
public fun test1(): Maybe<kotlin.String?>
|
||||
public fun test2(): Maybe<kotlin.String?>
|
||||
public fun test3(): Maybe<kotlin.String?>
|
||||
public fun test4(): Maybe<kotlin.String?>
|
||||
|
||||
public interface Maybe</*0*/ T> {
|
||||
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
|
||||
}
|
||||
|
||||
public final class None</*0*/ T> : Maybe<T> {
|
||||
public constructor None</*0*/ T>()
|
||||
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
|
||||
}
|
||||
|
||||
public final class Some</*0*/ T> : Maybe<T> {
|
||||
public constructor Some</*0*/ T>(/*0*/ value: T)
|
||||
public final val value: T
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
val test1: (String) -> Boolean =
|
||||
when {
|
||||
true -> {{ true }}
|
||||
else -> {{ false }}
|
||||
}
|
||||
|
||||
val test2: (String) -> Boolean =
|
||||
when {
|
||||
true -> {{ true }}
|
||||
else -> null!!
|
||||
}
|
||||
|
||||
val test3: (String) -> Boolean =
|
||||
when {
|
||||
true -> { s -> true }
|
||||
else -> null!!
|
||||
}
|
||||
|
||||
val test4: (String) -> Boolean =
|
||||
when {
|
||||
true -> { <!EXPECTED_PARAMETERS_NUMBER_MISMATCH!>s1, <!CANNOT_INFER_PARAMETER_TYPE!>s2<!><!> -> true }
|
||||
else -> null!!
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package
|
||||
|
||||
public val test1: (kotlin.String) -> kotlin.Boolean
|
||||
public val test2: (kotlin.String) -> kotlin.Boolean
|
||||
public val test3: (kotlin.String) -> kotlin.Boolean
|
||||
public val test4: (kotlin.String) -> kotlin.Boolean
|
||||
@@ -0,0 +1,17 @@
|
||||
import java.util.*
|
||||
import kotlin.comparisons.compareBy
|
||||
import kotlin.comparisons.nullsLast
|
||||
|
||||
class Foo(val a: String, val b: Int)
|
||||
|
||||
fun getComp(): Comparator<Foo?> =
|
||||
when {
|
||||
else -> nullsLast(compareBy({ it.a }, { it.b }))
|
||||
}
|
||||
|
||||
fun getCompInverted(): Comparator<Foo?> =
|
||||
nullsLast(
|
||||
when {
|
||||
else -> compareBy({ it.a }, { it.b })
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
package
|
||||
|
||||
public fun getComp(): java.util.Comparator<Foo?>
|
||||
public fun getCompInverted(): java.util.Comparator<Foo?>
|
||||
|
||||
public final class Foo {
|
||||
public constructor Foo(/*0*/ a: kotlin.String, /*1*/ b: kotlin.Int)
|
||||
public final val a: kotlin.String
|
||||
public final val b: kotlin.Int
|
||||
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
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
import java.util.*
|
||||
|
||||
fun <T> nullable(x: T): T? = x
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
fun <T> select(x1: T, x2: T): T = x1
|
||||
|
||||
val test1 =
|
||||
listOf(1, 2, 3).mapNotNullTo(ArrayList()) {
|
||||
if (true) nullable(it) else null
|
||||
}
|
||||
|
||||
val test2: MutableList<Int?> =
|
||||
listOf(1, 2, 3).mapNotNullTo(ArrayList()) {
|
||||
if (true) nullable(it) else null
|
||||
}
|
||||
|
||||
val test3: MutableList<Int> =
|
||||
listOf(1, 2, 3).mapNotNullTo(ArrayList()) {
|
||||
if (true) nullable(it) else null
|
||||
}
|
||||
|
||||
val test4: Collection<Int> =
|
||||
listOf(1, 2, 3).flatMapTo(LinkedHashSet()) {
|
||||
listOf(it)
|
||||
}
|
||||
|
||||
val test5: Collection<Int> =
|
||||
listOf(1, 2, 3).<!TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH!>flatMapTo(<!TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>LinkedHashSet<!>()) { // TODO
|
||||
if (true) listOf(it) else listOf(it)
|
||||
}<!>
|
||||
|
||||
val test6: Collection<Int> =
|
||||
listOf(1, 2, 3).flatMapTo(LinkedHashSet<Int>()) {
|
||||
if (true) listOf(it) else listOf(it)
|
||||
}
|
||||
|
||||
val test7: Collection<Int> =
|
||||
listOf(1, 2, 3).flatMapTo(LinkedHashSet()) {
|
||||
select(listOf(it), listOf(it))
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package
|
||||
|
||||
public val test1: java.util.ArrayList<kotlin.Int>
|
||||
public val test2: kotlin.collections.MutableList<kotlin.Int?>
|
||||
public val test3: kotlin.collections.MutableList<kotlin.Int>
|
||||
public val test4: kotlin.collections.Collection<kotlin.Int>
|
||||
public val test5: kotlin.collections.Collection<kotlin.Int>
|
||||
public val test6: kotlin.collections.Collection<kotlin.Int>
|
||||
public val test7: kotlin.collections.Collection<kotlin.Int>
|
||||
public fun </*0*/ T> nullable(/*0*/ x: T): T?
|
||||
@kotlin.Suppress(names = {"UNUSED_PARAMETER"}) public fun </*0*/ T> select(/*0*/ x1: T, /*1*/ x2: T): T
|
||||
@@ -3438,6 +3438,18 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("specialConstructsAndPlatformTypes.kt")
|
||||
public void testSpecialConstructsAndPlatformTypes() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/controlStructures/specialConstructsAndPlatformTypes.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("specialConstructsWithNullableExpectedType.kt")
|
||||
public void testSpecialConstructsWithNullableExpectedType() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/controlStructures/specialConstructsWithNullableExpectedType.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("tryReturnType.kt")
|
||||
public void testTryReturnType() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/controlStructures/tryReturnType.kt");
|
||||
@@ -18714,6 +18726,18 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt10809.kt")
|
||||
public void testKt10809() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/when/kt10809.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt10811.kt")
|
||||
public void testKt10811() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/when/kt10811.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt4434.kt")
|
||||
public void testKt4434() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/when/kt4434.kt");
|
||||
@@ -18834,6 +18858,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("whenAndLambdaWithExpectedType.kt")
|
||||
public void testWhenAndLambdaWithExpectedType() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/when/whenAndLambdaWithExpectedType.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("WhenTypeDisjunctions.kt")
|
||||
public void testWhenTypeDisjunctions() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/when/WhenTypeDisjunctions.kt");
|
||||
|
||||
+27
-6
@@ -95,12 +95,6 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt10192.kt")
|
||||
public void testKt10192() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/kt10192.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt9078.kt")
|
||||
public void testKt9078() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/kt9078.kt");
|
||||
@@ -1207,4 +1201,31 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/diagnostics/testsWithStdLib/when")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class When extends AbstractDiagnosticsTestWithStdLib {
|
||||
public void testAllFilesPresentInWhen() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/when"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("kt10192.kt")
|
||||
public void testKt10192() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/when/kt10192.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt10807.kt")
|
||||
public void testKt10807() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/when/kt10807.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("noTypeArgumentsInConstructor.kt")
|
||||
public void testNoTypeArgumentsInConstructor() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/when/noTypeArgumentsInConstructor.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1097,6 +1097,10 @@ public abstract class KotlinBuiltIns {
|
||||
return isNotNullConstructedFromGivenClass(type, FQ_NAMES.unit);
|
||||
}
|
||||
|
||||
public static boolean isUnitOrNullableUnit(@NotNull KotlinType type) {
|
||||
return isConstructedFromGivenClass(type, FQ_NAMES.unit);
|
||||
}
|
||||
|
||||
public boolean isBooleanOrSubtype(@NotNull KotlinType type) {
|
||||
return KotlinTypeChecker.DEFAULT.isSubtypeOf(type, getBooleanType());
|
||||
}
|
||||
|
||||
+1
-1
@@ -197,7 +197,7 @@ class ReferenceVariantsHelper(
|
||||
}
|
||||
|
||||
if (callType == CallType.SUPER_MEMBERS) { // we need to unwrap fake overrides in case of "super." because ShadowedDeclarationsFilter does not work correctly
|
||||
return descriptors.flatMapTo(LinkedHashSet()) {
|
||||
return descriptors.flatMapTo(LinkedHashSet<DeclarationDescriptor>()) {
|
||||
if (it is CallableMemberDescriptor && it.kind == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) it.overriddenDescriptors else listOf(it)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user