Skip to content

Commit 5d99a49

Browse files
committed
Merge pull request #623 from dotty-staging/make-tests-pass
Make tests pass
2 parents ef27460 + 3d240ad commit 5d99a49

18 files changed

+116
-62
lines changed

scripts/common

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ update() {
99
git fetch --tags "https://github.com/$1/$2.git"
1010
(git fetch "https://github.com/$1/$2.git" $4 && git checkout -fq FETCH_HEAD) #|| git checkout -fq $4 # || fallback is for local testing on tag
1111
git reset --hard
12+
cd -
1213
}
1314

1415
export LC_ALL=en_US.UTF-8

src/dotty/tools/dotc/Compiler.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class Compiler {
6868
new FunctionalInterfaces),
6969
List(new LambdaLift, // in this mini-phase block scopes are incorrect. No phases that rely on scopes should be here
7070
new Flatten,
71+
new ElimStaticThis,
7172
new RestoreScopes),
7273
List(/*new PrivateToStatic,*/
7374
new ExpandPrivate,

src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
418418
Thicket(valdef, clsdef)
419419
}
420420

421-
def initValue(tpe: Types.Type)(implicit ctx: Context) = {
421+
def defaultValue(tpe: Types.Type)(implicit ctx: Context) = {
422422
val tpw = tpe.widen
423423

424424
if (tpw isRef defn.IntClass) Literal(Constant(0))
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package dotty.tools.dotc
2+
package transform
3+
4+
import core._
5+
import Contexts.Context
6+
import Flags._
7+
import dotty.tools.dotc.ast.tpd
8+
import dotty.tools.dotc.core.StdNames._
9+
import dotty.tools.dotc.core.SymDenotations.SymDenotation
10+
import TreeTransforms.{MiniPhaseTransform, TransformerInfo}
11+
import dotty.tools.dotc.core.Types.{ThisType, TermRef}
12+
import Phases.Phase
13+
14+
/** Replace This references to module classes in static methods by global identifiers to the
15+
* corresponding modules.
16+
*/
17+
class ElimStaticThis extends MiniPhaseTransform {
18+
import ast.tpd._
19+
def phaseName: String = "elimStaticThis"
20+
21+
override def runsAfter: Set[Class[_ <: Phase]] = Set(classOf[Flatten])
22+
23+
override def transformThis(tree: This)(implicit ctx: Context, info: TransformerInfo): Tree =
24+
if (!tree.symbol.is(Package) && ctx.owner.enclosingMethod.is(JavaStatic)) {
25+
assert(tree.symbol.is(ModuleClass))
26+
ref(tree.symbol.sourceModule)
27+
}
28+
else tree
29+
30+
override def transformIdent(tree: tpd.Ident)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
31+
val meth = ctx.owner.enclosingMethod
32+
// We cannot use meth.enclosingClass because it skips other static classes,
33+
// so instead we require this phase to run after Flatten and use meth.owner
34+
if (meth.is(JavaStatic) && meth.owner.is(ModuleClass)) {
35+
tree.tpe match {
36+
case TermRef(thiz: ThisType, _) if (thiz.underlying.typeSymbol == meth.owner) =>
37+
ref(thiz.underlying.typeSymbol.sourceModule).select(tree.symbol)
38+
case _ => tree
39+
}
40+
}
41+
else tree
42+
}
43+
}
Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
1-
package dotty.tools.dotc.transform
1+
package dotty.tools.dotc
2+
package transform
23

3-
import dotty.tools.dotc.transform.TreeTransforms.{TransformerInfo, TreeTransform, TreeTransformer, MiniPhaseTransform}
4-
import dotty.tools.dotc.ast.{untpd, tpd}
5-
import dotty.tools.dotc.core.Contexts.Context
6-
import scala.collection.mutable.ListBuffer
7-
import dotty.tools.dotc.core.{Scopes, Flags}
8-
import dotty.tools.dotc.core.Symbols.NoSymbol
9-
import scala.annotation.tailrec
10-
import dotty.tools.dotc.core._
4+
import TreeTransforms.{MiniPhaseTransform, TransformerInfo}
5+
import ast.tpd
6+
import ast.Trees._
7+
import core._
8+
import Contexts.Context
119
import Symbols._
12-
import scala.Some
13-
import dotty.tools.dotc.transform.TreeTransforms.{NXTransformations, TransformerInfo, TreeTransform, TreeTransformer}
14-
import dotty.tools.dotc.core.Contexts.Context
15-
import scala.collection.mutable
16-
import dotty.tools.dotc.core.Names.Name
17-
import NameOps._
1810
import Types._
19-
import scala.collection.SortedSet
20-
import Decorators._
2111
import StdNames._
22-
import dotty.tools.dotc.util.Positions.Position
23-
import dotty.tools.dotc.config.JavaPlatform
2412

2513
/**
2614
* Replace Ident("_") in tree with default values of corresponding type:
@@ -29,21 +17,21 @@ import dotty.tools.dotc.config.JavaPlatform
2917
* classes: `null`
3018
*/
3119
class ElimWildcardIdents extends MiniPhaseTransform {
32-
import tpd._
20+
import ast.tpd._
3321
def phaseName: String = "elimWildcardIdents"
3422

35-
36-
override def transformDefDef(tree: tpd.DefDef)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
37-
def stripBlocks(arg: Tree): Tree = arg match {
38-
case b: Block if b.stats.isEmpty => stripBlocks(b.expr)
39-
case _ => arg
40-
}
41-
val b = stripBlocks(tree.rhs)
42-
b match {
43-
case x: Ident if (x.name == nme.WILDCARD && x.symbol.isClass) =>
44-
tpd.DefDef(tree.symbol.asTerm, tpd.initValue(x.tpe))
23+
def wildcardToDefaultValue(tree: Tree)(implicit ctx: Context) = {
24+
def recur(x: Tree): Tree = x match {
25+
case x: Ident if x.name == nme.WILDCARD && x.symbol.isClass => defaultValue(tree.tpe)
26+
case Block(Nil, y) => recur(y)
4527
case _ => tree
4628
}
29+
recur(tree)
4730
}
4831

32+
override def transformValDef(tree: tpd.ValDef)(implicit ctx: Context, info: TransformerInfo): tpd.Tree =
33+
cpy.ValDef(tree)(rhs = wildcardToDefaultValue(tree.rhs))
34+
35+
override def transformDefDef(tree: tpd.DefDef)(implicit ctx: Context, info: TransformerInfo): tpd.Tree =
36+
cpy.DefDef(tree)(rhs = wildcardToDefaultValue(tree.rhs))
4937
}

src/dotty/tools/dotc/transform/LambdaLift.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisTransform
292292
val encClass = local.enclosingClass
293293
val topClass = local.topLevelClass
294294
// member of a static object
295-
if (encClass.isStatic && encClass.isContainedIn(topClass)) {
295+
if (encClass.isStatic && encClass.isProperlyContainedIn(topClass)) {
296296
// though the second condition seems weird, it's not true for symbols which are defined in some
297297
// weird combinations of super calls.
298298
(encClass, EmptyFlags)

src/dotty/tools/dotc/transform/LazyVals.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class LazyVals extends MiniPhaseTransform with IdentityDenotTransformer {
177177
tpe, coord = x.symbol.coord
178178
).entered
179179

180-
val containerTree = ValDef(containerSymbol, initValue(tpe))
180+
val containerTree = ValDef(containerSymbol, defaultValue(tpe))
181181
if (x.tpe.isNotNull && tpe <:< defn.ObjectType) { // can use 'null' value instead of flag
182182
val slowPath = DefDef(x.symbol.asTerm, mkDefNonThreadSafeNonNullable(containerSymbol, x.rhs))
183183
Thicket(List(containerTree, slowPath))
@@ -234,7 +234,7 @@ class LazyVals extends MiniPhaseTransform with IdentityDenotTransformer {
234234
val thiz = This(claz)(ctx.fresh.setOwner(claz))
235235

236236
val resultSymbol = ctx.newSymbol(methodSymbol, lazyNme.result, containerFlags, tp)
237-
val resultDef = ValDef(resultSymbol, initValue(tp))
237+
val resultDef = ValDef(resultSymbol, defaultValue(tp))
238238

239239
val retrySymbol = ctx.newSymbol(methodSymbol, lazyNme.retry, containerFlags, defn.BooleanType)
240240
val retryDef = ValDef(retrySymbol, Literal(Constants.Constant(true)))
@@ -332,7 +332,7 @@ class LazyVals extends MiniPhaseTransform with IdentityDenotTransformer {
332332

333333
val containerName = ctx.freshName(x.name ++ StdNames.nme.LAZY_LOCAL).toTermName
334334
val containerSymbol = ctx.newSymbol(claz, containerName, (x.mods &~ containerFlagsMask | containerFlags).flags, tpe, coord = x.symbol.coord).entered
335-
val containerTree = ValDef(containerSymbol, initValue(tpe))
335+
val containerTree = ValDef(containerSymbol, defaultValue(tpe))
336336

337337
val offset = ref(companion).ensureApplied.select(offsetSymbol)
338338
val getFlag = Select(ref(helperModule), lazyNme.RLazyVals.get)

src/dotty/tools/dotc/transform/TraitConstructors.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class TraitConstructors extends MiniPhaseTransform with SymTransformer {
1818
import dotty.tools.dotc.ast.tpd._
1919
def phaseName: String = "traitConstructors"
2020

21-
2221
override def treeTransformPhase: Phase = this.phase
2322

2423
def transformSym(sym: SymDenotation)(implicit ctx: Context): SymDenotation = {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1337,10 +1337,22 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
13371337
}
13381338
}
13391339

1340+
/** Adapt an expression of constant type to a different constant type `tpe`. */
1341+
def adaptConstant(tree: Tree, tpe: ConstantType): Tree = {
1342+
def lit = Literal(tpe.value).withPos(tree.pos)
1343+
tree match {
1344+
case Literal(c) => lit
1345+
case tree @ Block(stats, expr) => tpd.cpy.Block(tree)(stats, adaptConstant(expr, tpe))
1346+
case tree =>
1347+
if (isIdempotentExpr(tree)) lit // See discussion in phase Literalize why we demand isIdempotentExpr
1348+
else Block(tree :: Nil, lit)
1349+
}
1350+
}
1351+
13401352
def adaptToSubType(wtp: Type): Tree = {
13411353
// try converting a constant to the target type
13421354
val folded = ConstFold(tree, pt)
1343-
if (folded ne tree) return folded
1355+
if (folded ne tree) return adaptConstant(folded, folded.tpe.asInstanceOf[ConstantType])
13441356
// drop type if prototype is Unit
13451357
if (pt isRef defn.UnitClass)
13461358
return tpd.Block(tree :: Nil, Literal(Constant(())))

test/dotc/tests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class tests extends CompilerTest {
137137
@Test def neg_escapingRefs = compileFile(negDir, "escapingRefs", xerrors = 2)
138138
@Test def neg_instantiateAbstract = compileFile(negDir, "instantiateAbstract", xerrors = 8)
139139
@Test def neg_selfInheritance = compileFile(negDir, "selfInheritance", xerrors = 5)
140-
140+
141141

142142
@Test def run_all = runFiles(runDir)
143143

File renamed without changes.
File renamed without changes.

tests/run/t5375.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
object Test extends dotty.runtime.LegacyApp {
1+
object Test {
22
val foos = (1 to 1000).toSeq
3-
try
4-
foos.par.map(i => if (i % 37 == 0) sys.error("i div 37") else i)
5-
catch {
6-
case ex: RuntimeException => println("Runtime exception")
3+
4+
def main(args: Array[String]): Unit = {
5+
try
6+
foos.par.map(i => if (i % 37 == 0) sys.error("i div 37") else i)
7+
catch {
8+
case ex: RuntimeException => println("Runtime exception")
9+
}
710
}
811
}

tests/run/t6052.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77

8-
object Test extends dotty.runtime.LegacyApp {
8+
object Test {
99
def seqarr(i: Int) = Array[Int]() ++ (0 until i)
1010
def pararr(i: Int) = seqarr(i).par
1111

@@ -15,7 +15,9 @@ object Test extends dotty.runtime.LegacyApp {
1515
assert(gseq == gpar, (gseq, gpar))
1616
}
1717

18-
for (i <- 0 until 20) check(i, _ > 0)
19-
for (i <- 0 until 20) check(i, _ % 2)
20-
for (i <- 0 until 20) check(i, _ % 4)
18+
def main(args: Array[String]): Unit = {
19+
for (i <- 0 until 20) check(i, _ > 0)
20+
for (i <- 0 until 20) check(i, _ % 2)
21+
for (i <- 0 until 20) check(i, _ % 4)
22+
}
2123
}

tests/run/t6260-delambdafy.check

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
f(C@2e)
22

33
apply
4-
get$Lambda

tests/run/t6410.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11

22

33

4-
object Test extends dotty.runtime.LegacyApp {
5-
val x = collection.parallel.mutable.ParArray.range(1,10) groupBy { _ % 2 } mapValues { _.size }
6-
println(x)
7-
val y = collection.parallel.immutable.ParVector.range(1,10) groupBy { _ % 2 } mapValues { _.size }
8-
println(y)
4+
object Test {
5+
def main(args: Array[String]): Unit = {
6+
val x = collection.parallel.mutable.ParArray.range(1,10) groupBy { _ % 2 } mapValues { _.size }
7+
println(x)
8+
val y = collection.parallel.immutable.ParVector.range(1,10) groupBy { _ % 2 } mapValues { _.size }
9+
println(y)
10+
}
911
}

tests/run/t6467.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ import collection._
66

77

88

9-
object Test extends dotty.runtime.LegacyApp {
9+
object Test {
1010

1111
def compare(s1: String, s2: String): Unit = {
1212
assert(s1 == s2, s1 + "\nvs.\n" + s2)
1313
}
1414

15-
compare(List(1, 2, 3, 4).aggregate(new java.lang.StringBuffer)(_ append _, _ append _).toString, "1234")
16-
compare(List(1, 2, 3, 4).par.aggregate(new java.lang.StringBuffer)(_ append _, _ append _).toString, "1234")
17-
compare(Seq(0 until 100: _*).aggregate(new java.lang.StringBuffer)(_ append _, _ append _).toString, (0 until 100).mkString)
18-
compare(Seq(0 until 100: _*).par.aggregate(new java.lang.StringBuffer)(_ append _, _ append _).toString, (0 until 100).mkString)
15+
def main(args: Array[String]): Unit = {
16+
compare(List(1, 2, 3, 4).aggregate(new java.lang.StringBuffer)(_ append _, _ append _).toString, "1234")
17+
compare(List(1, 2, 3, 4).par.aggregate(new java.lang.StringBuffer)(_ append _, _ append _).toString, "1234")
18+
compare(Seq(0 until 100: _*).aggregate(new java.lang.StringBuffer)(_ append _, _ append _).toString, (0 until 100).mkString)
19+
compare(Seq(0 until 100: _*).par.aggregate(new java.lang.StringBuffer)(_ append _, _ append _).toString, (0 until 100).mkString)
20+
}
1921

2022
}

tests/run/t7498.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55

66

77

8-
object Test extends dotty.runtime.LegacyApp {
8+
object Test {
99
import scala.collection.concurrent.TrieMap
1010

1111
class Collision(val idx: Int) {
1212
override def hashCode = idx % 10
1313
}
1414

15-
val tm = TrieMap[Collision, Unit]()
16-
for (i <- 0 until 1000) tm(new Collision(i)) = ()
15+
def main(args: Array[String]): Unit = {
16+
val tm = TrieMap[Collision, Unit]()
17+
for (i <- 0 until 1000) tm(new Collision(i)) = ()
1718

18-
tm.par.foreach(kv => ())
19+
tm.par.foreach(kv => ())
20+
}
1921
}
2022

0 commit comments

Comments
 (0)