scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Lambda-owned param ref in ctor incurs no field

Open som-snytt opened this issue 6 months ago • 2 comments

Fixes #22979

In Constructors, marking usages was too restrictive in checking that a ref is enclosed by the primary constructor, because anonfuns in the template have not been moved to the constructor yet. This commits allows refs from an anonfun owned by the "local dummy" or by an eager member of the sym's owner (the enclosing class), that is, it's an initializer.

There is a minor local refactor in splitStats for readability.

som-snytt avatar May 29 '25 20:05 som-snytt

The bad example was

class Lapse:
  def f = Lapse.DefaultSentinelFn()
object Lapse:
  private val DefaultSentinel: AnyRef = new AnyRef
  private val DefaultSentinelFn: () => AnyRef = () => DefaultSentinel

where the anonfun now has a bad RHS.

  @SourceFile("lapse.scala") final module class Lapse extends Object {
    def <init>(): Unit =
      {
        super()
        val DefaultSentinel: Object = new Object()
        val DefaultSentinelFn: Function0 =
          {
            closure(DefaultSentinel | this.$init$$$anonfun$1)
          }
        ()
      }
    private def writeReplace(): Object =
      new scala.runtime.ModuleSerializationProxy(classOf[Lapse])
    private final def $init$$$anonfun$1(DefaultSentinel$1: Object): Object =
      DefaultSentinel
  }

Probably lambalift thinks there is no work to do there because it is static. I haven't looked but will do so.

The code is from MapOps, where DefaultSentinelFn() is used in the companion class.

som-snytt avatar May 29 '25 22:05 som-snytt

The revised test failure is scalajs, which reduces the chance of my understanding it.

Test 'tests/run/i22979' failed with output:                                     
org.scalajs.linker.interface.LinkingException: There were linking errors
	at org.scalajs.linker.analyzer.Analyzer.reportErrors(Analyzer.scala:89)
	at org.scalajs.linker.analyzer.Analyzer.$anonfun$computeReachability$1(Analyzer.scala:65)

... or being able to run the test locally.

";sjsSandbox/run ;sjsSandbox/test ;sjsJUnitTests/test ;set sjsJUnitTests/scalaJSLinkerConfig ~= switchToESModules ;sjsJUnitTests/test ;sjsCompilerTests/test"

som-snytt avatar May 30 '25 11:05 som-snytt