-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7789 from dotty-staging/init-dotty
Safe initialization for Scala
- Loading branch information
Showing
166 changed files
with
5,080 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
63 changes: 63 additions & 0 deletions
63
compiler/src/dotty/tools/dotc/transform/init/Checker.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package dotty.tools.dotc | ||
package transform | ||
package init | ||
|
||
|
||
import dotty.tools.dotc._ | ||
import ast.tpd | ||
|
||
import dotty.tools.dotc.core._ | ||
import Contexts.Context | ||
import Types._ | ||
|
||
import dotty.tools.dotc.transform._ | ||
import MegaPhase._ | ||
|
||
|
||
import scala.collection.mutable | ||
|
||
|
||
class Checker extends MiniPhase { | ||
import tpd._ | ||
|
||
val phaseName = "initChecker" | ||
|
||
// cache of class summary | ||
private val baseEnv = Env(null, mutable.Map.empty) | ||
|
||
override val runsAfter = Set(Pickler.name) | ||
|
||
override def isEnabled(implicit ctx: Context): Boolean = | ||
super.isEnabled && ctx.settings.YcheckInit.value | ||
|
||
override def transformTypeDef(tree: TypeDef)(implicit ctx: Context): tpd.Tree = { | ||
if (!tree.isClassDef) return tree | ||
|
||
val cls = tree.symbol.asClass | ||
val instantiable: Boolean = | ||
cls.is(Flags.Module) || | ||
!cls.isOneOf(Flags.AbstractOrTrait) && { | ||
// see `Checking.checkInstantiable` in typer | ||
val tp = cls.appliedRef | ||
val stp = SkolemType(tp) | ||
val selfType = cls.givenSelfType.asSeenFrom(stp, cls) | ||
!selfType.exists || stp <:< selfType | ||
} | ||
|
||
// A concrete class may not be instantiated if the self type is not satisfied | ||
if (instantiable) { | ||
implicit val state = Checking.State( | ||
visited = mutable.Set.empty, | ||
path = Vector.empty, | ||
thisClass = cls, | ||
fieldsInited = mutable.Set.empty, | ||
parentsInited = mutable.Set.empty, | ||
env = baseEnv.withCtx(ctx) | ||
) | ||
|
||
Checking.checkClassBody(tree) | ||
} | ||
|
||
tree | ||
} | ||
} |
Oops, something went wrong.