From 2fd17703d34196b94fc6127465eee9025e04a36a Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Tue, 26 Dec 2023 12:26:40 +0900 Subject: [PATCH] add convertToNewSyntax setting. prepare Scala 3.4 --- .scalafmt.conf | 6 ++++ build.sbt | 2 +- io/src/main/scala/sbt/internal/io/Retry.scala | 6 ++-- .../internal/io/SourceModificationWatch.scala | 2 +- .../nio/LegacyFileTreeRepository.scala | 2 +- .../internal/nio/PollingWatchService.scala | 10 +++--- io/src/main/scala/sbt/io/IO.scala | 14 ++++---- .../scala/sbt/internal/io/RetrySpec.scala | 2 +- .../io/SourceModificationWatchSpec.scala | 34 +++++++++---------- .../test/scala/sbt/io/IOSpecification.scala | 4 +-- project/HouseRulesPluglin.scala | 4 +-- 11 files changed, 46 insertions(+), 40 deletions(-) diff --git a/.scalafmt.conf b/.scalafmt.conf index 4bcacf16..8ae105bc 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -22,3 +22,9 @@ danglingParentheses.preset = true trailingCommas = preserve runner.dialect = Scala212Source3 + +rewrite.scala3.convertToNewSyntax = true +runner.dialectOverride.allowSignificantIndentation = false +runner.dialectOverride.allowAsForImportRename = false +runner.dialectOverride.allowStarWildcardImport = false +runner.dialectOverride.allowPostfixStarVarargSplices = false diff --git a/build.sbt b/build.sbt index 853ce936..1eaf65d3 100644 --- a/build.sbt +++ b/build.sbt @@ -43,7 +43,7 @@ ThisBuild / publishTo := { Some("releases" at nexus + "service/local/staging/deploy/maven2") } -def commonSettings: Seq[Setting[_]] = Seq( +def commonSettings: Seq[Setting[?]] = Seq( scalaVersion := scala212, compile / javacOptions ++= Seq("-Xlint", "-Xlint:-serial"), crossScalaVersions := Seq(scala212, scala213, scala3), diff --git a/io/src/main/scala/sbt/internal/io/Retry.scala b/io/src/main/scala/sbt/internal/io/Retry.scala index 02018089..ebfc0668 100644 --- a/io/src/main/scala/sbt/internal/io/Retry.scala +++ b/io/src/main/scala/sbt/internal/io/Retry.scala @@ -20,18 +20,18 @@ private[sbt] object Retry { try System.getProperty("sbt.io.retry.limit", defaultLimit.toString).toInt catch { case NonFatal(_) => defaultLimit } } - private[sbt] def apply[@specialized T](f: => T, excludedExceptions: Class[_ <: IOException]*): T = + private[sbt] def apply[@specialized T](f: => T, excludedExceptions: Class[? <: IOException]*): T = apply(f, limit, excludedExceptions: _*) private[sbt] def apply[@specialized T]( f: => T, limit: Int, - excludedExceptions: Class[_ <: IOException]*, + excludedExceptions: Class[? <: IOException]*, ): T = apply(f, limit, 100, excludedExceptions: _*) private[sbt] def apply[@specialized T]( f: => T, limit: Int, sleepInMillis: Long, - excludedExceptions: Class[_ <: IOException]*, + excludedExceptions: Class[? <: IOException]*, ): T = { require(limit >= 1, "limit must be 1 or higher: was: " + limit) def filter(e: Exception): Boolean = excludedExceptions match { diff --git a/io/src/main/scala/sbt/internal/io/SourceModificationWatch.scala b/io/src/main/scala/sbt/internal/io/SourceModificationWatch.scala index 30535c3a..1c0bcf6a 100644 --- a/io/src/main/scala/sbt/internal/io/SourceModificationWatch.scala +++ b/io/src/main/scala/sbt/internal/io/SourceModificationWatch.scala @@ -116,7 +116,7 @@ private[sbt] final class WatchState private ( } /** Retrieve events from the `WatchService` */ - private[sbt] def pollEvents(): Iterable[(Path, WatchEvent[_])] = { + private[sbt] def pollEvents(): Iterable[(Path, WatchEvent[?])] = { val events = service.pollEvents() events.toIterable.flatMap { case (k, evs) => evs.map((k.watchable().asInstanceOf[Path], _)) diff --git a/io/src/main/scala/sbt/internal/nio/LegacyFileTreeRepository.scala b/io/src/main/scala/sbt/internal/nio/LegacyFileTreeRepository.scala index e84e471a..70fb4775 100644 --- a/io/src/main/scala/sbt/internal/nio/LegacyFileTreeRepository.scala +++ b/io/src/main/scala/sbt/internal/nio/LegacyFileTreeRepository.scala @@ -46,7 +46,7 @@ private[sbt] class LegacyFileTreeRepository(logger: WatchLogger, watchService: W private[this] val handle = observable.addObserver((event: FileEvent[FileAttributes]) => { val attributes = event match { - case _: Deletion[_] => NonExistent + case _: Deletion[?] => NonExistent case _ => event.attributes } val events: Seq[FileEvent[FileAttributes]] = fileCache.update(event.path, attributes) diff --git a/io/src/main/scala/sbt/internal/nio/PollingWatchService.scala b/io/src/main/scala/sbt/internal/nio/PollingWatchService.scala index d6988974..7767cdcf 100644 --- a/io/src/main/scala/sbt/internal/nio/PollingWatchService.scala +++ b/io/src/main/scala/sbt/internal/nio/PollingWatchService.scala @@ -139,8 +139,8 @@ private[sbt] class PollingWatchService(delay: FiniteDuration, timeSource: TimeSo () } override def isValid: Boolean = true - override def pollEvents(): JList[WatchEvent[_]] = - pollEventsImpl.asInstanceOf[JList[WatchEvent[_]]] + override def pollEvents(): JList[WatchEvent[?]] = + pollEventsImpl.asInstanceOf[JList[WatchEvent[?]]] override def reset(): Boolean = events.synchronized { events.clear() true @@ -181,9 +181,9 @@ private[sbt] class PollingWatchService(delay: FiniteDuration, timeSource: TimeSo Some(this) } event match { - case _: Creation[_] if acceptCreate => offer(event) - case _: Deletion[_] if acceptDelete => offer(event) - case _: Update[_] if acceptModify => offer(event) + case _: Creation[?] if acceptCreate => offer(event) + case _: Deletion[?] if acceptDelete => offer(event) + case _: Update[?] if acceptModify => offer(event) case _ => None } } diff --git a/io/src/main/scala/sbt/io/IO.scala b/io/src/main/scala/sbt/io/IO.scala index bdd28035..99d5b9aa 100644 --- a/io/src/main/scala/sbt/io/IO.scala +++ b/io/src/main/scala/sbt/io/IO.scala @@ -63,7 +63,7 @@ object IO { * If the location cannot be determined, an error is generated. * Note that for JDK 11 onwards, a module will return a jrt path. */ - def classLocationPath(cl: Class[_]): NioPath = { + def classLocationPath(cl: Class[?]): NioPath = { val u = classLocation(cl) val p = u.getProtocol match { case FileScheme => Option(toFile(u).toPath) @@ -87,7 +87,7 @@ object IO { * If the location cannot be determined or it is not a file, an error is generated. * Note that for JDK 11 onwards, the returned module path cannot be expressed as `File`, so it will return `None`. */ - def classLocationFileOption(cl: Class[_]): Option[File] = { + def classLocationFileOption(cl: Class[?]): Option[File] = { val u = classLocation(cl) urlAsFile(u) } @@ -109,7 +109,7 @@ object IO { "classLocationFile may not work on JDK 11. Use classfileLocation, classLocationFileOption, or classLocationPath instead.", "1.3.0" ) - def classLocationFile(cl: Class[_]): File = + def classLocationFile(cl: Class[?]): File = classLocationFileOption(cl).getOrElse(sys.error(s"Unable to create File from $cl")) /** @@ -128,7 +128,7 @@ object IO { * If the location cannot be determined or it is not a file, an error is generated. * Note that for JDK 11 onwards, a module will return a jrt URL such as `jrt:/java.base`. */ - def classLocation(cl: Class[_]): URL = { + def classLocation(cl: Class[?]): URL = { def localcl: Option[URL] = Option(cl.getProtectionDomain.getCodeSource) flatMap { codeSource => Option(codeSource.getLocation) @@ -187,7 +187,7 @@ object IO { * Returns a URL for the classfile containing the given class * If the location cannot be determined, an error is generated. */ - def classfileLocation(cl: Class[_]): URL = { + def classfileLocation(cl: Class[?]): URL = { val clsfile = s"${cl.getName.replace('.', '/')}.class" def localcl: Option[URL] = Option(cl.getClassLoader) flatMap { classLoader => @@ -1232,7 +1232,7 @@ object IO { */ def objectInputStream(wrapped: InputStream, loader: ClassLoader): ObjectInputStream = new ObjectInputStream(wrapped) { - override def resolveClass(osc: ObjectStreamClass): Class[_] = { + override def resolveClass(osc: ObjectStreamClass): Class[?] = { val c = Class.forName(osc.getName, false, loader) if (c eq null) super.resolveClass(osc) else c } @@ -1491,7 +1491,7 @@ object IO { setModifiedTimeOrFalse(targetFile, math.max(last, 0L)) } - private val excludeFileNotFound: immutable.Seq[Class[_ <: IOException]] = List( + private val excludeFileNotFound: immutable.Seq[Class[? <: IOException]] = List( classOf[FileNotFoundException] ) } diff --git a/io/src/test/scala/sbt/internal/io/RetrySpec.scala b/io/src/test/scala/sbt/internal/io/RetrySpec.scala index c058e851..298f3fe4 100644 --- a/io/src/test/scala/sbt/internal/io/RetrySpec.scala +++ b/io/src/test/scala/sbt/internal/io/RetrySpec.scala @@ -17,7 +17,7 @@ import java.util.concurrent.atomic.AtomicInteger import org.scalatest.flatspec.AnyFlatSpec final class RetrySpec extends AnyFlatSpec { - private val noExcluded: List[Class[_ <: IOException]] = List[Class[_ <: IOException]]() + private val noExcluded: List[Class[? <: IOException]] = List[Class[? <: IOException]]() "retry" should "throw first exception after number of failures" in { val i = new AtomicInteger() def throww(): Any = throw new IOException(i.incrementAndGet().toString) diff --git a/io/src/test/scala/sbt/internal/io/SourceModificationWatchSpec.scala b/io/src/test/scala/sbt/internal/io/SourceModificationWatchSpec.scala index bed33305..f8310472 100644 --- a/io/src/test/scala/sbt/internal/io/SourceModificationWatchSpec.scala +++ b/io/src/test/scala/sbt/internal/io/SourceModificationWatchSpec.scala @@ -458,16 +458,16 @@ private[sbt] trait EventMonitorSpec { self: AnyFlatSpec with Matchers => } def watchTest( - monitor: FileEventMonitor[FileEvent[_]], - filter: FileEvent[_] => Boolean, - check: Seq[FileEvent[_]] => Boolean + monitor: FileEventMonitor[FileEvent[?]], + filter: FileEvent[?] => Boolean, + check: Seq[FileEvent[?]] => Boolean )(modifier: => Unit): Boolean = { modifier val events = monitor.poll(maxWait * 2, filter) check(events) } - def watchTest(base: File, filter: FileEvent[_] => Boolean, check: Seq[FileEvent[_]] => Boolean)( + def watchTest(base: File, filter: FileEvent[?] => Boolean, check: Seq[FileEvent[?]] => Boolean)( modifier: => Unit ): Boolean = { val globs = base.toPath.toRealPath().toFile.scalaSourceGlobs @@ -496,7 +496,7 @@ private[sbt] trait EventMonitorSpec { self: AnyFlatSpec with Matchers => object EventMonitorSpec { type Event = FileEvent[FileAttributes] val AllPass: Any => Boolean = ((_: Any) => true).label("AllPass") - val NonEmpty: Seq[FileEvent[_]] => Boolean = ((_: Seq[FileEvent[_]]).nonEmpty).label("NonEmpty") + val NonEmpty: Seq[FileEvent[?]] => Boolean = ((_: Seq[FileEvent[?]]).nonEmpty).label("NonEmpty") private implicit class LabeledFunction[T, R](val f: T => R) extends AnyVal { def label(string: String): T => R = new (T => R) { override def apply(t: T): R = f(t) @@ -514,33 +514,33 @@ object EventMonitorSpec { realPath(path.getParent, fileName.map(newFileName.resolve) orElse Some(newFileName)) } } - def pathFilter(path: Path): FileEvent[_] => Boolean = { + def pathFilter(path: Path): FileEvent[?] => Boolean = { val real = realPath(path) - ((_: FileEvent[_]).path == real).label(s"PathFilter($real)") + ((_: FileEvent[?]).path == real).label(s"PathFilter($real)") } - def contains(path: Path): Seq[FileEvent[_]] => Boolean = { + def contains(path: Path): Seq[FileEvent[?]] => Boolean = { val real = realPath(path) - ((_: Seq[FileEvent[_]]).exists(_.path == real)).label(s"Contains($real)") + ((_: Seq[FileEvent[?]]).exists(_.path == real)).label(s"Contains($real)") } - def excludes(path: Path): Seq[FileEvent[_]] => Boolean = { + def excludes(path: Path): Seq[FileEvent[?]] => Boolean = { val real = realPath(path) - ((s: Seq[FileEvent[_]]) => s.nonEmpty && s.forall(_.path != real)).label(s"Excludes($real)") + ((s: Seq[FileEvent[?]]) => s.nonEmpty && s.forall(_.path != real)).label(s"Excludes($real)") } - def includesOnly(path: Path): Seq[FileEvent[_]] => Boolean = { + def includesOnly(path: Path): Seq[FileEvent[?]] => Boolean = { val real = realPath(path) - ((s: Seq[FileEvent[_]]) => s.nonEmpty && s.forall(_.path == real)).label(s"IncludesOnly($real)") + ((s: Seq[FileEvent[?]]) => s.nonEmpty && s.forall(_.path == real)).label(s"IncludesOnly($real)") } - def isDeletion(path: Path): FileEvent[_] => Boolean = { + def isDeletion(path: Path): FileEvent[?] => Boolean = { val real = realPath(path) - (_: FileEvent[_]) match { + (_: FileEvent[?]) match { case Deletion(p, _) if p == real => true case _ => false } } - def hasDeletion(path: Path): Seq[FileEvent[_]] => Boolean = { + def hasDeletion(path: Path): Seq[FileEvent[?]] => Boolean = { val real = realPath(path) val deletion = isDeletion(real) - ((_: Seq[FileEvent[_]]).exists(deletion)).label(s"HasDeletion($real)") + ((_: Seq[FileEvent[?]]).exists(deletion)).label(s"HasDeletion($real)") } trait Logger extends WatchLogger diff --git a/io/src/test/scala/sbt/io/IOSpecification.scala b/io/src/test/scala/sbt/io/IOSpecification.scala index 6baf7793..24a6474d 100644 --- a/io/src/test/scala/sbt/io/IOSpecification.scala +++ b/io/src/test/scala/sbt/io/IOSpecification.scala @@ -17,7 +17,7 @@ import java.nio.file.Files object IOSpecification extends Properties("IO") { property("IO.classLocationPath able to determine containing directories") = forAll(classes) { - (c: Class[_]) => + (c: Class[?]) => Try(IO.classLocationPath(c)).toOption.exists { case jar if jar.getFileName.toString.endsWith(".jar") => Files.isRegularFile(jar) @@ -28,7 +28,7 @@ object IOSpecification extends Properties("IO") { } } - implicit def classes: Gen[Class[_]] = + implicit def classes: Gen[Class[?]] = Gen.oneOf( this.getClass, classOf[java.lang.Integer], diff --git a/project/HouseRulesPluglin.scala b/project/HouseRulesPluglin.scala index 71fce48c..5e203724 100644 --- a/project/HouseRulesPluglin.scala +++ b/project/HouseRulesPluglin.scala @@ -7,9 +7,9 @@ object HouseRulesPlugin extends AutoPlugin { override def requires = plugins.JvmPlugin override def trigger = allRequirements - override def projectSettings: Seq[Def.Setting[_]] = baseSettings + override def projectSettings: Seq[Def.Setting[?]] = baseSettings - lazy val baseSettings: Seq[Def.Setting[_]] = Seq( + lazy val baseSettings: Seq[Def.Setting[?]] = Seq( scalacOptions ++= Seq("-encoding", "utf8"), scalacOptions ++= Seq("-deprecation", "-feature", "-unchecked"), scalacOptions += "-language:higherKinds",