Skip explicit API inspection for data class properties and add inspection for public API in interfaces

Add tests for almost all the cases
This commit is contained in:
Leonid Startsev
2019-10-14 20:20:48 +03:00
parent ebb7e434c8
commit 5ab262c977
20 changed files with 426 additions and 5 deletions
@@ -76,17 +76,15 @@ class ExplicitApiDeclarationChecker : DeclarationChecker {
/**
* Exclusion list:
* 1. Primary constructors of public API classes
* 2. Members of public API interfaces
* 3. do not report overrides of public API? effectively, this means 'no report on overrides at all'
* 2. Properties of data classes in public API
* 3. Overrides of public API. Effectively, this means 'no report on overrides at all'
* 4. Getters and setters (because getters can't change visibility and setter-only explicit visibility looks ugly)
*
* Do we need something like @PublicApiFile to disable (or invert) this inspection per-file?
*/
private fun excludeForDiagnostic(descriptor: DeclarationDescriptor): Boolean {
/* 1. */ if ((descriptor as? ClassConstructorDescriptor)?.isPrimary == true) return true
val isMemberOfPublicInterface =
(descriptor.containingDeclaration as? ClassDescriptor)?.let { DescriptorUtils.isInterface(it) && it.effectiveVisibility().publicApi }
/* 2. */ if (descriptor is CallableDescriptor && isMemberOfPublicInterface == true) return true
/* 2. */ if (descriptor is PropertyDescriptor && (descriptor.containingDeclaration as? ClassDescriptor)?.isData == true) return true
/* 3. */ if ((descriptor as? CallableDescriptor)?.overriddenDescriptors?.isNotEmpty() == true) return true
/* 4. */ if (descriptor is PropertyAccessorDescriptor) return true
return false
@@ -0,0 +1,15 @@
class <!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>Foo1()<!> {}
public class Foo2() {
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>fun method()<!> {}
public fun method2() {}
private fun method3() {}
<!NO_EXPLICIT_RETURN_TYPE_IN_API_MODE, NO_EXPLICIT_VISIBILITY_IN_API_MODE!>fun implicit()<!> = 10
<!NO_EXPLICIT_RETURN_TYPE_IN_API_MODE!>public fun implicit2()<!> = 10
public fun implicit3(): Int = 10
}
public data class FooData(val i: Int, val s: String)
data class <!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>FooData2(val i: Int, val s: String)<!>
@@ -0,0 +1,45 @@
package
public final class Foo1 {
public constructor Foo1()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Foo2 {
public constructor Foo2()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun implicit(): kotlin.Int
public final fun implicit2(): kotlin.Int
public final fun implicit3(): kotlin.Int
public final fun method(): kotlin.Unit
public final fun method2(): kotlin.Unit
private final fun method3(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final data class FooData {
public constructor FooData(/*0*/ i: kotlin.Int, /*1*/ s: kotlin.String)
public final val i: kotlin.Int
public final val s: kotlin.String
public final operator /*synthesized*/ fun component1(): kotlin.Int
public final operator /*synthesized*/ fun component2(): kotlin.String
public final /*synthesized*/ fun copy(/*0*/ i: kotlin.Int = ..., /*1*/ s: kotlin.String = ...): FooData
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
public final data class FooData2 {
public constructor FooData2(/*0*/ i: kotlin.Int, /*1*/ s: kotlin.String)
public final val i: kotlin.Int
public final val s: kotlin.String
public final operator /*synthesized*/ fun component1(): kotlin.Int
public final operator /*synthesized*/ fun component2(): kotlin.String
public final /*synthesized*/ fun copy(/*0*/ i: kotlin.Int = ..., /*1*/ s: kotlin.String = ...): FooData2
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
@@ -0,0 +1,3 @@
public class Bar {
companion <!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>object<!> {}
}
@@ -0,0 +1,15 @@
package
public final class Bar {
public constructor Bar()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public companion object Companion {
private constructor Companion()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -0,0 +1,12 @@
public class Foo1 () {}
public class Foo2 constructor() {}
public class Foo3 public constructor() {}
public class Foo4 private constructor() {}
public class Foo5 {
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>constructor()<!> {}
}
public class Foo6 {
public constructor() {}
}
@@ -0,0 +1,43 @@
package
public final class Foo1 {
public constructor Foo1()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Foo2 {
public constructor Foo2()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Foo3 {
public constructor Foo3()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Foo4 {
private constructor Foo4()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Foo5 {
public constructor Foo5()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Foo6 {
public constructor Foo6()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,6 @@
// !DIAGNOSTICS: -EXPERIMENTAL_FEATURE_WARNING
inline class <!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>Value1(<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>val inner: Int<!>)<!>
public inline class Value2(<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>val inner: Int<!>)
inline class <!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>Value3(public val inner: Int)<!>
public inline class Value4(public val inner: Int)
@@ -0,0 +1,33 @@
package
public final inline class Value1 {
public constructor Value1(/*0*/ inner: kotlin.Int)
public final val inner: kotlin.Int
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
public final inline class Value2 {
public constructor Value2(/*0*/ inner: kotlin.Int)
public final val inner: kotlin.Int
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
public final inline class Value3 {
public constructor Value3(/*0*/ inner: kotlin.Int)
public final val inner: kotlin.Int
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
public final inline class Value4 {
public constructor Value4(/*0*/ inner: kotlin.Int)
public final val inner: kotlin.Int
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
@@ -0,0 +1,33 @@
interface <!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>I1<!> {
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>fun i()<!>
}
public interface I2 {
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>fun i()<!>
}
public interface I3 {
public fun i()
public val v: Int
}
public interface I4 {
public fun i(): Int
public val v: Int
}
public class Impl: I3 {
override fun i() {}
override val v: Int
get() = 10
}
public class Impl2: I4 {
<!NO_EXPLICIT_RETURN_TYPE_IN_API_MODE!>override fun i()<!> = 10
<!NO_EXPLICIT_RETURN_TYPE_IN_API_MODE!>override val v<!> = 10
}
private class PrivateImpl: I4 {
override fun i() = 10
override val v = 10
}
@@ -0,0 +1,58 @@
package
public interface I1 {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public abstract fun i(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface I2 {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public abstract fun i(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface I3 {
public abstract val v: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public abstract fun i(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface I4 {
public abstract val v: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public abstract fun i(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Impl : I3 {
public constructor Impl()
public open override /*1*/ val v: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ fun i(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Impl2 : I4 {
public constructor Impl2()
public open override /*1*/ val v: kotlin.Int = 10
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ fun i(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
private final class PrivateImpl : I4 {
public constructor PrivateImpl()
public open override /*1*/ val v: kotlin.Int = 10
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ fun i(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,3 @@
private class Foo {
fun method() {}
}
@@ -0,0 +1,9 @@
package
private final class Foo {
public constructor Foo()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun method(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,18 @@
public class Foo(<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>val bar: Int<!>, private var bar2: String, internal var bar3: Long, public var bar4: Int) {
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>var simple: Int<!> = 10
public var simple2: Int = 10
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>val withGetter: Int<!>
get() = 10
public val withGetter2: Int
get() = 10
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>var getterAndSetter: Int<!> = 10
get() = field
set(v) { field = v }
public var getterAndSetter2: Int = 10
get() = field
set(v) { field = v }
}
@@ -0,0 +1,18 @@
package
public final class Foo {
public constructor Foo(/*0*/ bar: kotlin.Int, /*1*/ bar2: kotlin.String, /*2*/ bar3: kotlin.Long, /*3*/ bar4: kotlin.Int)
public final val bar: kotlin.Int
private final var bar2: kotlin.String
internal final var bar3: kotlin.Long
public final var bar4: kotlin.Int
public final var getterAndSetter: kotlin.Int
public final var getterAndSetter2: kotlin.Int
public final var simple: kotlin.Int
public final var simple2: kotlin.Int
public final val withGetter: kotlin.Int
public final val withGetter2: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,7 @@
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>fun foo()<!> {}
public fun foo2() {}
<!NO_EXPLICIT_RETURN_TYPE_IN_API_MODE, NO_EXPLICIT_VISIBILITY_IN_API_MODE!>fun bar()<!> = 10
<!NO_EXPLICIT_RETURN_TYPE_IN_API_MODE!>public fun bar2()<!> = 10
public fun bar3(): Int = 10
@@ -0,0 +1,7 @@
package
public fun bar(): kotlin.Int
public fun bar2(): kotlin.Int
public fun bar3(): kotlin.Int
public fun foo(): kotlin.Unit
public fun foo2(): kotlin.Unit
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.checkers
import org.jetbrains.kotlin.config.*
import org.jetbrains.kotlin.test.ConfigurationKind
abstract class AbstractDiagnosticsWithExplicitApi : AbstractDiagnosticsTest() {
override fun getConfigurationKind(): ConfigurationKind {
return ConfigurationKind.NO_KOTLIN_REFLECT
}
override fun defaultLanguageVersionSettings(): LanguageVersionSettings =
CompilerTestLanguageVersionSettings(
DEFAULT_DIAGNOSTIC_TESTS_FEATURES,
ApiVersion.KOTLIN_1_3,
LanguageVersion.KOTLIN_1_3,
mapOf(AnalysisFlags.explicitApiMode to ExplicitApiMode.STRICT)
)
}
@@ -0,0 +1,71 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.checkers;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TargetBackend;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/diagnostics/testsWithExplicitApi")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class DiagnosticsWithExplicitApiGenerated extends AbstractDiagnosticsWithExplicitApi {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInTestsWithExplicitApi() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithExplicitApi"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("classes.kt")
public void testClasses() throws Exception {
runTest("compiler/testData/diagnostics/testsWithExplicitApi/classes.kt");
}
@TestMetadata("companionObject.kt")
public void testCompanionObject() throws Exception {
runTest("compiler/testData/diagnostics/testsWithExplicitApi/companionObject.kt");
}
@TestMetadata("constructors.kt")
public void testConstructors() throws Exception {
runTest("compiler/testData/diagnostics/testsWithExplicitApi/constructors.kt");
}
@TestMetadata("inlineClasses.kt")
public void testInlineClasses() throws Exception {
runTest("compiler/testData/diagnostics/testsWithExplicitApi/inlineClasses.kt");
}
@TestMetadata("interfaces.kt")
public void testInterfaces() throws Exception {
runTest("compiler/testData/diagnostics/testsWithExplicitApi/interfaces.kt");
}
@TestMetadata("mustBeEffectivelyPublic.kt")
public void testMustBeEffectivelyPublic() throws Exception {
runTest("compiler/testData/diagnostics/testsWithExplicitApi/mustBeEffectivelyPublic.kt");
}
@TestMetadata("properties.kt")
public void testProperties() throws Exception {
runTest("compiler/testData/diagnostics/testsWithExplicitApi/properties.kt");
}
@TestMetadata("toplevel.kt")
public void testToplevel() throws Exception {
runTest("compiler/testData/diagnostics/testsWithExplicitApi/toplevel.kt");
}
}
@@ -111,6 +111,10 @@ fun main(args: Array<String>) {
model("diagnostics/testsWithUnsignedTypes")
}
testClass<AbstractDiagnosticsWithExplicitApi> {
model("diagnostics/testsWithExplicitApi")
}
testClass<AbstractMultiPlatformIntegrationTest> {
model("multiplatform", extension = null, recursive = true, excludeParentDirs = true)
}