Skip to content

Commit 2d7e73e

Browse files
gagandeepkalragriggtnicolasstuckitimotheeandres
committed
fix #13011, error out infinite recursive lazy val
Co-authored-by: Tom Grigg <griggt@users.noreply.github.com> Co-authored-by: Nicolas Stucki <nicolasstucki@users.noreply.github.com> Co-authored-by: Timothée Andres <timothee.andres@epfl.ch>
1 parent 3b5ff7a commit 2d7e73e

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ class CheckLoopingImplicits extends MiniPhase:
2020

2121
override def phaseName: String = CheckLoopingImplicits.name
2222

23-
override def transformDefDef(mdef: DefDef)(using Context): DefDef =
23+
override def transformValDef(mdef: ValDef)(using Context): Tree =
24+
transform(mdef)
25+
26+
override def transformDefDef(mdef: DefDef)(using Context): Tree =
27+
transform(mdef)
28+
29+
def transform(mdef: ValOrDefDef)(using Context): Tree =
2430
val sym = mdef.symbol
2531

2632
def checkNotSelfRef(t: RefTree) =
@@ -70,12 +76,12 @@ class CheckLoopingImplicits extends MiniPhase:
7076
checkNotLooping(finalizer)
7177
case SeqLiteral(elems, _) =>
7278
elems.foreach(checkNotLooping)
73-
case t: ValDef =>
74-
if !t.symbol.is(Lazy) then checkNotLooping(t.rhs)
79+
case t: ValDef =>
80+
checkNotLooping(t.rhs)
7581
case _ =>
7682

77-
if sym.isOneOf(GivenOrImplicit) then
83+
if sym.isOneOf(GivenOrImplicit | Lazy) then
7884
checkNotLooping(mdef.rhs)
7985
mdef
80-
end transformDefDef
86+
end transform
8187
end CheckLoopingImplicits

tests/neg-custom-args/fatal-warnings/i13542.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ val x5 =
4040

4141
val x6 =
4242
implicit def barToFoo4(bar: Bar): Foo =
43-
lazy val y = bar.toFoo // OK
43+
lazy val y = bar.toFoo // error
4444
if false then y else ???
4545
val foo: Foo = Bar(1)
4646

tests/neg/i13011.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class i13011 {
2+
lazy implicit val simple1: String = simple1 // error
3+
def f: Unit = {
4+
lazy val simple2: String = simple2 // error
5+
}
6+
7+
lazy val simple3: String = if true then this.simple3 else "a" // error
8+
9+
lazy val simple4: String = identity(this.simple4) // error
10+
11+
lazy val simple5: String = { // error
12+
this.simple5
13+
"aa"
14+
}
15+
16+
lazy val simple6: Function0[Any] = () => this.simple6 // Ok
17+
}

0 commit comments

Comments
 (0)