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

calling methods with default arguments on anonymous instances crashes the compiler #15315

Closed
rmgk opened this issue May 29, 2022 · 3 comments · Fixed by #19739
Closed

calling methods with default arguments on anonymous instances crashes the compiler #15315

rmgk opened this issue May 29, 2022 · 3 comments · Fixed by #19739

Comments

@rmgk
Copy link

rmgk commented May 29, 2022

Compiler version

3.1.2, 3.2.0-RC1-bin-20220527-001bfc3-NIGHTLY

Minimized code

class A:
  def f(x: Int = 1): Int = x

@main def run() =
  (new A{}).f()

Output (click arrow to expand)

abbreviated output:

exception occurred while compiling maintest.scala
java.util.NoSuchElementException: None.get while compiling maintest.scala
Exception in thread "main" java.util.NoSuchElementException: None.get
	at scala.None$.get(Option.scala:627)
	at scala.None$.get(Option.scala:626)
	at dotty.tools.dotc.transform.LambdaLift.transformTemplate(LambdaLift.scala:348)
exception occurred while compiling maintest.scala
java.util.NoSuchElementException: None.get while compiling maintest.scala
Exception in thread "main" java.util.NoSuchElementException: None.get
	at scala.None$.get(Option.scala:627)
	at scala.None$.get(Option.scala:626)
	at dotty.tools.dotc.transform.LambdaLift.transformTemplate(LambdaLift.scala:348)
	at dotty.tools.dotc.transform.LambdaLift.transformTemplate(LambdaLift.scala:344)
	at dotty.tools.dotc.transform.MegaPhase.goTemplate(MegaPhase.scala:1004)
	at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:363)
	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$1(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.transformNamed$1(MegaPhase.scala:228)
	at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:427)
	at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:279)
	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:280)
	at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:429)
	at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:300)
	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$1(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.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$1(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.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$1$$anonfun$1(Run.scala:259)
	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:1328)
	at dotty.tools.dotc.Run.runPhases$1(Run.scala:270)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:278)
	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:287)
	at dotty.tools.dotc.Run.compileSources(Run.scala:220)
	at dotty.tools.dotc.Run.compile(Run.scala:204)
	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)
@rmgk rmgk added itype:bug itype:crash stat:needs triage Every issue needs to have an "area" and "itype" label labels May 29, 2022
@szymon-rd szymon-rd added area:transform and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels May 30, 2022
@odersky
Copy link
Contributor

odersky commented Sep 12, 2022

Code after typer:

  class A() extends Object() {
    def f(x: Int): Int = x
    def f$default$1: Int @uncheckedVariance = 1
  }
  final lazy module val test$package: test$package = new test$package()
  final module class test$package() extends Object() { 
    this: test$package.type =>
    @main def run(): Int = 
      {
        final class $anon() extends A() {}
        (new $anon():A)
      }.f(
        {
          final class $anon() extends A() {}
          (new $anon():A)
        }.f$default$1
      )
  }

@nicolasstucki
Copy link
Contributor

nicolasstucki commented Feb 19, 2024

Similarly,

class A:
  def f(x: Int = 1): Int = x

def test() = (new A).f()

produces

// Scala 3
    def test(): Int = new A().f(new A().f$default$1)

I would assume the the prefix should be evaluated once. At least Scala 2 does evaluate the prefix once.

// Scala 2
    def test(): Int = {
      <artifact> val qual$1: Playground.A = new Playground.this.A();
      <artifact> val x$1: Int = qual$1.f$default$1;
      qual$1.f(x$1)
    }

@nicolasstucki
Copy link
Contributor

It does lift the expression if the prefix is a def

class A:
  def f(x: Int = 1): Int = x

def makeA =
    println("Make A")
    new A

def test() = makeA.f()

produces

  def test(): Int =
    val $1$: A = makeA
    $1$.f($1$.f$default$1)  

We are probably not detecting side effects correctly with the constructors. Seems to be related with pure initializers.

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Feb 20, 2024
Checking if the prefix is pure is not enough to know if we need to list
the prefix. In the case of default parameters, the prefix tree might be
used several times to compute the default values. This expression should
only be computed once and therefore it should be lifted if there is some
computation/allocation involved. Furthermore, if the prefix contains a
local definition, it must be lifted to avoid duplicating the definition.

A similar situation could happen with dependent default parameters. This
currently works as expected.

Fixes scala#15315
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Feb 28, 2024
Checking if the prefix is pure is not enough to know if we need to list
the prefix. In the case of default parameters, the prefix tree might be
used several times to compute the default values. This expression should
only be computed once and therefore it should be lifted if there is some
computation/allocation involved. Furthermore, if the prefix contains a
local definition, it must be lifted to avoid duplicating the definition.

A similar situation could happen with dependent default parameters. This
currently works as expected.

Fixes scala#15315
nicolasstucki added a commit that referenced this issue Feb 29, 2024
Checking if the prefix is pure is not enough to know if we need to list
the prefix. In the case of default parameters, the prefix tree might be
used several times to compute the default values. This expression should
only be computed once and therefore it should be lifted if there is some
computation/allocation involved. Furthermore, if the prefix contains a
local definition, it must be lifted to avoid duplicating the definition.

A similar situation could happen with dependent default parameters. This
currently works as expected.

Fixes #15315
@Kordyjan Kordyjan modified the milestones: Future versions, 3.4.2 Mar 28, 2024
@Kordyjan Kordyjan modified the milestones: 3.4.2, 3.5.0 May 10, 2024
WojciechMazur pushed a commit that referenced this issue Jul 2, 2024
Checking if the prefix is pure is not enough to know if we need to list
the prefix. In the case of default parameters, the prefix tree might be
used several times to compute the default values. This expression should
only be computed once and therefore it should be lifted if there is some
computation/allocation involved. Furthermore, if the prefix contains a
local definition, it must be lifted to avoid duplicating the definition.

A similar situation could happen with dependent default parameters. This
currently works as expected.

Fixes #15315

[Cherry-picked 71b983b]
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.

6 participants