Skip to content

Commit 21e0132

Browse files
committed
Put longlit deprecation behind -Xlint
Also add deprecation to -Xlint, where it just enables the deprecation setting.
1 parent 55df35d commit 21e0132

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

+11-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import dotty.tools.dotc.core.Contexts._
99
import dotty.tools.dotc.rewrites.Rewrites
1010
import dotty.tools.io.{AbstractFile, Directory, JDK9Reflectors, PlainDirectory}
1111

12-
import scala.util.chaining._
12+
import scala.util.chaining.given
1313

1414
class ScalaSettings extends SettingGroup with AllScalaSettings
1515

@@ -143,9 +143,13 @@ private sealed trait VerboseSettings:
143143
/** -W "Warnings" settings
144144
*/
145145
private sealed trait WarningSettings:
146-
self: SettingGroup =>
146+
self: SettingGroup & CommonScalaSettings =>
147147
val Whelp: Setting[Boolean] = BooleanSetting("-W", "Print a synopsis of warning options.")
148148
val XfatalWarnings: Setting[Boolean] = BooleanSetting("-Werror", "Fail the compilation if there are any warnings.", aliases = List("-Xfatal-warnings"))
149+
private def syncDeprecation(ss: Settings.SettingsState) =
150+
if Xlint.valueIn(ss).contains("deprecation") then self.deprecation.updateIn(ss, true) else ss
151+
val Xlint: Setting[List[String]] = EnumSetting("-Wlint", "warning", "Enable recommended warnings.", LintWarning.values, default = Nil, aliases = List("-Xlint"), onUpdate = syncDeprecation)
152+
def isLintEnabled(w: LintWarning)(using Context): Boolean = Xlint.value.contains(w.toString)
149153

150154
val Wunused: Setting[List[String]] = MultiChoiceSetting(
151155
name = "-Wunused",
@@ -327,3 +331,8 @@ private sealed trait YSettings:
327331

328332
val YforceInlineWhileTyping: Setting[Boolean] = BooleanSetting("-Yforce-inline-while-typing", "Make non-transparent inline methods inline when typing. Emulates the old inlining behavior of 3.0.0-M3.")
329333
end YSettings
334+
335+
/** Warnings that may be queried with `ctx.settings.isLintEnabled`. */
336+
enum LintWarning:
337+
case deprecation, longLit
338+
end LintWarning

compiler/src/dotty/tools/dotc/config/Settings.scala

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import dotty.tools.io.{AbstractFile, Directory, JarArchive, PlainDirectory}
99

1010
import annotation.tailrec
1111
import collection.mutable.ArrayBuffer
12-
import reflect.ClassTag
12+
import reflect.{ClassTag, Enum}
1313
import scala.util.{Success, Failure}
1414

1515
object Settings:
@@ -61,14 +61,16 @@ object Settings:
6161
prefix: String = "",
6262
aliases: List[String] = Nil,
6363
depends: List[(Setting[?], Any)] = Nil,
64-
propertyClass: Option[Class[?]] = None)(private[Settings] val idx: Int) {
64+
propertyClass: Option[Class[?]] = None,
65+
onUpdate: SettingsState => SettingsState = s => s,
66+
)(private[Settings] val idx: Int) {
6567

6668
private var changed: Boolean = false
6769

6870
def valueIn(state: SettingsState): T = state.value(idx).asInstanceOf[T]
6971

7072
def updateIn(state: SettingsState, x: Any): SettingsState = x match
71-
case _: T => state.update(idx, x)
73+
case _: T => onUpdate(state.update(idx, x))
7274
case _ => throw IllegalArgumentException(s"found: $x of type ${x.getClass.getName}, required: ${implicitly[ClassTag[T]]}")
7375

7476
def isDefaultIn(state: SettingsState): Boolean = valueIn(state) == default
@@ -265,6 +267,9 @@ object Settings:
265267
def MultiChoiceSetting(name: String, helpArg: String, descr: String, choices: List[String], default: List[String], aliases: List[String] = Nil): Setting[List[String]] =
266268
publish(Setting(name, descr, default, helpArg, Some(choices), aliases = aliases))
267269

270+
def EnumSetting[E <: Enum](name: String, helpArg: String, descr: String, choices: Array[E], default: List[String], aliases: List[String] = Nil, onUpdate: SettingsState => SettingsState): Setting[List[String]] =
271+
publish(Setting(name, descr, default, helpArg, Some(choices.toList.map(_.toString)), aliases = aliases, onUpdate = onUpdate))
272+
268273
def IntSetting(name: String, descr: String, default: Int, aliases: List[String] = Nil): Setting[Int] =
269274
publish(Setting(name, descr, default, aliases = aliases))
270275

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import util.Chars._
1111
import util.{SourcePosition, CharBuffer}
1212
import util.Spans.Span
1313
import config.Config
14+
import config.LintWarning
1415
import Tokens._
1516
import scala.annotation.{switch, tailrec}
1617
import scala.collection.mutable
@@ -1450,7 +1451,7 @@ object Scanners {
14501451
getFraction()
14511452
// 1l is an acknowledged bad practice
14521453
def lintel(): Unit =
1453-
if ch == 'l' then
1454+
if ch == 'l' && ctx.settings.isLintEnabled(LintWarning.longLit) then
14541455
val msg = "Lowercase el for long is not recommended because it is easy to confuse with numeral 1; use uppercase L instead"
14551456
report.deprecationWarning(msg, sourcePos(offset + litBuf.length))
14561457
// after int: 5e7f, 42L, 42.toDouble but not 42b.

tests/neg-with-compiler/longlit.scala

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// scalac: -Werror -Xlint:longLit,deprecation
2+
3+
trait DejectedLiterals:
4+
5+
def bad = 1l // error
6+
7+
def worse = 123l // error
8+
9+
def worstest = 32l // error
10+
11+
// Lowercase el for long is not recommended because it is easy to confuse with numeral 1; use uppercase L instead

0 commit comments

Comments
 (0)