Skip to content

Commit ed9be98

Browse files
committed
chore: use language import instead of -Ycompile-scala2-library
1 parent fb6cc9b commit ed9be98

File tree

13 files changed

+32
-21
lines changed

13 files changed

+32
-21
lines changed

compiler/src/dotty/tools/dotc/CompilationUnit.scala

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class CompilationUnit protected (val source: SourceFile, val info: CompilationUn
3030
/** Is this the compilation unit of a Java file */
3131
def isJava: Boolean = source.file.ext.isJava
3232

33+
def isScala2 = sourceVersion.map(_.isScala2).getOrElse(false)
34+
3335
/** Is this the compilation unit of a Java file, or TASTy derived from a Java file */
3436
def typedAsJava =
3537
val ext = source.file.ext

compiler/src/dotty/tools/dotc/ast/Desugar.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ object desugar {
703703
def isNonEnumCase = !isEnumCase && (isCaseClass || isCaseObject)
704704
val isValueClass = parents.nonEmpty && isAnyVal(parents.head)
705705
// This is not watertight, but `extends AnyVal` will be replaced by `inline` later.
706-
val caseClassInScala2Library = isCaseClass && ctx.settings.YcompileScala2Library.value
706+
val caseClassInScala2Library = isCaseClass && Feature.sourceVersion.isScala2
707707

708708
val originalTparams = constr1.leadingTypeParams
709709
val originalVparamss = asTermOnly(constr1.trailingParamss)
@@ -922,7 +922,7 @@ object desugar {
922922
val copyRestParamss = derivedVparamss.tail.nestedMap(vparam =>
923923
cpy.ValDef(vparam)(rhs = EmptyTree))
924924
var flags = Synthetic | constr1.mods.flags & copiedAccessFlags
925-
if ctx.settings.YcompileScala2Library.value then flags &~= Private
925+
if Feature.sourceVersion.isScala2 then flags &~= Private
926926
DefDef(
927927
nme.copy,
928928
joinParams(derivedTparams, copyFirstParams :: copyRestParamss),
@@ -983,7 +983,7 @@ object desugar {
983983
else {
984984
val appMods =
985985
var flags = Synthetic | constr1.mods.flags & copiedAccessFlags
986-
if ctx.settings.YcompileScala2Library.value then flags &~= Private
986+
if Feature.sourceVersion.isScala2 then flags &~= Private
987987
Modifiers(flags).withPrivateWithin(constr1.mods.privateWithin)
988988
val appParamss =
989989
derivedVparamss.nestedZipWithConserve(constrVparamss)((ap, cp) =>
@@ -1066,7 +1066,7 @@ object desugar {
10661066
paramss // drop leading () that got inserted by class
10671067
// TODO: drop this once we do not silently insert empty class parameters anymore
10681068
case paramss => paramss
1069-
val finalFlag = if ctx.settings.YcompileScala2Library.value then EmptyFlags else Final
1069+
val finalFlag = if Feature.sourceVersion.isScala2 then EmptyFlags else Final
10701070
// implicit wrapper is typechecked in same scope as constructor, so
10711071
// we can reuse the constructor parameters; no derived params are needed.
10721072
DefDef(

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Constants.Constant
1515
import scala.collection.mutable
1616

1717
import scala.annotation.tailrec
18+
import dotty.tools.dotc.config.Feature
1819

1920
trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] =>
2021

@@ -466,7 +467,7 @@ trait UntypedTreeInfo extends TreeInfo[Untyped] { self: Trees.Instance[Untyped]
466467
*/
467468
private def defKind(tree: Tree)(using Context): FlagSet = unsplice(tree) match {
468469
case EmptyTree | _: Import => NoInitsInterface
469-
case tree: TypeDef if ctx.settings.YcompileScala2Library.value =>
470+
case tree: TypeDef if Feature.sourceVersion.isScala2 =>
470471
if (tree.isClassDef) EmptyFlags else NoInitsInterface
471472
case tree: TypeDef => if (tree.isClassDef) NoInits else NoInitsInterface
472473
case tree: DefDef =>
@@ -479,7 +480,7 @@ trait UntypedTreeInfo extends TreeInfo[Untyped] { self: Trees.Instance[Untyped]
479480
NoInitsInterface
480481
else if tree.mods.is(Given) && tree.paramss.isEmpty then
481482
EmptyFlags // might become a lazy val: TODO: check whether we need to suppress NoInits once we have new lazy val impl
482-
else if ctx.settings.YcompileScala2Library.value then
483+
else if Feature.sourceVersion.isScala2 then
483484
EmptyFlags
484485
else
485486
NoInits

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

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Feature.isPreviewEnabled
88
import util.Property
99

1010
enum SourceVersion:
11+
case `2`
1112
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.
1213
case `3.2-migration`, `3.2`
1314
case `3.3-migration`, `3.3`
@@ -39,6 +40,8 @@ enum SourceVersion:
3940
def enablesNamedTuples = isAtLeast(`3.7`)
4041
def enablesBetterFors(using Context) = isAtLeast(`3.7`) && isPreviewEnabled
4142

43+
def isScala2 = this == `2`
44+
4245
object SourceVersion extends Property.Key[SourceVersion]:
4346
def defaultSourceVersion = `3.7`
4447

compiler/src/dotty/tools/dotc/core/Definitions.scala

+4-6
Original file line numberDiff line numberDiff line change
@@ -1537,9 +1537,7 @@ class Definitions {
15371537
denot.sourceModule.info = denot.typeRef // we run into a cyclic reference when patching if this line is omitted
15381538
patch2(denot, patchCls)
15391539

1540-
if ctx.settings.YcompileScala2Library.value then
1541-
()
1542-
else if denot.name == tpnme.Predef.moduleClassName && denot.symbol == ScalaPredefModuleClass then
1540+
if denot.name == tpnme.Predef.moduleClassName && denot.symbol == ScalaPredefModuleClass then
15431541
patchWith(ScalaPredefModuleClassPatch)
15441542
else if denot.name == tpnme.language.moduleClassName && denot.symbol == LanguageModuleClass then
15451543
patchWith(LanguageModuleClassPatch)
@@ -1880,7 +1878,7 @@ class Definitions {
18801878
|| tp.derivesFrom(defn.PolyFunctionClass) // TODO check for refinement?
18811879

18821880
private def withSpecMethods(cls: ClassSymbol, bases: List[Name], paramTypes: Set[TypeRef]) =
1883-
if !ctx.settings.YcompileScala2Library.value then
1881+
if !Feature.sourceVersion.isScala2 then
18841882
for base <- bases; tp <- paramTypes do
18851883
cls.enter(newSymbol(cls, base.specializedName(List(tp)), Method, ExprType(tp)))
18861884
cls
@@ -1923,7 +1921,7 @@ class Definitions {
19231921
case List(x, y) => Tuple2SpecializedParamClasses().contains(x.classSymbol) && Tuple2SpecializedParamClasses().contains(y.classSymbol)
19241922
case _ => false
19251923
&& base.owner.denot.info.member(base.name.specializedName(args)).exists // when dotc compiles the stdlib there are no specialised classes
1926-
&& !ctx.settings.YcompileScala2Library.value // We do not add the specilized TupleN methods/classes when compiling the stdlib
1924+
&& !Feature.sourceVersion.isScala2 // We do not add the specilized TupleN methods/classes when compiling the stdlib
19271925

19281926
def isSpecializableFunction(cls: ClassSymbol, paramTypes: List[Type], retType: Type)(using Context): Boolean =
19291927
paramTypes.length <= 2
@@ -1945,7 +1943,7 @@ class Definitions {
19451943
case _ =>
19461944
false
19471945
})
1948-
&& !ctx.settings.YcompileScala2Library.value // We do not add the specilized FunctionN methods/classes when compiling the stdlib
1946+
&& !Feature.sourceVersion.isScala2 // We do not add the specilized FunctionN methods/classes when compiling the stdlib
19491947

19501948
@tu lazy val Function0SpecializedApplyNames: List[TermName] =
19511949
for r <- Function0SpecializedReturnTypes

compiler/src/dotty/tools/dotc/transform/Pickler.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,10 @@ class Pickler extends Phase {
289289
// assert that Java sources didn't reach Pickler without `-Xjava-tasty`.
290290
assert(ctx.settings.XjavaTasty.value, "unexpected Java source file without -Xjava-tasty")
291291
val isOutline = isJavaAttr // TODO: later we may want outline for Scala sources too
292+
292293
val attributes = Attributes(
293294
sourceFile = sourceRelativePath,
294-
scala2StandardLibrary = ctx.settings.YcompileScala2Library.value,
295+
scala2StandardLibrary = Feature.sourceVersion.isScala2,
295296
explicitNulls = ctx.settings.YexplicitNulls.value,
296297
captureChecked = Feature.ccEnabled,
297298
withPureFuns = Feature.pureFunsEnabled,

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
101101
private var compilingScala2StdLib = false
102102
override def initContext(ctx: FreshContext): Unit =
103103
initContextCalled = true
104-
compilingScala2StdLib = ctx.settings.YcompileScala2Library.value(using ctx)
104+
compilingScala2StdLib = Feature.sourceVersion(using ctx).isScala2
105105

106106
val superAcc: SuperAccessors = new SuperAccessors(thisPhase)
107107
val synthMbr: SyntheticMembers = new SyntheticMembers(thisPhase)

compiler/src/dotty/tools/dotc/transform/SpecializeApplyMethods.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import SymDenotations.*, Scopes.*, StdNames.*, NameOps.*, Names.*
77
import MegaPhase.MiniPhase
88

99
import scala.collection.mutable
10+
import dotty.tools.dotc.config.Feature
1011

1112

1213
/** This phase synthesizes specialized methods for FunctionN, this is done
@@ -25,7 +26,7 @@ class SpecializeApplyMethods extends MiniPhase with InfoTransformer {
2526
override def description: String = SpecializeApplyMethods.description
2627

2728
override def isEnabled(using Context): Boolean =
28-
!ctx.settings.scalajs.value && !ctx.settings.YcompileScala2Library.value
29+
!ctx.settings.scalajs.value && !Feature.sourceVersion.isScala2
2930

3031
private def specApplySymbol(sym: Symbol, args: List[Type], ret: Type)(using Context): Symbol = {
3132
val name = nme.apply.specializedFunction(ret, args)

compiler/src/dotty/tools/dotc/transform/SyntheticMembers.scala

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import util.Spans.Span
1616
import config.Printers.derive
1717
import NullOpsDecorator.*
1818
import scala.runtime.Statics
19+
import dotty.tools.dotc.config.Feature
1920

2021
object SyntheticMembers {
2122

@@ -79,7 +80,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
7980

8081
private def existingDef(sym: Symbol, clazz: ClassSymbol)(using Context): Symbol =
8182
val existing = sym.matchingMember(clazz.thisType)
82-
if ctx.settings.YcompileScala2Library.value && clazz.isValueClass && (sym == defn.Any_equals || sym == defn.Any_hashCode) then
83+
if Feature.sourceVersion.isScala2 && clazz.isValueClass && (sym == defn.Any_equals || sym == defn.Any_hashCode) then
8384
NoSymbol
8485
else if existing != sym && !existing.is(Deferred) then
8586
existing
@@ -168,7 +169,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
168169
case nme.productPrefix if isEnumValue => nameRef
169170
case nme.productPrefix => ownNameLit
170171
case nme.productElement =>
171-
if ctx.settings.YcompileScala2Library.value then productElementBodyForScala2Compat(accessors.length, vrefss.head.head)
172+
if Feature.sourceVersion.isScala2 then productElementBodyForScala2Compat(accessors.length, vrefss.head.head)
172173
else productElementBody(accessors.length, vrefss.head.head)
173174
case nme.productElementName => productElementNameBody(accessors.length, vrefss.head.head)
174175
}
@@ -720,7 +721,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
720721
val syntheticMembers = serializableObjectMethod(clazz) ::: serializableEnumValueMethod(clazz) ::: caseAndValueMethods(clazz)
721722
checkInlining(syntheticMembers)
722723
val impl1 = cpy.Template(impl)(body = syntheticMembers ::: impl.body)
723-
if ctx.settings.YcompileScala2Library.value then impl1
724+
if Feature.sourceVersion.isScala2 then impl1
724725
else addMirrorSupport(impl1)
725726
}
726727

compiler/src/dotty/tools/dotc/typer/Namer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class Namer { typer: Typer =>
247247
tree match {
248248
case tree: TypeDef if tree.isClassDef =>
249249
var flags = checkFlags(tree.mods.flags)
250-
if ctx.settings.YcompileScala2Library.value then
250+
if Feature.sourceVersion.isScala2 then
251251
flags |= Scala2x
252252
val name = checkNoConflict(tree.name, tree.span).asTypeName
253253
val cls =

compiler/src/dotty/tools/dotc/typer/Typer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -3206,7 +3206,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
32063206

32073207
checkEnumParent(cls, firstParent)
32083208

3209-
if defn.ScalaValueClasses()(cls) && ctx.settings.YcompileScala2Library.value then
3209+
if defn.ScalaValueClasses()(cls) && Feature.sourceVersion.isScala2 then
32103210
constr1.symbol.resetFlag(Private)
32113211

32123212
val self1 = typed(self)(using ctx.outer).asInstanceOf[ValDef] // outer context where class members are not visible

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

+4
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ object language:
222222
@compileTimeOnly("`future-migration` can only be used at compile time in import statements")
223223
object `future-migration`
224224

225+
/** Set source version to 2 */
226+
@compileTimeOnly("`2` can only be used at compile time in import statements")
227+
private[scala] object `2`
228+
225229
/** Set source version to 3.0-migration.
226230
*
227231
* @see [[https://docs.scala-lang.org/scala3/guides/migration/compatibility-intro.html]]

project/Build.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ object Build {
12481248
Seq("-sourcepath", ((Compile/sourceManaged).value / "scala-library-src").toString)
12491249
},
12501250
Compile / doc / scalacOptions += "-Ydocument-synthetic-types",
1251-
scalacOptions += "-Ycompile-scala2-library",
1251+
scalacOptions ++= Seq("-source", "2"),
12521252
scalacOptions += "-Yscala2-unpickler:never",
12531253
scalacOptions += "-Werror:false",
12541254
Compile / compile / logLevel.withRank(KeyRanks.Invisible) := Level.Error,

0 commit comments

Comments
 (0)