Skip to content

Commit 494473f

Browse files
committed
Always avoid local symbols in tpd.seq
1 parent 8b941d6 commit 494473f

File tree

2 files changed

+60
-16
lines changed

2 files changed

+60
-16
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
7676
def seq(stats: List[Tree], expr: Tree)(implicit ctx: Context): Tree =
7777
if (stats.isEmpty) expr
7878
else expr match {
79-
case Block(estats, eexpr) => cpy.Block(expr)(stats ::: estats, eexpr)
79+
case Block(estats, eexpr) =>
80+
cpy.Block(expr)(stats ::: estats, eexpr).withType(ta.avoidingType(eexpr, stats))
8081
case _ => Block(stats, expr)
8182
}
8283

tests/run/Tuple.scala

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,81 @@
11
import annotation.showAsInfix
22

3+
object typelevel {
4+
erased def erasedValue[T]: T = ???
5+
class Typed[T](val value: T) { type Type = T }
6+
}
7+
38
sealed trait Tuple
9+
object Empty extends Tuple
410

5-
object Tuple {
6-
object Empty extends Tuple
11+
@showAsInfix
12+
final case class *: [H, T <: Tuple](hd: H, tl: T) extends Tuple
713

14+
object Tuple {
15+
import typelevel._
816
type Empty = Empty.type
917

10-
@showAsInfix
11-
final case class *: [H, T <: Tuple](hd: H, tl: T) extends Tuple
18+
class TupleOps(val xs: Tuple) extends AnyVal {
19+
transparent def *: [H] (x: H): Tuple = new *:(x, xs)
20+
transparent def size: Int = xs match {
21+
case Empty => 0
22+
case _ *: xs1 => xs1.size + 1
23+
}
24+
transparent def apply(n: Int): Any = xs match {
25+
case x *: _ if n == 0 => x
26+
case _ *: xs1 if n > 0 => xs1.apply(n - 1)
27+
}
28+
transparent def **: (ys: Tuple): Tuple = ys match {
29+
case Empty => xs
30+
case y *: ys1 => y *: (ys1 **: xs)
31+
}
32+
transparent def head = xs match {
33+
case x *: _ => x
34+
}
35+
transparent def tail = xs match {
36+
case _ *: xs => xs
37+
}
38+
}
1239

13-
class HListDeco(val xs: Tuple) extends AnyVal {
14-
transparent def *: [H] (x: H): Tuple = Tuple.*:.apply(x, xs)
40+
val emptyArray = Array[Object]()
1541

16-
transparent def size: Int = Tuple.size(xs)
17-
}
42+
transparent def toObj(t: Any) = t.asInstanceOf[Object]
1843

19-
transparent def size(xs: Tuple): Int = xs match {
20-
case Empty => 0
21-
case _ *: xs1 => size(xs1) + 1
44+
transparent def toArray(t: Tuple): Array[Object] = t.size match {
45+
case 0 => emptyArray
46+
case 1 => Array(toObj(t(0)))
47+
case 2 => Array(toObj(t(0)), toObj(t(1)))
48+
case 3 => Array(toObj(t(0)), toObj(t(1)), toObj(t(2)))
49+
case 4 => Array(toObj(t(0)), toObj(t(1)), toObj(t(2)), toObj(t(3)))
2250
}
2351

24-
transparent implicit def hlistDeco(xs: Tuple): HListDeco = new HListDeco(xs)
52+
transparent implicit def tupleDeco(xs: Tuple): TupleOps = new TupleOps(xs)
2553

2654
transparent def apply(): Tuple = Empty
2755
transparent def apply(x1: Any): Tuple = x1 *: Empty
2856
transparent def apply(x1: Any, x2: Any) = x1 *: x2 *: Empty
57+
transparent def apply(x1: Any, x2: Any, x3: Any) = x1 *: x2 *: x3 *: Empty
2958

3059
val xs0 = Tuple()
3160
val xs1 = Tuple(2)
3261
val xs2 = Tuple(2, "a")
33-
val s0 = xs0.size
34-
val s1 = xs1.size
35-
val s2 = xs2.size
62+
val xs3 = Tuple(true, 1, 2.0)
63+
transparent val s0 = xs0.size; val s0c: 0 = s0
64+
transparent val s1 = xs1.size; val s1c: 1 = s1
65+
transparent val s2 = xs2.size; val s2c: 2 = s2
66+
transparent val s3 = xs3.size; val s3c: 3 = s3
67+
val e0 = xs3(0); val e0c: Boolean = e0
68+
val e1 = xs3(1); val e1c: Int = e1
69+
val e2 = xs3(2); val e2c: Double = e2
70+
71+
val conc0 = xs0 **: xs3
72+
val conc1 = xs3 **: xs0
73+
val conc2 = xs2 **: xs3
74+
val e3c: Int = conc0(1)
75+
val e4c: Int = conc1(1)
76+
val e5c: Int = conc2(0)
77+
val e6c: Double = conc2(4)
78+
3679
}
3780

3881
object Test extends App

0 commit comments

Comments
 (0)