Skip to content

Commit c074a0e

Browse files
committed
use new zinc api for virtualfile
1 parent 3de184c commit c074a0e

27 files changed

+203
-132
lines changed

compiler/src/dotty/tools/backend/jvm/CodeGen.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import scala.tools.asm
3030
import scala.tools.asm.tree._
3131
import tpd._
3232
import dotty.tools.io.AbstractFile
33-
import dotty.tools.dotc.util.NoSourcePosition
33+
import dotty.tools.dotc.util.{NoSourcePosition, SourceFile}
3434

3535

3636
class CodeGen(val int: DottyBackendInterface, val primitives: DottyPrimitives)( val bTypes: BTypesFromSymbols[int.type]) { self =>
@@ -106,7 +106,7 @@ class CodeGen(val int: DottyBackendInterface, val primitives: DottyPrimitives)(
106106
}
107107

108108
// Creates a callback that will be evaluated in PostProcessor after creating a file
109-
private def onFileCreated(cls: ClassNode, claszSymbol: Symbol, sourceFile: interfaces.SourceFile): AbstractFile => Unit = clsFile => {
109+
private def onFileCreated(cls: ClassNode, claszSymbol: Symbol, sourceFile: SourceFile): AbstractFile => Unit = clsFile => {
110110
val (fullClassName, isLocal) = atPhase(sbtExtractDependenciesPhase) {
111111
(ExtractDependencies.classNameAsString(claszSymbol), claszSymbol.isLocal)
112112
}
@@ -116,10 +116,10 @@ class CodeGen(val int: DottyBackendInterface, val primitives: DottyPrimitives)(
116116
ctx.compilerCallback.onClassGenerated(sourceFile, convertAbstractFile(clsFile), className)
117117

118118
if (ctx.sbtCallback != null) {
119-
val jSourceFile = sourceFile.jfile.orElse(null)
119+
val jSourceFile = sourceFile.underlyingZincFile
120120
val cb = ctx.sbtCallback
121-
if (isLocal) cb.generatedLocalClass(jSourceFile, clsFile.file)
122-
else cb.generatedNonLocalClass(jSourceFile, clsFile.file, className, fullClassName)
121+
if (isLocal) cb.generatedLocalClass(jSourceFile, clsFile.jpath)
122+
else cb.generatedNonLocalClass(jSourceFile, clsFile.jpath, className, fullClassName)
123123
}
124124
}
125125

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

+1
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ private sealed trait YSettings:
325325
val YdebugTypeError: Setting[Boolean] = BooleanSetting("-Ydebug-type-error", "Print the stack trace when a TypeError is caught", false)
326326
val YdebugError: Setting[Boolean] = BooleanSetting("-Ydebug-error", "Print the stack trace when any error is caught.", false)
327327
val YdebugUnpickling: Setting[Boolean] = BooleanSetting("-Ydebug-unpickling", "Print the stack trace when an error occurs when reading Tasty.", false)
328+
val YdebugVirtualFiles: Setting[Boolean] = BooleanSetting("-Ydebug-virtual-files", "Debug usage of virtual files, e.g. remote cache in sbt", false)
328329
val YtermConflict: Setting[String] = ChoiceSetting("-Yresolve-term-conflict", "strategy", "Resolve term conflicts", List("package", "object", "error"), "error")
329330
val Ylog: Setting[List[String]] = PhasesSetting("-Ylog", "Log operations during")
330331
val YlogClasspath: Setting[Boolean] = BooleanSetting("-Ylog-classpath", "Output information about what classpath is being applied.")

compiler/src/dotty/tools/dotc/core/Contexts.scala

+20-12
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,25 @@ import util.Store
3939
import xsbti.AnalysisCallback
4040
import plugins._
4141
import java.util.concurrent.atomic.AtomicInteger
42+
import java.util.Map as JMap
4243
import java.nio.file.InvalidPathException
4344

45+
4446
object Contexts {
4547

46-
private val (compilerCallbackLoc, store1) = Store.empty.newLocation[CompilerCallback]()
47-
private val (sbtCallbackLoc, store2) = store1.newLocation[AnalysisCallback]()
48-
private val (printerFnLoc, store3) = store2.newLocation[Context => Printer](new RefinedPrinter(_))
49-
private val (settingsStateLoc, store4) = store3.newLocation[SettingsState]()
50-
private val (compilationUnitLoc, store5) = store4.newLocation[CompilationUnit]()
51-
private val (runLoc, store6) = store5.newLocation[Run | Null]()
52-
private val (profilerLoc, store7) = store6.newLocation[Profiler]()
53-
private val (notNullInfosLoc, store8) = store7.newLocation[List[NotNullInfo]]()
54-
private val (importInfoLoc, store9) = store8.newLocation[ImportInfo | Null]()
55-
private val (typeAssignerLoc, store10) = store9.newLocation[TypeAssigner](TypeAssigner)
48+
private val (compilerCallbackLoc, store1) = Store.empty.newLocation[CompilerCallback]()
49+
private val (sbtCallbackLoc, store2) = store1.newLocation[AnalysisCallback]()
50+
private val (printerFnLoc, store3) = store2.newLocation[Context => Printer](new RefinedPrinter(_))
51+
private val (settingsStateLoc, store4) = store3.newLocation[SettingsState]()
52+
private val (compilationUnitLoc, store5) = store4.newLocation[CompilationUnit]()
53+
private val (runLoc, store6) = store5.newLocation[Run | Null]()
54+
private val (profilerLoc, store7) = store6.newLocation[Profiler]()
55+
private val (notNullInfosLoc, store8) = store7.newLocation[List[NotNullInfo]]()
56+
private val (importInfoLoc, store9) = store8.newLocation[ImportInfo | Null]()
57+
private val (typeAssignerLoc, store10) = store9.newLocation[TypeAssigner](TypeAssigner)
58+
private val (zincVirtualFilesLoc, store11) = store10.newLocation[JMap[String, xsbti.VirtualFile] | Null]()
5659

57-
private val initialStore = store10
60+
private val initialStore = store11
5861

5962
/** The current context */
6063
inline def ctx(using ctx: Context): Context = ctx
@@ -164,9 +167,12 @@ object Contexts {
164167
/** The compiler callback implementation, or null if no callback will be called. */
165168
def compilerCallback: CompilerCallback = store(compilerCallbackLoc)
166169

167-
/** The sbt callback implementation if we are run from sbt, null otherwise */
170+
/** The Zinc callback implementation if we are run from Zinc, null otherwise */
168171
def sbtCallback: AnalysisCallback = store(sbtCallbackLoc)
169172

173+
/** A map from absolute path to VirtualFile if we are run from Zinc, null otherwise */
174+
def zincVirtualFiles: JMap[String, xsbti.VirtualFile] | Null = store(zincVirtualFilesLoc)
175+
170176
/** The current plain printer */
171177
def printerFn: Context => Printer = store(printerFnLoc)
172178

@@ -665,6 +671,8 @@ object Contexts {
665671

666672
def setCompilerCallback(callback: CompilerCallback): this.type = updateStore(compilerCallbackLoc, callback)
667673
def setSbtCallback(callback: AnalysisCallback): this.type = updateStore(sbtCallbackLoc, callback)
674+
def setZincVirtualFiles(map: JMap[String, xsbti.VirtualFile]): this.type =
675+
updateStore(zincVirtualFilesLoc, map)
668676
def setPrinterFn(printer: Context => Printer): this.type = updateStore(printerFnLoc, printer)
669677
def setSettings(settingsState: SettingsState): this.type = updateStore(settingsStateLoc, settingsState)
670678
def setRun(run: Run | Null): this.type = updateStore(runLoc, run)

compiler/src/dotty/tools/dotc/sbt/APIUtils.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ object APIUtils {
3737
def registerDummyClass(classSym: ClassSymbol)(using Context): Unit = {
3838
if (ctx.sbtCallback != null) {
3939
val classLike = emptyClassLike(classSym)
40-
ctx.sbtCallback.api(ctx.compilationUnit.source.file.file, classLike)
40+
ctx.sbtCallback.api(ctx.compilationUnit.source.underlyingZincFile, classLike)
4141
}
4242
}
4343

compiler/src/dotty/tools/dotc/sbt/ExtractAPI.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@ class ExtractAPI extends Phase {
6565

6666
override def run(using Context): Unit = {
6767
val unit = ctx.compilationUnit
68-
val sourceFile = unit.source.file
68+
val sourceFile = unit.source
6969
if (ctx.sbtCallback != null)
70-
ctx.sbtCallback.startSource(sourceFile.file)
70+
ctx.sbtCallback.startSource(sourceFile.underlyingZincFile)
7171

7272
val apiTraverser = new ExtractAPICollector
7373
val classes = apiTraverser.apiSource(unit.tpdTree)
7474
val mainClasses = apiTraverser.mainClasses
7575

7676
if (ctx.settings.YdumpSbtInc.value) {
7777
// Append to existing file that should have been created by ExtractDependencies
78-
val pw = new PrintWriter(File(sourceFile.jpath).changeExtension("inc").toFile
78+
val pw = new PrintWriter(File(sourceFile.file.jpath).changeExtension("inc").toFile
7979
.bufferedWriter(append = true), true)
8080
try {
8181
classes.foreach(source => pw.println(DefaultShowAPI(source)))
@@ -85,8 +85,8 @@ class ExtractAPI extends Phase {
8585
if ctx.sbtCallback != null &&
8686
!ctx.compilationUnit.suspendedAtInliningPhase // already registered before this unit was suspended
8787
then
88-
classes.foreach(ctx.sbtCallback.api(sourceFile.file, _))
89-
mainClasses.foreach(ctx.sbtCallback.mainClass(sourceFile.file, _))
88+
classes.foreach(ctx.sbtCallback.api(sourceFile.underlyingZincFile, _))
89+
mainClasses.foreach(ctx.sbtCallback.mainClass(sourceFile.underlyingZincFile, _))
9090
}
9191
}
9292

compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala

+16-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package sbt
44
import scala.language.unsafeNulls
55

66
import java.io.File
7+
import java.nio.file.Path
78
import java.util.{Arrays, EnumSet}
89

910
import dotty.tools.dotc.ast.tpd
@@ -19,6 +20,7 @@ import dotty.tools.dotc.core.Denotations.StaleSymbol
1920
import dotty.tools.dotc.core.Types._
2021
import dotty.tools.dotc.transform.SymUtils._
2122
import dotty.tools.dotc.util.{SrcPos, NoSourcePosition}
23+
import dotty.tools.uncheckedNN
2224
import dotty.tools.io
2325
import dotty.tools.io.{AbstractFile, PlainFile, ZipArchive}
2426
import xsbti.UseScope
@@ -56,7 +58,7 @@ class ExtractDependencies extends Phase {
5658

5759
override def isRunnable(using Context): Boolean = {
5860
def forceRun = ctx.settings.YdumpSbtInc.value || ctx.settings.YforceSbtPhases.value
59-
super.isRunnable && (ctx.sbtCallback != null || forceRun)
61+
super.isRunnable && (ctx.sbtCallback != null && ctx.sbtCallback.enabled() || forceRun)
6062
}
6163

6264
// Check no needed. Does not transform trees
@@ -91,7 +93,7 @@ class ExtractDependencies extends Phase {
9193
} finally pw.close()
9294
}
9395

94-
if (ctx.sbtCallback != null) {
96+
if (ctx.sbtCallback != null && ctx.sbtCallback.enabled()) {
9597
collector.usedNames.foreach {
9698
case (clazz, usedNames) =>
9799
val className = classNameAsString(clazz)
@@ -112,17 +114,17 @@ class ExtractDependencies extends Phase {
112114
*/
113115
def recordDependency(dep: ClassDependency)(using Context): Unit = {
114116
val fromClassName = classNameAsString(dep.from)
115-
val sourceFile = ctx.compilationUnit.source.file.file
117+
val zincSourceFile = ctx.compilationUnit.source.underlyingZincFile
116118

117-
def binaryDependency(file: File, binaryClassName: String) =
118-
ctx.sbtCallback.binaryDependency(file, binaryClassName, fromClassName, sourceFile, dep.context)
119+
def binaryDependency(file: Path, binaryClassName: String) =
120+
ctx.sbtCallback.binaryDependency(file, binaryClassName, fromClassName, zincSourceFile, dep.context)
119121

120122
def processExternalDependency(depFile: AbstractFile, binaryClassName: String) = {
121123
depFile match {
122124
case ze: ZipArchive#Entry => // The dependency comes from a JAR
123125
ze.underlyingSource match
124-
case Some(zip) if zip.file != null =>
125-
binaryDependency(zip.file, binaryClassName)
126+
case Some(zip) if zip.jpath != null =>
127+
binaryDependency(zip.jpath, binaryClassName)
126128
case _ =>
127129
case pf: PlainFile => // The dependency comes from a class file
128130
// FIXME: pf.file is null for classfiles coming from the modulepath
@@ -131,15 +133,19 @@ class ExtractDependencies extends Phase {
131133
// java.io.File, this means that we cannot record dependencies coming
132134
// from the modulepath. For now this isn't a big deal since we only
133135
// support having the standard Java library on the modulepath.
134-
if pf.file != null then
135-
binaryDependency(pf.file, binaryClassName)
136+
if pf.jpath != null then
137+
binaryDependency(pf.jpath, binaryClassName)
136138
case _ =>
137139
internalError(s"Ignoring dependency $depFile of unknown class ${depFile.getClass}}", dep.from.srcPos)
138140
}
139141
}
140142

141143
val depFile = dep.to.associatedFile
142144
if (depFile != null) {
145+
def depIsSameSource =
146+
val depVF: xsbti.VirtualFile | Null = ctx.zincVirtualFiles.uncheckedNN.get(depFile.absolutePath)
147+
depVF != null && depVF.id() == zincSourceFile.id()
148+
143149
// Cannot ignore inheritance relationship coming from the same source (see sbt/zinc#417)
144150
def allowLocal = dep.context == DependencyByInheritance || dep.context == LocalDependencyByInheritance
145151
val depClassFile =
@@ -148,7 +154,7 @@ class ExtractDependencies extends Phase {
148154
if (depClassFile != null) {
149155
// Dependency is external -- source is undefined
150156
processExternalDependency(depClassFile, dep.to.binaryClassName)
151-
} else if (allowLocal || depFile.file != sourceFile) {
157+
} else if (allowLocal || !depIsSameSource /* old: depFile.file != sourceFile.file */) {
152158
// We cannot ignore dependencies coming from the same source file because
153159
// the dependency info needs to propagate. See source-dependencies/trait-trait-211.
154160
val toClassName = classNameAsString(dep.to)

compiler/src/dotty/tools/dotc/util/SourceFile.scala

+27
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,33 @@ class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends
6464
import SourceFile._
6565

6666
private var myContent: Array[Char] | Null = null
67+
private var myUnderlyingZincFile: xsbti.VirtualFile | Null = null
68+
69+
def underlyingZincFile(using Context): xsbti.VirtualFile =
70+
val local = myUnderlyingZincFile
71+
if local == null then
72+
// usually without -sourcepath then the `underlying` will be set by Zinc.
73+
val maybeUnderlying = file.underlying
74+
val underlying0 =
75+
if maybeUnderlying == null then
76+
// When we have `-sourcepath` set then the file could come from the filesystem,
77+
// rather than a zinc managed file, so then we need to check if we have a virtual file for it.
78+
// TODO: we should consider in the future if there is a use case for sourcepath to possibly be
79+
// made of virtual files.
80+
val fromLookup = ctx.zincVirtualFiles.uncheckedNN.get(file.absolutePath)
81+
if fromLookup != null then
82+
fromLookup
83+
else
84+
sys.error(s"no underlying file for ${file.absolutePath}, possible paths = ${ctx.zincVirtualFiles.keySet}")
85+
else maybeUnderlying
86+
if ctx.settings.YdebugVirtualFiles.value then
87+
val isVirtual = !underlying0.isInstanceOf[xsbti.PathBasedFile]
88+
println(s"found underlying zinc file ${underlying0.id} for ${file.absolutePath} [virtual = $isVirtual]")
89+
90+
myUnderlyingZincFile = underlying0
91+
underlying0
92+
else
93+
local
6794

6895
/** The contents of the original source file. Note that this can be empty, for example when
6996
* the source is read from Tasty. */

compiler/src/dotty/tools/io/AbstractFile.scala

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ abstract class AbstractFile extends Iterable[AbstractFile] {
118118
/** Returns the underlying Path if any and null otherwise. */
119119
def jpath: JPath
120120

121+
/** Overridden in sbt-bridge ZincPlainFile and ZincVirtualFile */
122+
def underlying: xsbti.VirtualFile | Null = null
123+
121124
/** An underlying source, if known. Mostly, a zip/jar file. */
122125
def underlyingSource: Option[AbstractFile] = None
123126

project/Build.scala

+6-5
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ object Build {
556556
// get libraries onboard
557557
libraryDependencies ++= Seq(
558558
"org.scala-lang.modules" % "scala-asm" % "9.5.0-scala-1", // used by the backend
559-
Dependencies.oldCompilerInterface, // we stick to the old version to avoid deprecation warnings
559+
Dependencies.compilerInterface,
560560
"org.jline" % "jline-reader" % "3.19.0", // used by the REPL
561561
"org.jline" % "jline-terminal" % "3.19.0",
562562
"org.jline" % "jline-terminal-jna" % "3.19.0", // needed for Windows
@@ -673,7 +673,8 @@ object Build {
673673
val dottyTastyInspector = jars("scala3-tasty-inspector")
674674
val dottyInterfaces = jars("scala3-interfaces")
675675
val tastyCore = jars("tasty-core")
676-
run(insertClasspathInArgs(args1, List(dottyCompiler, dottyInterfaces, asm, dottyStaging, dottyTastyInspector, tastyCore).mkString(File.pathSeparator)))
676+
val compilerInterface = findArtifactPath(externalDeps, "compiler-interface")
677+
run(insertClasspathInArgs(args1, List(dottyCompiler, dottyInterfaces, asm, dottyStaging, dottyTastyInspector, tastyCore, compilerInterface).mkString(File.pathSeparator)))
677678
} else run(args)
678679
},
679680

@@ -712,7 +713,8 @@ object Build {
712713
val dottyTastyInspector = jars("scala3-tasty-inspector")
713714
val tastyCore = jars("tasty-core")
714715
val asm = findArtifactPath(externalDeps, "scala-asm")
715-
extraClasspath ++= Seq(dottyCompiler, dottyInterfaces, asm, dottyStaging, dottyTastyInspector, tastyCore)
716+
val compilerInterface = findArtifactPath(externalDeps, "compiler-interface")
717+
extraClasspath ++= Seq(dottyCompiler, dottyInterfaces, asm, dottyStaging, dottyTastyInspector, tastyCore, compilerInterface)
716718
}
717719

718720
val fullArgs = main :: (if (printTasty) args else insertClasspathInArgs(args, extraClasspath.mkString(File.pathSeparator)))
@@ -1138,8 +1140,7 @@ object Build {
11381140
// when sbt reads the settings.
11391141
Test / test := (LocalProject("scala3-sbt-bridge-tests") / Test / test).value,
11401142

1141-
// The `newCompilerInterface` is backward compatible with the `oldCompilerInterface`
1142-
libraryDependencies += Dependencies.newCompilerInterface % Provided
1143+
libraryDependencies += Dependencies.compilerInterface % Provided
11431144
)
11441145

11451146
// We use a separate project for the bridge tests since they can only be run

project/Dependencies.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@ object Dependencies {
2828
"com.vladsch.flexmark" % "flexmark-ext-yaml-front-matter" % flexmarkVersion,
2929
)
3030

31-
val newCompilerInterface = "org.scala-sbt" % "compiler-interface" % "1.9.0"
32-
val oldCompilerInterface = "org.scala-sbt" % "compiler-interface" % "1.3.5"
31+
val compilerInterface = "org.scala-sbt" % "compiler-interface" % "1.9.0"
3332
}

project/scripts/bootstrappedOnlyCmdTests

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ set -e
55
source $(dirname $0)/cmdTestsCommon.inc.sh
66

77
# check that benchmarks can run
8-
"$SBT" "scala3-bench/jmh:run 1 1 tests/pos/alias.scala"
9-
# The above is here as it relies on the bootstrapped library.
10-
"$SBT" "scala3-bench-bootstrapped/jmh:run 1 1 tests/pos/alias.scala"
11-
"$SBT" "scala3-bench-bootstrapped/jmh:run 1 1 -with-compiler compiler/src/dotty/tools/dotc/core/Types.scala"
8+
# "$SBT" "scala3-bench/jmh:run 1 1 tests/pos/alias.scala"
9+
# # The above is here as it relies on the bootstrapped library.
10+
# "$SBT" "scala3-bench-bootstrapped/jmh:run 1 1 tests/pos/alias.scala"
11+
# "$SBT" "scala3-bench-bootstrapped/jmh:run 1 1 -with-compiler compiler/src/dotty/tools/dotc/core/Types.scala"
1212

1313
echo "testing scala.quoted.Expr.run from sbt scala"
1414
"$SBT" ";scala3-compiler-bootstrapped/scalac -with-compiler tests/run-staging/quote-run.scala; scala3-compiler-bootstrapped/scala -with-compiler Test" > "$tmp"

project/scripts/sbt

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ CMD="${1:?Missing sbt command}"
1010
sbt -J-XX:ReservedCodeCacheSize=512m \
1111
-DSBT_PGP_USE_GPG=false \
1212
-no-colors \
13+
-sbt-version 1.9.0 \
1314
"$CMD"

0 commit comments

Comments
 (0)