Correct diagnostics and quick-fix for T::class with non-reified type parameter #KT-9590 fixed

This commit is contained in:
Simon Ogorodnik
2016-09-09 04:58:15 +03:00
committed by Mikhail Glukhikh
parent 998399bcd8
commit 2b63bcaa19
15 changed files with 152 additions and 7 deletions

View File

@@ -50,7 +50,7 @@ public class ReifiedTypeParameterSubstitutionChecker implements CallChecker {
if (argumentDeclarationDescriptor instanceof TypeParameterDescriptor &&
!((TypeParameterDescriptor) argumentDeclarationDescriptor).isReified()) {
context.getTrace().report(Errors.TYPE_PARAMETER_AS_REIFIED.on(reportErrorOn, parameter));
context.getTrace().report(Errors.TYPE_PARAMETER_AS_REIFIED.on(reportErrorOn, (TypeParameterDescriptor) argumentDeclarationDescriptor));
}
else if (TypeUtilsKt.cannotBeReified(argument)) {
context.getTrace().report(Errors.REIFIED_TYPE_FORBIDDEN_SUBSTITUTION.on(reportErrorOn, argument));

View File

@@ -48,12 +48,11 @@ import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
import org.jetbrains.kotlin.resolve.scopes.receivers.Receiver
import org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver
import org.jetbrains.kotlin.resolve.source.toSourceElement
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.KotlinTypeFactory
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.TypeUtils.NO_EXPECTED_TYPE
import org.jetbrains.kotlin.types.expressions.typeInfoFactory.createTypeInfo
import org.jetbrains.kotlin.types.typeUtil.isTypeParameter
import java.lang.UnsupportedOperationException
import javax.inject.Inject
sealed class DoubleColonLHS(val type: KotlinType) {
@@ -119,7 +118,12 @@ class DoubleColonExpressionResolver(
reportError = !isAllowedInClassLiteral(type)
}
if (type.isMarkedNullable || reportError) {
val typeParameterDescriptor = TypeUtils.getTypeParameterDescriptorOrNull(type)
if (type is SimpleType && !type.isMarkedNullable && typeParameterDescriptor != null && !typeParameterDescriptor.isReified) {
c.trace.report(TYPE_PARAMETER_AS_REIFIED.on(expression, typeParameterDescriptor))
}
else if (type.isMarkedNullable || reportError) {
c.trace.report(CLASS_LITERAL_LHS_NOT_A_CLASS.on(expression))
}
}

View File

@@ -9,7 +9,7 @@ val l1 = <!CLASS_LITERAL_LHS_NOT_A_CLASS!>List<String>?::class<!>
val l2 = <!CLASS_LITERAL_LHS_NOT_A_CLASS!>List?::class<!>
fun <T : Any> foo() {
val t1 = <!CLASS_LITERAL_LHS_NOT_A_CLASS!>T::class<!>
val t1 = <!TYPE_PARAMETER_AS_REIFIED!>T::class<!>
val t2 = <!CLASS_LITERAL_LHS_NOT_A_CLASS!>T?::class<!>
}

View File

@@ -0,0 +1,3 @@
fun <T: Any> dereferenceClass(): Any =
<!TYPE_PARAMETER_AS_REIFIED!>T::class<!>

View File

@@ -0,0 +1,3 @@
package
public fun </*0*/ T : kotlin.Any> dereferenceClass(): kotlin.Any

View File

@@ -8339,6 +8339,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("ClassDereference.kt")
public void testClassDereference() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/tpAsReified/ClassDereference.kt");
doTest(fileName);
}
@TestMetadata("Conventions.kt")
public void testConventions() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/tpAsReified/Conventions.kt");

View File

@@ -0,0 +1,68 @@
/*
* 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 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.
*/
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction
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.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.psiUtil.getStrictParentOfType
class AddReifiedToTypeParameterOfFunctionFix(
typeParameter: KtTypeParameter,
val function: KtNamedFunction
) : AddModifierFix(typeParameter, KtTokens.REIFIED_KEYWORD) {
val inlineFix = AddInlineToFunctionWithReifiedFix(function)
override fun getText() = "Make ${getElementName(element)} reified and ${getElementName(function)} inline"
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
super.invoke(project, editor, file)
inlineFix.invoke(project, editor, file)
}
companion object Factory : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val element = Errors.TYPE_PARAMETER_AS_REIFIED.cast(diagnostic)
val function = element.psiElement.getStrictParentOfType<KtNamedFunction>()
val parameter = function?.typeParameterList?.parameters?.get(element.a.index) ?: return null
return AddReifiedToTypeParameterOfFunctionFix(parameter, function)
}
}
}

View File

@@ -418,5 +418,7 @@ class QuickFixRegistrar : QuickFixContributor {
UNRESOLVED_REFERENCE.registerFactory(CreateTypeParameterByRefActionFactory)
FINAL_UPPER_BOUND.registerFactory(InlineTypeParameterFix)
TYPE_PARAMETER_AS_REIFIED.registerFactory(AddReifiedToTypeParameterOfFunctionFix)
}
}

View File

@@ -0,0 +1,4 @@
// "Make 'T' reified and 'dereferenceClass' inline" "true"
fun <T: Any> dereferenceClass(): Any =
T::class<caret>

View File

@@ -0,0 +1,4 @@
// "Make 'T' reified and 'dereferenceClass' inline" "true"
inline fun <reified T: Any> dereferenceClass(): Any =
T::class<caret>

View File

@@ -0,0 +1,6 @@
// "Make 'R' reified and 'flatten' inline" "true"
// WITH_RUNTIME
fun <T: Iterable<Array<R>>, R> T.flatten(): Array<R> {
return this.flatMap { it.asIterable() }.toTypedArray<caret>()
}

View File

@@ -0,0 +1,6 @@
// "Make 'R' reified and 'flatten' inline" "true"
// WITH_RUNTIME
inline fun <T: Iterable<Array<R>>, reified R> T.flatten(): Array<R> {
return this.flatMap { it.asIterable() }.toTypedArray<caret>()
}

View File

@@ -0,0 +1,6 @@
// "Make 'T' reified and 'flatten' inline" "true"
// WITH_RUNTIME
fun <T> Array<Array<T>>.flatten(): Array<T> {
return this.flatMap { it.asIterable() }.toTypedArray<caret>()
}

View File

@@ -0,0 +1,6 @@
// "Make 'T' reified and 'flatten' inline" "true"
// WITH_RUNTIME
inline fun <reified T> Array<Array<T>>.flatten(): Array<T> {
return this.flatMap { it.asIterable() }.toTypedArray<caret>()
}

View File

@@ -413,6 +413,33 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/addReifiedToTypeParameterOfFunctionFix")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddReifiedToTypeParameterOfFunctionFix extends AbstractQuickFixTest {
public void testAllFilesPresentInAddReifiedToTypeParameterOfFunctionFix() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/addReifiedToTypeParameterOfFunctionFix"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("doubleColonClass.kt")
public void testDoubleColonClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addReifiedToTypeParameterOfFunctionFix/doubleColonClass.kt");
doTest(fileName);
}
@TestMetadata("secondTypeParameter.kt")
public void testSecondTypeParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addReifiedToTypeParameterOfFunctionFix/secondTypeParameter.kt");
doTest(fileName);
}
@TestMetadata("toTypedArray.kt")
public void testToTypedArray() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addReifiedToTypeParameterOfFunctionFix/toTypedArray.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/addRunBeforeLambda")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)