-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Wildcard types are not properly inferred in pattern matching #13998
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
Comments
Got a little worried, perhaps just paranoid, of the use of Array, so I defined my mini array type, and dropped the second pattern to minimise it a little more, and got it to yield (under scala> case class Arr[A](head: A)
// defined case class Arr
scala> val at: Arr[_ <: String] = Arr("text")
val at: Arr[? <: String] = Arr(text)
scala> at match { case Arr(text) => text: String }
-- [E007] Type Mismatch Error: -------------------------------------------------
1 |at match { case Arr(text) => text: String }
| ^^^^
| Found: (text : A$1)
| Required: String
Explanation
===========
Tree: text
I tried to show that
(text : A$1)
conforms to
String
but the comparison trace ended with `false`:
==> (text : A$1) <: String
==> (text : A$1) <: String (recurring)
==> (text : A$1) <: String (recurring)
==> A$1 <: String (left is approximated)
==> A$1 <: String (recurring)
==> Any <: String (left is approximated)
==> Any <: String (recurring)
<== Any <: String (recurring) = false
<== Any <: String (left is approximated) = false
==> Any <: String in frozen constraint
==> Any <: String (recurring) in frozen constraint
<== Any <: String (recurring) in frozen constraint = false
<== Any <: String in frozen constraint = false
<== A$1 <: String (recurring) = false
<== A$1 <: String (left is approximated) = false
<== (text : A$1) <: String (recurring) = false
<== (text : A$1) <: String (recurring) = false
<== (text : A$1) <: String = false
The tests were made under the empty constraint
1 error found Looks to me like the type variable |
Looks even worst with a covariant type, as the binding is already typed as Any: scala> case class Arr2[+A](head: A)
// defined case class Arr2
scala> val at2: Arr2[_ <: String] = Arr2("text")
val at2: Arr2[? <: String] = Arr2(text)
scala> at2 match { case Arr2(text) => text: String }
-- [E007] Type Mismatch Error: -------------------------------------------------
1 |at2 match { case Arr2(text) => text: String }
| ^^^^
| Found: (text : Any)
| Required: String
Explanation
===========
Tree: text
I tried to show that
(text : Any)
conforms to
String
but the comparison trace ended with `false`:
==> (text : Any) <: String
==> (text : Any) <: String (recurring)
==> (text : Any) <: String (recurring)
==> Any <: String (left is approximated)
==> Any <: String (recurring)
<== Any <: String (recurring) = false
<== Any <: String (left is approximated) = false
<== (text : Any) <: String (recurring) = false
<== (text : Any) <: String (recurring) = false
<== (text : Any) <: String = false
The tests were made under the empty constraint
1 error found |
Dang. This is a pretty fundamental bug. (And, it works in Scala 2.) |
Uh oh!
There was an error while loading. Please reload this page.
Compiler version
3.1.0
Minimized code
Output
It does not compile
Expectation
It should compile
The text was updated successfully, but these errors were encountered: