-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Don't change reference to parameter. The previous stability check omitted this case.
diesalbla
approved these changes
Jan 27, 2019
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Don't change reference to parameter.
The previous stability check omitted this case.
Rebase of #7611
Fixes scala/bug#6880