Skip to content
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

By-name field passed to anonymous class constuctor #14010

Closed
barthelmannk opened this issue Nov 28, 2021 · 1 comment · Fixed by #14057
Closed

By-name field passed to anonymous class constuctor #14010

barthelmannk opened this issue Nov 28, 2021 · 1 comment · Fixed by #14057
Milestone

Comments

@barthelmannk
Copy link

barthelmannk commented Nov 28, 2021

Compiler version

3.1.0

Minimized code

abstract class LazyList[+T] {
  def head: T
  def tail: LazyList[T]
  def isEmpty: Boolean
  def push[E >: T](top: => E): LazyList[E] =
    new Push[E](top, this)
  //def map[R](f: T => R): LazyList[R]
  def append[E >: T](that: => LazyList[E]): LazyList[E]
}

private class Push[+T](top: => T, stack: => LazyList[T]) extends LazyList[T] {
  override def head: T =
    top
  override def tail: LazyList[T] =
    stack
  override def isEmpty: Boolean =
    false
  //override def map[R](f: T => R): LazyList[R] =
  //  new Push[R](f(top), stack.map(f)) {
  //        override def map[R2](f2: R => R2): LazyList[R2] =
  //          Push.this.map(f2 compose f)
  //      }
  override def append[E >: T](that: => LazyList[E]): LazyList[E] =
    new Push[E](top, stack.append(that)) {
          override def append[E2 >: E](that2: => LazyList[E2]): LazyList[E2] =
            Push.this.append(that.append(that2))
        }
}

object LazyList {
  val empty =
    new LazyList[Nothing] {
          override def head: Nothing =
            throw new NoSuchElementException
          override def tail: LazyList[Nothing] =
            throw new UnsupportedOperationException
          override def isEmpty: Boolean =
            true
          //override def map[R](f: _ => R): LazyList[R] =
          //  this
          override def append[E](that: => LazyList[E]): LazyList[E] =
            that
        }
  def apply[T](elements: T*): LazyList[T] =
    elements.foldRight[LazyList[T]](empty)(new Push(_, _))
}

Output

exception occurred while compiling LazyList.scala
java.lang.UnsupportedOperationException: mkClosure($anon$superArg$1()) while compiling LazyList.scala
Exception in thread "main" java.lang.UnsupportedOperationException: mkClosure($anon$superArg$1())
	at dotty.tools.package$.unsupported(package.scala:25)
	at dotty.tools.dotc.transform.TransformByNameApply.mkByNameClosure(TransformByNameApply.scala:37)
	at dotty.tools.dotc.transform.TransformByNameApply.transformArg$1(TransformByNameApply.scala:55)
	at dotty.tools.dotc.transform.TransformByNameApply.$anonfun$1(TransformByNameApply.scala:62)
	at dotty.tools.dotc.core.Decorators$ListDecorator$.zipWithConserve$extension(Decorators.scala:153)
	at dotty.tools.dotc.transform.TransformByNameApply.transformApply(TransformByNameApply.scala:62)
	at dotty.tools.dotc.transform.MegaPhase.goApply(MegaPhase.scala:644)
	at dotty.tools.dotc.transform.MegaPhase.goApply(MegaPhase.scala:645)
	at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:281)
	at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:429)
	at dotty.tools.dotc.transform.MegaPhase.recur$2(MegaPhase.scala:453)
	at dotty.tools.dotc.transform.MegaPhase.transformTrees(MegaPhase.scala:453)
	at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:360)
	at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:429)
	at dotty.tools.dotc.transform.MegaPhase.transformNamed$1(MegaPhase.scala:256)
	at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:427)
	at dotty.tools.dotc.transform.MegaPhase.transformStat$2(MegaPhase.scala:437)
	at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
	at dotty.tools.dotc.transform.MegaPhase.transformStats(MegaPhase.scala:442)
	at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:299)
	at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:429)
	at dotty.tools.dotc.transform.MegaPhase.mapDefDef$1(MegaPhase.scala:249)
	at dotty.tools.dotc.transform.MegaPhase.transformNamed$1(MegaPhase.scala:252)
	at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:427)
	at dotty.tools.dotc.transform.MegaPhase.transformStat$2(MegaPhase.scala:437)
	at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
	at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
	at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
	at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
	at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
	at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
	at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
	at dotty.tools.dotc.transform.MegaPhase.transformStats(MegaPhase.scala:442)
	at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:362)
	at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:429)
	at dotty.tools.dotc.transform.MegaPhase.transformNamed$1(MegaPhase.scala:256)
	at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:427)
	at dotty.tools.dotc.transform.MegaPhase.transformStat$2(MegaPhase.scala:437)
	at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
	at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
	at dotty.tools.dotc.transform.MegaPhase.transformStats(MegaPhase.scala:442)
	at dotty.tools.dotc.transform.MegaPhase.mapPackage$1(MegaPhase.scala:382)
	at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:385)
	at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:429)
	at dotty.tools.dotc.transform.MegaPhase.transformUnit(MegaPhase.scala:448)
	at dotty.tools.dotc.transform.MegaPhase.run(MegaPhase.scala:460)
	at dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:308)
	at scala.collection.immutable.List.map(List.scala:246)
	at dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:309)
	at dotty.tools.dotc.Run.runPhases$4$$anonfun$4(Run.scala:261)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
	at dotty.tools.dotc.Run.runPhases$5(Run.scala:272)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:280)
	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
	at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:68)
	at dotty.tools.dotc.Run.compileUnits(Run.scala:289)
	at dotty.tools.dotc.Run.compileSources(Run.scala:222)
	at dotty.tools.dotc.Run.compile(Run.scala:206)
	at dotty.tools.dotc.Driver.doCompile(Driver.scala:39)
	at dotty.tools.dotc.Driver.process(Driver.scala:199)
	at dotty.tools.dotc.Driver.process(Driver.scala:167)
	at dotty.tools.dotc.Driver.process(Driver.scala:179)
	at dotty.tools.dotc.Driver.main(Driver.scala:209)
	at dotty.tools.dotc.Main.main(Main.scala)

Expectation

I think, the code should compile (or at least cause some legible error message). ;)

The culprit is the first parameter, top, to class Push. With a plain parameter instead of a by-name, the code compiles.

In the example, there are other by-name parameters commented out, which work just fine. Also commented out is another method, map, which looks quite similar to method append and doesn't cause problems. It is possible to replace class Push with an anonymous subclass of LazyList, and everything will work just fine.

I've seen earlier bug reports which looked a bit similar, but they seem to be closed already.

@nicolasstucki
Copy link
Contributor

Minimized

class Foo(top: => Int) {
  def foo: Any = new Foo(top) { }
}

@nicolasstucki nicolasstucki changed the title Problem with by-name class parameter passed to anonymous subclass [UnsupportedOperationException: mkClosure($anon$superArg$1())] By-name field passed to anonymous class constuctor Nov 29, 2021
odersky added a commit to dotty-staging/dotty that referenced this issue Dec 6, 2021
olsdavis pushed a commit to olsdavis/dotty that referenced this issue Apr 4, 2022
@Kordyjan Kordyjan added this to the 3.1.2 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants