8000 Constructor keeps ref to private local field by som-snytt · Pull Request #7688 · scala/scala · GitHub
[go: up one dir, main page]

Skip to content

Constructor keeps ref to private local field #7688

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

Merged
merged 1 commit into from
Feb 4, 2019

Conversation

som-snytt
Copy link
Contributor
@som-snytt som-snytt commented Jan 25, 2019

Don't change reference to parameter.
The previous stability check omitted this case.

Rebase of #7611
Fixes scala/bug#6880

Don't change reference to parameter.
The previous stability check omitted this case.
@scala-jenkins scala-jenkins added this to the 2.13.0-RC1 milestone Jan 25, 2019
@adriaanm adriaanm merged commit 9bc34e9 into scala:2.13.x Feb 4, 2019
@som-snytt som-snytt deleted the issue/6880 branch February 4, 2019 16:30
lrytz added a commit that referenced this pull request Nov 25, 2019
This reverts commit 546ed4b.
The test was already added in #7688
lrytz added a commit to lrytz/scala that referenced this pull request Jul 29, 2020
This is not trivial to fix unfortunately.

The `constructors` phase creates the primary constructor and moves
statements from the class body into it. While doing that, it rewrites
reads of to class parameter fields to the constructor parameters, except
for variables - otherwise writes to the variable are not reflected
(this is the fix done in scala#7688).

Only after that, the phase checks which `private[this]` fields are not
used. The implementation for that uses the triage of the class body
elements (primary constr, aux constrs, other defs) done in the first
step.

So if we remove the `private[this] var` field in the second step
(because it's never written, and never read outside of the constructor),
the constructor ends up with an access to a non-existing field.

The fix here delays rewriting reads of class parameter fields to
constructor parameters until after the omittable fields are computed.
lrytz added a commit to lrytz/scala that referenced this pull request Jul 29, 2020
This is not trivial to fix unfortunately.

The `constructors` phase creates the primary constructor and moves
statements from the class body into it. While doing that, it rewrites
reads of to class parameter fields to the constructor parameters, except
for variables - otherwise writes to the variable are not reflected
(this is the fix done in scala#7688).

Only after that, the phase checks which `private[this]` fields are not
used. The implementation for that uses the triage of the class body
elements (primary constr, aux constrs, other defs) done in the first
step.

So if we remove the `private[this] var` field in the second step
(because it's never written, and never read outside of the constructor),
the constructor ends up with an access to a non-existing field.

The fix here delays rewriting reads of class parameter fields to
constructor parameters until after the omittable fields are computed.
lrytz added a commit to lrytz/scala that referenced this pull request Sep 25, 2020
Reads of `var` class parameters in the constructor are not rewritten to
the constructor parameter, they keep referring to the field to ensure
potential field writes are reflected (this was fixed in scala#7688).

This means that the corresponding field cannot be elided even if it's
never written.

The alternative solution would be to detect `private[this] var` fields
that are only read in the constructor. However this is difficult to
implement, see scala#9143.
lrytz added a commit to lrytz/scala that referenced this pull request Sep 28, 2020
Reads of `var` class parameters in the constructor are not rewritten to
the constructor parameter, they keep referring to the field to ensure
potential field writes are reflected (this was fixed in scala#7688).

This means that the corresponding field cannot be elided even if it's
never written.

The alternative solution would be to detect `private[this] var` fields
that are only read in the constructor. However this is difficult to
implement, see scala#9143.
lrytz added a commit to lrytz/scala that referenced this pull request Oct 22, 2020
Reads of `var` class parameters in the constructor are not rewritten to
the constructor parameter, they keep referring to the field to ensure
potential field writes are reflected (this was fixed in scala#7688).

This means that the corresponding field cannot be elided even if it's
never written.

The alternative solution would be to detect `private[this] var` fields
that are only read in the constructor. However this is difficult to
implement, see scala#9143.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0