Skip to content

Commit 6f6ba8c

Browse files
committed
Resolve SeqType based on a compiler flag
Relying on the classpath didn't work as it was sometime preferred over the source path. We need to knows which standard library we are compiling against even before symbols are entered (e.g. during desugaring). This flag will go away once we drop support for the 2.12 standard library. But for now, we need it to compile the 2.13 standard library.
1 parent 04c7aa1 commit 6f6ba8c

File tree

7 files changed

+32
-56
lines changed

7 files changed

+32
-56
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ object desugar {
486486

487487
// TODO When the Scala library is updated to 2.13.x add the override keyword to this generated method.
488488
// (because Product.scala was updated)
489-
def productElemNameMethod = {
489+
def productElemNameMeth = {
490490
val methodParam = makeSyntheticParameter(tpt = scalaDot(tpnme.Int))
491491
val paramRef = Ident(methodParam.name)
492492

@@ -499,11 +499,11 @@ object desugar {
499499
} :+ defaultCase
500500
val body = Match(paramRef, patternMatchCases)
501501
DefDef(nme.productElementName, Nil, List(List(methodParam)), javaDotLangDot(tpnme.String), body)
502-
.withFlags(Synthetic)
502+
.withFlags(if (ctx.settings.YnewLibrary.value) Override | Synthetic else Synthetic)
503503
}
504504

505505
if (isCaseClass)
506-
productElemNameMethod :: copyMeths ::: enumTagMeths ::: productElemMeths
506+
productElemNameMeth :: copyMeths ::: enumTagMeths ::: productElemMeths
507507
else Nil
508508
}
509509

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ class ScalaSettings extends Settings.SettingGroup {
9191
val Ylog: Setting[List[String]] = PhasesSetting("-Ylog", "Log operations during")
9292
val YemitTastyInClass: Setting[Boolean] = BooleanSetting("-Yemit-tasty-in-class", "Generate tasty in the .class file and add an empty *.hasTasty file.")
9393
val YlogClasspath: Setting[Boolean] = BooleanSetting("-Ylog-classpath", "Output information about what classpath is being applied.")
94-
val YdisableFlatCpCaching: Setting[Boolean] = BooleanSetting("-YdisableFlatCpCaching", "Do not cache flat classpath representation of classpath elements from jars across compiler instances.")
94+
val YdisableFlatCpCaching: Setting[Boolean] = BooleanSetting("-YdisableFlatCpCaching", "Do not cache flat classpath representation of classpath elements from jars across compiler instances.")
9595

9696
val Yscala2Unpickler: Setting[String] = StringSetting("-Yscala2-unpickler", "", "Control where we may get Scala 2 symbols from. This is either \"always\", \"never\", or a classpath.", "always")
97+
// TODO: Remove once we drop support for 2.12 standard library
98+
val YnewLibrary: Setting[Boolean] = BooleanSetting("-Ynew-library", "Inform the compiler that we are using the 2.13 standard library.")
9799

98100
val YnoImports: Setting[Boolean] = BooleanSetting("-Yno-imports", "Compile without importing scala.*, java.lang.*, or Predef.")
99101
val YnoInline: Setting[Boolean] = BooleanSetting("-Yno-inline", "Suppress inlining.")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ class Definitions {
396396
def newArrayMethod(implicit ctx: Context): TermSymbol = DottyArraysModule.requiredMethod("newArray")
397397

398398
// TODO: Remove once we drop support for 2.12 standard library
399-
private[this] lazy val isNewCollections = ctx.base.staticRef("scala.collection.IterableOnce".toTypeName).exists
399+
private def isNewCollections = ctx.settings.YnewLibrary.value
400400

401401
def getWrapVarargsArrayModule: Symbol = if (isNewCollections) ScalaRuntimeModule else ScalaPredefModule
402402

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class CompilationTests extends ParallelTesting {
4848
compileFile("tests/pos-special/completeFromSource/Test2.scala", defaultOptions.and("-sourcepath", "tests/pos-special")) +
4949
compileFile("tests/pos-special/completeFromSource/Test3.scala", defaultOptions.and("-sourcepath", "tests/pos-special", "-scansource")) +
5050
compileFile("tests/pos-special/completeFromSource/nested/Test4.scala", defaultOptions.and("-sourcepath", "tests/pos-special", "-scansource")) +
51+
compileFile("tests/pos-special/repeatedArgs213.scala", defaultOptions.and("-Ynew-library")) +
5152
compileFilesInDir("tests/pos-special/fatal-warnings", defaultOptions.and("-Xfatal-warnings", "-feature")) +
5253
compileList(
5354
"compileMixed",
@@ -162,7 +163,8 @@ class CompilationTests extends ParallelTesting {
162163
compileFilesInDir("tests/neg-custom-args/isInstanceOf", allowDeepSubtypes and "-Xfatal-warnings") +
163164
compileFile("tests/neg-custom-args/i3627.scala", allowDeepSubtypes) +
164165
compileFile("tests/neg-custom-args/matchtype-loop.scala", allowDeepSubtypes) +
165-
compileFile("tests/neg-custom-args/completeFromSource/nested/Test1.scala", defaultOptions.and("-sourcepath", "tests/neg-custom-args", "-scansource"))
166+
compileFile("tests/neg-custom-args/completeFromSource/nested/Test1.scala", defaultOptions.and("-sourcepath", "tests/neg-custom-args", "-scansource")) +
167+
compileFile("tests/neg-custom-args/repeatedArgs213.scala", defaultOptions.and("-Ynew-library"))
166168
}.checkExpectedErrors()
167169

168170
// Run tests -----------------------------------------------------------------
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import scala.collection.{immutable, mutable}
2+
import java.nio.file.Paths
3+
4+
// Start of Test
5+
class repeatedArgs {
6+
def bar(xs: String*): Int = xs.length
7+
8+
def test(xs: immutable.Seq[String], ys: collection.Seq[String], zs: Array[String]): Unit = {
9+
bar("a", "b", "c")
10+
bar(xs: _*)
11+
bar(ys: _*) // error: immutable.Seq expected, found Seq
12+
bar(zs: _*) // old-error: Remove (compiler generated) Array to Seq convertion in 2.13?
13+
14+
Paths.get("Hello", "World")
15+
Paths.get("Hello", xs: _*)
16+
Paths.get("Hello", ys: _*) // error: immutable.Seq expected, found Seq
17+
Paths.get("Hello", zs: _*)
18+
}
19+
}

tests/neg/repeatedArgs213.scala

Lines changed: 0 additions & 43 deletions
This file was deleted.

tests/pos/repeatedArgs213.scala renamed to tests/pos-special/repeatedArgs213.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import scala.collection.{immutable, mutable}
22
import java.nio.file.Paths
33

4-
// Code below is to trick the compiler into thinking that we are
5-
// compiling with the 2.13 standard library on the classpath.
6-
package scala.collection {
7-
class IterableOnce
8-
}
9-
4+
// Missing from 2.12 standard library
105
package scala.runtime {
116
object ScalaRunTime {
127
abstract class ArraySeq[+A] extends immutable.Seq[A]
@@ -27,7 +22,8 @@ package scala.runtime {
2722

2823
// Start of Test
2924
class repeatedArgs {
30-
def bar(xs: String*): Int = xs.length
25+
def bar(xs: String*): Int = bat(xs)
26+
def bat(xs: immutable.Seq[String]) = xs.length
3127

3228
def test(xs: immutable.Seq[String]): Unit = {
3329
bar("a", "b", "c")

0 commit comments

Comments
 (0)