Add quick-fix for CANNOT_CHECK_FOR_ERASED #KT-18742 Fixed

This commit is contained in:
Toshiaki Kameyama
2017-09-17 15:54:07 +03:00
committed by Mikhail Glukhikh
parent 6a1b6d10d8
commit d87c0b164f
7 changed files with 105 additions and 0 deletions
@@ -0,0 +1,60 @@
/*
* 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.
* 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.
*/
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtTypeParameter
import org.jetbrains.kotlin.psi.KtTypeReference
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
class MakeTypeParameterReifiedAndFunctionInlineFix(
typeReference: KtTypeReference,
function: KtNamedFunction,
private val typeParameter: KtTypeParameter
) : KotlinQuickFixAction<KtTypeReference>(typeReference) {
private val inlineFix = AddInlineToFunctionWithReifiedFix(function)
override fun getText() = "Make type parameter reified and function inline"
override fun getFamilyName() = text
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
inlineFix.invoke(project, editor, file)
AddModifierFix(typeParameter, KtTokens.REIFIED_KEYWORD).invoke(project, editor, file)
}
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtTypeReference>? {
val element = Errors.CANNOT_CHECK_FOR_ERASED.cast(diagnostic)
val typeReference = element.psiElement as? KtTypeReference ?: return null
val function = typeReference.getStrictParentOfType<KtNamedFunction>() ?: return null
val typeParameter = function.typeParameterList?.parameters?.firstOrNull {
it.descriptor == element.a.constructor.declarationDescriptor
} ?: return null
return MakeTypeParameterReifiedAndFunctionInlineFix(typeReference, function, typeParameter)
}
}
}
@@ -459,6 +459,8 @@ class QuickFixRegistrar : QuickFixContributor {
TYPE_PARAMETER_AS_REIFIED.registerFactory(AddReifiedToTypeParameterOfFunctionFix)
CANNOT_CHECK_FOR_ERASED.registerFactory(MakeTypeParameterReifiedAndFunctionInlineFix)
TOO_MANY_CHARACTERS_IN_CHARACTER_LITERAL.registerFactory(TooLongCharLiteralToStringFix)
UNUSED_VALUE.registerFactory(RemoveUnusedValueFix)
@@ -0,0 +1,6 @@
// "Make type parameter reified and function inline" "false"
// ACTION: Change type arguments to <*>
// ACTION: Convert to block body
// ACTION: Introduce local variable
// ERROR: Cannot check for instance of erased type: List<Int>
fun test(a: List<Any>) = a is List<Int><caret>
@@ -0,0 +1,6 @@
// "Make type parameter reified and function inline" "false"
// ACTION: Change type arguments to <*>
// ACTION: Convert to block body
// ACTION: Introduce local variable
// ERROR: Cannot check for instance of erased type: List<Int>
fun <T> test(a: List<Any>) = a is List<Int><caret>
@@ -0,0 +1,2 @@
// "Make type parameter reified and function inline" "true"
fun <T> test(a: String) = a is T<caret>
@@ -0,0 +1,2 @@
// "Make type parameter reified and function inline" "true"
inline fun <reified T> test(a: String) = a is T
@@ -6717,6 +6717,33 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/makeTypeParameterReifiedAndFunctionInline")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class MakeTypeParameterReifiedAndFunctionInline extends AbstractQuickFixTest {
public void testAllFilesPresentInMakeTypeParameterReifiedAndFunctionInline() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/makeTypeParameterReifiedAndFunctionInline"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("noTypeParameter.kt")
public void testNoTypeParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/makeTypeParameterReifiedAndFunctionInline/noTypeParameter.kt");
doTest(fileName);
}
@TestMetadata("noTypeParameter2.kt")
public void testNoTypeParameter2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/makeTypeParameterReifiedAndFunctionInline/noTypeParameter2.kt");
doTest(fileName);
}
@TestMetadata("typeParameter.kt")
public void testTypeParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/makeTypeParameterReifiedAndFunctionInline/typeParameter.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/memberVisibilityCanBePrivate")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)