`asInstanceOf` causes type mismatch error when used in method argument position
See original GitHub issueCompiler version
3.0.0
Minimized code
object X {
class CA[A]
type C = CA[_]
val c: C = ???
def f[A](r: CA[A]) = ()
// works
f(c)
// works
val x = c.asInstanceOf[C]
f(x)
// error
f(c.asInstanceOf[C])
// works
f(c.asInstanceOf[c.type])
}
Output
[error] -- [E007] Type Mismatch Error: a.scala:16:18
[error] 16 | f(c.asInstanceOf[C])
[error] | ^^^^^^^^^^^^^^^^^
[error] | Found: X.C
[error] | Required: X.CA[A]
[error] |
[error] | where: A is a type variable
[error] one error found
Expectation
No error.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Strange type mismatch error - scala - Stack Overflow
Short answer is: instead of errorFixed.cellValueFactory = features => ReadOnlyBooleanWrapper(features.value.fixed). you should use
Read more >Unreducible application of HKT in alias to F[F[?]] #11889
japgolly mentioned this issue on Jun 11, 2021. asInstanceOf causes type mismatch error when used in method argument position #12739.
Read more >[Solved]-Type mismatch in parameterized class - but why?-scala
Coding example for the question Type mismatch in parameterized class - but why? ... asInstanceOf[AnyRef]) <console>:9: error: type mismatch; found : AnyRef ...
Read more >Type mismatch on: <name> - HCL Product Documentation
The following conditions could have caused this error. You tried to pass an argument to a sub or function by reference, but the...
Read more >Too specific an object for first argument of foldLeft - Scala Users
I have a call to foldLeft, where the 2nd argument is a function (Bdd,Int)=>Bdd, ... Error:(14, 78) type mismatch; found : Bdd required:...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is a bug in the compiler, as the following works:
We have two places where capture conversion kicks in. One is in typer that handles top-level wildcards. One is in TypeComparer which only works for a stable path.
The reason why the first mechanism does not work for
f(c.asInstanceOf[C])
is that we forget to dealias incaptureWildcards
:https://github.com/lampepfl/dotty/blob/fcd837addc5b466b055da069960e48c5d4d5c1dc/compiler/src/dotty/tools/dotc/typer/Inferencing.scala#L506-L530
With the following change, the original code compiles:
This isn’t the first time I’ve reported simple substitution not holding (see here and here). Issues like this are 100% going to be considered Scala 3 puzzlers, which is such a shame after such great work was done to close out so many Scala 2 puzzlers and quirks.