From a6d5ddd7e97c182566481813d59e7fc89c577d9d Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 1 Nov 2023 13:32:27 +0100 Subject: [PATCH] Disallow `_` for wildcard arguments of types and use `?` in future * In `future-migration` we emit the deprecation warning and enable the patch with `-rewrite`. * In `future` we emit we make this syntax an error --- compiler/src/dotty/tools/dotc/parsing/Parsers.scala | 6 ++++-- compiler/test-resources/repl/i13208.scala | 6 +++--- sbt-test/compilerReporter/i14576/Test.scala | 3 --- sbt-test/compilerReporter/i14576/build.sbt | 2 +- tests/neg/wildcard-type-syntax-future-migration.check | 5 +++++ tests/neg/wildcard-type-syntax-future-migration.scala | 8 ++++++++ tests/neg/wildcard-type-syntax-future.scala | 6 ++++++ tests/pos/wildcard-type-syntax-future-migration.scala | 6 ++++++ tests/pos/wildcard-type-syntax.scala | 6 ++++++ 9 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 tests/neg/wildcard-type-syntax-future-migration.check create mode 100644 tests/neg/wildcard-type-syntax-future-migration.scala create mode 100644 tests/neg/wildcard-type-syntax-future.scala create mode 100644 tests/pos/wildcard-type-syntax-future-migration.scala create mode 100644 tests/pos/wildcard-type-syntax.scala diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index 48653ad96197..203972ddbd87 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -1856,8 +1856,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)) diff --git a/compiler/test-resources/repl/i13208.scala b/compiler/test-resources/repl/i13208.scala index 61ace43c732d..c729c2c0448a 100644 --- a/compiler/test-resources/repl/i13208.scala +++ b/compiler/test-resources/repl/i13208.scala @@ -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 \ No newline at end of file diff --git a/sbt-test/compilerReporter/i14576/Test.scala b/sbt-test/compilerReporter/i14576/Test.scala index d94a49145f81..a605dd0c1129 100644 --- a/sbt-test/compilerReporter/i14576/Test.scala +++ b/sbt-test/compilerReporter/i14576/Test.scala @@ -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 diff --git a/sbt-test/compilerReporter/i14576/build.sbt b/sbt-test/compilerReporter/i14576/build.sbt index 9831c23c103e..f04d51a1b909 100644 --- a/sbt-test/compilerReporter/i14576/build.sbt +++ b/sbt-test/compilerReporter/i14576/build.sbt @@ -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 := { diff --git a/tests/neg/wildcard-type-syntax-future-migration.check b/tests/neg/wildcard-type-syntax-future-migration.check new file mode 100644 index 000000000000..d3e0a8569b6a --- /dev/null +++ b/tests/neg/wildcard-type-syntax-future-migration.check @@ -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. diff --git a/tests/neg/wildcard-type-syntax-future-migration.scala b/tests/neg/wildcard-type-syntax-future-migration.scala new file mode 100644 index 000000000000..953498becb39 --- /dev/null +++ b/tests/neg/wildcard-type-syntax-future-migration.scala @@ -0,0 +1,8 @@ +//> using options -Werror + +import scala.language.`future-migration` + +def test = + Seq() match + case _: List[_] => // error + case _: Seq[?] => diff --git a/tests/neg/wildcard-type-syntax-future.scala b/tests/neg/wildcard-type-syntax-future.scala new file mode 100644 index 000000000000..750fd55ec226 --- /dev/null +++ b/tests/neg/wildcard-type-syntax-future.scala @@ -0,0 +1,6 @@ +import scala.language.future + +def test = + Seq() match + case _: List[_] => // error + case _: Seq[?] => diff --git a/tests/pos/wildcard-type-syntax-future-migration.scala b/tests/pos/wildcard-type-syntax-future-migration.scala new file mode 100644 index 000000000000..3075c609ffdc --- /dev/null +++ b/tests/pos/wildcard-type-syntax-future-migration.scala @@ -0,0 +1,6 @@ +import scala.language.`future-migration` + +def test = + Seq() match + case _: List[_] => // warn + case _: Seq[?] => diff --git a/tests/pos/wildcard-type-syntax.scala b/tests/pos/wildcard-type-syntax.scala new file mode 100644 index 000000000000..6f931505f55a --- /dev/null +++ b/tests/pos/wildcard-type-syntax.scala @@ -0,0 +1,6 @@ +//> using options -Werror + +def test = + Seq() match + case _: List[_] => // error + case _: Seq[?] =>