Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Lambda-owned param ref in ctor incurs no field #23286

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

som-snytt
Copy link
Contributor

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 som-snytt force-pushed the issue/22979-lambda-field branch from 417ced9 to 0e22004 Compare May 29, 2025 22:10
@som-snytt
Copy link
Contributor Author

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
Copy link
Contributor Author

som-snytt commented May 30, 2025

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpectedly retained constructor argument
1 participant