aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/core/TyperState.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2013-11-22 13:42:09 +0100
committerMartin Odersky <odersky@gmail.com>2013-11-22 13:42:09 +0100
commitb14291436172bf53cb40fdd2e94491e36a7da115 (patch)
tree67bb0321df0960ba283fc685b0233f0cebaa145c /src/dotty/tools/dotc/core/TyperState.scala
parente38813ac1362a1d528dfa1ee79f0f8b0d6f7ccb8 (diff)
downloaddotty-b14291436172bf53cb40fdd2e94491e36a7da115.tar.gz
dotty-b14291436172bf53cb40fdd2e94491e36a7da115.tar.bz2
dotty-b14291436172bf53cb40fdd2e94491e36a7da115.zip
Dropping cinsistency checking logic from typerstates.
Because of the new typerstate design, we can enforce most invariants by design, or in a purely local manner. We can therefore drop the checkConsistent checks and replace them with much more lightweight assertions that make sure that only committable states are committed and only committable states can create typevars.
Diffstat (limited to 'src/dotty/tools/dotc/core/TyperState.scala')
-rw-r--r--src/dotty/tools/dotc/core/TyperState.scala85
1 files changed, 14 insertions, 71 deletions
diff --git a/src/dotty/tools/dotc/core/TyperState.scala b/src/dotty/tools/dotc/core/TyperState.scala
index 8b5e2c85e..53782c642 100644
--- a/src/dotty/tools/dotc/core/TyperState.scala
+++ b/src/dotty/tools/dotc/core/TyperState.scala
@@ -10,17 +10,18 @@ import reporting._
import printing.{Showable, Printer}
import printing.Texts._
import collection.mutable
-import annotation.elidable
class TyperState(val reporter: Reporter) extends DotClass with Showable {
/** The current constraint set */
def constraint: Constraint = new Constraint(SimpleMap.Empty)
+ def constraint_=(c: Constraint): Unit = {}
def uninstVars = constraint.uninstVars
- /** A map that records for instantiated type vars their instance type.
- * Used only in a temporary way for contexts that may be retracted
+ /** Gives for each instantiated type var that does not yet have its `inst` field
+ * set, the instance value stored in the constraint. Storing instances in constraints
+ * is done only in a temporary way for contexts that may be retracted
* without also retracting the type var as a whole.
*/
def instType(tvar: TypeVar): Type = constraint.at(tvar.origin) match {
@@ -28,20 +29,16 @@ class TyperState(val reporter: Reporter) extends DotClass with Showable {
case tp => tp
}
- def constraint_=(c: Constraint): Unit = {}
-
+ /** A fresh typer state with the same constraint as this one.
+ * @param isCommittable The constraint can be committed to an exclosing context.
+ */
def fresh(isCommittable: Boolean): TyperState = this
+ /** Commit state so that it gets propagated to enclosing context */
def commit()(implicit ctx: Context): Unit = unsupported("commit")
- def isCommittable: Boolean = false
-
- @elidable(elidable.FINER)
- def checkConsistent(implicit ctx: Context) = ()
- @elidable(elidable.FINER)
- def enableChecking(b: Boolean): Boolean = true
-
- def withCheckingDisabled[T](op: => T)(implicit ctx: Context): T = op
+ /** Is it allowed to commit this state? */
+ def isCommittable: Boolean = false
override def toText(printer: Printer): Text = "ImmutableTyperState"
}
@@ -50,13 +47,9 @@ class MutableTyperState(previous: TyperState, reporter: Reporter, override val i
extends TyperState(reporter) {
private var myConstraint: Constraint = previous.constraint
- private var checkingEnabled: Boolean = true
override def constraint = myConstraint
- override def constraint_=(c: Constraint) = {
- myConstraint = c
- checkConsistent()
- }
+ override def constraint_=(c: Constraint) = myConstraint = c
override def fresh(isCommittable: Boolean): TyperState =
new MutableTyperState(this, new StoreReporter, isCommittable)
@@ -68,11 +61,10 @@ extends TyperState(reporter) {
* instantiated instead.
*/
override def commit()(implicit ctx: Context) = {
- checkConsistent
val targetState = ctx.typerState
- val prev = targetState.enableChecking(false)
+ assert(isCommittable)
+ assert(targetState.isCommittable)
targetState.constraint = constraint
- targetState.enableChecking(prev)
val toCollect = new mutable.ListBuffer[PolyType]
constraint foreachTypeVar { tvar =>
@@ -90,59 +82,10 @@ extends TyperState(reporter) {
for (poly <- toCollect)
targetState.constraint = targetState.constraint.remove(poly)
- targetState.checkConsistent // !!! DEBUG
-
reporter.flush()
}
- @elidable(elidable.FINER)
- def checkConsistent(show: Showable => String = MutableTyperState.toStr): Unit = if (checkingEnabled) {
- def err(msg: String, what: Showable) = s"$msg: ${show(what)}\n${show(this)}"
- for (tvar <- uninstVars)
- assert(constraint contains tvar.origin, err("unconstrained type var", tvar.origin))
- if (isCommittable) {
- val undetParams = uninstVars map (_.origin)
- for (param <- constraint.domainParams)
- assert(undetParams contains param, err("junk constraint on", param))
- }
- }
-
- @elidable(elidable.FINER)
- override def checkConsistent(implicit ctx: Context): Unit = checkConsistent(_.show)
-
- @elidable(elidable.FINER)
- override def enableChecking(b: Boolean) = {
- val prev = checkingEnabled
- checkingEnabled = b
- prev
- }
-
- override def withCheckingDisabled[T](op: => T)(implicit ctx: Context): T = {
- val prev = enableChecking(false)
- var thrown = false
- try op
- catch {
- case ex: Throwable =>
- thrown = true
- throw ex
- }
- finally {
- enableChecking(prev)
- if (!thrown) checkConsistent
- }
- }
-
- override def toText(printer: Printer): Text = {
- val header: Text = "Typer state:"
- val uninstVarsText =
- " uninstVars: " ~
- Text(uninstVars map (_.toText(printer)), ", ") ~ "."
- val constrainedText =
- " constrained types: " ~ constraint.constrainedTypesText(printer) ~ "."
- val constraintText =
- " constraint: " ~ constraint.constraintText(3, printer)
- Text.lines(List(header, uninstVarsText, constrainedText, constraintText))
- }
+ override def toText(printer: Printer): Text = constraint.toText(printer)
}
object MutableTyperState {