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 7, 2023
1 parent f61026d commit 7f26941
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 9 deletions.
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1861,8 +1861,10 @@ object Parsers {
val start = in.skipToken()
Ident(tpnme.USCOREkw).withSpan(Span(start, in.lastOffset, start))
else
if sourceVersion.isAtLeast(future) then
deprecationWarning(em"`_` is deprecated for wildcard arguments of types: use `?` instead")
report.errorOrMigrationWarning(
em"`_` is deprecated for wildcard arguments of types: use `?` instead${rewriteNotice(`future-migration`)}",
in.sourcePos(), from = future)
if sourceVersion == `future-migration` then
patch(source, Span(in.offset, in.offset + 1), "?")
val start = in.skipToken()
typeBounds().withSpan(Span(start, in.lastOffset, start))
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:future-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
2 changes: 1 addition & 1 deletion sbt-test/compilerReporter/i14576/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ lazy val root = (project in file("."))
},
assertDeprecationSummary := {
assert {
FakePrintWriter.messages.exists(_.contains("there were 3 deprecation warnings; re-run with -deprecation for details"))
FakePrintWriter.messages.exists(_.contains("there were 2 deprecation warnings; re-run with -deprecation for details"))
}
},
assertNoDeprecationSummary := {
Expand Down
5 changes: 5 additions & 0 deletions tests/neg/wildcard-type-syntax-future-migration.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Error: tests/neg/wildcard-type-syntax-future-migration.scala:7:17 ---------------------------------------------------
7 | case _: List[_] => // error
| ^
| `_` is deprecated for wildcard arguments of types: use `?` instead
| This construct can be rewritten automatically under -rewrite -source future-migration.
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
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[?] =>
6 changes: 6 additions & 0 deletions tests/pos/wildcard-type-syntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//> using options -Werror

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

0 comments on commit 7f26941

Please sign in to comment.