Skip to content

Commit 1ef5cfd

Browse files
committed
[CI Only] Check approach
1 parent d30c925 commit 1ef5cfd

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -874,10 +874,7 @@ object Contexts {
874874
boundCache = boundCache.updated(sym, bounds)
875875
bounds
876876
}
877-
).reporting({ res =>
878-
// i"gadt bounds $sym: $res"
879-
""
880-
}, gadts)
877+
)// .reporting({ res => i"gadt bounds $sym: $res" }, gadts)
881878
}
882879
}
883880

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

+16-7
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
148148

149149
def isSubType(tp1: Type, tp2: Type)(implicit nc: AbsentContext): Boolean = isSubType(tp1, tp2, NoApprox)
150150

151-
protected def recur(tp1: Type, tp2: Type): Boolean = trace(s"isSubType ${traceInfo(tp1, tp2)} $approx", subtyping) {
151+
protected def frozenDescr = if (frozenConstraint) " frozen" else ""
152+
protected def recur(tp1: Type, tp2: Type): Boolean = trace(s"isSubType ${traceInfo(tp1, tp2)} $approx$frozenDescr", subtyping) {
152153

153154
def monitoredIsSubType = {
154155
if (pendingSubTypes == null) {
@@ -541,11 +542,18 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
541542
}
542543
compareTypeLambda
543544
case OrType(tp21, tp22) =>
544-
val tp1a = tp1.widenDealiasKeepRefiningAnnots
545-
if (tp1a ne tp1)
546-
// Follow the alias; this might avoid truncating the search space in the either below
547-
// Note that it's safe to widen here because singleton types cannot be part of `|`.
548-
return recur(tp1a, tp2)
545+
def isSingletonlessTypeUnion(tp: Type): Boolean = tp match {
546+
case OrType(a, b) => isSingletonlessTypeUnion(a) && isSingletonlessTypeUnion(b)
547+
case _ => !tp.isSingleton
548+
}
549+
if (isSingletonlessTypeUnion(tp21) && isSingletonlessTypeUnion(tp22)) {
550+
val tp1a = tp1.widenDealiasKeepRefiningAnnots
551+
if (tp1a ne tp1)
552+
// Follow the alias; this might avoid truncating the search space in the either below
553+
// Note that it's safe to widen here because we ensured that
554+
// singleton types are not top-level members of tp2
555+
return recur(tp1a, tp2)
556+
}
549557

550558
// Rewrite T1 <: (T211 & T212) | T22 to T1 <: (T211 | T22) and T1 <: (T212 | T22)
551559
// and analogously for T1 <: T21 | (T221 & T222)
@@ -1238,7 +1246,8 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
12381246
* Test that the resulting bounds are still satisfiable.
12391247
*/
12401248
private def narrowGADTBounds(tr: NamedType, bound: Type, approx: ApproxState, isUpper: Boolean): Boolean = {
1241-
val boundImprecise = if (isUpper) approx.high else approx.low
1249+
val boundImprecise = approx.high || approx.low
1250+
// val boundImprecise = if (isUpper) approx.high else approx.low
12421251
ctx.mode.is(Mode.GADTflexible) && !frozenConstraint && !boundImprecise && {
12431252
val tparam = tr.symbol
12441253
gadts.println(i"narrow gadt bound of $tparam: ${tparam.info} from ${if (isUpper) "above" else "below"} to $bound ${bound.toString} ${bound.isRef(tparam)}")

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

+35-11
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,11 @@ class Typer extends Namer
10251025

10261026
/** gadtSyms = "all type parameters of enclosing methods appearing in selector type" */
10271027
def gadtSyms(selType: Type)(implicit ctx: Context): Set[Symbol] = trace(i"GADT syms of $selType", gadts) {
1028+
// def enclosingRealMethod(sym: Symbol): Symbol =
1029+
// if (sym.isRealMethod) sym else if (sym.exists) enclosingRealMethod(sym) else NoSymbol
1030+
// val erm = enclosingRealMethod(ctx.owner)
1031+
// ctx.tree
1032+
// println(i"gadtSyms: owner=${ctx.owner} erm=$erm info=${erm.initial} mbr=${erm.info.allMembers} candidate=${erm.termRef.widenTermRefExpr.asInstanceOf[PolyType].paramInfoss}%, %")
10281033
val accu = new TypeAccumulator[Set[Symbol]] {
10291034
def apply(tsyms: Set[Symbol], t: Type): Set[Symbol] = {
10301035
val tsyms1 = t match {
@@ -1520,19 +1525,38 @@ class Typer extends Namer
15201525
if (sym is Implicit) checkImplicitConversionDefOK(sym)
15211526
val tpt1 = checkSimpleKinded(typedType(tpt))
15221527

1523-
var rhsCtx = ctx
1524-
if (sym.isConstructor && !sym.isPrimaryConstructor && tparams1.nonEmpty) {
1525-
// for secondary constructors we need a context that "knows"
1526-
// that their type parameters are aliases of the class type parameters.
1527-
// See pos/i941.scala
1528-
rhsCtx = ctx.fresh.setFreshGADTBounds
1529-
(tparams1, sym.owner.typeParams).zipped.foreach { (tdef, tparam) =>
1530-
val tr = tparam.typeRef
1531-
rhsCtx.gadt.addBound(tdef.symbol, tr, isUpper = false)
1532-
rhsCtx.gadt.addBound(tdef.symbol, tr, isUpper = true)
1528+
val rhsCtx: Context = {
1529+
var _result: FreshContext = null
1530+
def resultCtx(): FreshContext = {
1531+
if (_result == null) _result = ctx.fresh
1532+
_result
15331533
}
1534+
1535+
if (tparams1.nonEmpty) {
1536+
resultCtx().setFreshGADTBounds
1537+
if (!sym.isConstructor) {
1538+
// if we're _not_ in a constructor, allow constraining type parameters
1539+
tparams1.foreach { tdef =>
1540+
val TypeBounds(lo, hi) = tdef.symbol.info.bounds
1541+
resultCtx().gadt.addBound(tdef.symbol, lo, isUpper = false)
1542+
resultCtx().gadt.addBound(tdef.symbol, hi, isUpper = true)
1543+
}
1544+
} else if (!sym.isPrimaryConstructor) {
1545+
// otherwise, for secondary constructors we need a context that "knows"
1546+
// that their type parameters are aliases of the class type parameters.
1547+
// See pos/i941.scala
1548+
(tparams1, sym.owner.typeParams).zipped.foreach { (tdef, tparam) =>
1549+
val tr = tparam.typeRef
1550+
resultCtx().gadt.addBound(tdef.symbol, tr, isUpper = false)
1551+
resultCtx().gadt.addBound(tdef.symbol, tr, isUpper = true)
1552+
}
1553+
}
1554+
}
1555+
1556+
if (sym.isInlineMethod) resultCtx().addMode(Mode.InlineableBody)
1557+
1558+
if (_result ne null) _result else ctx
15341559
}
1535-
if (sym.isInlineMethod) rhsCtx = rhsCtx.addMode(Mode.InlineableBody)
15361560
val rhs1 = typedExpr(ddef.rhs, tpt1.tpe)(rhsCtx)
15371561

15381562
if (sym.isInlineMethod) PrepareInlineable.registerInlineInfo(sym, ddef.rhs, _ => rhs1)

tests/pos/candidate.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object buffer2 {
2+
var Some(z) = Some(0)
3+
}

0 commit comments

Comments
 (0)