Skip to content

Init: Promote this after all its fields have been initialized #11278

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 5 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/init/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,16 @@ object Checking {
case Promote(pot) =>
pot match {
case pot: ThisRef =>
PromoteThis(pot, eff.source, state2.path).toErrors

// If we have all fields initialized, then we can promote This to hot.
val classRef = state.thisClass.info.asInstanceOf[ClassInfo].appliedRef
val allFieldsInited = classRef.fields.forall { denot =>
val sym = denot.symbol
sym.isOneOf(Flags.Lazy | Flags.Deferred) || state.fieldsInited.contains(sym)
}
if (allFieldsInited)
Errors.empty
else
PromoteThis(pot, eff.source, state2.path).toErrors
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be good to cache safely promoted potentials to avoid re-computation. See #10627.

case _: Cold =>
PromoteCold(eff.source, state2.path).toErrors

Expand Down
4 changes: 4 additions & 0 deletions tests/init/pos/early-promote-simple.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class P() {
val a = 1
List(this)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to add a test that a warm inner class object promotes to hot because of the outer object being hot.