Skip to content

Commit 90de157

Browse files
authored
Merge pull request #13354 from dwijnand/fp-in-scala-crash
Avoid crash by handling by-names under Typed
2 parents 09fb2fe + c90d779 commit 90de157

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

compiler/src/dotty/tools/dotc/transform/TransformByNameApply.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ abstract class TransformByNameApply extends MiniPhase { thisPhase: DenotTransfor
4545
if (argType.isBottomType) argType = formal.widenExpr
4646
def wrap(arg: Tree) =
4747
ref(defn.cbnArg).appliedToType(argType).appliedTo(arg).withSpan(arg.span)
48-
arg match {
48+
def unTyped(t: Tree): Tree = t match { case Typed(expr, _) => unTyped(expr) case _ => t }
49+
unTyped(arg) match {
4950
case Apply(Select(qual, nme.apply), Nil)
5051
if qual.tpe.derivesFrom(defn.Function0) && (isPureExpr(qual) || qual.symbol.isAllOf(Inline | Param)) =>
5152
wrap(qual)

tests/pos/i13349.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
sealed trait Stream[+A]{
2+
import Stream.*;
3+
4+
def foldRight[B](z: => B)(f: (A, => B) => B): B =
5+
this match {
6+
case Cons(h,t) => f(h(), t().foldRight(z)(f))
7+
case _ => z
8+
}
9+
10+
def append[B >: A](other : => Stream[B]) : Stream[B] =
11+
foldRight(other : Stream[B])((elem, stream) => cons(elem, stream))
12+
13+
}
14+
15+
case object Empty extends Stream[Nothing]
16+
case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A]
17+
18+
object Stream {
19+
20+
def cons[A](hd: => A, tl: => Stream[A]): Stream[A] = {
21+
lazy val head = hd
22+
lazy val tail = tl
23+
Cons(() => head, () => tail)
24+
}
25+
26+
def empty[A]: Stream[A] = Empty
27+
28+
def apply[A](as: A*): Stream[A] =
29+
if (as.isEmpty) empty else cons(as.head, apply(as.tail: _*))
30+
31+
}

tests/pos/i13349min.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Foo:
2+
def foo(x: => Foo) = bar(x: Foo)
3+
def bar(x: => Foo) = x

0 commit comments

Comments
 (0)