Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Experiment - Update to Kotlin 1.9.30-dev-3330 #37

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions arrow-reflect-annotations/src/main/kotlin/arrow/meta/Meta.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ annotation class Meta {
newElement: FirFunctionCall
): FirCall {
val args = newElement.arguments
val argsApplied = args.mapIndexed { n, expr -> "args[$n] as ${+expr.typeRef}" }
val argsApplied = args.mapIndexed { n, expr -> "args[$n] as ${+expr.resolvedType}" }
val name = newElement.toResolvedCallableSymbol()?.callableId?.asSingleFqName()?.asString()

return compile(
Expand Down Expand Up @@ -182,7 +182,7 @@ annotation class Meta {
}

interface ArrayOfCall : FrontendTransformer {
fun FirMetaCheckerContext.arrayOfCall(arrayOfCall: FirArrayOfCall): FirStatement
fun FirMetaCheckerContext.arrayOfCall(arrayOfCall: FirArrayLiteral): FirStatement
}

interface AssignmentOperatorStatement : FrontendTransformer {
Expand Down Expand Up @@ -394,7 +394,7 @@ annotation class Meta {
}

interface QualifiedAccess : FrontendTransformer {
fun FirMetaCheckerContext.qualifiedAccess(qualifiedAccess: FirQualifiedAccess): FirStatement
fun FirMetaCheckerContext.qualifiedAccess(qualifiedAccess: FirQualifiedAccessExpression): FirStatement
}

interface QualifiedAccessExpression : FrontendTransformer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.jetbrains.kotlin.fir.extensions.MemberGenerationContext
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.coneType
import org.jetbrains.kotlin.fir.types.renderReadableWithFqNames
Expand Down Expand Up @@ -128,6 +129,9 @@ abstract class FirMetaContext(
?: source?.text?.toString()
?: error("$this has no source psi text element")

operator fun ConeKotlinType.unaryPlus(): String =
type.renderReadableWithFqNames().replace("/", ".")

val String.call: FirCall
get() =
compile(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:OptIn(PrivateForInline::class)

package arrow.meta

import com.intellij.psi.PsiDocumentManager
Expand All @@ -6,21 +8,29 @@ import org.jetbrains.kotlin.KtInMemoryTextSourceFile
import org.jetbrains.kotlin.KtIoFileSourceFile
import org.jetbrains.kotlin.KtSourceFile
import org.jetbrains.kotlin.backend.jvm.JvmIrDeserializerImpl
import org.jetbrains.kotlin.backend.jvm.JvmIrTypeSystemContext
import org.jetbrains.kotlin.builtins.DefaultBuiltIns
import org.jetbrains.kotlin.cli.common.messages.*
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.*
import org.jetbrains.kotlin.cli.common.modules.ModuleBuilder
import org.jetbrains.kotlin.cli.jvm.compiler.*
import org.jetbrains.kotlin.config.*
import org.jetbrains.kotlin.constant.EvaluatedConstTracker
import org.jetbrains.kotlin.diagnostics.*
import org.jetbrains.kotlin.diagnostics.impl.BaseDiagnosticsCollector
import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.analysis.collectors.FirDiagnosticsCollector
import org.jetbrains.kotlin.fir.backend.Fir2IrConfiguration
import org.jetbrains.kotlin.fir.backend.Fir2IrResult
import org.jetbrains.kotlin.fir.backend.jvm.FirJvmVisibilityConverter
import org.jetbrains.kotlin.fir.backend.jvm.JvmFir2IrExtensions
import org.jetbrains.kotlin.fir.builder.AbstractRawFirBuilder
import org.jetbrains.kotlin.fir.builder.BodyBuildingMode
import org.jetbrains.kotlin.fir.builder.RawFirBuilder
import org.jetbrains.kotlin.fir.builder.PsiRawFirBuilder
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.lightTree.LightTree2Fir
import org.jetbrains.kotlin.fir.pipeline.Fir2IrActualizedResult
import org.jetbrains.kotlin.fir.pipeline.ModuleCompilerAnalyzedOutput
import org.jetbrains.kotlin.fir.resolve.*
import org.jetbrains.kotlin.fir.resolve.calls.ImplicitDispatchReceiverValue
import org.jetbrains.kotlin.fir.resolve.dfa.DataFlowAnalyzerContext
Expand Down Expand Up @@ -53,6 +63,12 @@ import org.jetbrains.kotlin.readSourceFileWithMapping
import java.io.File
import java.util.concurrent.atomic.AtomicInteger
import org.jetbrains.kotlin.fir.pipeline.convertToIrAndActualize
import org.jetbrains.kotlin.fir.pipeline.convertToIrAndActualizeForJvm
import org.jetbrains.kotlin.fir.resolve.providers.FirSymbolProvider
import org.jetbrains.kotlin.fir.resolve.providers.impl.FirCachingCompositeSymbolProvider
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
import org.jetbrains.kotlin.util.PrivateForInline
import kotlin.reflect.jvm.internal.impl.builtins.jvm.JvmBuiltIns

class FirResult(
val session: FirSession,
Expand Down Expand Up @@ -87,7 +103,7 @@ class TemplateCompiler(

data class TemplateResult(
val firResults: List<FirResult>,
val irResults: List<Fir2IrResult>
val irResults: List<Fir2IrActualizedResult>
)

var compiling: Boolean = false
Expand All @@ -105,7 +121,7 @@ class TemplateCompiler(
println("parsing source:\n$source")
println("session: ${session::class}")
val outputs: ArrayList<FirResult> = arrayListOf()
val irOutput: ArrayList<Fir2IrResult> = arrayListOf()
val irOutput: ArrayList<Fir2IrActualizedResult> = arrayListOf()
val messageCollector: MessageCollector = MessageCollector.NONE
for (module in chunk) {
val moduleConfiguration = projectConfiguration//.applyModuleProperties(module, buildFile)
Expand All @@ -121,7 +137,7 @@ class TemplateCompiler(

if (produceIr) {
outputs.forEach {
irOutput.add(convertToIR(it, moduleConfiguration))
irOutput.add(convertToIR(metaCheckerContext, it, moduleConfiguration))
}
}
}
Expand All @@ -147,18 +163,46 @@ class TemplateCompiler(
return firResult
}

fun convertToIR(firResult: FirResult, moduleConfiguration: CompilerConfiguration): Fir2IrResult {
fun convertToIR(metaCheckerContext: FirMetaCheckerContext?, firResult: FirResult, moduleConfiguration: CompilerConfiguration): Fir2IrActualizedResult {
val diagnosticReporter = DiagnosticReporterFactory.createPendingReporter()
val fir2IrExtensions = JvmFir2IrExtensions(moduleConfiguration, JvmIrDeserializerImpl(), JvmIrMangler)
val linkViaSignatures = moduleConfiguration.getBoolean(JVMConfigurationKeys.LINK_VIA_SIGNATURES)
val scopeFiles = firResult.scopeDeclarations.filterIsInstance<FirFile>()
val files = firResult.files + scopeFiles
val validatedFirResult = with(firResult) {
org.jetbrains.kotlin.fir.pipeline.FirResult(platformOutput = org.jetbrains.kotlin.fir.pipeline.ModuleCompilerAnalyzedOutput(
session = session, scopeSession = scopeSession, fir = files
), commonOutput = null)
org.jetbrains.kotlin.fir.pipeline.FirResult(
listOf(
ModuleCompilerAnalyzedOutput(session = session, scopeSession = scopeSession, fir = files)
)
)
}
val fir2IrResult = validatedFirResult.convertToIrAndActualize(fir2IrExtensions= fir2IrExtensions, irGeneratorExtensions = emptyList(), linkViaSignatures = linkViaSignatures)

val fir2IrConfiguration = Fir2IrConfiguration(
languageVersionSettings = moduleConfiguration.languageVersionSettings,
diagnosticReporter = diagnosticReporter,
linkViaSignatures = moduleConfiguration.getBoolean(JVMConfigurationKeys.LINK_VIA_SIGNATURES),
evaluatedConstTracker = moduleConfiguration
.putIfAbsent(CommonConfigurationKeys.EVALUATED_CONST_TRACKER, EvaluatedConstTracker.create()),
inlineConstTracker = moduleConfiguration[CommonConfigurationKeys.INLINE_CONST_TRACKER],
// TODO: read notes on these new flags
allowNonCachedDeclarations = false,
// TODO: see KT-61514
useIrFakeOverrideBuilder = moduleConfiguration.getBoolean(CommonConfigurationKeys.USE_IR_FAKE_OVERRIDE_BUILDER)
)

val fir2IrResult = validatedFirResult.convertToIrAndActualizeForJvm(
fir2IrExtensions= fir2IrExtensions,
irGeneratorExtensions = emptyList(),
fir2IrConfiguration = fir2IrConfiguration,
)
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled()
val diagnosticsContext = metaCheckerContext?.checkerContext
if (diagnosticsContext != null) {
diagnosticReporter.diagnostics.forEach {
metaCheckerContext.diagnosticReporter.report(it, diagnosticsContext)
println("error: [" + it.factory.name + "] " + it.factory.ktRenderer.render(it))
}
}

return fir2IrResult
}

Expand Down Expand Up @@ -204,20 +248,31 @@ class TemplateCompiler(
val scopeSession: ScopeSession,
scopeDeclarations: List<FirDeclaration>
) {

private val processors: List<FirResolveProcessor> = createAllCompilerResolveProcessors(
session,
scopeSession,
scopeDeclarations
)

@OptIn(SessionConfiguration::class)
fun process(files: List<FirFile>) {
for (processor in processors) {
if (processor.session.symbolProvider is FirCachingCompositeSymbolProvider) {
// TODO: clear the cache?
//val wrapProviders = (processor.session.symbolProvider as FirCachingCompositeSymbolProvider).providers
// TODO: or keep the cache and wrap it to prevent clearing:
val wrapProviders = listOf(processor.session.symbolProvider)
processor.session.register(FirSymbolProvider::class,
FirCachingCompositeSymbolProvider(processor.session, wrapProviders, true)
)
}
processor.beforePhase()
when (processor) {
is FirTransformerBasedResolveProcessor -> {
for (file in files) {
processor.processFile(file)
withFileAnalysisExceptionWrapping(file) {
processor.processFile(file)
}
}
}

Expand Down Expand Up @@ -258,7 +313,7 @@ class TemplateCompiler(

fun FirSession.buildFirFromKtFiles(ktFiles: Collection<KtFile>): List<FirFile> {
val firProvider = (firProvider as? FirProviderImpl)
val builder = RawFirBuilder(this, kotlinScopeProvider, BodyBuildingMode.NORMAL)
val builder = PsiRawFirBuilder(this, kotlinScopeProvider, BodyBuildingMode.NORMAL)
return ktFiles.map {
builder.buildFirFile(it).also { firFile ->
firProvider?.recordFile(firFile)
Expand Down Expand Up @@ -345,7 +400,7 @@ class FirBodyResolveTransformerAdapter(
implicitTypeOnly = false,
scopeSession = scopeSession,
outerBodyResolveContext = BodyResolveContext(
ReturnTypeCalculatorForFullBodyResolve,
ReturnTypeCalculatorForFullBodyResolve.Default,
DataFlowAnalyzerContext(session),
scopeDeclarations.filterIsInstance<FirClassLikeDeclaration>().toSet()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.builder.buildSimpleFunctionCopy
import org.jetbrains.kotlin.fir.extensions.AnnotationFqn
import org.jetbrains.kotlin.fir.extensions.FirDeclarationGenerationExtension
import org.jetbrains.kotlin.fir.extensions.FirDeclarationPredicateRegistrar
import org.jetbrains.kotlin.fir.extensions.MemberGenerationContext
import org.jetbrains.kotlin.fir.extensions.*
import org.jetbrains.kotlin.fir.extensions.predicate.DeclarationPredicate
import org.jetbrains.kotlin.fir.resolve.defaultType
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
Expand All @@ -37,7 +34,7 @@ class FirMetaCodegenExtension(
private fun metaContext(generationContext: MemberGenerationContext?): FirMetaContext =
FirMetaMemberGenerationContext(templateCompiler, session, generationContext)

override fun generateNestedClassLikeDeclaration(owner: FirClassSymbol<*>, name: Name): FirClassLikeSymbol<*>? {
override fun generateNestedClassLikeDeclaration(owner: FirClassSymbol<*>, name: Name, context: NestedClassGenerationContext): FirClassLikeSymbol<*>? {
return if (!templateCompiler.compiling) {
val firClass: FirClass? = invokeMeta(
true,
Expand All @@ -46,7 +43,7 @@ class FirMetaCodegenExtension(
Meta.Generate.Members.NestedClasses::class,
"nestedClasses"
)
firClass?.symbol ?: super.generateNestedClassLikeDeclaration(owner, name)
firClass?.symbol ?: super.generateNestedClassLikeDeclaration(owner, name, context)
} else null
}

Expand Down Expand Up @@ -142,7 +139,7 @@ class FirMetaCodegenExtension(
} else super.generateProperties(callableId, context)
}

override fun getCallableNamesForClass(classSymbol: FirClassSymbol<*>): Set<Name> =
override fun getCallableNamesForClass(classSymbol: FirClassSymbol<*>, context: MemberGenerationContext): Set<Name> =
constructors(classSymbol).orEmpty() +
properties(classSymbol).orEmpty() +
functions(classSymbol).orEmpty()
Expand Down Expand Up @@ -197,7 +194,7 @@ class FirMetaCodegenExtension(
)
// else null

override fun getNestedClassifiersNames(classSymbol: FirClassSymbol<*>): Set<Name> =
override fun getNestedClassifiersNames(classSymbol: FirClassSymbol<*>, context: NestedClassGenerationContext): Set<Name> =
nestedClasses(classSymbol).orEmpty()

fun topLevelFunctions(): Set<CallableId>? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package arrow.reflect.compiler.plugin.runners

import arrow.reflect.compiler.plugin.services.*
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms
import org.jetbrains.kotlin.test.FirParser
import org.jetbrains.kotlin.test.TargetBackend
import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder
import org.jetbrains.kotlin.test.directives.FirDiagnosticsDirectives
import org.jetbrains.kotlin.test.directives.configureFirParser
import org.jetbrains.kotlin.test.frontend.fir.FirFrontendFacade
import org.jetbrains.kotlin.test.initIdeaConfiguration
import org.jetbrains.kotlin.test.model.DependencyKind
Expand Down Expand Up @@ -37,6 +39,8 @@ fun TestConfigurationBuilder.commonFirWithPluginFrontendConfiguration() {
+AdditionalFilesDirectives.SOME_FILE_DIRECTIVE
}

configureFirParser(FirParser.LightTree)

globalDefaults {
targetBackend = TargetBackend.JVM_IR
targetPlatform = JvmPlatforms.defaultJvmPlatform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package arrow.reflect.compiler.plugin.services

import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoot
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.test.FirParser
import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder
import org.jetbrains.kotlin.test.directives.configureFirParser
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.EnvironmentConfigurator
import org.jetbrains.kotlin.test.services.RuntimeClasspathProvider
Expand Down Expand Up @@ -58,5 +60,6 @@ class MetaRuntimeClasspathProvider(testServices: TestServices) : RuntimeClasspat
fun TestConfigurationBuilder.configureForRuntimeAnnotationLibrary() {
useConfigurators(::PluginAnnotationsConfigurator)
useCustomRuntimeClasspathProviders(::MetaRuntimeClasspathProvider)
configureFirParser(FirParser.LightTree)
}

2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
arrowGradleConfig = "0.11.0"
classgraph = "4.8.154"
kotlin = "1.8.255-SNAPSHOT"
kotlin = "1.9.30-dev-3330"
reflections = "0.10.2"

[libraries]
Expand Down
7 changes: 4 additions & 3 deletions sandbox/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

val kotlinVersion = "1.8.255-SNAPSHOT"
val kotlinVersion = "1.9.30-dev-3330"

plugins {
id("org.jetbrains.kotlin.jvm") version "1.8.255-SNAPSHOT"
id("org.jetbrains.kotlin.jvm") version "1.9.30-dev-3330"
id("io.arrow-kt.reflect") version "0.1.0"
application
}
Expand All @@ -22,7 +23,7 @@ tasks {
withType<KotlinCompile>().configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
useK2.set(true)
languageVersion.set(KotlinVersion.KOTLIN_2_0)
freeCompilerArgs.add("-Xcontext-receivers")
}
}
Expand Down
2 changes: 1 addition & 1 deletion sandbox/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
arrowGradleConfig = "0.11.0"
classgraph = "4.8.154"
kotlin = "1.8.255-SNAPSHOT"
kotlin = "1.9.30-dev-3330"
reflections = "0.10.2"

[libraries]
Expand Down
16 changes: 9 additions & 7 deletions sandbox/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
maven(url = "https://oss.sonatype.org/content/repositories/snapshots")
maven(url = "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap")
mavenLocal {
content {
includeGroup("io.arrow-kt")
includeGroup("io.arrow-kt.reflect")
}
}
mavenCentral()
gradlePluginPortal()
maven(url = "https://oss.sonatype.org/content/repositories/snapshots")
maven(url = "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap")
maven(url = "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev/")
}
}

dependencyResolutionManagement {
repositories {
mavenCentral()
maven(url = "https://oss.sonatype.org/content/repositories/snapshots")
maven(url = "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap")
mavenLocal {
content {
includeGroup("io.arrow-kt")
includeGroup("io.arrow-kt.reflect")
}
}
mavenCentral()
maven(url = "https://oss.sonatype.org/content/repositories/snapshots")
maven(url = "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap")
maven(url = "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev/")
}
}

Expand Down
Loading