From a3a782613a8ef9d5354bcfa6e9fcd27a4d901b73 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Wed, 8 Feb 2017 09:49:26 +0100 Subject: [PATCH] Cleanup --- .../cli/common/repl/AggregatedReplState.kt | 2 +- .../kotlin/cli/common/repl/BasicReplState.kt | 5 +- .../cli/common/repl/GenericReplEvaluator.kt | 56 ------------------- .../KotlinJsr223JvmInvocableScriptEngine.kt | 35 ++---------- .../repl/KotlinJsr223JvmScriptEngineBase.kt | 1 - .../kotlin/cli/common/repl/replUtil.kt | 6 +- .../cli/jvm/repl/GenericCompilerState.kt | 1 - .../kotlin/cli/jvm/repl/GenericRepl.kt | 1 + .../kotlin/cli/jvm/repl/GenericReplChecker.kt | 1 - .../kotlin/cli/jvm/repl/ReplInterpreter.kt | 6 +- .../client/KotlinRemoteReplCompilerClient.kt | 2 - .../kotlin/daemon/CompileServiceImpl.kt | 4 +- .../kotlin/daemon/CompilerDaemonTest.kt | 8 +-- 13 files changed, 17 insertions(+), 111 deletions(-) diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/AggregatedReplState.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/AggregatedReplState.kt index a70e9badb51..56c4a8cf440 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/AggregatedReplState.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/AggregatedReplState.kt @@ -69,7 +69,7 @@ open class AggregatedReplStateHistory(val history1: IReplStageHistory(val state1: IReplStageState, val state2: IReplStageState, override val lock: ReentrantReadWriteLock = ReentrantReadWriteLock()) +open class AggregatedReplStageState(val state1: IReplStageState, val state2: IReplStageState, final override val lock: ReentrantReadWriteLock = ReentrantReadWriteLock()) : IReplStageState> { override val history: IReplStageHistory> = AggregatedReplStateHistory(state1.history, state2.history, lock) diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/BasicReplState.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/BasicReplState.kt index 1f8b21a87b5..cc7b3536e1d 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/BasicReplState.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/BasicReplState.kt @@ -24,7 +24,6 @@ import kotlin.concurrent.write data class LineId(override val no: Int, override val generation: Int, private val codeHash: Int) : ILineId, Serializable { - constructor(no: Int, generation: Int, code: String): this(no, generation, code.hashCode()) constructor(codeLine: ReplCodeLine): this(codeLine.no, codeLine.generation, codeLine.code.hashCode()) override fun compareTo(other: ILineId): Int = (other as? LineId)?.let { @@ -65,11 +64,9 @@ open class BasicReplStageHistory(override val lock: ReentrantReadWriteLock = } } -open class BasicReplStageState: IReplStageState { +open class BasicReplStageState(override final val lock: ReentrantReadWriteLock = ReentrantReadWriteLock()): IReplStageState { override val currentGeneration: Int get() = history.currentGeneration.get() - override val lock = ReentrantReadWriteLock() - override val history: BasicReplStageHistory = BasicReplStageHistory(lock) } diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplEvaluator.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplEvaluator.kt index 392231fba77..aae06db8aad 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplEvaluator.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplEvaluator.kt @@ -27,19 +27,6 @@ open class GenericReplEvaluator(val baseClasspath: Iterable, protected val repeatingMode: ReplRepeatingMode = ReplRepeatingMode.REPEAT_ONLY_MOST_RECENT ) : ReplEvaluator { -// private val evaluatedHistory = ReplHistory() - -// override val history: List get() = stateLock.read { evaluatedHistory.copySources() } - -// private class HistoryActions( -// val effectiveHistory: List, -// val verify: (compareHistory: SourceList?) -> Int?, -// val addPlaceholder: (line: CompiledReplCodeLine, value: EvalClassWithInstanceAndLoader) -> Unit, -// val removePlaceholder: (line: CompiledReplCodeLine) -> Boolean, -// val addFinal: (line: CompiledReplCodeLine, value: EvalClassWithInstanceAndLoader) -> Unit, -// val processClasses: (compileResult: ReplCompileResult.CompiledClasses) -> Pair> -// ) - override fun createState(lock: ReentrantReadWriteLock): IReplStageState<*> = GenericReplEvaluatorState(baseClasspath, baseClassloader, lock) override fun eval(state: IReplStageState<*>, @@ -48,17 +35,6 @@ open class GenericReplEvaluator(val baseClasspath: Iterable, invokeWrapper: InvokeWrapper?): ReplEvalResult { state.lock.write { val evalState = state.asState(GenericReplEvaluatorState::class.java) -// val verifyHistory = compileResult.state.dropLast(1) -// val defaultHistoryActor = HistoryActions( -// effectiveHistory = evalState.history.map { it.item }, -// verify = { line -> evalState.history.firstMismatchingHistory(line) }, -// addPlaceholder = { line, value -> evalState.history.add(line, value) }, -// removePlaceholder = { line -> evalState.history.removeLast(line) }, -// addFinal = { line, value -> evalState.history.add(line, value) }, -// processClasses = { compiled -> -// prependClassLoaderWithNewClasses(evalState.history.copyValues(), compiled, ) -// }) - val historyActor = when (repeatingMode) { ReplRepeatingMode.NONE -> HistoryActionsForNoRepeat(evalState) ReplRepeatingMode.REPEAT_ONLY_MOST_RECENT -> { @@ -68,19 +44,6 @@ open class GenericReplEvaluator(val baseClasspath: Iterable, } else { HistoryActionsForRepeatRecentOnly(evalState) -// val trimmedHistory = ReplHistory(evalState.history.copyAll().dropLast(1)) -// HistoryActions -// effectiveHistory = trimmedHistory.copyValues(), -// verify = { trimmedHistory.firstMismatchingHistory(it) }, -// addPlaceholder = { _, _ -> NO_ACTION() }, -// removePlaceholder = { NO_ACTION_THAT_RETURNS(true) }, -// addFinal = { line, value -> -// evalState.history.removeLast(line) -// evalState.history.add(line, value) -// }, -// processClasses = { _ -> -// Pair(lastItem.second.classLoader, lastItem.second.klass.java) -// }) } } ReplRepeatingMode.REPEAT_ANY_PREVIOUS -> { @@ -90,25 +53,6 @@ open class GenericReplEvaluator(val baseClasspath: Iterable, } else { HistoryActionsForRepeatAny(evalState, matchingItem) -// val historyCopy = evalState.history.copyAll() -// val matchingItem = historyCopy.first { it.first.source == compileResult.compiledCodeLine.source } -// val trimmedHistory = ReplHistory(evalState.history.copyAll().takeWhile { it != matchingItem }) -// HistoryActions( -// effectiveHistory = trimmedHistory.copyValues(), -// verify = { trimmedHistory.firstMismatchingHistory(it) }, -// addPlaceholder = { _, _ -> NO_ACTION() }, -// removePlaceholder = { NO_ACTION_THAT_RETURNS(true) }, -// addFinal = { line, value -> -// val extraLines = evalState.history.resetToLine(line) -// evalState.history.removeLast(line) -// evalState.history.add(line, value) -// extraLines.forEach { -// evalState.history.add(it.first, it.second) -// } -// }, -// processClasses = { _ -> -// Pair(matchingItem.second.classLoader, matchingItem.second.klass.java) -// }) } } } diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmInvocableScriptEngine.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmInvocableScriptEngine.kt index 40254f10b1f..e43f5cba55b 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmInvocableScriptEngine.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmInvocableScriptEngine.kt @@ -60,10 +60,10 @@ interface KotlinJsr223JvmInvocableScriptEngine : Invocable { private fun invokeImpl(prioritizedCallOrder: List, name: String, args: Array): Any? { // TODO: cache the method lookups? - val (fn, mapping, invokeWrapper) = prioritizedCallOrder.asSequence().map { attempt -> - val candidates = attempt.klass.functions.filter { it.name == name } - candidates.findMapping(listOf(attempt.instance) + args)?.let { - Triple(it.first, it.second, attempt.invokeWrapper) + val (fn, mapping, invokeWrapper) = prioritizedCallOrder.asSequence().map { (klass, instance, _, invokeWrapper) -> + val candidates = klass.functions.filter { it.name == name } + candidates.findMapping(listOf(instance) + args)?.let { + Triple(it.first, it.second, invokeWrapper) } }.filterNotNull().firstOrNull() ?: throw NoSuchMethodException("no suitable function '$name' found") @@ -110,33 +110,6 @@ interface KotlinJsr223JvmInvocableScriptEngine : Invocable { } } -private fun invokeImpl(prioritizedCallOrder: List, name: String, args: Array): Any? { - // TODO: cache the method lookups? - - val (fn, mapping, invokeWrapper) = prioritizedCallOrder.asSequence().map { attempt -> - val candidates = attempt.klass.functions.filter { it.name == name } - candidates.findMapping(listOf(attempt.instance) + args)?.let { - Triple(it.first, it.second, attempt.invokeWrapper) - } - }.filterNotNull().firstOrNull() ?: throw NoSuchMethodException("no suitable function '$name' found") - - val res = try { - if (invokeWrapper != null) { - invokeWrapper.invoke { - fn.callBy(mapping) - } - } - else { - fn.callBy(mapping) - } - } - catch (e: Throwable) { - // ignore everything in the stack trace until this constructor call - throw ScriptException(renderReplStackTrace(e.cause!!, startFromMethodName = fn.name)) - } - return if (fn.returnType.classifier == Unit::class) Unit else res -} - private fun Iterable>.findMapping(args: List): Pair, Map>? { for (fn in this) { val mapping = tryCreateCallableMapping(fn, args) diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmScriptEngineBase.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmScriptEngineBase.kt index 0405d9000d7..cc065c9daa1 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmScriptEngineBase.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmScriptEngineBase.kt @@ -18,7 +18,6 @@ package org.jetbrains.kotlin.cli.common.repl import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation import java.io.Reader -import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.locks.ReentrantReadWriteLock import javax.script.* diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/replUtil.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/replUtil.kt index 3405b73c549..8b7b83b7d76 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/replUtil.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/replUtil.kt @@ -26,7 +26,7 @@ fun makeScriptBaseName(codeLine: ReplCodeLine) = fun renderReplStackTrace(cause: Throwable, startFromMethodName: String): String { val newTrace = arrayListOf() var skip = true - for ((i, element) in cause.stackTrace.withIndex().reversed()) { + for ((_, element) in cause.stackTrace.withIndex().reversed()) { if ("${element.className}.${element.methodName}" == startFromMethodName) { skip = false } @@ -43,10 +43,6 @@ fun renderReplStackTrace(cause: Throwable, startFromMethodName: String): String return Throwables.getStackTraceAsString(cause) } -fun NO_ACTION(): Unit = Unit -fun NO_ACTION_THAT_RETURNS(v: T): T = v - - internal fun ClassLoader.listAllUrlsAsFiles(): List { val parents = generateSequence(this) { loader -> loader.parent }.filterIsInstance(URLClassLoader::class.java) return parents.fold(emptyList()) { accum, loader -> diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericCompilerState.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericCompilerState.kt index d20e9aa7091..e42b503b7d6 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericCompilerState.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericCompilerState.kt @@ -22,7 +22,6 @@ import org.jetbrains.kotlin.cli.jvm.repl.messages.DiagnosticMessageHolder import org.jetbrains.kotlin.descriptors.ScriptDescriptor import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.script.KotlinScriptExternalDependencies -import java.util.concurrent.atomic.AtomicLong import java.util.concurrent.locks.ReentrantReadWriteLock class ReplCompilerStageHistory(private val state: GenericReplCompilerState) : BasicReplStageHistory(state.lock) { diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericRepl.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericRepl.kt index ef46c900f60..60947de0977 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericRepl.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericRepl.kt @@ -24,6 +24,7 @@ import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.script.KotlinScriptDefinition import java.util.concurrent.locks.ReentrantReadWriteLock +// TODO: remove if unused after switching old repl to the new infrastruct open class GenericRepl protected constructor( disposable: Disposable, scriptDefinition: KotlinScriptDefinition, diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplChecker.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplChecker.kt index 274cafa81f9..ca45e0eea3d 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplChecker.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplChecker.kt @@ -33,7 +33,6 @@ import org.jetbrains.kotlin.cli.jvm.repl.messages.ReplTerminalDiagnosticMessageH import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.config.JVMConfigurationKeys import org.jetbrains.kotlin.config.JvmTarget -import org.jetbrains.kotlin.descriptors.ScriptDescriptor import org.jetbrains.kotlin.idea.KotlinLanguage import org.jetbrains.kotlin.parsing.KotlinParserDefinition import org.jetbrains.kotlin.psi.KtFile diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.kt index 8a1634bae55..fe8c503fa2d 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.kt @@ -185,7 +185,7 @@ class ReplInterpreter( private fun renderStackTrace(cause: Throwable, startFromMethodName: String): String { val newTrace = arrayListOf() var skip = true - for ((i, element) in cause.stackTrace.withIndex().reversed()) { + for ((_, element) in cause.stackTrace.withIndex().reversed()) { if ("${element.className}.${element.methodName}" == startFromMethodName) { skip = false } @@ -214,8 +214,8 @@ class ReplInterpreter( state.beforeCompile() KotlinCodegenFacade.generatePackage( state, - script.getContainingKtFile().packageFqName, - setOf(script.getContainingKtFile()), + script.containingKtFile.packageFqName, + setOf(script.containingKtFile), errorHandler ) } diff --git a/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/KotlinRemoteReplCompilerClient.kt b/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/KotlinRemoteReplCompilerClient.kt index a68b61797aa..b3473721b2e 100644 --- a/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/KotlinRemoteReplCompilerClient.kt +++ b/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/KotlinRemoteReplCompilerClient.kt @@ -20,8 +20,6 @@ import org.jetbrains.kotlin.cli.common.messages.MessageCollector import org.jetbrains.kotlin.cli.common.repl.* import org.jetbrains.kotlin.daemon.common.* import java.io.File -import java.io.InputStream -import java.io.OutputStream import java.util.concurrent.locks.ReentrantReadWriteLock // TODO: reduce number of ports used then SOCKET_ANY_FREE_PORT is passed (same problem with other calls) diff --git a/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt b/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt index da19e0f4df3..a5219398f5b 100644 --- a/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt +++ b/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt @@ -740,7 +740,7 @@ class CompileServiceImpl( operationsTracer: RemoteOperationsTracer?, body: (PrintStream, EventManager, Profiler) -> ExitCode): CompileService.CallResult = ifAlive { - withValidClientOrSessionProxy(sessionId) { session -> + withValidClientOrSessionProxy(sessionId) { _ -> operationsTracer?.before("compile") val rpcProfiler = if (daemonOptions.reportPerf) WallAndThreadTotalProfiler() else DummyProfiler() val eventManger = EventManagerImpl() @@ -770,7 +770,7 @@ class CompileServiceImpl( tracer: RemoteOperationsTracer?, body: (EventManager, Profiler) -> ExitCode): CompileService.CallResult = ifAlive { - withValidClientOrSessionProxy(sessionId) { session -> + withValidClientOrSessionProxy(sessionId) { _ -> tracer?.before("compile") val rpcProfiler = if (daemonOptions.reportPerf) WallAndThreadTotalProfiler() else DummyProfiler() val eventManger = EventManagerImpl() diff --git a/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt b/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt index a201e14687a..95ea70567cc 100644 --- a/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt @@ -362,7 +362,7 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() { val daemon = KotlinCompilerClient.connectToCompileService(compilerId, flagFile, daemonJVMOptions, daemonOptions, DaemonReportingTargets(out = System.err), autostart = true) assertNotNull("failed to connect daemon", daemon) - val (registry, port) = findPortAndCreateRegistry(10, 16384, 65535) + val (_, port) = findPortAndCreateRegistry(10, 16384, 65535) val tracer = SynchronizationTracer(CountDownLatch(1), CountDownLatch(PARALLEL_THREADS_TO_COMPILE), port) val resultCodes = arrayOfNulls(PARALLEL_THREADS_TO_COMPILE) @@ -683,13 +683,13 @@ internal fun File.ifLogNotContainsSequence(vararg patterns: String, body: (LineP internal fun File.assertLogContainsSequence(vararg patterns: String) { ifLogNotContainsSequence(*patterns) { - pattern,lineNo -> fail("Pattern '${pattern.regex}' is not found in the log file '$absolutePath'") + pattern, _ -> fail("Pattern '${pattern.regex}' is not found in the log file '$absolutePath'") } } internal fun File.isLogContainsSequence(vararg patterns: String): Boolean { var res = true - ifLogNotContainsSequence(*patterns) { l,c -> res = false } + ifLogNotContainsSequence(*patterns) { _, _ -> res = false } return res } @@ -736,7 +736,7 @@ open class TestKotlinScriptDummyDependenciesResolver : ScriptDependenciesResolve } @ScriptTemplateDefinition(resolver = TestKotlinScriptDummyDependenciesResolver::class) -abstract class ScriptWithNoParam() +abstract class ScriptWithNoParam internal fun classpathFromClassloader(): List = (TestKotlinScriptDummyDependenciesResolver::class.java.classLoader as? URLClassLoader)?.urLs