Skip to content

Commit

Permalink
Merge pull request #64 from DavidGregory084/source-options
Browse files Browse the repository at this point in the history
Add -Xsource and -source options to the ScalacOptions DSL.
  • Loading branch information
DavidGregory084 authored Apr 13, 2022
2 parents 531062a + 8b18926 commit 311dc9e
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 4 deletions.
60 changes: 60 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.typesafe.tools.mima.core._

// Common settings

name := "sbt-tpolecat"
Expand Down Expand Up @@ -54,6 +56,64 @@ mimaPreviousArtifacts := Set(
projectID.value.withRevision("0.2.0")
)

mimaBinaryIssueFilters ++= Seq(
// New ScalacOptions DSL methods
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.source210"
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.source211"
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.source212"
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.source213"
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.source3"
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.sourceFuture"
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.sourceFutureMigration"
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.source3Migration"
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.source31"
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.io$github$davidgregory084$ScalacOptions$_setter_$source210_="
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.io$github$davidgregory084$ScalacOptions$_setter_$source211_="
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.io$github$davidgregory084$ScalacOptions$_setter_$source212_="
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.io$github$davidgregory084$ScalacOptions$_setter_$source213_="
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.io$github$davidgregory084$ScalacOptions$_setter_$source3_="
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.io$github$davidgregory084$ScalacOptions$_setter_$sourceFuture_="
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.io$github$davidgregory084$ScalacOptions$_setter_$sourceFutureMigration_="
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.io$github$davidgregory084$ScalacOptions$_setter_$source3Migration_="
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"io.github.davidgregory084.ScalacOptions.io$github$davidgregory084$ScalacOptions$_setter_$source31_="
)
)

// Testing

scriptedBufferLog := false
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/io/github/davidgregory084/ScalaVersion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ case class ScalaVersion(major: Long, minor: Long, patch: Long) {
object ScalaVersion {
val V2_11_0 = ScalaVersion(2, 11, 0)
val V2_12_0 = ScalaVersion(2, 12, 0)
val V2_12_2 = ScalaVersion(2, 12, 2)
val V2_13_0 = ScalaVersion(2, 13, 0)
val V2_13_2 = ScalaVersion(2, 13, 2)
val V2_13_3 = ScalaVersion(2, 13, 3)
val V2_13_4 = ScalaVersion(2, 13, 4)
val V2_13_5 = ScalaVersion(2, 13, 5)
val V2_13_6 = ScalaVersion(2, 13, 6)
val V3_0_0 = ScalaVersion(3, 0, 0)
val V3_1_0 = ScalaVersion(3, 1, 0)

implicit val scalaVersionOrdering: Ordering[ScalaVersion] =
Ordering.by(version => (version.major, version.minor, version.patch))
Expand Down
70 changes: 70 additions & 0 deletions src/main/scala/io/github/davidgregory084/ScalacOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,46 @@ trait ScalacOptions {
val feature =
new ScalacOption(List("-feature"))

/** Enable features that will be available in a future version of Scala, for purposes of early
* migration and alpha testing.
*/
def scala3Source(version: String, isSupported: ScalaVersion => Boolean = _ >= V3_0_0) =
new ScalacOption(List("-source", version), isSupported)

/** Enable features that will be available in Scala 3.0.x with Scala 2.x compatibility mode, for
* purposes of early migration and alpha testing.
*
* Same as the enabled default `-source:3.0` but with additional helpers to migrate from 2.13.
*
* In addition:
* - flags some Scala 2 constructs that are disallowed in Scala 3 as migration warnings instead
* of hard errors
* - changes some rules to be more lenient and backwards compatible with Scala 2.13
* - gives some additional warnings where the semantics has changed between Scala 2.13 and 3.0
* - in conjunction with -rewrite, offer code rewrites from Scala 2.13 to 3.0
*/
val source3Migration = scala3Source("3.0-migration", version => version >= V3_0_0)

/** Enable features that will be available in future versions of Scala 3.x, for purposes of early
* migration and alpha testing.
*/
val sourceFuture = scala3Source("future", version => version >= V3_0_0)

/** Enable features that will be available in future versions of Scala 3.x with Scala 2.x
* compatibility mode, for purposes of early migration and alpha testing.
*
* Same as `-source:future` but with additional helpers to migrate from 3.0.
*
* Similarly to the helpers available under 3.0-migration, these include migration warnings and
* optional rewrites.
*/
val sourceFutureMigration = scala3Source("future-migration", version => version >= V3_0_0)

/** Enable features that will be available in Scala 3.1.x, for purposes of early migration and
* alpha testing.
*/
val source31 = scala3Source("3.1", version => version >= V3_1_0)

/** Enable or disable language features
*/
def languageFeatureOption(name: String, isSupported: ScalaVersion => Boolean = _ => true) =
Expand Down Expand Up @@ -257,6 +297,36 @@ trait ScalacOptions {
disableLintBynameImplicit
)

/** Treat compiler input as Scala source for the specified version.
*/
def source(version: String, isSupported: ScalaVersion => Boolean = _ >= V3_0_0) =
advancedOption(s"source:$version", isSupported)

/** Treat compiler input as Scala source for version 2.10.
*/
val source210 = source("2.10", version => version < V2_13_2)

/** Treat compiler input as Scala source for version 2.11.
*/
val source211 = source("2.11", version => version < V2_13_2)

/** Treat compiler input as Scala source for version 2.12.
*/
val source212 = source("2.12", version => version < V2_13_2)

/** Treat compiler input as Scala source for version 2.13.
*/
val source213 = source("2.13", version => version.isBetween(V2_12_2, V3_0_0))

/** Treat compiler input as Scala source for version 3.x:
*
* - Most deprecated syntax generates an error.
* - Infix operators can start a line in the middle of a multiline expression.
* - Implicit search and overload resolution follow Scala 3 handling of contravariance when
* checking specificity.
*/
val source3 = source("3", version => version.isBetween(V2_12_2, V3_0_0))

/** Advanced options (-X)
*/
val advancedOptions: Set[ScalacOption] = ListSet(
Expand Down
19 changes: 15 additions & 4 deletions src/sbt-test/sbt-tpolecat/scalacOptions/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ crossScalaVersions := Seq(
Scala31
)

tpolecatDevModeOptions ++= Set(
ScalacOptions.source213,
ScalacOptions.source3Migration
)

tpolecatReleaseModeOptions ++= ScalacOptions.optimizerOptions("**")

val Scala211Options =
Expand Down Expand Up @@ -96,7 +101,8 @@ val Scala212Options =
"-Ywarn-unused:locals",
"-Ywarn-unused:params",
"-Ywarn-unused:patvars",
"-Ywarn-unused:privates"
"-Ywarn-unused:privates",
"-Xsource:2.13"
)

val Scala213Options =
Expand Down Expand Up @@ -140,7 +146,8 @@ val Scala213Options =
"-Wunused:locals",
"-Wunused:params",
"-Wunused:patvars",
"-Wunused:privates"
"-Wunused:privates",
"-Xsource:2.13"
)

val Scala30Options =
Expand All @@ -153,7 +160,9 @@ val Scala30Options =
"-language:experimental.macros",
"-language:higherKinds",
"-language:implicitConversions",
"-Ykind-projector"
"-Ykind-projector",
"-source",
"3.0-migration"
)

val Scala31Options =
Expand All @@ -166,7 +175,9 @@ val Scala31Options =
"-language:experimental.macros",
"-language:higherKinds",
"-language:implicitConversions",
"-Ykind-projector"
"-Ykind-projector",
"-source",
"3.0-migration"
)

TaskKey[Unit]("checkDevMode") := {
Expand Down

0 comments on commit 311dc9e

Please sign in to comment.