Skip to content

Preserve singletons in unions when they're explicitly written in the code #844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -790,9 +790,9 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
(defn.AnyType /: tps)(glb)

/** The least upper bound of two types
* @note We do not admit singleton types in or-types as lubs.
* @param keepSingletons If true, do not widen singletons when forming an OrType
*/
def lub(tp1: Type, tp2: Type): Type = /*>|>*/ ctx.traceIndented(s"lub(${tp1.show}, ${tp2.show})", subtyping, show = true) /*<|<*/ {
def lub(tp1: Type, tp2: Type, keepSingletons: Boolean = false): Type = /*>|>*/ ctx.traceIndented(s"lub(${tp1.show}, ${tp2.show}, $keepSingletons)", subtyping, show = true) /*<|<*/ {
if (tp1 eq tp2) tp1
else if (!tp1.exists) tp1
else if (!tp2.exists) tp2
Expand All @@ -805,8 +805,8 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
val t2 = mergeIfSuper(tp2, tp1)
if (t2.exists) t2
else {
val tp1w = tp1.widen
val tp2w = tp2.widen
val tp1w = if (keepSingletons) tp1.widenExpr else tp1.widen
val tp2w = if (keepSingletons) tp2.widenExpr else tp2.widen
if ((tp1 ne tp1w) || (tp2 ne tp2w)) lub(tp1w, tp2w)
else orType(tp1w, tp2w) // no need to check subtypes again
}
Expand All @@ -816,7 +816,7 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {

/** The least upper bound of a list of types */
final def lub(tps: List[Type]): Type =
(defn.NothingType /: tps)(lub)
(defn.NothingType /: tps)(lub(_, _))

/** Merge `t1` into `tp2` if t1 is a subtype of some &-summand of tp2.
*/
Expand Down Expand Up @@ -1207,9 +1207,9 @@ class ExplainingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
super.hasMatchingMember(name, tp1, tp2)
}

override def lub(tp1: Type, tp2: Type) =
traceIndented(s"lub(${show(tp1)}, ${show(tp2)})") {
super.lub(tp1, tp2)
override def lub(tp1: Type, tp2: Type, keepSingletons: Boolean = false) =
traceIndented(s"lub(${show(tp1)}, ${show(tp2)}, $keepSingletons)") {
super.lub(tp1, tp2, keepSingletons)
}

override def glb(tp1: Type, tp2: Type) =
Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
case AndType(l, r) =>
simplify(l, theMap) & simplify(r, theMap)
case OrType(l, r) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified is called in lots of places for types that are not written. So it should not do the lub with keepSingletons = true.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is OK, because if we call simplified on a type not written by a user which contains an OrType, then this OrType should have been constructed in the code by calling | or lub, so this OrType should not have a singleton in it. And if we do not keep singletons in simplified, then we will always lose them because we call simplified in Typer#adapt.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about that. Constraint handling does not do lubs. The purpose of simplify is to clean up after it. See it this way: If simplify only gets code where ortypes are computed by lubs, why would it do the lubs again?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If simplify only gets code where ortypes are computed by lubs, why would it do the lubs again?

Because we call simplify on the types inside the OrType, and the lub of the result might not be an OrType?

I am not sure about that. Constraint handling does not do lubs.

OK, so this should only have an impact when we instantiate type variables, but we already approximate unions in that case: https://github.com/lampepfl/dotty/blob/master/src/dotty/tools/dotc/core/Types.scala#L2481 isn't that enough? If it's not, we can always special case the .simplified call when instantiating type variables to not keep singletons: https://github.com/lampepfl/dotty/blob/master/src/dotty/tools/dotc/core/Types.scala#L2475

simplify(l, theMap) | simplify(r, theMap)
ctx.typeComparer.lub(simplify(l, theMap), simplify(r, theMap), keepSingletons = true)
case _ =>
(if (theMap != null) theMap else new SimplifyMap).mapOver(tp)
}
Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/typer/TypeAssigner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ trait TypeAssigner {
tree.withType(left.tpe & right.tpe)

def assignType(tree: untpd.OrTypeTree, left: Tree, right: Tree)(implicit ctx: Context) =
tree.withType(left.tpe | right.tpe)
tree.withType(ctx.typeComparer.lub(left.tpe, right.tpe, keepSingletons = true))

// RefinedTypeTree is missing, handled specially in Typer and Unpickler.

Expand Down
1 change: 1 addition & 0 deletions test/dotc/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class tests extends CompilerTest {
@Test def neg_validate = compileFile(negDir, "validate", xerrors = 18)
@Test def neg_validateParsing = compileFile(negDir, "validate-parsing", xerrors = 7)
@Test def neg_validateRefchecks = compileFile(negDir, "validate-refchecks", xerrors = 2)
@Test def neg_singletonsLubs = compileFile(negDir, "singletons-lubs", xerrors = 2)

@Test def run_all = runFiles(runDir)

Expand Down
7 changes: 7 additions & 0 deletions tests/neg/singletons-lubs.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object Test {
def oneOrTwo(x: 1 | 2): 1 | 2 = x
def test: Unit = {
val foo: 3 | 4 = 1 // error
oneOrTwo(foo) // error
}
}
11 changes: 11 additions & 0 deletions tests/pos/singletons-lubs.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
object Test {
def oneOrTwo(x: 1 | 2): 1 | 2 = x
def test: Unit = {
val foo: 1 | 2 = 1
oneOrTwo(oneOrTwo(foo))
1 match {
case x: (1 | 2) => oneOrTwo(x)
//case x @ (1 | 2) => oneOrTwo(x) // disallowed to avoid deep subtyping checks
}
}
}