-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Crash in backend in secondary constructor edge case #18927
Comments
Crashes in 3.2 and 3.1 as well, not a regression |
Tree checker also doesnt like this snippet:
|
The causeHere is a simplified version of the tree after ...
class C extends Object {
...
def <init>($outer: B): Unit =
{
class Inner extends Object {
def <init>($outer: B.this.C): Unit =
{
...
$outer = $outer
...
}
private val $outer: B.this.C
}
()
}
private val $outer: B
}
... The |
Found the bug:
override def transform(tree: Tree)(using Context) = tree match {
....
case tree: RefTree if tree.symbol.is(ParamAccessor) && tree.symbol.name == nme.OUTER =>
ref(outerParam)
... The problem is that if there are any nested constructors in the constructor, they will also be transformed. And all will point to the same $outer. |
Fix #18927 The transformer in `mapOuter` in `Constructors` was transforming trees it should not: ```scala override def transform(tree: Tree)(using Context) = tree match { [....] case tree: RefTree if tree.symbol.is(ParamAccessor) && tree.symbol.name == nme.OUTER => ref(outerParam) [...] ``` There were two problems: - This case transformed LHS of `$outer` assignments in constructors. So, instead of setting the `$outer` field in the current class with the constructor, it was replaced with the $outer of the outer class. That resulted in not assigning any value to the `$outer` in inner class, and double assignment to the val in outer class. - LHS of the accessor def was also transformed, so it was evaluated to the `$outer` of the outer class. This only happened when the nested class is created in the secondary constructor, as the primary constructor is not transformed (only the template body)
Compiler version
3.4.0-RC1-bin-20231113-0dfe593-NIGHTLY
Minimized code
Minimized code from
tests/init/neg/secondary-ctor4
test:Output
The text was updated successfully, but these errors were encountered: