[FIR] KT-56505 Fix checkVisibilityModifier taking wrong modifier list
- KT-56505 occurred because `source.getChild(KtNodeTypes.MODIFIER_LIST)` returns any modifier list in the subtree of the source element, not necessarily the modifier list belonging to the checked element. `depth = 1` restricts the search to the modifier list belonging to the checked element itself. - For example, given `f1` from KT-56505, `getChild` would return the modifier list of `public var foo = 0`. Because it contains a visibility modifier, `f1` wasn't marked with `NO_EXPLICIT_VISIBILITY_IN_API_MODE`. ^KT-56505 fixed
This commit is contained in:
committed by
Space Team
parent
e86c877733
commit
5d391bc261
+6
@@ -32720,6 +32720,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
|
|||||||
runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/interfaces.kt");
|
runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/interfaces.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("kt56505.kt")
|
||||||
|
public void testKt56505() throws Exception {
|
||||||
|
runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/kt56505.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("localFunctions.kt")
|
@TestMetadata("localFunctions.kt")
|
||||||
public void testLocalFunctions() throws Exception {
|
public void testLocalFunctions() throws Exception {
|
||||||
|
|||||||
+3
-3
@@ -1,6 +1,6 @@
|
|||||||
// EXPLICIT_API_MODE: STRICT
|
// EXPLICIT_API_MODE: STRICT
|
||||||
|
|
||||||
fun f1() {
|
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>fun f1<!>() {
|
||||||
class LocalClass {
|
class LocalClass {
|
||||||
public var foo = 0
|
public var foo = 0
|
||||||
}
|
}
|
||||||
@@ -71,7 +71,7 @@ public interface I2 {
|
|||||||
|
|
||||||
public var baz2: Int = 0
|
public var baz2: Int = 0
|
||||||
|
|
||||||
class J1 {
|
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>class J1<!> {
|
||||||
protected val baz: Int = 0
|
protected val baz: Int = 0
|
||||||
<!REDUNDANT_VISIBILITY_MODIFIER!>protected<!> get() = field * 2
|
<!REDUNDANT_VISIBILITY_MODIFIER!>protected<!> get() = field * 2
|
||||||
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>var baf<!>: Int = 0
|
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>var baf<!>: Int = 0
|
||||||
@@ -80,7 +80,7 @@ class J1 {
|
|||||||
field = value
|
field = value
|
||||||
}
|
}
|
||||||
|
|
||||||
var buf: Int = 0
|
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>var buf<!>: Int = 0
|
||||||
<!GETTER_VISIBILITY_DIFFERS_FROM_PROPERTY_VISIBILITY!>private<!> get() = 42
|
<!GETTER_VISIBILITY_DIFFERS_FROM_PROPERTY_VISIBILITY!>private<!> get() = 42
|
||||||
protected set(value) {
|
protected set(value) {
|
||||||
field = value
|
field = value
|
||||||
|
|||||||
+6
@@ -32816,6 +32816,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
|
|||||||
runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/interfaces.kt");
|
runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/interfaces.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("kt56505.kt")
|
||||||
|
public void testKt56505() throws Exception {
|
||||||
|
runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/kt56505.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("localFunctions.kt")
|
@TestMetadata("localFunctions.kt")
|
||||||
public void testLocalFunctions() throws Exception {
|
public void testLocalFunctions() throws Exception {
|
||||||
|
|||||||
+6
@@ -32720,6 +32720,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
|
|||||||
runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/interfaces.kt");
|
runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/interfaces.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("kt56505.kt")
|
||||||
|
public void testKt56505() throws Exception {
|
||||||
|
runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/kt56505.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("localFunctions.kt")
|
@TestMetadata("localFunctions.kt")
|
||||||
public void testLocalFunctions() throws Exception {
|
public void testLocalFunctions() throws Exception {
|
||||||
|
|||||||
+3
-4
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.fir.declarations.impl.FirPrimaryConstructor
|
|||||||
import org.jetbrains.kotlin.fir.declarations.utils.effectiveVisibility
|
import org.jetbrains.kotlin.fir.declarations.utils.effectiveVisibility
|
||||||
import org.jetbrains.kotlin.fir.declarations.utils.isData
|
import org.jetbrains.kotlin.fir.declarations.utils.isData
|
||||||
import org.jetbrains.kotlin.fir.declarations.utils.isOverride
|
import org.jetbrains.kotlin.fir.declarations.utils.isOverride
|
||||||
import org.jetbrains.kotlin.fir.declarations.utils.visibility
|
|
||||||
import org.jetbrains.kotlin.fir.resolve.transformers.publishedApiEffectiveVisibility
|
import org.jetbrains.kotlin.fir.resolve.transformers.publishedApiEffectiveVisibility
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||||
@@ -48,8 +47,8 @@ object FirExplicitApiDeclarationChecker : FirDeclarationSyntaxChecker<FirDeclara
|
|||||||
// Enum entries do not have visibilities
|
// Enum entries do not have visibilities
|
||||||
if (element is FirEnumEntry) return
|
if (element is FirEnumEntry) return
|
||||||
if (!element.effectiveVisibility.publicApi && element.publishedApiEffectiveVisibility == null) return
|
if (!element.effectiveVisibility.publicApi && element.publishedApiEffectiveVisibility == null) return
|
||||||
val lastDeclaration = context.containingDeclarations.lastOrNull()
|
val lastContainingDeclaration = context.containingDeclarations.lastOrNull()
|
||||||
if ((lastDeclaration as? FirMemberDeclaration)?.effectiveVisibility?.publicApi == false) {
|
if ((lastContainingDeclaration as? FirMemberDeclaration)?.effectiveVisibility?.publicApi == false) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +63,7 @@ object FirExplicitApiDeclarationChecker : FirDeclarationSyntaxChecker<FirDeclara
|
|||||||
context: CheckerContext,
|
context: CheckerContext,
|
||||||
reporter: DiagnosticReporter
|
reporter: DiagnosticReporter
|
||||||
) {
|
) {
|
||||||
val visibilityModifier = source.getChild(KtNodeTypes.MODIFIER_LIST)?.getChild(KtTokens.VISIBILITY_MODIFIERS)
|
val visibilityModifier = source.getChild(KtNodeTypes.MODIFIER_LIST, depth = 1)?.getChild(KtTokens.VISIBILITY_MODIFIERS)
|
||||||
if (visibilityModifier != null) return
|
if (visibilityModifier != null) return
|
||||||
|
|
||||||
if (explicitVisibilityIsNotRequired(declaration, context)) return
|
if (explicitVisibilityIsNotRequired(declaration, context)) return
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
// FIR_IDENTICAL
|
||||||
|
// SKIP_TXT
|
||||||
|
// ISSUE: KT-56505
|
||||||
|
|
||||||
|
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>fun f1<!>() {
|
||||||
|
class LocalClass {
|
||||||
|
public var foo = 0
|
||||||
|
}
|
||||||
|
LocalClass().foo = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>class J1<!> {
|
||||||
|
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>var buf<!>: Int = 0
|
||||||
|
<!GETTER_VISIBILITY_DIFFERS_FROM_PROPERTY_VISIBILITY!>private<!> get() = 42
|
||||||
|
protected set(value) {
|
||||||
|
field = value
|
||||||
|
}
|
||||||
|
}
|
||||||
Generated
+6
@@ -32816,6 +32816,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
|
|||||||
runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/interfaces.kt");
|
runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/interfaces.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("kt56505.kt")
|
||||||
|
public void testKt56505() throws Exception {
|
||||||
|
runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/kt56505.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("localFunctions.kt")
|
@TestMetadata("localFunctions.kt")
|
||||||
public void testLocalFunctions() throws Exception {
|
public void testLocalFunctions() throws Exception {
|
||||||
|
|||||||
Reference in New Issue
Block a user