Add warning on self recursive call for Scala 3 like Scala 2 has?

Lately, I lost some time on a problem that basically boiled down to this:

trait Test {
  val name: String }

object Test {
  def apply(name: String): Test = new Test {
    val name: String = name } }


Now, on Scala 3.3.2, 3.3.3 and 3.4.0 this simply returns null and that is that. In Scala 2.12.19 you at least get a warning:

value name does nothing other than call itself recursively

Could that be added to Scala 3 too?

And while w’re at it, it would also be really, really nice if constructions like this:

object test {
  val a: Int = a + 1 }

would generate a warning. At the moment, no version af Scala (see for example 3.4, 2.13) does this. Is it possible at all? Something like: “a is used with its default, uninitialised value”?

(Btw, Scala 2.13.13 gives an interesting ResolveException)

1 Like

-Ysafe-init has fancy graphics.

$ ~/projects/dotty/bin/scalac -d /tmp/sandbox -Ysafe-init bad-recursion.scala
-- Warning: bad-recursion.scala:7:8 ------------------------------------------------------------------------------------
7 |    val name: String = name } }
  |        ^
  |        Access non-initialized value name. Calling trace:
  |        ├── def apply(name: String): Test = new Test {       [ bad-recursion.scala:6 ]
  |        │                                   ^
  |        └── val name: String = name } }      [ bad-recursion.scala:7 ]
  |                               ^^^^
1 warning found

Interesting. I missed that, or more likely, I may have read about it somewhere, sometime ago and than forgot about it on the “moment suprème”. It would be nice if it was under the -W switches or even better, if there was a -Wall switch which was guaranteed to produce all warnings. It is better to switch warnings you know can’t hurt to off instead of missing potential dangerous ones because you do not know its switch.

Thank you for pointing this out though. :+1:

1 Like

When the feature is stable, it will be mainstreamed.

The scastie update is at this PR.

I always wanted to contribute to scastie.


cc @liufengyun

Furthermore, things like the Scala 3 example project from metals should include those flags
Also I don’t think there is any mention of those flags on the getting started with Scala pages