CreateExpect: shouldn't generate expect declaration from actual function with default implementation from interface

#KT-32737 Fixed
This commit is contained in:
Dmitry Gridin
2019-10-02 10:16:45 +07:00
parent e9f8693a73
commit 847295bf1c
14 changed files with 86 additions and 0 deletions
@@ -30,11 +30,13 @@ import org.jetbrains.kotlin.idea.quickfix.TypeAccessibilityChecker
import org.jetbrains.kotlin.idea.refactoring.createKotlinFile
import org.jetbrains.kotlin.idea.refactoring.fqName.fqName
import org.jetbrains.kotlin.idea.refactoring.introduce.showErrorHint
import org.jetbrains.kotlin.idea.refactoring.isInterfaceClass
import org.jetbrains.kotlin.idea.util.*
import org.jetbrains.kotlin.idea.util.application.runWriteAction
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
import org.jetbrains.kotlin.psi.psiUtil.hasActualModifier
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.checkers.ExpectedActualDeclarationChecker
@@ -389,6 +391,11 @@ fun TypeAccessibilityChecker.isCorrectAndHaveNonPrivateModifier(declaration: KtN
return false
}
if (declaration is KtFunction && declaration.hasBody() && declaration.containingClassOrObject?.isInterfaceClass() == true) {
if (showErrorHint) showInaccessibleDeclarationError(declaration, "The function declaration shouldn't have a default implementation")
return false
}
if (!showErrorHint) return checkAccessibility(declaration)
val types = incorrectTypes(declaration).ifEmpty { return true }
@@ -0,0 +1,3 @@
// DISABLE-ERRORS
expect interface My
@@ -0,0 +1,5 @@
// DISABLE-ERRORS
expect interface My {
fun foo(param: String)
}
@@ -0,0 +1,6 @@
// "Create expected function in common module testModule_Common" "true"
// DISABLE-ERRORS
actual interface My {
actual fun <caret>foo(param: String)
}
@@ -0,0 +1,6 @@
// "Create expected function in common module testModule_Common" "true"
// DISABLE-ERRORS
actual interface My {
actual fun <caret>foo(param: String)
}
@@ -0,0 +1,3 @@
// DISABLE-ERRORS
expect interface My
@@ -0,0 +1,3 @@
// DISABLE-ERRORS
expect interface My
@@ -0,0 +1,7 @@
// "Create expected function in common module testModule_Common" "true"
// SHOULD_FAIL_WITH: "The function declaration shouldn&#39;t have a default implementation"
// DISABLE-ERRORS
actual interface My {
actual fun <caret>foo(param: String) = param.length
}
@@ -0,0 +1,7 @@
// "Create expected function in common module testModule_Common" "true"
// SHOULD_FAIL_WITH: "The function declaration shouldn&#39;t have a default implementation"
// DISABLE-ERRORS
actual interface My {
actual fun <caret>foo(param: String) = param.length
}
@@ -0,0 +1,3 @@
// DISABLE-ERRORS
expect interface My
@@ -0,0 +1,3 @@
// DISABLE-ERRORS
expect interface My
@@ -0,0 +1,9 @@
// "Create expected function in common module testModule_Common" "true"
// SHOULD_FAIL_WITH: "The function declaration shouldn&#39;t have a default implementation"
// DISABLE-ERRORS
actual interface My {
actual fun <caret>foo(param: String) {
}
}
@@ -0,0 +1,9 @@
// "Create expected function in common module testModule_Common" "true"
// SHOULD_FAIL_WITH: "The function declaration shouldn&#39;t have a default implementation"
// DISABLE-ERRORS
actual interface My {
actual fun <caret>foo(param: String) {
}
}
@@ -553,6 +553,21 @@ public class QuickFixMultiModuleTestGenerated extends AbstractQuickFixMultiModul
runTest("idea/testData/multiModuleQuickFix/createExpect/function2/");
}
@TestMetadata("functionInInterface")
public void testFunctionInInterface() throws Exception {
runTest("idea/testData/multiModuleQuickFix/createExpect/functionInInterface/");
}
@TestMetadata("functionWithImplementationInInterface")
public void testFunctionWithImplementationInInterface() throws Exception {
runTest("idea/testData/multiModuleQuickFix/createExpect/functionWithImplementationInInterface/");
}
@TestMetadata("functionWithImplementationInInterface2")
public void testFunctionWithImplementationInInterface2() throws Exception {
runTest("idea/testData/multiModuleQuickFix/createExpect/functionWithImplementationInInterface2/");
}
@TestMetadata("inlineClass")
public void testInlineClass() throws Exception {
runTest("idea/testData/multiModuleQuickFix/createExpect/inlineClass/");