Skip to content

Commit aef5851

Browse files
committed
Clean up impl
1 parent 219588d commit aef5851

File tree

6 files changed

+47
-32
lines changed

6 files changed

+47
-32
lines changed

Diff for: compiler/src/dotty/tools/dotc/classpath/AggregateClassPath.scala

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ case class AggregateClassPath(aggregates: Seq[ClassPath]) extends ClassPath {
3333
packageIndex.getOrElseUpdate(pkg.dottedString, aggregates.filter(_.hasPackage(pkg)))
3434
}
3535

36-
override def asURLs: Seq[URL] = aggregates.flatMap(_.asURLs)
36+
override def asURLs: Seq[URL] =
37+
aggregates.flatMap {
38+
case v: VirtualDirectoryClassPath => Seq()
39+
case a => a.asURLs
40+
}
3741

3842
override def asClassPathStrings: Seq[String] = aggregates.map(_.asClassPathString).distinct
3943

Diff for: compiler/src/dotty/tools/dotc/config/JavaPlatform.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ class JavaPlatform extends Platform {
2727
case _ => false
2828
})
2929

30-
def addToClassPath(cPath: ClassPath): Unit = currentClassPath.get match {
30+
def addToClassPath(cPath: ClassPath)(using Context): Unit = classPath match {
3131
case AggregateClassPath(entries) =>
3232
currentClassPath = Some(AggregateClassPath(entries :+ cPath))
3333
case cp: ClassPath =>
3434
currentClassPath = Some(AggregateClassPath(cp :: cPath :: Nil))
3535
}
3636
/** Update classpath with a substituted subentry */
37-
def updateClassPath(subst: Map[ClassPath, ClassPath]): Unit = currentClassPath.get match {
37+
def updateClassPath(subst: Map[ClassPath, ClassPath])(using Context): Unit = classPath match {
3838
case AggregateClassPath(entries) =>
3939
currentClassPath = Some(AggregateClassPath(entries map (e => subst.getOrElse(e, e))))
4040
case cp: ClassPath =>

Diff for: compiler/src/dotty/tools/dotc/config/Platform.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ abstract class Platform {
1919
def classPath(using Context): ClassPath
2020

2121
/** Update classpath with a substitution that maps entries to entries */
22-
def updateClassPath(subst: Map[ClassPath, ClassPath]): Unit
23-
def addToClassPath(cPath: ClassPath): Unit
22+
def updateClassPath(subst: Map[ClassPath, ClassPath])(using Context): Unit
23+
24+
/** Add new entry to classpath */
25+
def addToClassPath(cPath: ClassPath)(using Context): Unit
2426

2527
/** Any platform-specific phases. */
2628
//def platformPhases: List[SubComponent]

Diff for: compiler/src/dotty/tools/dotc/core/Contexts.scala

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import scala.annotation.internal.sharable
3636
import DenotTransformers.DenotTransformer
3737
import dotty.tools.dotc.profile.Profiler
3838
import dotty.tools.dotc.sbt.interfaces.{IncrementalCallback, ProgressCallback}
39+
import dotty.tools.dotc.classpath.ClassPathFactory
3940
import util.Property.Key
4041
import util.Store
4142
import plugins.*
@@ -918,8 +919,11 @@ object Contexts {
918919
/** Initializes the `ContextBase` with a starting context.
919920
* This initializes the `platform` and the `definitions`.
920921
*/
921-
def initialize()(using Context): Unit = {
922+
def initialize(previousOutputDir: Option[AbstractFile] = None)(using Context): Unit = {
922923
_platform = newPlatform
924+
previousOutputDir.foreach(cp =>
925+
_platform.nn.addToClassPath(ClassPathFactory.newClassPath(cp))
926+
)
923927
definitions.init()
924928
}
925929

Diff for: compiler/src/dotty/tools/dotc/core/Definitions.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -2220,7 +2220,7 @@ class Definitions {
22202220

22212221
private var isInitialized = false
22222222

2223-
def init()(using Context): Unit = {
2223+
def init(require: Boolean = false)(using Context): Unit = {
22242224
this.initCtx = ctx
22252225
if (!isInitialized) {
22262226
// force initialization of every symbol that is synthesized or hijacked by the compiler

Diff for: compiler/src/dotty/tools/repl/ReplDriver.scala

+30-25
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package dotty.tools.repl
22

33
import scala.language.unsafeNulls
4-
import java.io.{PrintStream, File as JFile}
4+
import java.io.{File => JFile, PrintStream}
55
import java.nio.charset.StandardCharsets
66
import dotty.tools.dotc.ast.Trees.*
77
import dotty.tools.dotc.ast.{tpd, untpd}
8-
import dotty.tools.dotc.classpath.{AggregateClassPath, ClassPathFactory, ZipAndJarClassPathFactory}
8+
import dotty.tools.dotc.classpath.ClassPathFactory
99
import dotty.tools.dotc.config.CommandLineParser.tokenize
1010
import dotty.tools.dotc.config.Properties.{javaVersion, javaVmName, simpleVersionString}
1111
import dotty.tools.dotc.core.Contexts.*
1212
import dotty.tools.dotc.core.Decorators.*
13-
import dotty.tools.dotc.core.Phases.{typerPhase, unfusedPhases}
13+
import dotty.tools.dotc.core.Phases.{unfusedPhases, typerPhase}
1414
import dotty.tools.dotc.core.Denotations.Denotation
1515
import dotty.tools.dotc.core.Flags.*
1616
import dotty.tools.dotc.core.Mode
@@ -94,7 +94,7 @@ class ReplDriver(settings: Array[String],
9494
initCtx.settings.YwithBestEffortTasty.name
9595
)
9696

97-
private def setupRootCtx(settings: Array[String], rootCtx: Context) = {
97+
private def setupRootCtx(settings: Array[String], rootCtx: Context, previousOutputDir: Option[AbstractFile] = None) = {
9898
val incompatible = settings.intersect(incompatibleOptions)
9999
val filteredSettings =
100100
if !incompatible.isEmpty then
@@ -107,7 +107,7 @@ class ReplDriver(settings: Array[String],
107107
case Some((files, ictx)) => inContext(ictx) {
108108
shouldStart = true
109109
if files.nonEmpty then out.println(i"Ignoring spurious arguments: $files%, %")
110-
ictx.base.initialize()
110+
ictx.base.initialize(previousOutputDir)
111111
ictx
112112
}
113113
case None =>
@@ -523,44 +523,49 @@ class ReplDriver(settings: Array[String],
523523

524524
val entries = flatten(jarFile)
525525

526-
def classNameOf(classFile: AbstractFile): String = {
526+
def tryClassLoad(classFile: AbstractFile): Option[String] = {
527527
val input = classFile.input
528528
try {
529529
val reader = new ClassReader(input)
530-
reader.getClassName.replace('/', '.')
531-
} finally {
530+
val clsName = reader.getClassName.replace('/', '.')
531+
rendering.myClassLoader.loadClass(clsName)
532+
Some(clsName)
533+
} catch
534+
case _: ClassNotFoundException => None
535+
finally {
532536
input.close()
533537
}
534538
}
535539

536-
def alreadyDefined(clsName: String) = state.context.platform.classPath(using state.context).findClassFile(clsName).isDefined
537-
val existingClass = entries.filter(_.ext.isClass).map(classNameOf).find(alreadyDefined)
540+
val existingClass = entries.filter(_.ext.isClass).find(tryClassLoad(_).isDefined)
538541
if (existingClass.nonEmpty)
539542
out.println(s"The path '$f' cannot be loaded, it contains a classfile that already exists on the classpath: ${existingClass.get}")
540543
state
541544
else
542545
val cp = state.context.platform.classPath(using state.context).asClassPathString
543-
// println(s"CURRENT CP STRING: $cp")
544546
val newCP = s"$cp${JFile.pathSeparator}$path"
545-
println(s"UPDATED CP: $newCP")
546547

547548
// add to compiler class path
548-
// println(s"INIT state classPath = ${state.context.platform.classPath(using state.context).asClassPathString}")
549-
// val cpCP = ClassPathFactory.newClassPath(jarFile)(using state.context)
550-
// state.context.platform.addToClassPath(cpCP)
551-
// println(s"classPath after add = ${state.context.platform.classPath(using state.context).asClassPathString}")
552-
553-
// recreate initial context
554-
resetToInitial(List("-classpath", newCP))
555-
// rootCtx = setupRootCtx(Array(), rootCtx.fresh.setSetting(rootCtx.settings.classpath, newCP))
549+
val prevOutputDir = rootCtx.settings.outputDir.valueIn(rootCtx.settingsState)
550+
val ctxToUse = initCtx.fresh.setSetting(rootCtx.settings.classpath, newCP)
551+
rootCtx = setupRootCtx(
552+
Array(),
553+
ctxToUse,
554+
previousOutputDir = Some(prevOutputDir)
555+
)
556556
val s = state.copy(context = rootCtx)
557557

558-
// new class loader
559-
val oldCL = rendering.classLoader()(using state.context)
560-
val newCL = fromURLsParallelCapable(s.context.platform.classPath(using s.context).asURLs, oldCL)
561-
rendering.myClassLoader = new AbstractFileClassLoader(state.context.settings.outputDir.default, newCL)
562-
// out.println(s"Added '$path' to classpath.")
558+
// new class loader with previous output dir and specified jar
559+
val prevClassLoader = rendering.classLoader()(using state.context)
560+
val jarClassLoader = fromURLsParallelCapable(
561+
ClassPathFactory.newClassPath(jarFile)(using rootCtx).asURLs, prevClassLoader)
562+
val replOutputClassLoader = new AbstractFileClassLoader(
563+
prevOutputDir, jarClassLoader)
564+
rendering.myClassLoader = new AbstractFileClassLoader(
565+
rootCtx.settings.outputDir.valueIn(rootCtx.settingsState), replOutputClassLoader)
566+
out.println(s"Added '$path' to classpath.")
563567
s
568+
564569
case TypeOf(expr) =>
565570
expr match {
566571
case "" => out.println(s":type <expression>")

0 commit comments

Comments
 (0)