Skip to content

Commit

Permalink
Merge pull request #127 from sideeffffect/Vimplicits
Browse files Browse the repository at this point in the history
Add -Vimplicits and -Vtype-diffs
  • Loading branch information
DavidGregory084 authored Jul 11, 2023
2 parents 73e7763 + de84282 commit 57e9b17
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
java-version: ${{ matrix.java-version }}
jdkFile: ${{ steps.download-java.outputs.jdkFile }}

- name: Scalafmt
run: sbt scalafmtCheckAll scalafmtSbtCheck

- name: Run tests
run: |
sbt headerCheck
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ tpolecatDevModeOptions ~= { opts =>

You can customise the environment variable that is used to enable this mode by modifying the `ThisBuild / tpolecatDevModeEnvVar` key. Default: `"SBT_TPOLECAT_DEV"`.

#### Verbose mode

To enable the verbose mode, use the `tpolecatVerboseMode` command or define the environment variable `SBT_TPOLECAT_VERBOSE`.

In this mode all development mode options are enabled, and several verbose options that are commonly used for debugging implicit resolution and providing detailed compile errors are enabled.

You can customise the options that are enabled in this mode by modifying the `tpolecatVerboseModeOptions` key. Default: `tpolecatDevModeOptions.value ++ ScalacOptions.verboseOptions`.

For example, to disable verbose implicit logging you could customise the verbose mode options as follows:

```scala
tpolecatVerboseModeOptions ~= { opts =>
opts.filterNot(Set(ScalacOptions.verboseImplicits))
}
```

You can customise the environment variable that is used to enable this mode by modifying the `ThisBuild / tpolecatVerboseModeEnvVar` key. Default: `"SBT_TPOLECAT_VERBOSE"`.

#### Continuous integration mode

To enable the continuous integration mode, use the `tpolecatCiMode` command or define the environment variable `SBT_TPOLECAT_CI`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package io.github.davidgregory084

sealed abstract class OptionsMode extends Product with Serializable

case object VerboseMode extends OptionsMode
case object DevMode extends OptionsMode
case object CiMode extends OptionsMode
case object ReleaseMode extends OptionsMode
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,10 @@ private[davidgregory084] trait ScalacOptions {
/** Warn when non-Unit expression results are unused.
*/
val warnValueDiscard =
warnOption("value-discard", version => version.isBetween(V2_13_0, V3_0_0) || version.isAtLeast(V3_3_0))
warnOption(
"value-discard",
version => version.isBetween(V2_13_0, V3_0_0) || version.isAtLeast(V3_3_0)
)

/** Warn when an expression is ignored because it is followed by another expression.
*/
Expand All @@ -589,29 +592,44 @@ private[davidgregory084] trait ScalacOptions {
/** Warn if an implicit parameter is unused.
*/
val warnUnusedImplicits =
warnUnusedOption("implicits", version => version.isBetween(V2_13_0, V3_0_0) || version.isAtLeast(V3_3_0))
warnUnusedOption(
"implicits",
version => version.isBetween(V2_13_0, V3_0_0) || version.isAtLeast(V3_3_0)
)

/** Warn if an explicit parameter is unused.
*/
val warnUnusedExplicits =
warnUnusedOption("explicits", version => version.isBetween(V2_13_0, V3_0_0) || version.isAtLeast(V3_3_0))
warnUnusedOption(
"explicits",
version => version.isBetween(V2_13_0, V3_0_0) || version.isAtLeast(V3_3_0)
)

/** Warn if an import selector is not referenced.
*/
val warnUnusedImports =
warnUnusedOption("imports", version => version.isBetween(V2_13_0, V3_0_0) || version.isAtLeast(V3_3_0))
warnUnusedOption(
"imports",
version => version.isBetween(V2_13_0, V3_0_0) || version.isAtLeast(V3_3_0)
)

/** Warn if a local definition is unused.
*/
val warnUnusedLocals =
warnUnusedOption("locals", version => version.isBetween(V2_13_0, V3_0_0) || version.isAtLeast(V3_3_0))
warnUnusedOption(
"locals",
version => version.isBetween(V2_13_0, V3_0_0) || version.isAtLeast(V3_3_0)
)

/** Warn if either explicit or implicit parameters are unused.
*
* Equivalent to -Wunused:explicits,implicits.
*/
val warnUnusedParams =
warnUnusedOption("params", version => version.isBetween(V2_13_0, V3_0_0) || version.isAtLeast(V3_3_0))
warnUnusedOption(
"params",
version => version.isBetween(V2_13_0, V3_0_0) || version.isAtLeast(V3_3_0)
)

/** Warn if a variable bound in a pattern is unused.
*/
Expand All @@ -621,7 +639,10 @@ private[davidgregory084] trait ScalacOptions {
/** Warn if a private member is unused.
*/
val warnUnusedPrivates =
warnUnusedOption("privates", version => version.isBetween(V2_13_0, V3_0_0) || version.isAtLeast(V3_3_0))
warnUnusedOption(
"privates",
version => version.isBetween(V2_13_0, V3_0_0) || version.isAtLeast(V3_3_0)
)

/** Unused warning options (-Wunused:)
*/
Expand Down Expand Up @@ -653,6 +674,39 @@ private[davidgregory084] trait ScalacOptions {
warnError
)

/** Verbose options (-V)
*/
def verboseOption(name: String, isSupported: ScalaVersion => Boolean = _ => true) =
ScalacOption(s"-V$name", isSupported)

/** Print dependent missing implicits.
*/
val verboseImplicits =
verboseOption("implicits", _.isBetween(V2_13_0, V3_0_0))

/** Print found/required error messages as colored diffs.
*/
val verboseTypeDiffs =
verboseOption("type-diffs", _.isBetween(V2_13_6, V3_0_0))

/** Explain type errors in more detail.
*/
val explainTypes =
ScalacOption("-explaintypes", _ < V3_0_0)

/** Explain errors in more detail.
*/
val explain = ScalacOption("-explain", _ >= V3_0_0)

/** Verbose options (-V)
*/
val verboseOptions: Set[ScalacOption] = ListSet(
verboseImplicits,
verboseTypeDiffs,
explainTypes,
explain
)

/** The default set of Scala compiler options defined by sbt-tpolecat.
*/
val default: Set[ScalacOption] = ListSet(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ object TpolecatPlugin extends AutoPlugin {
"The mode to use for configuring scalac options via the sbt-tpolecat plugin."
)

val tpolecatVerboseModeEnvVar = settingKey[String](
"The environment variable to use to enable the sbt-tpolecat verbose mode."
)

val tpolecatDevModeEnvVar = settingKey[String](
"The environment variable to use to enable the sbt-tpolecat development mode."
)
Expand All @@ -76,6 +80,10 @@ object TpolecatPlugin extends AutoPlugin {
"The environment variable to use to enable the sbt-tpolecat release mode."
)

val tpolecatVerboseModeOptions = settingKey[Set[ScalacOption]](
"The set of scalac options that will be applied by the sbt-tpolecat plugin in the verbose mode."
)

val tpolecatDevModeOptions = settingKey[Set[ScalacOption]](
"The set of scalac options that will be applied by the sbt-tpolecat plugin in the development mode."
)
Expand Down Expand Up @@ -109,9 +117,13 @@ object TpolecatPlugin extends AutoPlugin {

val commandAliases =
addCommandAlias(
"tpolecatDevMode",
"set every tpolecatOptionsMode := _root_.io.github.davidgregory084.DevMode"
"tpolecatVerboseMode",
"set every tpolecatOptionsMode := _root_.io.github.davidgregory084.VerboseMode"
) ++
addCommandAlias(
"tpolecatDevMode",
"set every tpolecatOptionsMode := _root_.io.github.davidgregory084.DevMode"
) ++
addCommandAlias(
"tpolecatCiMode",
"set every tpolecatOptionsMode := _root_.io.github.davidgregory084.CiMode"
Expand All @@ -123,13 +135,15 @@ object TpolecatPlugin extends AutoPlugin {

override def buildSettings: Seq[Setting[_]] = Seq(
tpolecatDefaultOptionsMode := CiMode,
tpolecatVerboseModeEnvVar := "SBT_TPOLECAT_VERBOSE",
tpolecatDevModeEnvVar := "SBT_TPOLECAT_DEV",
tpolecatCiModeEnvVar := "SBT_TPOLECAT_CI",
tpolecatReleaseModeEnvVar := "SBT_TPOLECAT_RELEASE",
tpolecatOptionsMode := {
if (sys.env.contains(tpolecatReleaseModeEnvVar.value)) ReleaseMode
else if (sys.env.contains(tpolecatCiModeEnvVar.value)) CiMode
else if (sys.env.contains(tpolecatDevModeEnvVar.value)) DevMode
else if (sys.env.contains(tpolecatVerboseModeEnvVar.value)) VerboseMode
else tpolecatDefaultOptionsMode.value
},
tpolecatDevModeOptions := ScalacOptions.default
Expand Down Expand Up @@ -191,6 +205,9 @@ object TpolecatPlugin extends AutoPlugin {
supportedOptionsFor(scalaVersion.value, selectedOptions)
}
),
Def.derive(
tpolecatVerboseModeOptions := tpolecatDevModeOptions.value ++ ScalacOptions.verboseOptions
),
Def.derive(
tpolecatCiModeOptions := tpolecatDevModeOptions.value + ScalacOptions.fatalWarnings
),
Expand All @@ -199,6 +216,7 @@ object TpolecatPlugin extends AutoPlugin {
),
Def.derive(tpolecatScalacOptions := {
tpolecatOptionsMode.value match {
case VerboseMode => tpolecatVerboseModeOptions.value
case DevMode => tpolecatDevModeOptions.value
case CiMode => tpolecatCiModeOptions.value
case ReleaseMode => tpolecatReleaseModeOptions.value
Expand Down
17 changes: 17 additions & 0 deletions plugin/src/sbt-test/sbt-tpolecat/scalacOptions/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,23 @@ TaskKey[Unit]("checkDevMode") := {
assertEquals(actualOptions, expectedOptions)
}

TaskKey[Unit]("checkVerboseMode") := {
val scalaV = scalaVersion.value

val expectedOptions = scalaV match {
case Scala211 => Scala211Options ++ Seq("-explaintypes")
case Scala212 => Scala212Options ++ Seq("-explaintypes")
case Scala213 => Scala213Options ++ Seq("-Vimplicits", "-Vtype-diffs", "-explaintypes")
case Scala30 => Scala30Options ++ Seq("-explain")
case Scala31 => Scala31Options ++ Seq("-explain")
case Scala33 => Scala33Options ++ Seq("-explain")
}

val actualOptions = scalacOptions.value

assertEquals(actualOptions, expectedOptions)
}

TaskKey[Unit]("checkCiMode") := {
val scalaV = scalaVersion.value

Expand Down
4 changes: 4 additions & 0 deletions plugin/src/sbt-test/sbt-tpolecat/scalacOptions/test
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
> tpolecatDevMode
> +checkDevMode
> +compile
# Check verbose mode options
> tpolecatVerboseMode
> +checkVerboseMode
> +compile
# Check CI mode options
> tpolecatCiMode
> +checkCiMode
Expand Down

0 comments on commit 57e9b17

Please sign in to comment.