Skip to content

Warning for infinite recursive lazy vals #13749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ class CheckLoopingImplicits extends MiniPhase:

override def phaseName: String = CheckLoopingImplicits.name

override def transformDefDef(mdef: DefDef)(using Context): DefDef =
override def transformValDef(mdef: ValDef)(using Context): Tree =
transform(mdef)

override def transformDefDef(mdef: DefDef)(using Context): Tree =
transform(mdef)

def transform(mdef: ValOrDefDef)(using Context): Tree =
val sym = mdef.symbol

def checkNotSelfRef(t: RefTree) =
Expand Down Expand Up @@ -70,12 +76,12 @@ class CheckLoopingImplicits extends MiniPhase:
checkNotLooping(finalizer)
case SeqLiteral(elems, _) =>
elems.foreach(checkNotLooping)
case t: ValDef =>
if !t.symbol.is(Lazy) then checkNotLooping(t.rhs)
case t: ValDef =>
checkNotLooping(t.rhs)
case _ =>

if sym.isOneOf(GivenOrImplicit) then
if sym.isOneOf(GivenOrImplicit | Lazy) then
checkNotLooping(mdef.rhs)
mdef
end transformDefDef
end transform
end CheckLoopingImplicits
2 changes: 1 addition & 1 deletion compiler/test-resources/repl/10886
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ scala> type Channel = "A" | "B"

scala> type SelChannel[C <: Tuple] = C match { case x *: xs => x | SelChannel[xs] case _ => Nothing }

scala> lazy val a: SelChannel[("A", "B", "C")] = a
scala> lazy val a: SelChannel[("A", "B", "C")] = "A"
lazy val a: "A" | ("B" | ("C" | Nothing))

scala>:type a
Expand Down
21 changes: 21 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i13011.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class i13011 {
lazy implicit val simple1: String = simple1 // error
def f: Unit = {
lazy val simple2: String = simple2 // error
}

lazy val simple3: String = if true then this.simple3 else "a" // error

def firstDigitIsEven(n: Int): Boolean = if n % 10 == n then n % 2 == 0 else firstDigitIsEven(n / 10)

lazy val simple4: String = if firstDigitIsEven(22) then this.simple4 else "a" // ok

lazy val simple5: String = identity(this.simple5) // error

lazy val simple6: String = { // error
this.simple6
"aa"
}

lazy val simple7: Function0[Any] = () => this.simple7 // Ok
}
2 changes: 1 addition & 1 deletion tests/neg-custom-args/fatal-warnings/i13542.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ val x5 =

val x6 =
implicit def barToFoo4(bar: Bar): Foo =
lazy val y = bar.toFoo // OK
lazy val y = bar.toFoo // error
if false then y else ???
val foo: Foo = Bar(1)

Expand Down