Skip to content

Commit 83cf427

Browse files
authored
Merge pull request #14978 from dotty-staging/deprecate-3.1-migration
deprecate 3.1-migration, err on import
2 parents d5ae77e + 1e18c01 commit 83cf427

File tree

7 files changed

+26
-3
lines changed

7 files changed

+26
-3
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@ enum SourceVersion:
1717

1818
object SourceVersion extends Property.Key[SourceVersion]:
1919

20-
val allSourceVersionNames = values.toList.map(_.toString.toTermName)
20+
/** language versions that may appear in a language import, are deprecated, but not removed from the standard library. */
21+
val illegalSourceVersionNames = List("3.1-migration").map(_.toTermName)
22+
23+
/** language versions that the compiler recognises. */
24+
val validSourceVersionNames = values.toList.map(_.toString.toTermName)
25+
26+
/** All source versions that can be recognised from a language import. e.g. `import language.3.1` */
27+
val allSourceVersionNames = validSourceVersionNames ::: illegalSourceVersionNames
2128
end SourceVersion

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,6 +3150,15 @@ object Parsers {
31503150
syntaxError(i"source version import is only allowed at the toplevel", id.span)
31513151
else if ctx.compilationUnit.sourceVersion.isDefined then
31523152
syntaxError(i"duplicate source version import", id.span)
3153+
else if illegalSourceVersionNames.contains(imported) then
3154+
val candidate =
3155+
val nonMigration = imported.toString.replace("-migration", "")
3156+
validSourceVersionNames.find(_.show == nonMigration)
3157+
val baseMsg = i"`$imported` is not a valid source version"
3158+
val msg = candidate match
3159+
case Some(member) => i"$baseMsg, did you mean language.`$member`?"
3160+
case _ => baseMsg
3161+
syntaxError(msg, id.span)
31533162
else
31543163
ctx.compilationUnit.sourceVersion = Some(SourceVersion.valueOf(imported.toString))
31553164
case None =>

library/src/scala/runtime/stdLibPatches/language.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,13 @@ object language:
149149
object `3.0`
150150

151151
/** Set source version to 3.1-migration.
152+
*
153+
* This is a no-op, and should not be used. A syntax error will be reported upon import.
152154
*
153155
* @see [[https://docs.scala-lang.org/scala3/guides/migration/compatibility-intro.html]]
154156
*/
155157
@compileTimeOnly("`3.1-migration` can only be used at compile time in import statements")
158+
@deprecated("`3.1-migration` is not valid, use `3.1` instead", since = "3.2")
156159
object `3.1-migration`
157160

158161
/** Set source version to 3.1

tests/neg/i12457.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import language.`3.1-migration`
1+
import language.`3.1`
22

33
trait X [ X <: Z , Z >: X [ R ] ] // error
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/source-import-3-1-migration.scala:1:16 -------------------------------------------------------------
2+
1 |import language.`3.1-migration` // error
3+
| ^^^^^^^^^^^^^^^
4+
| `3.1-migration` is not a valid source version, did you mean language.`3.1`?
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import language.`3.1-migration` // error

tests/pos/source-import-3-1-migration.scala

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)