Skip to content

Commit 8e2221d

Browse files
committed
Don't widen in lubs
Widen in lubs only if - the type is unstable, or - we are after erasure This need to be complemented by performance work as otherwise complexity of subtype checking will explode.
1 parent 399fc02 commit 8e2221d

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,8 +1530,10 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
15301530
val t2 = mergeIfSuper(tp2, tp1, canConstrain)
15311531
if (t2.exists) t2
15321532
else {
1533-
val tp1w = tp1.widen
1534-
val tp2w = tp2.widen
1533+
def widen(tp: Type) =
1534+
if (ctx.scala2Mode || ctx.erasedTypes) tp.widen else tp.widenIfUnstable
1535+
val tp1w = widen(tp1)
1536+
val tp2w = widen(tp2)
15351537
if ((tp1 ne tp1w) || (tp2 ne tp2w)) lub(tp1w, tp2w)
15361538
else orType(tp1w, tp2w) // no need to check subtypes again
15371539
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -827,11 +827,6 @@ trait Checking {
827827
}
828828
else tpt
829829

830-
/** Check that `tpt` does not refer to a singleton type */
831-
def checkNotSingleton(tpt: Tree, where: String)(implicit ctx: Context): Tree =
832-
if (tpt.tpe.isSingleton) errorTree(tpt, ex"Singleton type ${tpt.tpe} is not allowed $where")
833-
else tpt
834-
835830
/** Verify classes extending AnyVal meet the requirements */
836831
def checkDerivedValueClass(clazz: Symbol, stats: List[Tree])(implicit ctx: Context): Unit =
837832
Checking.checkDerivedValueClass(clazz, stats)
@@ -1048,7 +1043,6 @@ trait NoChecking extends ReChecking {
10481043
override def checkNoDoubleDeclaration(cls: Symbol)(implicit ctx: Context): Unit = ()
10491044
override def checkParentCall(call: Tree, caller: ClassSymbol)(implicit ctx: Context): Unit = ()
10501045
override def checkSimpleKinded(tpt: Tree)(implicit ctx: Context): Tree = tpt
1051-
override def checkNotSingleton(tpt: Tree, where: String)(implicit ctx: Context): Tree = tpt
10521046
override def checkDerivedValueClass(clazz: Symbol, stats: List[Tree])(implicit ctx: Context): Unit = ()
10531047
override def checkTraitInheritance(parentSym: Symbol, cls: ClassSymbol, pos: SourcePosition)(implicit ctx: Context): Unit = ()
10541048
override def checkCaseInheritance(parentSym: Symbol, caseCls: ClassSymbol, pos: SourcePosition)(implicit ctx: Context): Unit = ()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,7 @@ class Typer extends Namer
13611361
checkSimpleKinded(checkNoWildcard(arg)))
13621362
else if (tpt1.symbol == defn.orType)
13631363
checkedArgs = checkedArgs.mapconserve(arg =>
1364-
checkNotSingleton(checkSimpleKinded(checkNoWildcard(arg)), "in a union type"))
1364+
checkSimpleKinded(checkNoWildcard(arg)))
13651365
assignType(cpy.AppliedTypeTree(tree)(tpt1, checkedArgs), tpt1, checkedArgs)
13661366
}
13671367
}

tests/pos/single-unions.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object A {
2+
val x1: 3 | 4 = 3
3+
val x2: 3 | 4 = 4
4+
val x3: 3 | 4 = if (???) 3 else 4
5+
}

0 commit comments

Comments
 (0)