Skip to content

Commit 52b26f8

Browse files
authored
chore: filter allowed source versions by import and by settings (#23215)
Closes #23214 We also split the logic to handle illegal imports separately as in #23082, we want to authorise `2.13` in imports but not in the settings
2 parents a1b5d43 + acacffe commit 52b26f8

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ object ScalaSettingsProperties:
2424
ScalaRelease.values.toList.map(_.show)
2525

2626
def supportedSourceVersions: List[String] =
27-
(SourceVersion.values.toList.diff(SourceVersion.illegalSourceVersionNames)).toList.map(_.toString)
27+
SourceVersion.values.diff(SourceVersion.illegalInSettings)
28+
.map(_.toString).toList
2829

2930
def supportedLanguageFeatures: List[ChoiceWithHelp[String]] =
3031
Feature.values.map((n, d) => ChoiceWithHelp(n.toString, d))

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import Feature.isPreviewEnabled
88
import util.Property
99

1010
enum SourceVersion:
11-
case `3.0-migration`, `3.0`, `3.1` // Note: do not add `3.1-migration` here, 3.1 is the same language as 3.0.
11+
case `3.0-migration`, `3.0`
12+
case `3.1-migration`, `3.1`
1213
case `3.2-migration`, `3.2`
1314
case `3.3-migration`, `3.3`
1415
case `3.4-migration`, `3.4`
@@ -40,10 +41,18 @@ enum SourceVersion:
4041
def enablesBetterFors(using Context) = isAtLeast(`3.7`) && isPreviewEnabled
4142

4243
object SourceVersion extends Property.Key[SourceVersion]:
43-
def defaultSourceVersion = `3.7`
44+
45+
/* The default source version used by the built compiler */
46+
val defaultSourceVersion = `3.7`
47+
48+
/* Illegal source versions that may not appear in the settings `-source:<...>` */
49+
val illegalInSettings = List(`3.1-migration`, `never`)
50+
51+
/* Illegal source versions that may not appear as an import `import scala.language.<...>` */
52+
val illegalInImports = List(`3.1-migration`, `never`)
4453

4554
/** language versions that may appear in a language import, are deprecated, but not removed from the standard library. */
46-
val illegalSourceVersionNames = List("3.1-migration", "never").map(_.toTermName)
55+
val illegalSourceVersionNames = illegalInImports.map(_.toString.toTermName)
4756

4857
/** language versions that the compiler recognises. */
4958
val validSourceVersionNames = values.toList.map(_.toString.toTermName)

compiler/test/dotty/tools/dotc/config/ScalaSettingsTests.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,11 @@ class ScalaSettingsTests:
299299
)
300300
assertEquals(result, Right(reporting.Action.Error))
301301

302+
@Test def `illegal source versions are not accepted when parsing the settings`: Unit =
303+
for source <- SourceVersion.illegalInSettings do
304+
val settings = ScalaSettings
305+
val result = settings.processArguments(List("-source", source.toString()), true)
306+
assertEquals(0, result.warnings.length)
307+
assertEquals(1, result.errors.length)
308+
302309
end ScalaSettingsTests

0 commit comments

Comments
 (0)