Skip to content

Commit 5c35b48

Browse files
committed
Deprecation warnings for old syntax: var x = _
* In `3.4` we emit the deprecation warning * In `future` we emit we make this syntax an error * Add patch. Not ideal because we need to use the full path of `uninitialized`
1 parent f61026d commit 5c35b48

File tree

18 files changed

+68
-21
lines changed

18 files changed

+68
-21
lines changed

community-build/src/scala/dotty/communitybuild/projects.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ object projects:
362362
project = "shapeless-3",
363363
sbtTestCommand = "testJVM; testJS",
364364
sbtDocCommand = forceDoc("typeable", "deriving"),
365-
scalacOptions = SbtCommunityProject.scalacOptions.filter(_ != "-Ysafe-init"), // due to -Xfatal-warnings
365+
scalacOptions = "-source" :: "3.3" :: SbtCommunityProject.scalacOptions.filter(_ != "-Ysafe-init"), // due to -Xfatal-warnings
366366
)
367367

368368
lazy val xmlInterpolator = SbtCommunityProject(

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3655,11 +3655,14 @@ object Parsers {
36553655
subExpr() match
36563656
case rhs0 @ Ident(name) if placeholderParams.nonEmpty && name == placeholderParams.head.name
36573657
&& !tpt.isEmpty && mods.is(Mutable) && lhs.forall(_.isInstanceOf[Ident]) =>
3658-
if sourceVersion.isAtLeast(future) then
3659-
deprecationWarning(
3660-
em"""`= _` has been deprecated; use `= uninitialized` instead.
3661-
|`uninitialized` can be imported with `scala.compiletime.uninitialized`.""",
3662-
rhsOffset)
3658+
report.gradualErrorOrMigrationWarning(
3659+
em"""`= _` has been deprecated; use `= uninitialized` instead.
3660+
|`uninitialized` can be imported with `scala.compiletime.uninitialized`.${rewriteNotice(`3.4-migration`)}""",
3661+
in.sourcePos(rhsOffset),
3662+
warnFrom = `3.4`,
3663+
errorFrom = future)
3664+
if sourceVersion.isMigrating && sourceVersion.isAtLeast(`3.4-migration`) then
3665+
patch(source, Span(rhsOffset, rhsOffset + 1), "scala.compiletime.uninitialized")
36633666
placeholderParams = placeholderParams.tail
36643667
atSpan(rhs0.span) { Ident(nme.WILDCARD) }
36653668
case rhs0 => rhs0

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class CompilationTests {
6060
compileFile("tests/rewrites/rewrites.scala", defaultOptions.and("-source", "3.0-migration").and("-rewrite", "-indent")),
6161
compileFile("tests/rewrites/rewrites3x.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
6262
compileFile("tests/rewrites/rewrites3x-fatal-warnings.scala", defaultOptions.and("-rewrite", "-source", "future-migration", "-Xfatal-warnings")),
63+
compileFile("tests/rewrites/uninitialized-var.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
6364
compileFile("tests/rewrites/with-type-operator.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
6465
compileFile("tests/rewrites/filtering-fors.scala", defaultOptions.and("-rewrite", "-source", "3.2-migration")),
6566
compileFile("tests/rewrites/refutable-pattern-bindings.scala", defaultOptions.and("-rewrite", "-source", "3.2-migration")),

language-server/src/dotty/tools/languageserver/DottyLanguageServer.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import scala.collection._
1515
import scala.jdk.CollectionConverters._
1616
import scala.util.control.NonFatal
1717
import scala.io.Codec
18+
import scala.compiletime.uninitialized
1819

1920
import dotc._
2021
import ast.{Trees, tpd, untpd}
@@ -54,14 +55,14 @@ class DottyLanguageServer extends LanguageServer
5455
import lsp4j.jsonrpc.messages.{Either => JEither}
5556
import lsp4j._
5657

57-
private[this] var rootUri: String = _
58+
private[this] var rootUri: String = uninitialized
5859

59-
private[this] var myClient: DottyClient = _
60+
private[this] var myClient: DottyClient = uninitialized
6061
def client: DottyClient = myClient
6162

62-
private[this] var myDrivers: mutable.Map[ProjectConfig, InteractiveDriver] = _
63+
private[this] var myDrivers: mutable.Map[ProjectConfig, InteractiveDriver] = uninitialized
6364

64-
private[this] var myDependentProjects: mutable.Map[ProjectConfig, mutable.Set[ProjectConfig]] = _
65+
private[this] var myDependentProjects: mutable.Map[ProjectConfig, mutable.Set[ProjectConfig]] = uninitialized
6566

6667
def drivers: Map[ProjectConfig, InteractiveDriver] = thisServer.synchronized {
6768
if myDrivers == null then

library/src/scala/util/control/NonLocalReturns.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package scala.util.control
22

3+
import scala.compiletime.uninitialized
4+
35
/** Library implementation of nonlocal return.
46
*
57
* Usage:
@@ -21,7 +23,7 @@ package scala.util.control
2123
object NonLocalReturns {
2224
@deprecated("Use scala.util.boundary.Break instead", "3.3")
2325
class ReturnThrowable[T] extends ControlThrowable {
24-
private var myResult: T = _
26+
private var myResult: T = uninitialized
2527
def throwReturn(result: T): Nothing = {
2628
myResult = result
2729
throw this

presentation-compiler/src/main/dotty/tools/pc/MetalsDriver.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import dotty.tools.dotc.interactive.InteractiveDriver
77
import dotty.tools.dotc.reporting.Diagnostic
88
import dotty.tools.dotc.util.SourceFile
99

10+
import scala.compiletime.uninitialized
11+
1012
/**
1113
* MetalsDriver is a wrapper class that provides a compilation cache for InteractiveDriver.
1214
* MetalsDriver skips running compilation if
@@ -29,7 +31,7 @@ class MetalsDriver(
2931
override val settings: List[String]
3032
) extends InteractiveDriver(settings):
3133

32-
@volatile private var lastCompiledURI: URI = _
34+
@volatile private var lastCompiledURI: URI = uninitialized
3335

3436
private def alreadyCompiled(uri: URI, content: Array[Char]): Boolean =
3537
compilationUnits.get(uri) match

project/Build.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,8 @@ object Build {
578578
lazy val commonDottyCompilerSettings = Seq(
579579
// Note: bench/profiles/projects.yml should be updated accordingly.
580580
Compile / scalacOptions ++= Seq("-Yexplicit-nulls", "-Ysafe-init"),
581-
581+
// Use source 3.3 to avoid fatal migration warnings on scalajs-ir
582+
scalacOptions ++= Seq("-source", "3.3"),
582583
// Generate compiler.properties, used by sbt
583584
(Compile / resourceGenerators) += Def.task {
584585
import java.util._
@@ -1254,6 +1255,7 @@ object Build {
12541255
val mtagsVersion = "1.0.0"
12551256

12561257
Seq(
1258+
scalacOptions ++= Seq("-source", "3.3"), // To avoid fatal migration warnings
12571259
libraryDependencies ++= Seq(
12581260
"org.lz4" % "lz4-java" % "1.8.0",
12591261
"io.get-coursier" % "interface" % "1.0.18",

sbt-test/compilerReporter/i14576/Test.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,5 @@ object Test:
1010
def f(x: Text) = println(x.str)
1111
f("abc")
1212

13-
// private[this] and = _ are deprecated under -source:future
14-
private[this] var x: AnyRef = _
15-
1613
// under -source:future, `_` is deprecated for wildcard arguments of types: use `?` instead
1714
val xs: List[_] = Nil

sbt-test/compilerReporter/i14576/build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ lazy val root = (project in file("."))
2424
},
2525
assertDeprecationSummary := {
2626
assert {
27-
FakePrintWriter.messages.exists(_.contains("there were 3 deprecation warnings; re-run with -deprecation for details"))
27+
FakePrintWriter.messages.exists(_.contains("there was 1 deprecation warning; re-run with -deprecation for details"))
2828
}
2929
},
3030
assertNoDeprecationSummary := {

tests/neg/i4812.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//> using options -Werror
22
object Test:
3-
var prev: Any = _
3+
var prev: Any = scala.compiletime.uninitialized
44

55
def test[T](x: T): T =
66
class A(val elem: (T, Boolean))
@@ -55,7 +55,7 @@ object Test:
5555

5656
def test6[T](x: T): T =
5757
class A { var b: B = null }
58-
class B { var a: A = null; var elem: T = _ }
58+
class B { var a: A = null; var elem: T = scala.compiletime.uninitialized }
5959
prev match
6060
case prev: A => // error: the type test for A cannot be checked at runtime
6161
prev.b.elem
@@ -88,7 +88,7 @@ object Test:
8888
case x: B => x
8989

9090
sealed class A
91-
var prevA: A = _
91+
var prevA: A = scala.compiletime.uninitialized
9292
def test10: A =
9393
val methodCallId = System.nanoTime()
9494
class B(val id: Long) extends A

tests/neg/uninitialized-3.4.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Error: tests/neg/uninitialized-3.4.scala:7:15 -----------------------------------------------------------------------
2+
7 | var a: Int = _ // error: migration warning
3+
| ^
4+
| `= _` has been deprecated; use `= uninitialized` instead.
5+
| `uninitialized` can be imported with `scala.compiletime.uninitialized`.
6+
| This construct can be rewritten automatically under -rewrite -source 3.4-migration.

tests/neg/uninitialized-3.4.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//> using options -Werror
2+
3+
import scala.language.`3.4`
4+
import scala.compiletime.uninitialized
5+
6+
class Foo:
7+
var a: Int = _ // error: migration warning
8+
var b: Int = uninitialized
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//> using options -Werror
2+
3+
import scala.language.`future-migration`
4+
import scala.compiletime.uninitialized
5+
6+
class Foo:
7+
var a: Int = _ // error: migration warning
8+
var b: Int = uninitialized

tests/neg/uninitialized-future.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import scala.language.future
2+
import scala.compiletime.uninitialized
3+
4+
class Foo:
5+
var a: Int = _ // error
6+
var b: Int = uninitialized

tests/patmat/i12805-fallout.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import scala.annotation.unchecked.uncheckedVariance
2+
import scala.compiletime.uninitialized
23

34
type Untyped = Null
45

@@ -7,7 +8,7 @@ class Type
78
abstract class Tree[-T >: Untyped] {
89
type ThisTree[T >: Untyped] <: Tree[T]
910

10-
protected var myTpe: T @uncheckedVariance = _
11+
protected var myTpe: T @uncheckedVariance = uninitialized
1112

1213
def withType(tpe: Type): ThisTree[Type] = {
1314
val tree = this.asInstanceOf[ThisTree[Type]]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import scala.language.`future-migration`
2+
import scala.compiletime.uninitialized
3+
4+
class Foo:
5+
var a: Int = _ // warn
6+
var b: Int = uninitialized
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Foo:
2+
var a: Int = scala.compiletime.uninitialized
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Foo:
2+
var a: Int = _

0 commit comments

Comments
 (0)