Skip to content

Commit

Permalink
Generate AbstractZincFile during -sourcepath workflow
Browse files Browse the repository at this point in the history
In certain codepath, unit.source.file is a PlainFile, which causes a MatchError.

This PR converts PlainFile to AbstractZincFile, avoiding the MatchError.
  • Loading branch information
Friendseeker committed Sep 28, 2024
1 parent 4d4c4ac commit 99fbd8f
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

package xsbt

import xsbti.{ AnalysisCallback, Severity }
import xsbti.{ AnalysisCallback, AnalysisCallback3, Severity }
import xsbti.compile._

import scala.tools.nsc._
Expand All @@ -20,6 +20,7 @@ import java.nio.file.{ Files, Path }

import scala.reflect.io.PlainFile
import scala.reflect.ReflectAccess._
import scala.reflect.internal.util.BatchSourceFile

/** Defines the interface of the incremental compiler hiding implementation details. */
sealed abstract class CallbackGlobal(
Expand Down Expand Up @@ -78,6 +79,15 @@ sealed class ZincCompiler(settings: Settings, dreporter: DelegatingReporter, out
extends CallbackGlobal(settings, dreporter, output)
with ZincGlobalCompat {

override def getSourceFile(f: AbstractFile): BatchSourceFile = {
val file = (f, callback) match {
case (plainFile: PlainFile, callback3: AnalysisCallback3) =>
AbstractZincFile(callback3.toVirtualFile(plainFile.file.toPath))
case _ => f
}
super.getSourceFile(file)
}

final class ZincRun(compileProgress: CompileProgress) extends Run {
override def informUnitStarting(phase: Phase, unit: CompilationUnit): Unit = {
compileProgress.startUnit(phase.name, unit.source.path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
*/

package xsbti;

import xsbti.compile.analysis.ReadSourceInfos;

import java.nio.file.Path;

/**
* Extension to {@link AnalysisCallback2}.
* Similar to {@link AnalysisCallback2}, it serves as compatibility layer for Scala compilers.
*/
public interface AnalysisCallback3 extends AnalysisCallback2 {
VirtualFile toVirtualFile(Path path);

ReadSourceInfos getSourceInfos();
}
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,8 @@ private final class AnalysisCallback(
()
}

override def toVirtualFile(path: Path): VirtualFile = converter.toVirtualFile(path)

override def problem2(
category: String,
pos: Position,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,10 @@ case class ProjectStructure(
}
val scalacOptions: List[String] =
Option(map.get("scalac.options")).toList
.flatMap(_.toString.split(" +").toList) ++
.flatMap(_.toString.split(" +").toList.map(_.replace(
"[basedir]",
baseDirectory.toAbsolutePath.toString
))) ++
// for now assume export pipelining for all pipelining subprojects
(if (incOptions.pipelining) List("-Ypickle-java", "-Ypickle-write", earlyOutput.toString)
else Nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ class TestCallback extends AnalysisCallback3 {
override def isPickleJava: Boolean = false

override def getPickleJarPair = Optional.empty()


override def toVirtualFile(path: Path): VirtualFile = {
throw new UnsupportedOperationException("This method should not be called in tests")
}

override def getSourceInfos: ReadSourceInfos = new TestSourceInfos
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class B {
def f: A = new A()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scalac.options = -sourcepath [basedir]/src/main/scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class A
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class B {
def f = new A()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
> compile

$ copy-file changes/B.scala src/main/scala/B.scala
$ touch src/main/scala/A.scala

> compile

0 comments on commit 99fbd8f

Please sign in to comment.