Skip to content

Commit 9b96317

Browse files
committed
Further improve comments around GadtConstraint
1 parent c9b093b commit 9b96317

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ sealed abstract class GadtConstraint extends Showable {
1919

2020
/** Full bounds of `sym`, including TypeRefs to other lower/upper symbols.
2121
*
22-
* Note that underlying operations perform subtype checks - for this reason, recursing on `fullBounds`
23-
* of some symbol when comparing types might lead to infinite recursion. Consider `bounds` instead.
22+
* @note this performs subtype checks between ordered symbols.
23+
* Using this in isSubType can lead to infinite recursion. Consider `bounds` instead.
2424
*/
2525
def fullBounds(sym: Symbol)(implicit ctx: Context): TypeBounds
2626

2727
/** Is `sym1` ordered to be less than `sym2`? */
2828
def isLess(sym1: Symbol, sym2: Symbol)(implicit ctx: Context): Boolean
2929

30-
/** Add symbols to constraint, preserving the underlying bounds and handling inter-dependencies. */
30+
/** Add symbols to constraint, correctly handling inter-dependencies.
31+
*
32+
* @see [[ConstraintHandling.addToConstraint]]
33+
*/
3134
def addToConstraint(syms: List[Symbol])(implicit ctx: Context): Boolean
3235
def addToConstraint(sym: Symbol)(implicit ctx: Context): Boolean = addToConstraint(sym :: Nil)
3336

@@ -36,8 +39,7 @@ sealed abstract class GadtConstraint extends Showable {
3639

3740
/** Is the symbol registered in the constraint?
3841
*
39-
* Note that this is returns `true` even if `sym` is already instantiated to some type,
40-
* unlike [[Constraint.contains]].
42+
* @note this is true even if the symbol is constrained to be equal to another type, unlike [[Constraint.contains]].
4143
*/
4244
def contains(sym: Symbol)(implicit ctx: Context): Boolean
4345

@@ -81,8 +83,9 @@ final class ProperGadtConstraint private(
8183

8284
val poly1 = PolyType(params.map { sym => DepParamName.fresh(sym.name.toTypeName) })(
8385
pt => params.map { param =>
84-
// replace the symbols in bound type `tp` which are in dependent positions
85-
// with their internal TypeParamRefs
86+
// In bound type `tp`, replace the symbols in dependent positions with their internal TypeParamRefs.
87+
// The replaced symbols will be later picked up in `ConstraintHandling#addToConstraint`
88+
// and used as orderings.
8689
def substDependentSyms(tp: Type, isUpper: Boolean)(implicit ctx: Context): Type = {
8790
def loop(tp: Type) = substDependentSyms(tp, isUpper)
8891
tp match {
@@ -119,7 +122,7 @@ final class ProperGadtConstraint private(
119122
tv
120123
}
121124

122-
// the replaced symbols will be stripped off the bounds by `addToConstraint` and used as orderings
125+
// The replaced symbols are picked up here.
123126
addToConstraint(poly1, tvars).reporting({ _ =>
124127
i"added to constraint: $params%, %\n$debugBoundsDescription"
125128
}, gadts)

compiler/src/dotty/tools/dotc/typer/Inferencing.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,16 @@ object Inferencing {
301301
if (bounds.hi <:< bounds.lo || bounds.hi.classSymbol.is(Final) || fromScala2x)
302302
tvar.instantiate(fromBelow = false)
303303
else {
304-
// since the symbols we're creating may have inter-dependencies in their bounds,
305-
// we add them to the GADT constraint later, simultaneously
304+
// We do not add the created symbols to GADT constraint immediately, since they may have inter-dependencies.
305+
// Instead, we simultaneously add them later on.
306306
val wildCard = ctx.newPatternBoundSymbol(UniqueName.fresh(tvar.origin.paramName), bounds, span, addToGadt = false)
307307
tvar.instantiateWith(wildCard.typeRef)
308308
patternBound += wildCard
309309
}
310310
}
311311
}
312312
val res = patternBound.toList
313+
// We add the created symbols to GADT constraint here.
313314
if (res.nonEmpty) ctx.gadt.addToConstraint(res)
314315
res
315316
}

0 commit comments

Comments
 (0)