Skip to content

Spurious illegal premature access to the unconstructed this error when using local methods inside a by-name closure in super-class constructor parameter #11969

Open
@neko-kai

Description

@neko-kai

reproduction steps

Minimized example:

package example

import scala.util.Try

class X(a: Any)

final class XImpl
  extends X({
    def res = Try(helper())

    def helper() = ()

    res
  })

problem

The above fails to compile with an error:

Error:(10, 19) Implementation restriction: access of method helper$1 from <$anon: Function0>, would require illegal premature access to the unconstructed `this` of class XImpl in package example
    def res = Try(helper())

However, the error can be worked around by enclosing the block in a redundant function with ().pipe { _ => ... }, in which case it will compile and work as expected at runtime:

package example

import scala.util.Try
import scala.util.chaining._

class X(a: Any)

final class XImpl
  extends X(().pipe { _ =>
    def res = Try(helper())

    def helper() = ()

    res
  })

Which shows that the code doesn't actually trigger an implementation restriction.

expectation

Expected it to compile. This bug affects classes that pass an effect value or a block of code to the superconstructor such as this. dotty is not affected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)should compile

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions