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
Anonymous class initialization in secondary constructor with self reference results in compiler crash #9368
Comments
Imported From: https://issues.scala-lang.org/browse/SI-9368?orig=1 |
@liff said: class Foo(x: Int) {
def this() = {
this(1)
new { x }
}
} The problem seems to have something to do with the outer class of the anonymous class being resolved to This particular error goes away by changing ownerClass to: final def outerClass: Symbol =
if (this == NoSymbol) {
// ideally we shouldn't get here, but it's better to harden against this than suffer the infinite loop in SI-9133
devWarningDumpStack("NoSymbol.outerClass", 15)
NoSymbol
} else if (owner.isClass) owner
else if (owner.isAuxiliaryConstructor) owner.enclClass // <------
else if (isClassLocalToConstructor) owner.enclClass.outerClass
else owner.outerClass But unfortunately that breaks t4842, t6666, t1909 and ‘origins’. I haven't been able to figure out how to avoid that :) |
On 2.12.7 the former fails with
and the smaller repo fails with
|
On 2.13.x branch at least it's not crashing: scala> :paste
// Entering paste mode (ctrl-D to finish)
case class Event[T](sender: T)
class Foo(bar: String) {
self =>
def this() {
this("baz")
new { Event(self) } // crashes compiler
}
}
object Main extends App {
new Foo()
}
// Exiting paste mode, now interpreting.
new { Event(self) } // crashes compiler
^
On line 9: error: Internal error: unable to find the outer accessor symbol of <$anon: Object>
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Foo(x: Int) {
def this() = {
this(1)
new { x }
}
}
// Exiting paste mode, now interpreting.
new { x }
^
<pastie>:4: warning: a pure expression does nothing in statement position; multiline expressions may require enclosing parentheses
new { x }
^
<pastie>:4: error: Internal error: unable to find the outer accessor symbol of <$anon: Object> |
Looks like this check didn't trigger properly: In any case this looks like implementation restriction. |
In the second example |
This following snippet of code when compiled will break the compiler:
I've tested the code on versions 2.11.(1, 4, 7) and all of them will fail with the same error message of {{value is not a member of Object}}.
This issue seems to be related to SI-7853.
Compiling
Main.scala
withscalac
version 2.11.7 on my Windows machine results in the following stack trace:This at least seems to be a very specific issue issue with using self references in anonymous classes also in secondary constructors as this will compile without crashing the compiler which serves as a temporary fix in some cases:
The text was updated successfully, but these errors were encountered: