Avoid non-null assertions for inline classes based on nullable types

Note that there are more places where assertions for inline classes should refined:
  - lateinit vars
  - values that come from Java
  - type casts (interfaces to inline class type)
This commit is contained in:
Mikhail Zarechenskiy
2018-02-20 12:46:14 +03:00
parent e3c58eced1
commit f23b5103ec
11 changed files with 115 additions and 15 deletions
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve.jvm
@@ -30,6 +19,7 @@ import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker
import org.jetbrains.kotlin.resolve.calls.checkers.CallCheckerContext
import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.isNullableUnderlyingType
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.kotlin.types.*
@@ -62,7 +52,12 @@ class RuntimeAssertionInfo(val needNotNullAssertion: Boolean, val message: Strin
// Cases when nullability assertion needed: T! -> T, T$ -> T
// Expected type either T?, T! or T$
if (TypeUtils.isNullableType(expectedType) || expectedType.hasEnhancedNullability()) return false
if (TypeUtils.isNullableType(expectedType) ||
expectedType.hasEnhancedNullability() ||
expectedType.isNullableUnderlyingType()
) {
return false
}
// Expression type is not nullable and not enhanced (neither T?, T! or T$)
val isExpressionTypeNullable = TypeUtils.isNullableType(expressionType)