question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`asInstanceOf` causes type mismatch error when used in method argument position

See original GitHub issue

Compiler 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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
liufengyuncommented, Jun 12, 2021

This is a bug in the compiler, as the following works:

object O {

  class CA[A](var x: A)
  type C = CA[_]

  val c: CA[_] = CA(5)
  def f[A](r: CA[A]): A = r.x

  def g(): CA[_] = CA("hello")

  f(g())
  f(c.asInstanceOf[CA[_]])
}

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 in captureWildcards:

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:

--- a/compiler/src/dotty/tools/dotc/typer/Inferencing.scala
+++ b/compiler/src/dotty/tools/dotc/typer/Inferencing.scala
@@ -503,7 +503,7 @@ object Inferencing {
     *  $i is a skolem of type `scala.internal.TypeBox`, and `CAP` is its
     *  type member. See the documentation of `TypeBox` for a rationale why we do this.
     */
-  def captureWildcards(tp: Type)(using Context): Type = tp match {
+  def captureWildcards(tp: Type)(using Context): Type = tp.dealias match {
     case tp @ AppliedType(tycon, args) if tp.hasWildcardArg =>
1reaction
japgollycommented, Jun 12, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found