Skip to content
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

Rework implicit allocation in constructors #1255

Merged
merged 7 commits into from
May 9, 2020
Merged

Rework implicit allocation in constructors #1255

merged 7 commits into from
May 9, 2020

Conversation

dcodeIO
Copy link
Member

@dcodeIO dcodeIO commented May 7, 2020

As reported in #1225 some weirdness is happening in constructors, caused by late this allocations. While investigating, it turned out that this mechanism is connected to quite a few other assumptions made by inlining and reference counting, so I decided to implement a new but more conservative mechanism.

With the change, the allocation is injected right at the start of the constructor as soon as any code path requires it (falls-through or accesses this). As a side-effect, the following special cases are no longer allowed:

  • A constructor with an explicit return must not access this. Not yet sure if this can become a warning instead of an error.
  • A class with an explicitly returning constructor must be sealed since super calls in this scenario will break due to the resulting this mismatch.

On the upside, implicit allocations in constructors are now guaranteed to be performed exactly once, reducing code size in scenarios where this is first accessed in branches.

fixes #1225

@dcodeIO
Copy link
Member Author

dcodeIO commented May 8, 2020

This now generally allows returning in constructors as long as this is returned. If something else than this is returned somewhere, the class must be @sealed. In any case, if something else than this is returned but this is being accessed somewhere in the constructor or the constructor falls through, a pedantic diagnostic is emitted hinting at the potentially dropped this allocation.

@dcodeIO
Copy link
Member Author

dcodeIO commented May 8, 2020

Also renamed @sealed to @final now, so we can reuse it for methods or anything else final as well eventually.

@dcodeIO dcodeIO merged commit 0c02d01 into master May 9, 2020
@dcodeIO dcodeIO deleted the issue-1225 branch May 14, 2020 21:11
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.

Weird behavior with constructor parameter properties
2 participants