Skip to content

Refactoring the global object initialization checker #17996

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 8 commits into from
Jul 29, 2023

Conversation

EnzeXing
Copy link
Contributor

This PR refactors the global object initialization checker as implemented in compiler/src/dotty/tools/dotc/transform/init/Objects.scala with the following:

  1. Refactoring the type hierarchy for abstract values by adding a new base class ValueElement which has Cold and Ref as child classes. This separates the first-order abstract values (Cold and Ref) from second-order abstract values (Refset)
  2. Adding documentation for the data structures and abstract syntax used in the initialization checker
  3. Adding return handlers which removes the warning for return statements in secondary constructors, which is shown in tests/init-global/pos/secondary-constructor-return.scala

Copy link
Contributor

@olhotak olhotak left a comment

Choose a reason for hiding this comment

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

We can discuss more in our meeting.

(outer.widen(1), Env.NoEnv)
else
// klass.enclosingMethod returns its primary constructor
Env.resolveEnv(klass.owner.enclosingMethod, outer, summon[Env.Data]).getOrElse(Cold -> Env.NoEnv)
Env.resolveEnv(klass.owner.enclosingMethod, outer.asInstanceOf[ValueElement], summon[Env.Data]).getOrElse(Cold -> Env.NoEnv)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here, we know a more specific type of outer because we pattern matched on it.

I'd suggest renaming value in the pattern match case to outer. Then we'd have the more specific type for it.

@EnzeXing EnzeXing marked this pull request as ready for review June 25, 2023 09:48
@EnzeXing
Copy link
Contributor Author

EnzeXing commented Jul 1, 2023

@liufengyun @olhotak @q-ata Could any of you review this PR again? I've made changes to the widen method

@EnzeXing
Copy link
Contributor Author

EnzeXing commented Jul 1, 2023

By the way, the rewrites compilation tests failed here but I cannot reproduce on my end. Should I run testCompilation rewrites on my end?

@@ -161,32 +191,36 @@ object Objects:
*
* @param owner The static object whose initialization creates the array.
*/
case class OfArray(owner: ClassSymbol, regions: Regions.Data)(using @constructorOnly ctx: Context)
extends Ref(valsMap = mutable.Map.empty, varsMap = mutable.Map.empty, outersMap = mutable.Map.empty):
case class OfArray(owner: ClassSymbol, regions: Regions.Data)(using @constructorOnly ctx: Context) extends ValueElement:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@liufengyun Now that OfArray is not a subtype of Ref, do we need to explicitly handle OfArray in select and assign?

Copy link
Contributor

Choose a reason for hiding this comment

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

As discussed in the meeting, for select and assign, we can issue an internal error (it should not be reachable). For call, we can move the code currently in ref: Ref to array: OfArray.

EnzeXing added 3 commits July 22, 2023 19:56
Adding documentation for abstract syntax

Removing warnings for returns in secondary constructors

Removing assertions in call and eval

Refactor type hierarchy for abstract value

Adding comment for secondary constructor returns

Rewriting widen as abstract method

Removing original widen

Resolving conflict

Removing abstract widen
* refMap = Ref -> ( valsMap, varsMap, outersMap ) // refMap stores field informations of an object or instance
* valsMap = valsym -> val // maps immutable fields to their values
* varsMap = valsym -> addr // each mutable field has an abstract address
* outersMap = class -> val // maps outer objects to their values
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As we have discussed, outer could be any possible value in our current implementation (see outerValue)

Copy link
Contributor

@olhotak olhotak left a comment

Choose a reason for hiding this comment

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

Otherwise LGTM. Can merge after review from @liufengyun .

Co-authored-by: Ondřej Lhoták <olhotak@uwaterloo.ca>
Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

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

Otherwise, LGTM, thank you @EnzeXing .

@olhotak olhotak merged commit a73316a into scala:main Jul 29, 2023
@Kordyjan Kordyjan added this to the 3.4.0 milestone Aug 1, 2023
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