Optimize const vals by inlining them at use sites

#KT-11734 Fixed
 #KT-13570 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2016-12-05 12:02:52 +03:00
parent 8c1e165f18
commit 0134b8819b
28 changed files with 210 additions and 136 deletions
@@ -21,8 +21,10 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.codegen.ExpressionCodegen
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.project.languageVersionSettings
import org.jetbrains.kotlin.psi.KtConstantExpression
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtFile
@@ -44,8 +46,12 @@ class WrongPrimitiveLiteralFix(element: KtConstantExpression, type: KotlinType)
private val typeName = DescriptorUtils.getFqName(type.constructor.declarationDescriptor!!)
private val expectedTypeIsFloat = KotlinBuiltIns.isFloat(type)
private val expectedTypeIsDouble = KotlinBuiltIns.isDouble(type)
private val constValue
= ExpressionCodegen.getPrimitiveOrStringCompileTimeConstant(element, element.analyze(BodyResolveMode.PARTIAL))?.value as? Number
private val constValue = run {
val shouldInlineCosntVals = element.languageVersionSettings.supportsFeature(LanguageFeature.InlineConstVals)
ExpressionCodegen.getPrimitiveOrStringCompileTimeConstant(
element, element.analyze(BodyResolveMode.PARTIAL), shouldInlineCosntVals)?.value as? Number
}
private val fixedExpression = buildString {
if (expectedTypeIsFloat || expectedTypeIsDouble) {