Skip to content

Commit

Permalink
improvement: Use the same minor version statically and cache dynamic …
Browse files Browse the repository at this point in the history
…resolution
  • Loading branch information
tgodzik committed Aug 9, 2024
1 parent cee908d commit a5aae38
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 45 deletions.
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ lazy val rules = projectMatrix
collectionCompat
)
else
Nil
List(
"org.scala-lang" %% "scala3-presentation-compiler" % scalaVersion.value
)
}
)
.defaultAxes(VirtualAxis.jvm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import scala.meta.internal.pc.ScalafixGlobal
import buildinfo.RulesBuildInfo
import metaconfig.Configured
import scalafix.internal.compat.CompilerCompat._
import scalafix.internal.pc.ExplicitResultTypesFallback
import scalafix.internal.pc.PcExplicitResultTypes
import scalafix.internal.v1.LazyValue
import scalafix.patch.Patch
import scalafix.v1._

final class ExplicitResultTypes(
val config: ExplicitResultTypesConfig,
global: LazyValue[Option[ScalafixGlobal]],
fallback: LazyValue[Option[ExplicitResultTypesFallback]]
fallback: LazyValue[Option[PcExplicitResultTypes]]
) extends SemanticRule("ExplicitResultTypes")
with ExplicitResultTypesBase[Scala2Printer] {

Expand Down Expand Up @@ -82,7 +82,7 @@ final class ExplicitResultTypes(
new ExplicitResultTypes(
c,
LazyValue.now(None),
LazyValue.now(Option(ExplicitResultTypesFallback(config)))
LazyValue.now(Option(PcExplicitResultTypes.dynamic(config)))
)
)
} else {
Expand All @@ -108,7 +108,7 @@ final class ExplicitResultTypes(

class Scala2Printer(
globalPrinter: Option[CompilerTypePrinter],
fallback: LazyValue[Option[ExplicitResultTypesFallback]]
fallback: LazyValue[Option[PcExplicitResultTypes]]
) extends Printer {
def defnType(
defn: Defn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ package scalafix.internal.rule

import scala.meta.*

import buildinfo.RulesBuildInfo
import dotty.tools.pc.ScalaPresentationCompiler
import metaconfig.Configured
import scalafix.internal.pc.ExplicitResultTypesFallback
import scalafix.internal.pc.PcExplicitResultTypes
import scalafix.patch.Patch
import scalafix.v1.*

final class ExplicitResultTypes(
val config: ExplicitResultTypesConfig,
fallback: Option[ExplicitResultTypesFallback]
fallback: Option[PcExplicitResultTypes]
) extends SemanticRule("ExplicitResultTypes")
with ExplicitResultTypesBase[Scala3Printer] {

def this() = this(ExplicitResultTypesConfig.default, None)

val compilerScalaVersion: String = RulesBuildInfo.scalaVersion

private def toBinaryVersion(v: String) = v.split('.').take(2).mkString(".")

override def description: String =
"Inserts type annotations for inferred public members."

Expand All @@ -34,7 +40,20 @@ final class ExplicitResultTypes(
ExplicitResultTypesConfig.default
)
.map(c =>
new ExplicitResultTypes(c, Option(ExplicitResultTypesFallback(config)))
new ExplicitResultTypes(
c,
Option {
if (
toBinaryVersion(config.scalaVersion) == toBinaryVersion(
compilerScalaVersion
)
)
PcExplicitResultTypes
.static(config, new ScalaPresentationCompiler())
else
PcExplicitResultTypes.dynamic(config)
}
)
)
}

Expand All @@ -50,7 +69,7 @@ final class ExplicitResultTypes(
}

class Scala3Printer(
fallback: Option[ExplicitResultTypesFallback]
fallback: Option[PcExplicitResultTypes]
) extends Printer {

def defnType(
Expand Down
44 changes: 25 additions & 19 deletions scalafix-rules/src/main/scala/scalafix/internal/pc/Embedded.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package scalafix.internal.pc

import java.net.URLClassLoader
import java.nio.file.Path
import java.util.ServiceLoader

import scala.jdk.CollectionConverters._
Expand All @@ -11,28 +10,20 @@ import scala.meta.pc.PresentationCompiler
import coursierapi.Dependency
import coursierapi.Fetch
import coursierapi.MavenRepository
import scala.collection.concurrent.TrieMap

object Embedded {

private val presentationCompilers: TrieMap[String, URLClassLoader] =
TrieMap.empty

def presentationCompiler(
scalaVersion: String
): PresentationCompiler = {
val deps =
scala3PresentationCompilerDependencies(scalaVersion)
val jars = Fetch
.create()
.addDependencies(deps: _*)
.addRepositories(
MavenRepository.of(
"https://oss.sonatype.org/content/repositories/snapshots"
)
)
.fetch()
.asScala
.map(_.toPath())
.toSeq
val classloader = newPresentationCompilerClassLoader(jars)

): PresentationCompiler = {
val classloader = presentationCompilers.getOrElseUpdate(
scalaVersion,
newPresentationCompilerClassLoader(scalaVersion)
)
val presentationCompilerClassname =
if (supportPresentationCompilerInDotty(scalaVersion)) {
"dotty.tools.pc.ScalaPresentationCompiler"
Expand Down Expand Up @@ -92,8 +83,23 @@ object Embedded {
}

private def newPresentationCompilerClassLoader(
jars: Seq[Path]
scalaVersion: String
): URLClassLoader = {

val deps =
scala3PresentationCompilerDependencies(scalaVersion)
val jars = Fetch
.create()
.addDependencies(deps: _*)
.addRepositories(
MavenRepository.of(
"https://oss.sonatype.org/content/repositories/snapshots"
)
)
.fetch()
.asScala
.map(_.toPath())
.toSeq
val allJars = jars.iterator
val allURLs = allJars.map(_.toUri.toURL).toArray
// Share classloader for a subset of types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import scalafix.v1._
*
* @param pc
*/
final class ExplicitResultTypesFallback private[ExplicitResultTypesFallback] (
final class PcExplicitResultTypes(
pc: LazyValue[Option[PresentationCompiler]]
) {

Expand Down Expand Up @@ -86,31 +86,50 @@ final class ExplicitResultTypesFallback private[ExplicitResultTypesFallback] (

}

object ExplicitResultTypesFallback {
def apply(config: Configuration): ExplicitResultTypesFallback = {
object PcExplicitResultTypes {
private def configure(
config: Configuration,
pc: PresentationCompiler
): PresentationCompiler = {
val symbolReplacements =
config.conf.dynamic.ExplicitResultTypes.symbolReplacements
.as[Map[String, String]]
.getOrElse(Map.empty)

pc.withConfiguration(
PresentationCompilerConfigImpl(
symbolPrefixes = symbolReplacements.asJava
)
).newInstance(
"ExplicitResultTypes",
config.scalacClasspath.map(_.toNIO).asJava,
// getting assertion errors if included
config.scalacOptions.filter(!_.contains("-release")).asJava
)
}

def dynamic(config: Configuration): PcExplicitResultTypes = {
val newPc: LazyValue[Option[PresentationCompiler]] =
LazyValue.from { () =>
Try(
Embedded
.presentationCompiler(config.scalaVersion)
.withConfiguration(
PresentationCompilerConfigImpl(
symbolPrefixes = symbolReplacements.asJava
)
)
.newInstance(
"ExplicitResultTypes",
config.scalacClasspath.map(_.toNIO).asJava,
// getting assertion errors if included
config.scalacOptions.filter(!_.contains("-release")).asJava
)
configure(
config,
Embedded.presentationCompiler(config.scalaVersion)
)
)
}
new ExplicitResultTypesFallback(newPc)
new PcExplicitResultTypes(newPc)
}

def static(
config: Configuration,
pc: PresentationCompiler
): PcExplicitResultTypes = {
val newPc: LazyValue[Option[PresentationCompiler]] =
LazyValue.from { () =>
Try(configure(config, pc))
}
new PcExplicitResultTypes(newPc)
}

}

0 comments on commit a5aae38

Please sign in to comment.