Skip to content

Commit 36abd04

Browse files
Merge pull request #2617 from biboudis/fix-2468
Fix #2468: Replace wildcards in a mini-phase
2 parents b583461 + f8f2f1d commit 36abd04

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ class Compiler {
9999
new ElimStaticThis, // Replace `this` references to static objects by global identifiers
100100
new Flatten, // Lift all inner classes to package scope
101101
new RestoreScopes), // Repair scopes rendered invalid by moving definitions in prior phases of the group
102-
List(new MoveStatics, // Move static methods to companion classes
102+
List(new TransformWildcards, // Replace wildcards with default values
103+
new MoveStatics, // Move static methods to companion classes
103104
new ExpandPrivate, // Widen private definitions accessed from nested classes
104105
new SelectStatic, // get rid of selects that would be compiled into GetStatic
105106
new CollectEntryPoints, // Find classes with main methods
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package dotty.tools.dotc
2+
package transform
3+
4+
import TreeTransforms._
5+
import core.DenotTransformers._
6+
import core.Contexts._
7+
import ast.tpd
8+
9+
/** This phase transforms wildcards in valdefs with their default value.
10+
* In particular for every valdef that is declared:
11+
* `val x : T = _` to `val x : T = <zero of T>`
12+
*
13+
*/
14+
class TransformWildcards extends MiniPhaseTransform with IdentityDenotTransformer { thisTransform =>
15+
import tpd._
16+
17+
override def phaseName = "transformWildcards"
18+
19+
override def checkPostCondition(tree: Tree)(implicit ctx: Context): Unit = {
20+
tree match {
21+
case vDef: ValDef => assert(!tpd.isWildcardArg(vDef.rhs))
22+
case _ =>
23+
}
24+
}
25+
26+
override def transformValDef(tree: ValDef)(implicit ctx: Context, info: TransformerInfo): Tree = {
27+
if (ctx.owner.isClass) tree
28+
else cpy.ValDef(tree)(rhs = tree.rhs.wildcardToDefault)
29+
}
30+
}

tests/pos/i2468.scala

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
object Test {
3+
4+
class A {
5+
private[this] var x: String = _
6+
}
7+
8+
class B {
9+
private[this] var x: String = _
10+
x = "foo"
11+
}
12+
13+
class C {
14+
private[this] var x1: Int = _
15+
private[this] var x2: Unit = _
16+
private[this] var x3: Char = _
17+
private[this] var x4: Boolean = _
18+
private[this] var x5: Float = _
19+
private[this] var x6: Double = _
20+
private[this] var x7: Char = _
21+
private[this] var x8: Byte = _
22+
private[this] var x9: AnyVal = _
23+
private[this] var x10: D = _
24+
}
25+
26+
class D(x: Int) extends AnyVal
27+
}

0 commit comments

Comments
 (0)