From 38d9123a5705706f2a4a2ad1aae86f8cf8c66374 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Thu, 8 Jun 2017 14:01:23 +0300 Subject: [PATCH] [NI] Use weaker upper constraint for case {T & Any} <: S --- .../TypeCheckerContextForConstraintSystem.kt | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/TypeCheckerContextForConstraintSystem.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/TypeCheckerContextForConstraintSystem.kt index 239bb8086de..ac50e77b753 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/TypeCheckerContextForConstraintSystem.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/TypeCheckerContextForConstraintSystem.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -117,6 +117,25 @@ abstract class TypeCheckerContextForConstraintSystem : TypeCheckerContext(errorT return true } +// Consider the following example: +// fun id(x: T): T = x +// fun id2(x: S?, y: S): S = y +// +// fun checkLeftAssoc(a: Int?) : Int { +// return id2(id(a), 3) +// } +// +// fun box() : String { +// return "OK" +// } +// +// here we try to add constraint {Any & T} <: S from `id(a)` +// Previously we thought that if `Any` isn't a subtype of S => T <: S, which is wrong, now we use weaker upper constraint +// TODO: rethink, maybe we should take nullability into account somewhere else + if (notTypeVariables.any { NullabilityChecker.isSubtypeOfAny(it) }) { + return typeVariables.all { simplifyUpperConstraint(it, superType.makeNullableAsSpecified(true)) } + } + return typeVariables.all { simplifyUpperConstraint(it, superType) } }