Native: make forward declarations expect in metadata compilations

If we are compiling metadata, make synthetic forward declarations
`expect`, because otherwise `getFirstClassifierDiscriminateHeaders`
would prefer it over a "real" `expect` declaration from a commonized
interop library, which would ruin the whole idea of using synthetic
forward declarations only when no proper definitions are found.

^KT-51377 Fixed
This commit is contained in:
Svyatoslav Scherbina
2022-03-11 15:09:38 +00:00
committed by Space
parent 76fc4328d1
commit 54fb4d9b70
4 changed files with 33 additions and 10 deletions
@@ -32,7 +32,8 @@ interface KlibResolvedModuleDescriptorsFactory {
languageVersionSettings: LanguageVersionSettings, languageVersionSettings: LanguageVersionSettings,
friendModuleFiles: Set<File>, friendModuleFiles: Set<File>,
includedLibraryFiles: Set<File>, includedLibraryFiles: Set<File>,
additionalDependencyModules: Iterable<ModuleDescriptorImpl> additionalDependencyModules: Iterable<ModuleDescriptorImpl>,
isForMetadataCompilation: Boolean,
): KotlinResolvedModuleDescriptors ): KotlinResolvedModuleDescriptors
} }
@@ -190,6 +190,7 @@ class KlibMetadataModuleDescriptorFactoryImpl(
} ?: provider } ?: provider
} }
// Used from IDEA plugin.
fun createForwardDeclarationHackPackagePartProvider( fun createForwardDeclarationHackPackagePartProvider(
storageManager: StorageManager, storageManager: StorageManager,
module: ModuleDescriptorImpl module: ModuleDescriptorImpl
@@ -200,7 +201,8 @@ class KlibMetadataModuleDescriptorFactoryImpl(
module, module,
fqName, fqName,
Name.identifier(supertypeName), Name.identifier(supertypeName),
classKind classKind,
isExpect = true
) )
val packageFragmentProvider = PackageFragmentProviderImpl( val packageFragmentProvider = PackageFragmentProviderImpl(
@@ -40,7 +40,8 @@ class KlibResolvedModuleDescriptorsFactoryImpl(
languageVersionSettings: LanguageVersionSettings, languageVersionSettings: LanguageVersionSettings,
friendModuleFiles: Set<File>, friendModuleFiles: Set<File>,
includedLibraryFiles: Set<File>, includedLibraryFiles: Set<File>,
additionalDependencyModules: Iterable<ModuleDescriptorImpl> additionalDependencyModules: Iterable<ModuleDescriptorImpl>,
isForMetadataCompilation: Boolean,
): KotlinResolvedModuleDescriptors { ): KotlinResolvedModuleDescriptors {
val moduleDescriptors = mutableListOf<ModuleDescriptorImpl>() val moduleDescriptors = mutableListOf<ModuleDescriptorImpl>()
@@ -69,7 +70,20 @@ class KlibResolvedModuleDescriptorsFactoryImpl(
} }
} }
val forwardDeclarationsModule = createForwardDeclarationsModule(builtIns, storageManager) val forwardDeclarationsModule = createForwardDeclarationsModule(
builtIns,
storageManager,
// If we are compiling metadata, make synthetic forward declarations `expect`,
// because otherwise `getFirstClassifierDiscriminateHeaders` would prefer it over a
// "real" `expect` declaration from a commonized interop library, which would ruin
// the whole idea of using synthetic forward declarations only when no proper definitions
// are found.
//
// If we are compiling for the actual native platform, continue using non-expect
// forward declarations (to prevent getting non-actualized expects into the backend,
// and to prevent related klib signature changes).
isExpect = isForMetadataCompilation,
)
// Set inter-dependencies between module descriptors, add forwarding declarations module. // Set inter-dependencies between module descriptors, add forwarding declarations module.
for (module in moduleDescriptors) { for (module in moduleDescriptors) {
@@ -88,7 +102,8 @@ class KlibResolvedModuleDescriptorsFactoryImpl(
fun createForwardDeclarationsModule( fun createForwardDeclarationsModule(
builtIns: KotlinBuiltIns?, builtIns: KotlinBuiltIns?,
storageManager: StorageManager storageManager: StorageManager,
isExpect: Boolean
): ModuleDescriptorImpl { ): ModuleDescriptorImpl {
val module = createDescriptorOptionalBuiltsIns(FORWARD_DECLARATIONS_MODULE_NAME, storageManager, builtIns, SyntheticModulesOrigin) val module = createDescriptorOptionalBuiltsIns(FORWARD_DECLARATIONS_MODULE_NAME, storageManager, builtIns, SyntheticModulesOrigin)
@@ -99,7 +114,8 @@ class KlibResolvedModuleDescriptorsFactoryImpl(
module, module,
fqName, fqName,
Name.identifier(supertypeName), Name.identifier(supertypeName),
classKind classKind,
isExpect
) )
val packageFragmentProvider = PackageFragmentProviderImpl( val packageFragmentProvider = PackageFragmentProviderImpl(
@@ -150,7 +166,8 @@ class ForwardDeclarationsPackageFragmentDescriptor(
module: ModuleDescriptor, module: ModuleDescriptor,
fqName: FqName, fqName: FqName,
supertypeName: Name, supertypeName: Name,
classKind: ClassKind classKind: ClassKind,
isExpect: Boolean
) : PackageFragmentDescriptorImpl(module, fqName) { ) : PackageFragmentDescriptorImpl(module, fqName) {
private val memberScope = object : MemberScopeImpl() { private val memberScope = object : MemberScopeImpl() {
@@ -166,7 +183,7 @@ class ForwardDeclarationsPackageFragmentDescriptor(
} }
private fun createDeclaration(name: Name): ClassDescriptor { private fun createDeclaration(name: Name): ClassDescriptor {
return ClassDescriptorImpl( return object : ClassDescriptorImpl(
this@ForwardDeclarationsPackageFragmentDescriptor, this@ForwardDeclarationsPackageFragmentDescriptor,
name, name,
Modality.FINAL, Modality.FINAL,
@@ -175,7 +192,9 @@ class ForwardDeclarationsPackageFragmentDescriptor(
SourceElement.NO_SOURCE, SourceElement.NO_SOURCE,
false, false,
LockBasedStorageManager.NO_LOCKS LockBasedStorageManager.NO_LOCKS
).apply { ) {
override fun isExpect(): Boolean = isExpect
}.apply {
this.initialize(MemberScope.Empty, emptySet(), null) this.initialize(MemberScope.Empty, emptySet(), null)
} }
} }
@@ -41,7 +41,8 @@ internal object TopDownAnalyzerFacadeForKonan {
val resolvedModuleDescriptors = nativeFactories.DefaultResolvedDescriptorsFactory.createResolved( val resolvedModuleDescriptors = nativeFactories.DefaultResolvedDescriptorsFactory.createResolved(
config.resolvedLibraries, projectContext.storageManager, module.builtIns, config.languageVersionSettings, config.resolvedLibraries, projectContext.storageManager, module.builtIns, config.languageVersionSettings,
config.friendModuleFiles, config.resolve.includedLibraries.map { it.libraryFile }.toSet(), listOf(module)) config.friendModuleFiles, config.resolve.includedLibraries.map { it.libraryFile }.toSet(), listOf(module),
isForMetadataCompilation = config.metadataKlib)
val additionalPackages = mutableListOf<PackageFragmentProvider>() val additionalPackages = mutableListOf<PackageFragmentProvider>()
if (!module.isNativeStdlib()) { if (!module.isNativeStdlib()) {