-
Notifications
You must be signed in to change notification settings - Fork 1.1k
calling methods with default arguments on anonymous instances crashes the compiler #15315
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
Comments
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
)
} |
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)
} |
It does lift the expression if the prefix is a 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
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
Compiler version
3.1.2, 3.2.0-RC1-bin-20220527-001bfc3-NIGHTLY
Minimized code
Output (click arrow to expand)
abbreviated output:
The text was updated successfully, but these errors were encountered: