Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e0bbca6

Browse files
committedOct 31, 2017
Fix unused on poly methods with term no args
1 parent b7d9b81 commit e0bbca6

File tree

4 files changed

+71
-35
lines changed

4 files changed

+71
-35
lines changed
 

‎compiler/src/dotty/tools/dotc/transform/UnusedRefs.scala

+10-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ class UnusedRefs extends MiniPhase {
2828

2929
/** Check what the phase achieves, to be called at any point after it is finished. */
3030
override def checkPostCondition(tree: Tree)(implicit ctx: Context): Unit = tree match {
31-
case _: Apply | _: RefTree => assert(!tree.symbol.is(Unused))
31+
case _: Apply | _: TypeApply | _: RefTree => assert(!tree.symbol.is(Unused), tree)
3232
case _ =>
3333
}
3434

3535
/* Tree transform */
3636

37+
override def transformTypeApply(tree: TypeApply)(implicit ctx: Context): Tree = transformUnused(tree)
3738
override def transformApply(tree: Apply)(implicit ctx: Context): Tree = transformUnused(tree)
3839
override def transformIdent(tree: Ident)(implicit ctx: Context): Tree = transformUnused(tree)
3940
override def transformSelect(tree: Select)(implicit ctx: Context): Tree = transformUnused(tree)
@@ -42,11 +43,16 @@ class UnusedRefs extends MiniPhase {
4243
if (!tree.symbol.is(Unused)) tree
4344
else {
4445
tree.tpe.widen match {
45-
case _: MethodType => tree // Do the transformation higher in the tree if needed
46+
case _: MethodOrPoly => tree // Do the transformation higher in the tree if needed
4647
case _ =>
4748
tree match {
48-
case _: RefTree => defaultValue(tree.tpe)
49-
case Apply(_ , args) => seq(args, defaultValue(tree.tpe))
49+
case _: RefTree | _: TypeApply => defaultValue(tree.tpe)
50+
case Apply(_ , args) =>
51+
def allArgs(t: Tree, acc: List[Tree]): List[Tree] = t match {
52+
case Apply(fun, args) => allArgs(fun, args ::: acc)
53+
case _ => acc
54+
}
55+
seq(allArgs(tree, Nil), defaultValue(tree.tpe))
5056
}
5157
}
5258
}

‎tests/run/unused-frameless.scala

+43-31
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,6 @@ final case class R[K <: String, V](v: V)
1717

1818
trait Selector[L <: HList, K, V]
1919

20-
object Selector {
21-
implicit def caseFound[T <: HList, K <: String, V]
22-
: Selector[R[K, V] :: T, K, V] = {
23-
println("Selector.caseFound")
24-
null
25-
}
26-
27-
implicit def caseRecur[H, T <: HList, K <: String, V]
28-
(implicit i: Selector[T, K, V])
29-
: Selector[H :: T, K, V] = {
30-
println("Selector.caseRecur")
31-
null
32-
}
33-
}
34-
3520
// Subset of Frameless
3621
// ----------------------------------------------------------------------------
3722

@@ -83,34 +68,46 @@ case class Column[T, A](label: String)
8368
@implicitNotFound(msg = "No column ${K} in type ${T}")
8469
trait Exists[T, K, V]
8570

86-
object Exists {
87-
implicit def derive[T, H <: HList, K, V]
88-
(implicit
89-
g: LabelledGeneric[T] { type Repr = H },
90-
s: Selector[H, K, V]
91-
): Exists[T, K, V] = {
71+
object UsedExists {
72+
implicit def derive[T, H <: HList, K, V](implicit g: LabelledGeneric[T] { type Repr = H }, s: Selector[H, K, V]): Exists[T, K, V] = {
9273
println("Exists.derive")
9374
null
9475
}
76+
77+
implicit def caseFound[T <: HList, K <: String, V]: Selector[R[K, V] :: T, K, V] = {
78+
println("Selector.caseFound")
79+
null
80+
}
81+
82+
implicit def caseRecur[H, T <: HList, K <: String, V](implicit i: Selector[T, K, V]): Selector[H :: T, K, V] = {
83+
println("Selector.caseRecur")
84+
null
85+
}
9586
}
9687

9788
object UnusedExists {
98-
implicit unused def derive[T, H <: HList, K, V]
99-
(implicit
100-
g: LabelledGeneric[T] { type Repr = H },
101-
s: Selector[H, K, V]
102-
): Exists[T, K, V] = {
89+
implicit unused def derive[T, H <: HList, K, V](implicit unused g: LabelledGeneric[T] { type Repr = H }, s: Selector[H, K, V] ): Exists[T, K, V] = {
10390
println("UnusedExists.derive")
10491
null
10592
}
93+
94+
unused implicit def caseFound[T <: HList, K <: String, V]: Selector[R[K, V] :: T, K, V] = {
95+
println("Selector.caseFound")
96+
null
97+
}
98+
99+
unused implicit def caseRecur[H, T <: HList, K <: String, V](implicit i: Selector[T, K, V]): Selector[H :: T, K, V] = {
100+
println("Selector.caseRecur")
101+
null
102+
}
106103
}
107104

108105
// X4 Example
109106
// ----------------------------------------------------------------------------
110107

111108
case class X4[A, B, C, D](a: A, b: B, c: C, d: D)
112109

113-
object X4 {
110+
object UsedX4 {
114111
// Macro generated
115112
implicit def x4Repr[A, B, C, D]: LabelledGeneric[X4[A, B, C, D]] {
116113
type Repr = R["a", A] :: R["b", B] :: R["c", C] :: R["d", D] :: HNil
@@ -120,6 +117,16 @@ object X4 {
120117
}
121118
}
122119

120+
object UnusedX4 {
121+
// Macro generated
122+
implicit unused def x4Repr[A, B, C, D]: LabelledGeneric[X4[A, B, C, D]] {
123+
type Repr = R["a", A] :: R["b", B] :: R["c", C] :: R["d", D] :: HNil
124+
} = {
125+
println("X4.x4Repr")
126+
null
127+
}
128+
}
129+
123130
object Test {
124131
def main(args: Array[String]): Unit = {
125132
val source: Vector[X4[Int, String, Double, Boolean]] =
@@ -131,16 +138,21 @@ object Test {
131138

132139
{
133140
import UnusedExists._
141+
import UnusedX4._
134142
println("unused")
135143
val unusedD = ds.col("d")
136144
val outSpark1: Vector[Boolean] = ds.select(unusedD).collect()
137145
assert(outSpark1 == outColl)
138146
}
139147

140-
println("used")
141-
val usedD = ds.col("d")
142-
val outSpark2: Vector[Boolean] = ds.select(usedD).collect()
143-
assert(outSpark2 == outColl)
148+
{
149+
import UsedExists._
150+
import UsedX4._
151+
println("used")
152+
val usedD = ds.col("d")
153+
val outSpark2: Vector[Boolean] = ds.select(usedD).collect()
154+
assert(outSpark2 == outColl)
155+
}
144156

145157
println("end")
146158
}

‎tests/run/unused-poly-ref.check

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
5
2+
6
3+
fun

‎tests/run/unused-poly-ref.scala

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
object Test {
2+
3+
def main(args: Array[String]): Unit = {
4+
fun(foo(bar(5))(bar(6)))
5+
}
6+
7+
def fun(unused a: Int): Unit = println("fun")
8+
9+
unused def foo[P](unused x: Int)(unused y: Int): Int = x
10+
11+
def bar(x: Int) = {
12+
println(x)
13+
x
14+
}
15+
}

0 commit comments

Comments
 (0)
Please sign in to comment.