-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Union of literal types is not inferred correctly #22219
Comments
The problem persists into 3.6.2. The issue appears to specifically affect unions of singleton types from literals + occuring inside a pair type. Other union types will be inferred correctly: trait Cell[+T]:
def foreach[U](f: T => U): U
trait A
trait B extends A
trait C extends A
trait D extends A
type Union1 = B | C | D
object X
object Y
object Z
type Union2 = X.type | Y.type | Z.type
type Union3 = "1" | "2" | "3"
trait Test:
val cell1: Cell[(String,Union1)]
val cell2: Cell[(String,Union2)]
val cell3: Cell[(String,Union3)]
val cell4: Cell[Union3]
def test =
cell1.foreach { (s,u) => summon[u.type <:< Union1] } // ok
cell2.foreach { (s,u) => summon[u.type <:< Union2] } // ok
cell2.foreach { p => val u = p._2; summon[u.type <:< Union2] } // ok
cell3.foreach { (s,u) => summon[u.type <:< Union3] } // error
cell3.foreach { p => val u = p._2; summon[u.type <:< Union3] } // error
cell4.foreach { u => summon[u.type <:< Union3] } // ok (!) |
Spree results: List[(String, MonthNumber)](
"January" -> 1,
"February" -> 2,
"March" -> 3,
"April" -> 4,
"May" -> 5,
"June"-> 6,
"July" -> 7,
"August" -> 8,
"September" -> 9,
"October" -> 10,
"November" -> 11,
"December" -> 12
).foreach { (name, number) =>
summon[number.type <:< MonthNumber]
} desugars into: .foreach(x$1: (String, MonthNumber) => {
val name = x$1._1
val number = x$1._2
summon[number.type <:< MonthNumber]
}) This snippet can then be minimized to: type MonthNumber = 1 | 2
val x: MonthNumber = 1
val y = x I don't know typer that much, but I suppose this is some kind of simplification during type inference. The place where union type is widened to Int is scala3/compiler/src/dotty/tools/dotc/typer/Namer.scala Lines 2153 to 2154 in c5bf0e0
If someone knows whether this behaviour is intended and can elaborate I'd be glad. |
The culprit is |
Compiler version
3.3.4
Minimized code
Output
Expectation
Compiles
The text was updated successfully, but these errors were encountered: