Skip to content

Commit

Permalink
Deprecation warnings for old syntax: _ type wildcards
Browse files Browse the repository at this point in the history
* In `3.4` we emit the deprecation warning and enable the
  patch with `-rewrite`.
* In `future` we emit we make this syntax an error
  • Loading branch information
nicolasstucki committed Nov 9, 2023
1 parent 4991296 commit 57d08e5
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 9 deletions.
12 changes: 9 additions & 3 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1870,9 +1870,15 @@ object Parsers {
val start = in.skipToken()
Ident(tpnme.USCOREkw).withSpan(Span(start, in.lastOffset, start))
else
if !inTypeMatchPattern && sourceVersion.isAtLeast(future) then
deprecationWarning(em"`_` is deprecated for wildcard arguments of types: use `?` instead")
patch(source, Span(in.offset, in.offset + 1), "?")
if !inTypeMatchPattern then
report.gradualErrorOrMigrationWarning(
em"`_` is deprecated for wildcard arguments of types: use `?` instead${rewriteNotice(`3.4-migration`)}",
in.sourcePos(),
warnFrom = `3.4`,
errorFrom = future)
if sourceVersion.isMigrating && sourceVersion.isAtLeast(`3.4-migration`) then
patch(source, Span(in.offset, in.offset + 1), "?")
end if
val start = in.skipToken()
typeBounds().withSpan(Span(start, in.lastOffset, start))
// Allow symbols -_ and +_ through for compatibility with code written using kind-projector in Scala 3 underscore mode.
Expand Down
6 changes: 3 additions & 3 deletions compiler/test-resources/repl/i13208.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//> using options -source:future -deprecation
//> using options -source:3.4-migration
scala> type M[X] = X match { case Int => String case _ => Int }
scala> type N[X] = X match { case List[_] => Int }
1 warning found
-- Deprecation Warning: --------------------------------------------------------
-- Migration Warning: ----------------------------------------------------------
1 | type N[X] = X match { case List[_] => Int }
| ^
| `_` is deprecated for wildcard arguments of types: use `?` instead
| `_` is deprecated for wildcard arguments of types: use `?` instead
3 changes: 0 additions & 3 deletions sbt-test/compilerReporter/i14576/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,3 @@ object Test:

// private[this] and = _ are deprecated under -source:future
private[this] var x: AnyRef = _

// under -source:future, `_` is deprecated for wildcard arguments of types: use `?` instead
val xs: List[_] = Nil
5 changes: 5 additions & 0 deletions tests/neg/wildcard-type-syntax-3.4.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Error: tests/neg/wildcard-type-syntax-3.4.scala:7:17 ----------------------------------------------------------------
7 | case _: List[_] => // error: migration warning
| ^
| `_` is deprecated for wildcard arguments of types: use `?` instead
| This construct can be rewritten automatically under -rewrite -source 3.4-migration.
8 changes: 8 additions & 0 deletions tests/neg/wildcard-type-syntax-3.4.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//> using options -Werror

import scala.language.`3.4-migration`

def test =
Seq() match
case _: List[_] => // error: migration warning
case _: Seq[?] =>
8 changes: 8 additions & 0 deletions tests/neg/wildcard-type-syntax-future-migration.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//> using options -Werror

import scala.language.`future-migration`

def test =
Seq() match
case _: List[_] => // error: migration warning
case _: Seq[?] =>
6 changes: 6 additions & 0 deletions tests/neg/wildcard-type-syntax-future.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import scala.language.future

def test =
Seq() match
case _: List[_] => // error
case _: Seq[?] =>
6 changes: 6 additions & 0 deletions tests/pos/wildcard-type-syntax-future-migration.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import scala.language.`future-migration`

def test =
Seq() match
case _: List[_] => // warn
case _: Seq[?] =>
8 changes: 8 additions & 0 deletions tests/pos/wildcard-type-syntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//> using options -Werror

import scala.language.`3.3`

def test =
Seq() match
case _: List[_] =>
case _: Seq[?] =>

0 comments on commit 57d08e5

Please sign in to comment.