Skip to content

Only allow StoreModule at the top-level of constructor bodies. #5057

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
Nov 2, 2024

Conversation

sjrd
Copy link
Member

@sjrd sjrd commented Oct 21, 2024

This allows to get rid of Env.inConstructor in the ClassDefChecker.

In theory, this is a breaking change. However, we never emitted IR with StoreModules that would violate the new rule.

In fact, all the IR we emit is even stricter: there is always a StoreModule immediately after the super constructor call of (JS) module class constructors, and never anywhere else.


Followup to #5020 (review)

@gzm0 Should we check the even stricter version?

@sjrd sjrd requested a review from gzm0 October 21, 2024 09:28
@sjrd
Copy link
Member Author

sjrd commented Oct 21, 2024

Hum actually we never emit StoreModules for JS module classes. 🤔

Should we? Maybe not, and we should forbid them there as well.

As a reminder, the purpose of StoreModule is that the module instance cache gets completed early. This way, after the super constructor call returns, we can have ~cyclic dependencies between modules, allowing some apparently cyclic initialization schemes to go through. That was critical of Scala objects to support existing initialization patterns in Scala (notably in the std lib), but if nobody complained so far, maybe it's fine not to do it for JS objects?

This allows to get rid of `Env.inConstructor` in the
`ClassDefChecker`.

In theory, this is a breaking change. However, we never emitted IR
with `StoreModule`s that would violate the new rule.

In fact, all the IR we emit is even stricter: there is always a
`StoreModule` *immediately after* the super constructor call of
(JS) module class constructors, and never anywhere else.
@sjrd sjrd force-pushed the restrict-store-module-to-ctor-top-level branch from 5678e9a to 7840ce1 Compare October 21, 2024 10:18
@gzm0
Copy link
Contributor

gzm0 commented Nov 2, 2024

but if nobody complained so far, maybe it's fine not to do it for JS objects?

IMO that violates the principle of least surprise. The object part of JS objects, is really a Scala semantic, so I would not diverge unless we have to.

there is always a StoreModule immediately after the super constructor call of (JS) module class constructors, and never anywhere else.

Would it make sense to make StoreModule implicit?

Copy link
Contributor

@gzm0 gzm0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK to merge. IMO this PR should go in irrespective of what the answers to the two higher level comments are.

@sjrd
Copy link
Member Author

sjrd commented Nov 2, 2024

IMO that violates the principle of least surprise. The object part of JS objects, is really a Scala semantic, so I would not diverge unless we have to.

OK. In that case I propose the following sequence:

  1. Merge this PR as is.
  2. In the other PR, rebase then have 2 commits:
    1. The existing commit but take out the JS module exception, and instead introduce a deserialization hack to insert the missing StoreModules.
    2. Change the compiler to add the missing StoreModules.

Does that make sense?

@gzm0 gzm0 merged commit 771add2 into scala-js:main Nov 2, 2024
3 checks passed
@gzm0
Copy link
Contributor

gzm0 commented Nov 2, 2024

Yes, that plan SGTM.

@sjrd sjrd deleted the restrict-store-module-to-ctor-top-level branch November 2, 2024 16:07
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.

2 participants