Skip to content

Commit 6ccdbd9

Browse files
authored
Split out immutable GadtConstraint (#16602)
This change eagerly allocates a GadtConstraint whenever its state is changed, rather than simply changing the reference(s) in, what's now called, `GadtConstraintHandling`, such as `constraint`. Previous to a recent change, in order to restore the GADT constraints, a fresh copy was eagerly created in a number of cases, just so it can be used to restore, for the cases in which it must be restored. The recent change exposed the underlying pieces of the GADT constraint and allowed those to be restored as components. Now we still do that, but as a packaged up GadtConstraint that we created once, eagerly. That also makes sure that invariants in components are upheld, like the mappings.
2 parents d63e572 + b1a035a commit 6ccdbd9

17 files changed

+229
-241
lines changed

compiler/src/dotty/tools/dotc/core/Constraint.scala

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ abstract class Constraint extends Showable {
7171
*/
7272
def nonParamBounds(param: TypeParamRef)(using Context): TypeBounds
7373

74+
/** The current bounds of type parameter `param` */
75+
def bounds(param: TypeParamRef)(using Context): TypeBounds
76+
7477
/** A new constraint which is derived from this constraint by adding
7578
* entries for all type parameters of `poly`.
7679
* @param tvars A list of type variables associated with the params,

compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala

+1-10
Original file line numberDiff line numberDiff line change
@@ -749,16 +749,7 @@ trait ConstraintHandling {
749749
}
750750

751751
/** The current bounds of type parameter `param` */
752-
def bounds(param: TypeParamRef)(using Context): TypeBounds = {
753-
val e = constraint.entry(param)
754-
if (e.exists) e.bounds
755-
else {
756-
// TODO: should we change the type of paramInfos to nullable?
757-
val pinfos: List[param.binder.PInfo] | Null = param.binder.paramInfos
758-
if (pinfos != null) pinfos(param.paramNum) // pinfos == null happens in pos/i536.scala
759-
else TypeBounds.empty
760-
}
761-
}
752+
def bounds(param: TypeParamRef)(using Context): TypeBounds = constraint.bounds(param)
762753

763754
/** Add type lambda `tl`, possibly with type variables `tvars`, to current constraint
764755
* and propagate all bounds.

compiler/src/dotty/tools/dotc/core/Contexts.scala

+11-10
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ object Contexts {
141141
def tree: Tree[?]
142142
def scope: Scope
143143
def typerState: TyperState
144-
def gadt: GadtConstraint
144+
def gadt: GadtConstraint = gadtState.gadt
145+
def gadtState: GadtState
145146
def searchHistory: SearchHistory
146147
def source: SourceFile
147148

@@ -410,7 +411,7 @@ object Contexts {
410411
val constrCtx = outersIterator.dropWhile(_.outer.owner == owner).next()
411412
superOrThisCallContext(owner, constrCtx.scope)
412413
.setTyperState(typerState)
413-
.setGadt(gadt)
414+
.setGadtState(gadtState)
414415
.fresh
415416
.setScope(this.scope)
416417
}
@@ -541,8 +542,8 @@ object Contexts {
541542
private var _typerState: TyperState = uninitialized
542543
final def typerState: TyperState = _typerState
543544

544-
private var _gadt: GadtConstraint = uninitialized
545-
final def gadt: GadtConstraint = _gadt
545+
private var _gadtState: GadtState = uninitialized
546+
final def gadtState: GadtState = _gadtState
546547

547548
private var _searchHistory: SearchHistory = uninitialized
548549
final def searchHistory: SearchHistory = _searchHistory
@@ -567,7 +568,7 @@ object Contexts {
567568
_owner = origin.owner
568569
_tree = origin.tree
569570
_scope = origin.scope
570-
_gadt = origin.gadt
571+
_gadtState = origin.gadtState
571572
_searchHistory = origin.searchHistory
572573
_source = origin.source
573574
_moreProperties = origin.moreProperties
@@ -624,12 +625,12 @@ object Contexts {
624625
this._scope = typer.scope
625626
setTypeAssigner(typer)
626627

627-
def setGadt(gadt: GadtConstraint): this.type =
628-
util.Stats.record("Context.setGadt")
629-
this._gadt = gadt
628+
def setGadtState(gadtState: GadtState): this.type =
629+
util.Stats.record("Context.setGadtState")
630+
this._gadtState = gadtState
630631
this
631632
def setFreshGADTBounds: this.type =
632-
setGadt(gadt.fresh)
633+
setGadtState(gadtState.fresh)
633634

634635
def setSearchHistory(searchHistory: SearchHistory): this.type =
635636
util.Stats.record("Context.setSearchHistory")
@@ -721,7 +722,7 @@ object Contexts {
721722
.updated(notNullInfosLoc, Nil)
722723
.updated(compilationUnitLoc, NoCompilationUnit)
723724
c._searchHistory = new SearchRoot
724-
c._gadt = GadtConstraint.empty
725+
c._gadtState = GadtState(GadtConstraint.empty)
725726
c
726727
end FreshContext
727728

0 commit comments

Comments
 (0)