diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index 42fd06a4ccb..fd28cf8e1e2 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -32275,6 +32275,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/visibility/privateCompanionInSuperClass.kt"); } + @Test + @TestMetadata("privateDeclarationInAnotherFile.kt") + public void testPrivateDeclarationInAnotherFile() throws Exception { + runTest("compiler/testData/diagnostics/tests/visibility/privateDeclarationInAnotherFile.kt"); + } + @Test @TestMetadata("protectedInternal.kt") public void testProtectedInternal() throws Exception { diff --git a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionParameterType.fir.txt b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionParameterType.fir.txt index f4e53760773..fda72a4306f 100644 --- a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionParameterType.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionParameterType.fir.txt @@ -17,7 +17,7 @@ FILE: exposedFunctionParameterType.kt super() } - public final fun foo(value: R|A.AInner|): R|kotlin/Unit| { + public final fun foo(value: ): R|kotlin/Unit| { } } diff --git a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionParameterType.kt b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionParameterType.kt index b7581351fc1..b2deaa3dc3f 100644 --- a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionParameterType.kt +++ b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionParameterType.kt @@ -3,7 +3,7 @@ class A { } class B { - fun foo(value: A.AInner) { + fun foo(value: A.AInner) { } } diff --git a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionReturnType.fir.txt b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionReturnType.fir.txt index 8b9ca103bf8..ca0f5c2ba83 100644 --- a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionReturnType.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionReturnType.fir.txt @@ -17,7 +17,7 @@ FILE: exposedFunctionReturnType.kt super() } - public final fun foo(str: R|kotlin/String|): R|A.InnerA| + public final fun foo(str: R|kotlin/String|): } private final enum class Some : R|kotlin/Enum| { diff --git a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionReturnType.kt b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionReturnType.kt index 6383df3c4fd..6e081a5c914 100644 --- a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionReturnType.kt +++ b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedFunctionReturnType.kt @@ -5,7 +5,7 @@ class A { } abstract class B { - fun foo(str: String): A.InnerA + fun foo(str: String): A.InnerA } private enum class Some { diff --git a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedPropertyType.fir.txt b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedPropertyType.fir.txt index 5bef611f511..7fff7d52b03 100644 --- a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedPropertyType.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedPropertyType.fir.txt @@ -57,16 +57,16 @@ FILE: exposedPropertyType.kt public get(): R|kotlin/Int| public set(value: R|kotlin/Int|): R|kotlin/Unit| - public final var var4: R|A.AInnerPrivate| - public get(): R|A.AInnerPrivate| - public set(value: R|A.AInnerPrivate|): R|kotlin/Unit| + public final var var4: + public get(): + public set(value: ): R|kotlin/Unit| public final var var5: R|A.AInnerPublic| public get(): R|A.AInnerPublic| public set(value: R|A.AInnerPublic|): R|kotlin/Unit| - public final var var6: R|A.AInnerProtectedEnum| - public get(): R|A.AInnerProtectedEnum| - public set(value: R|A.AInnerProtectedEnum|): R|kotlin/Unit| + public final var var6: + public get(): + public set(value: ): R|kotlin/Unit| } diff --git a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedPropertyType.kt b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedPropertyType.kt index 83b67b4d4a8..79568459bb7 100644 --- a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedPropertyType.kt +++ b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedPropertyType.kt @@ -17,7 +17,7 @@ class Property { var var1: String var var2: String var var3: Int - var var4: A.AInnerPrivate + var var4: A.AInnerPrivate var var5: A.AInnerPublic - var var6: A.AInnerProtectedEnum + var var6: A.AInnerProtectedEnum } diff --git a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedSupertype.fir.txt b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedSupertype.fir.txt index 65502ac1513..a45b9c733dd 100644 --- a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedSupertype.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedSupertype.fir.txt @@ -74,7 +74,7 @@ FILE: exposedSupertype.kt } } - public final class Test4 : R|E|, R|A.AProtectedI| { + public final class Test4 : R|E|, { public constructor(): R|Test4| { super() } diff --git a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedSupertype.kt b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedSupertype.kt index 45d9f084fe0..08c5787cc23 100644 --- a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedSupertype.kt +++ b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedSupertype.kt @@ -46,7 +46,7 @@ class Test3 : C.CPublicI, A.AProtectedI { } diff --git a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeAlias.fir.txt b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeAlias.fir.txt index 9eb1e176cc0..2530acdf46b 100644 --- a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeAlias.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeAlias.fir.txt @@ -17,7 +17,7 @@ FILE: exposedTypeAlias.kt super() } - public final typealias AInner = R|A.Inner| + public final typealias AInner = public final inner class Inner : R|kotlin/Any| { public B.constructor(): R|B.Inner| { @@ -35,7 +35,7 @@ FILE: exposedTypeAlias.kt public final typealias BInner = R|B.Inner| } - public final typealias AInner0 = R|A.Inner| + public final typealias AInner0 = public final typealias BInner0 = R|B.Inner| private final typealias MyString = R|kotlin/String| public final fun foo(): R|MyString| { diff --git a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeAlias.kt b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeAlias.kt index aa8ecb763e7..6be2753e91a 100644 --- a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeAlias.kt +++ b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeAlias.kt @@ -2,7 +2,7 @@ class A { private inner class Inner } class B { - typealias AInner = A.Inner + typealias AInner = A.Inner inner class Inner } @@ -10,9 +10,9 @@ class C { typealias BInner = B.Inner } -typealias AInner0 = A.Inner +typealias AInner0 = A.Inner typealias BInner0 = B.Inner private typealias MyString = String -fun foo(): MyString = "" \ No newline at end of file +fun foo(): MyString = "" diff --git a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeParameters.fir.txt b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeParameters.fir.txt index 4acb6369cb2..3acba58ffeb 100644 --- a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeParameters.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeParameters.fir.txt @@ -42,8 +42,8 @@ FILE: exposedTypeParameters.kt } } - public final class Test3 : R|kotlin/Any| { - public constructor(): R|Test3| { + public final class Test3> : R|kotlin/Any| { + public constructor>(): R|Test3| { super() } @@ -80,5 +80,5 @@ FILE: exposedTypeParameters.kt } } - public abstract interface Test8 : R|kotlin/Any| { + public abstract interface Test8, M : R|E|> : R|kotlin/Any| { } diff --git a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeParameters.kt b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeParameters.kt index ef94befdb5e..3c242d65565 100644 --- a/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeParameters.kt +++ b/compiler/fir/analysis-tests/testData/resolve/visibility/exposedTypeParameters.kt @@ -15,7 +15,7 @@ public class Test1A> public class Test2 // invalid, D is protected -public class Test3C.D> +public class Test3C.D> // valid, B is internal internal class Test4 @@ -32,4 +32,4 @@ public class Container : C { } // invalid, A is private, B is internal, D is protected -public interface Test8A, P: B, F: C, N: C.D, M: E> +public interface Test8A, P: B, F: C, N: C.D, M: E> diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/lowPriorityInResolution.fir.txt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/lowPriorityInResolution.fir.txt index 3f5fe2987cd..5b08a48a535 100644 --- a/compiler/fir/analysis-tests/testData/resolveWithStdlib/lowPriorityInResolution.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/lowPriorityInResolution.fir.txt @@ -1,5 +1,5 @@ FILE: lowPriorityInResolution.kt - @R|kotlin/Suppress|(names = vararg(String(INVISIBLE_MEMBER), String(INVISIBLE_REFERENCE))) @R|kotlin/internal/LowPriorityInOverloadResolution|() public final fun foo(): R|kotlin/Int| { + @R|kotlin/Suppress|(names = vararg(String(INVISIBLE_MEMBER), String(INVISIBLE_REFERENCE))) @() public final fun foo(): R|kotlin/Int| { ^foo Int(1) } public final fun foo(): R|kotlin/String| { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 550f6ad757c..63c2977c1ef 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -32275,6 +32275,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/visibility/privateCompanionInSuperClass.kt"); } + @Test + @TestMetadata("privateDeclarationInAnotherFile.kt") + public void testPrivateDeclarationInAnotherFile() throws Exception { + runTest("compiler/testData/diagnostics/tests/visibility/privateDeclarationInAnotherFile.kt"); + } + @Test @TestMetadata("protectedInternal.kt") public void testProtectedInternal() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index 685e0684edc..89020d2ee3e 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -32275,6 +32275,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/visibility/privateCompanionInSuperClass.kt"); } + @Test + @TestMetadata("privateDeclarationInAnotherFile.kt") + public void testPrivateDeclarationInAnotherFile() throws Exception { + runTest("compiler/testData/diagnostics/tests/visibility/privateDeclarationInAnotherFile.kt"); + } + @Test @TestMetadata("protectedInternal.kt") public void testProtectedInternal() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/FirTypeResolver.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/FirTypeResolver.kt index ff1f4bb71ce..0a23e964f90 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/FirTypeResolver.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/FirTypeResolver.kt @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.fir.resolve import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.FirSessionComponent import org.jetbrains.kotlin.fir.declarations.FirFile +import org.jetbrains.kotlin.fir.diagnostics.ConeDiagnostic import org.jetbrains.kotlin.fir.resolve.transformers.ScopeClassDeclaration import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.FirTypeRef @@ -20,7 +21,7 @@ abstract class FirTypeResolver : FirSessionComponent { isOperandOfIsOperator: Boolean, useSiteFile: FirFile?, supertypeSupplier: SupertypeSupplier - ): ConeKotlinType + ): Pair } val FirSession.typeResolver: FirTypeResolver by FirSession.sessionComponentAccessor() diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirTypeResolverImpl.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirTypeResolverImpl.kt index 1253845ac76..611cf2ac509 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirTypeResolverImpl.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirTypeResolverImpl.kt @@ -6,19 +6,19 @@ package org.jetbrains.kotlin.fir.resolve.providers.impl import org.jetbrains.kotlin.KtSourceElement -import org.jetbrains.kotlin.fir.* +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.ThreadSafeMutableState import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.impl.FirOuterClassTypeParameterRef import org.jetbrains.kotlin.fir.declarations.utils.isEnumClass import org.jetbrains.kotlin.fir.declarations.utils.isLocal +import org.jetbrains.kotlin.fir.diagnostics.ConeDiagnostic import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic import org.jetbrains.kotlin.fir.diagnostics.ConeUnexpectedTypeArgumentsError import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind +import org.jetbrains.kotlin.fir.render import org.jetbrains.kotlin.fir.resolve.* -import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeOuterClassArgumentsRequired -import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeUnresolvedQualifierError -import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeUnsupportedDynamicType -import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeWrongNumberOfTypeArgumentsError +import org.jetbrains.kotlin.fir.resolve.diagnostics.* import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor import org.jetbrains.kotlin.fir.resolve.transformers.ScopeClassDeclaration @@ -30,8 +30,10 @@ import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl import org.jetbrains.kotlin.fir.types.impl.ConeTypeParameterTypeImpl import org.jetbrains.kotlin.fir.types.impl.FirImplicitBuiltinTypeRef +import org.jetbrains.kotlin.fir.visibilityChecker import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.StandardClassIds +import org.jetbrains.kotlin.utils.addToStdlib.runIf @ThreadSafeMutableState class FirTypeResolverImpl(private val session: FirSession) : FirTypeResolver() { @@ -101,18 +103,18 @@ class FirTypeResolverImpl(private val session: FirSession) : FirTypeResolver() { scopeClassDeclaration: ScopeClassDeclaration, useSiteFile: FirFile?, supertypeSupplier: SupertypeSupplier - ): Pair?, ConeSubstitutor?> { + ): Triple?, ConeSubstitutor?, ConeDiagnostic?> { return when (typeRef) { is FirResolvedTypeRef -> { val resultSymbol = typeRef.coneTypeSafe()?.lookupTag?.let(symbolProvider::getSymbolByLookupTag) - resultSymbol to null + Triple(resultSymbol, null, null) } is FirUserTypeRef -> { val qualifierResolver = session.qualifierResolver var acceptedSymbol: FirBasedSymbol<*>? = null - var firstNonApplicable: FirBasedSymbol<*>? = null var substitutor: ConeSubstitutor? = null + val notApplicableSymbols = mutableListOf, ConeSubstitutor>>() val qualifier = typeRef.qualifier val scopes = scopeClassDeclaration.scopes val containingDeclarations = scopeClassDeclaration.containingDeclarations @@ -121,36 +123,48 @@ class FirTypeResolverImpl(private val session: FirSession) : FirTypeResolver() { if (acceptedSymbol != null) { break } + val collectNonApplicable = notApplicableSymbols.isEmpty() scope.processClassifiersByNameWithSubstitution(qualifier.first().name) { symbol, substitutorFromScope -> - if (acceptedSymbol != null) { - return@processClassifiersByNameWithSubstitution - } - + if (acceptedSymbol != null) return@processClassifiersByNameWithSubstitution val resolvedSymbol = resolveSymbol(symbol, qualifier, qualifierResolver) + ?: return@processClassifiersByNameWithSubstitution if (resolvedSymbol.isVisible(useSiteFile, containingDeclarations, supertypeSupplier)) { acceptedSymbol = resolvedSymbol substitutor = substitutorFromScope - } else { - firstNonApplicable = resolvedSymbol + } else if (collectNonApplicable) { + resolvedSymbol.isVisible(useSiteFile, containingDeclarations, supertypeSupplier) + notApplicableSymbols += resolvedSymbol to substitutorFromScope } } } - if (acceptedSymbol == null) { - acceptedSymbol = firstNonApplicable - } + when { + acceptedSymbol != null -> Triple(acceptedSymbol, substitutor, null) - // TODO: Imports - val resultSymbol: FirBasedSymbol<*>? = acceptedSymbol ?: qualifierResolver.resolveSymbol(qualifier) - resultSymbol to substitutor + notApplicableSymbols.size == 1 -> { + val (notApplicableSymbol, resultingSubstitutor) = notApplicableSymbols.single() + Triple(notApplicableSymbol, resultingSubstitutor, ConeVisibilityError(notApplicableSymbol)) + } + + else -> { + val symbolFromQualifier = qualifierResolver.resolveSymbol(qualifier) + val diagnostic = runIf( + symbolFromQualifier != null && + !symbolFromQualifier.isVisible(useSiteFile, containingDeclarations, supertypeSupplier) + ) { + ConeVisibilityError(symbolFromQualifier!!) + } + Triple(symbolFromQualifier, null, diagnostic) + } + } } is FirImplicitBuiltinTypeRef -> { - resolveBuiltInQualified(typeRef.id, session) to null + Triple(resolveBuiltInQualified(typeRef.id, session), null, null) } - else -> null to null + else -> Triple(null, null, null) } } @@ -455,11 +469,11 @@ class FirTypeResolverImpl(private val session: FirSession) : FirTypeResolver() { isOperandOfIsOperator: Boolean, useSiteFile: FirFile?, supertypeSupplier: SupertypeSupplier - ): ConeKotlinType { + ): Pair { return when (typeRef) { - is FirResolvedTypeRef -> typeRef.type + is FirResolvedTypeRef -> typeRef.type to null is FirUserTypeRef -> { - val (symbol, substitutor) = resolveToSymbol(typeRef, scopeClassDeclaration, useSiteFile, supertypeSupplier) + val (symbol, substitutor, diagnostic) = resolveToSymbol(typeRef, scopeClassDeclaration, useSiteFile, supertypeSupplier) resolveUserType( typeRef, symbol, @@ -467,10 +481,10 @@ class FirTypeResolverImpl(private val session: FirSession) : FirTypeResolver() { areBareTypesAllowed, scopeClassDeclaration.topContainer ?: scopeClassDeclaration.containingDeclarations.lastOrNull(), isOperandOfIsOperator - ) + ) to diagnostic } - is FirFunctionTypeRef -> createFunctionalType(typeRef) - is FirDynamicTypeRef -> ConeKotlinErrorType(ConeUnsupportedDynamicType()) + is FirFunctionTypeRef -> createFunctionalType(typeRef) to null + is FirDynamicTypeRef -> ConeKotlinErrorType(ConeUnsupportedDynamicType()) to null else -> error(typeRef.render()) } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSpecificTypeResolverTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSpecificTypeResolverTransformer.kt index e6987282a2e..343928d00d5 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSpecificTypeResolverTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSpecificTypeResolverTransformer.kt @@ -11,6 +11,7 @@ import org.jetbrains.kotlin.fakeElement import org.jetbrains.kotlin.fir.* import org.jetbrains.kotlin.fir.declarations.FirFile import org.jetbrains.kotlin.fir.declarations.FirResolvePhase +import org.jetbrains.kotlin.fir.diagnostics.ConeDiagnostic import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic import org.jetbrains.kotlin.fir.diagnostics.ConeUnexpectedTypeArgumentsError import org.jetbrains.kotlin.fir.resolve.SupertypeSupplier @@ -77,17 +78,15 @@ class FirSpecificTypeResolverTransformer( withBareTypes(allowed = false) { typeRef.transformChildren(this, data) } - return transformType( + val (resolvedType, diagnostic) = typeResolver.resolveType( typeRef, - typeResolver.resolveType( - typeRef, - data, - areBareTypesAllowed, - isOperandOfIsOperator, - currentFile, - supertypeSupplier - ) + data, + areBareTypesAllowed, + isOperandOfIsOperator, + currentFile, + supertypeSupplier ) + return transformType(typeRef, resolvedType, diagnostic) } @OptIn(PrivateForInline::class) @@ -98,15 +97,17 @@ class FirSpecificTypeResolverTransformer( functionTypeRef.transformChildren(this, data) val scopeOwnerLookupNames = data.scopes.flatMap { it.scopeOwnerLookupNames } session.lookupTracker?.recordTypeLookup(functionTypeRef, scopeOwnerLookupNames, currentFile?.source) - val resolvedType = typeResolver.resolveType( + val resolvedTypeWithDiagnostic = typeResolver.resolveType( functionTypeRef, data, areBareTypesAllowed, isOperandOfIsOperator, currentFile, supertypeSupplier - ).takeIfAcceptable() - return if (resolvedType != null && resolvedType !is ConeClassErrorType) { + ) + val resolvedType = resolvedTypeWithDiagnostic.first.takeIfAcceptable() + val diagnostic = resolvedTypeWithDiagnostic.second + return if (resolvedType != null && resolvedType !is ConeClassErrorType && diagnostic == null) { buildResolvedTypeRef { source = functionTypeRef.source type = resolvedType @@ -119,39 +120,51 @@ class FirSpecificTypeResolverTransformer( if (resolvedType != null) { type = resolvedType } - diagnostic = (resolvedType as? ConeClassErrorType)?.diagnostic - ?: ConeSimpleDiagnostic("Unresolved functional type: ${functionTypeRef.render()}") + this.diagnostic = diagnostic ?: (resolvedType as? ConeClassErrorType)?.diagnostic + ?: ConeSimpleDiagnostic("Unresolved functional type: ${functionTypeRef.render()}") } } } - private fun transformType(typeRef: FirTypeRef, resolvedType: ConeKotlinType): FirResolvedTypeRef { - return if (resolvedType !is ConeClassErrorType) { - buildResolvedTypeRef { - source = typeRef.source - type = resolvedType - annotations += typeRef.annotations - delegatedTypeRef = typeRef - } - } else { - buildErrorTypeRef { - val typeRefSourceKind = typeRef.source?.kind - val diagnosticSource = resolvedType.diagnostic.safeAs() - ?.source.safeAs() + private fun transformType(typeRef: FirTypeRef, resolvedType: ConeKotlinType, diagnostic: ConeDiagnostic?): FirResolvedTypeRef { + return when { + resolvedType is ConeClassErrorType -> { + buildErrorTypeRef { + val typeRefSourceKind = typeRef.source?.kind + val diagnosticSource = resolvedType.diagnostic.safeAs() + ?.source.safeAs() - source = if (diagnosticSource != null) { - if (typeRefSourceKind is KtFakeSourceElementKind) { - diagnosticSource.fakeElement(typeRefSourceKind) + source = if (diagnosticSource != null) { + if (typeRefSourceKind is KtFakeSourceElementKind) { + diagnosticSource.fakeElement(typeRefSourceKind) + } else { + diagnosticSource + } } else { - diagnosticSource + typeRef.source } - } else { - typeRef.source - } delegatedTypeRef = typeRef + type = resolvedType + + this.diagnostic = resolvedType.diagnostic + } + } + diagnostic != null -> { + buildErrorTypeRef { + source = typeRef.source + this.diagnostic = diagnostic type = resolvedType - diagnostic = resolvedType.diagnostic + delegatedTypeRef = typeRef + } + } + else -> { + buildResolvedTypeRef { + source = typeRef.source + type = resolvedType + annotations += typeRef.annotations + delegatedTypeRef = typeRef + } } } } diff --git a/compiler/testData/diagnostics/tests/annotations/requireKotlin.fir.kt b/compiler/testData/diagnostics/tests/annotations/requireKotlin.fir.kt index 06734896514..a57ce10c79e 100644 --- a/compiler/testData/diagnostics/tests/annotations/requireKotlin.fir.kt +++ b/compiler/testData/diagnostics/tests/annotations/requireKotlin.fir.kt @@ -2,11 +2,11 @@ package kotlin.io import kotlin.internal.* -@RequireKotlin("1.x") +@RequireKotlin("1.x") class IllegalVersion() -@RequireKotlin("1.2") +@RequireKotlin("1.2") class LegalMinimum() -@RequireKotlin("1.2", versionKind = RequireKotlinVersionKind.COMPILER_VERSION, message = "Requires newer compiler version to be inlined correctly.") +@RequireKotlin("1.2", versionKind = RequireKotlinVersionKind.COMPILER_VERSION, message = "Requires newer compiler version to be inlined correctly.") class LegalStdLib() diff --git a/compiler/testData/diagnostics/tests/imports/ImportProtectedClass.fir.kt b/compiler/testData/diagnostics/tests/imports/ImportProtectedClass.fir.kt index bad5d7c283b..db91b0b2d13 100644 --- a/compiler/testData/diagnostics/tests/imports/ImportProtectedClass.fir.kt +++ b/compiler/testData/diagnostics/tests/imports/ImportProtectedClass.fir.kt @@ -10,5 +10,5 @@ class Bar: Foo() { protected fun foo(): Nested? = null } -private fun foo(): Nested? = null -private fun bar(): p.Foo.Nested? = null \ No newline at end of file +private fun foo(): Nested? = null +private fun bar(): p.Foo.Nested? = null diff --git a/compiler/testData/diagnostics/tests/imports/InaccessiblePrivateClass.fir.kt b/compiler/testData/diagnostics/tests/imports/InaccessiblePrivateClass.fir.kt index a8f68e5484f..9fc4628950d 100644 --- a/compiler/testData/diagnostics/tests/imports/InaccessiblePrivateClass.fir.kt +++ b/compiler/testData/diagnostics/tests/imports/InaccessiblePrivateClass.fir.kt @@ -15,4 +15,4 @@ package p1 import p2.* val x: X = X() -val y: Y = Y() +val y: Y = Y() diff --git a/compiler/testData/diagnostics/tests/imports/PackageLocalClassReferencedError.fir.kt b/compiler/testData/diagnostics/tests/imports/PackageLocalClassReferencedError.fir.kt index 043c14b4f00..8210bd78380 100644 --- a/compiler/testData/diagnostics/tests/imports/PackageLocalClassReferencedError.fir.kt +++ b/compiler/testData/diagnostics/tests/imports/PackageLocalClassReferencedError.fir.kt @@ -8,4 +8,4 @@ package a import pack1.* -private class X : SomeClass() +private class X : SomeClass() diff --git a/compiler/testData/diagnostics/tests/imports/PrivateClassReferencedError.fir.kt b/compiler/testData/diagnostics/tests/imports/PrivateClassReferencedError.fir.kt index 8b3da8f5b2a..3aaf0cf0583 100644 --- a/compiler/testData/diagnostics/tests/imports/PrivateClassReferencedError.fir.kt +++ b/compiler/testData/diagnostics/tests/imports/PrivateClassReferencedError.fir.kt @@ -11,4 +11,4 @@ package a import pack1.SomeClass.* -private class X : N() +private class X : N() diff --git a/compiler/testData/diagnostics/tests/inference/upperBounds/typeParameterAsUpperBound.fir.kt b/compiler/testData/diagnostics/tests/inference/upperBounds/typeParameterAsUpperBound.fir.kt index c37f1768c20..a2b6bafc8fd 100644 --- a/compiler/testData/diagnostics/tests/inference/upperBounds/typeParameterAsUpperBound.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/upperBounds/typeParameterAsUpperBound.fir.kt @@ -1,7 +1,7 @@ // !CHECK_TYPE -@kotlin.internal.InlineOnly +@kotlin.internal.InlineOnly public inline fun C.ifEmpty(f: () -> R): R where C : Collection<*>, C : R = if (isEmpty()) f() else this public fun listOf(t: T): List = TODO() diff --git a/compiler/testData/diagnostics/tests/j+k/innerNestedClassFromJava.fir.kt b/compiler/testData/diagnostics/tests/j+k/innerNestedClassFromJava.fir.kt index 22576156bde..e3d7761ea36 100644 --- a/compiler/testData/diagnostics/tests/j+k/innerNestedClassFromJava.fir.kt +++ b/compiler/testData/diagnostics/tests/j+k/innerNestedClassFromJava.fir.kt @@ -25,8 +25,8 @@ package b fun f() { val c1: a.M.Inner val c2: a.M.Nested - val c3: a.M.PrInner - val c4: a.M.PrNested + val c3: a.M.PrInner + val c4: a.M.PrNested } diff --git a/compiler/testData/diagnostics/tests/javac/imports/ImportProtectedClass.fir.kt b/compiler/testData/diagnostics/tests/javac/imports/ImportProtectedClass.fir.kt index 71fb84a31dc..e7a90ee7eea 100644 --- a/compiler/testData/diagnostics/tests/javac/imports/ImportProtectedClass.fir.kt +++ b/compiler/testData/diagnostics/tests/javac/imports/ImportProtectedClass.fir.kt @@ -15,5 +15,5 @@ class Bar : Foo() { protected fun foo(): Nested? = null } -private fun foo(): Nested? = null -private fun bar(): p.Foo.Nested? = null +private fun foo(): Nested? = null +private fun bar(): p.Foo.Nested? = null diff --git a/compiler/testData/diagnostics/tests/javac/imports/PackagePrivateAndPublicNested.fir.kt b/compiler/testData/diagnostics/tests/javac/imports/PackagePrivateAndPublicNested.fir.kt index adb9f0540ab..fc77f1a3a49 100644 --- a/compiler/testData/diagnostics/tests/javac/imports/PackagePrivateAndPublicNested.fir.kt +++ b/compiler/testData/diagnostics/tests/javac/imports/PackagePrivateAndPublicNested.fir.kt @@ -11,9 +11,9 @@ package a import p.Foo import p.Foo.Nested -class Bar : Foo() { - protected fun foo(): Nested? = null +class Bar : Foo() { + protected fun foo(): Nested? = null } -private fun foo(): Nested? = null -private fun bar(): p.Foo.Nested? = null +private fun foo(): Nested? = null +private fun bar(): p.Foo.Nested? = null diff --git a/compiler/testData/diagnostics/tests/privateInFile/topLevelAnnotationCall.fir.kt b/compiler/testData/diagnostics/tests/privateInFile/topLevelAnnotationCall.fir.kt index f95ca28dae9..88ec8703ef4 100644 --- a/compiler/testData/diagnostics/tests/privateInFile/topLevelAnnotationCall.fir.kt +++ b/compiler/testData/diagnostics/tests/privateInFile/topLevelAnnotationCall.fir.kt @@ -17,11 +17,11 @@ class C1 // FILE: 2.kt package pp -@A(foo) +@A(foo) fun f2() {} -@A(foo) +@A(foo) val p2 = "" -@A(foo) +@A(foo) class C2 diff --git a/compiler/testData/diagnostics/tests/privateInFile/visibility.fir.kt b/compiler/testData/diagnostics/tests/privateInFile/visibility.fir.kt index 67518456343..9fb008ec5e4 100644 --- a/compiler/testData/diagnostics/tests/privateInFile/visibility.fir.kt +++ b/compiler/testData/diagnostics/tests/privateInFile/visibility.fir.kt @@ -32,7 +32,7 @@ fun test() { y.bar() foo() - val u : A = A() + val u : A = A() val z = x x = 30 @@ -43,7 +43,7 @@ fun test() { xx = 40 } -class B : A() {} +class B : A() {} class Q { class W { diff --git a/compiler/testData/diagnostics/tests/scopes/visibility2.fir.kt b/compiler/testData/diagnostics/tests/scopes/visibility2.fir.kt index 81d23b2c0d2..7356323953d 100644 --- a/compiler/testData/diagnostics/tests/scopes/visibility2.fir.kt +++ b/compiler/testData/diagnostics/tests/scopes/visibility2.fir.kt @@ -29,13 +29,13 @@ fun test() { y.bar() foo() - val u : A = A() - val a : java.util.Arrays.ArrayList; + val u : A = A() + val a : java.util.Arrays.ArrayList; val po = PO } -class B : A() {} +class B : A() {} class Q { class W { diff --git a/compiler/testData/diagnostics/tests/scopes/visibility3.fir.kt b/compiler/testData/diagnostics/tests/scopes/visibility3.fir.kt index 0558ddecc90..0b0c86b8c7c 100644 --- a/compiler/testData/diagnostics/tests/scopes/visibility3.fir.kt +++ b/compiler/testData/diagnostics/tests/scopes/visibility3.fir.kt @@ -28,7 +28,7 @@ fun test() { y.bar() foo() - val u : A = A() + val u : A = A() val z = x x = 30 @@ -36,7 +36,7 @@ fun test() { val po = PO } -class B : A() {} +class B : A() {} class Q { class W { diff --git a/compiler/testData/diagnostics/tests/typealias/constructorCallThroughPrivateAlias.fir.kt b/compiler/testData/diagnostics/tests/typealias/constructorCallThroughPrivateAlias.fir.kt index bc72f271c10..7def821f1d5 100644 --- a/compiler/testData/diagnostics/tests/typealias/constructorCallThroughPrivateAlias.fir.kt +++ b/compiler/testData/diagnostics/tests/typealias/constructorCallThroughPrivateAlias.fir.kt @@ -30,5 +30,5 @@ fun baz() { a.A3("") checkType { _() } - val x: a.A2 = B("") // A2 is unresolved because it's private in file, OK + val x: a.A2 = B("") // A2 is unresolved because it's private in file, OK } diff --git a/compiler/testData/diagnostics/tests/typealias/privateInFile.fir.kt b/compiler/testData/diagnostics/tests/typealias/privateInFile.fir.kt index 8524ddd6fa6..ecdbd0134b3 100644 --- a/compiler/testData/diagnostics/tests/typealias/privateInFile.fir.kt +++ b/compiler/testData/diagnostics/tests/typealias/privateInFile.fir.kt @@ -4,12 +4,12 @@ private class C { companion object } -private typealias TA = C +private typealias TA = C -private val test1: C = C() -private val test1co: C.Companion = C +private val test1: C = C() +private val test1co: C.Companion = C -private val test2: TA = TA() +private val test2: TA = TA() private val test2co = TA // FILE: file2.kt diff --git a/compiler/testData/diagnostics/tests/visibility/privateDeclarationInAnotherFile.fir.kt b/compiler/testData/diagnostics/tests/visibility/privateDeclarationInAnotherFile.fir.kt new file mode 100644 index 00000000000..91bbebaf260 --- /dev/null +++ b/compiler/testData/diagnostics/tests/visibility/privateDeclarationInAnotherFile.fir.kt @@ -0,0 +1,13 @@ +// FILE: 1.kt + +private class Private { + class Public +} + +// FILE: 2.kt + +import Private.Public + +private fun test_1(x: Private.Public, y: Public) { + +} diff --git a/compiler/testData/diagnostics/tests/visibility/privateDeclarationInAnotherFile.kt b/compiler/testData/diagnostics/tests/visibility/privateDeclarationInAnotherFile.kt new file mode 100644 index 00000000000..8aad93e9e35 --- /dev/null +++ b/compiler/testData/diagnostics/tests/visibility/privateDeclarationInAnotherFile.kt @@ -0,0 +1,13 @@ +// FILE: 1.kt + +private class Private { + class Public +} + +// FILE: 2.kt + +import Private.Public + +private fun test_1(x: Private.Public, y: Public) { + +} diff --git a/compiler/testData/diagnostics/tests/visibility/privateDeclarationInAnotherFile.txt b/compiler/testData/diagnostics/tests/visibility/privateDeclarationInAnotherFile.txt new file mode 100644 index 00000000000..490f22d193f --- /dev/null +++ b/compiler/testData/diagnostics/tests/visibility/privateDeclarationInAnotherFile.txt @@ -0,0 +1,17 @@ +package + +private fun test_1(/*0*/ x: Private.Public, /*1*/ y: Private.Public): kotlin.Unit + +private final class Private { + public constructor Private() + 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 Public { + public constructor Public() + 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 + } +} diff --git a/compiler/testData/diagnostics/testsWithStdLib/InaccessibleInternalClass.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/InaccessibleInternalClass.fir.kt index f8ac5490bdf..eed901d7436 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/InaccessibleInternalClass.fir.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/InaccessibleInternalClass.fir.kt @@ -11,5 +11,5 @@ import p.* interface I { val v1: FilteringSequence - val v2: IndexingSequence + val v2: IndexingSequence } diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index fec6ee12225..8eef8d49439 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -32371,6 +32371,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/visibility/privateCompanionInSuperClass.kt"); } + @Test + @TestMetadata("privateDeclarationInAnotherFile.kt") + public void testPrivateDeclarationInAnotherFile() throws Exception { + runTest("compiler/testData/diagnostics/tests/visibility/privateDeclarationInAnotherFile.kt"); + } + @Test @TestMetadata("protectedInternal.kt") public void testProtectedInternal() throws Exception {