-
Notifications
You must be signed in to change notification settings - Fork 1.1k
asInstanceOf
causes type mismatch error when used in method argument position
#12739
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
asInstanceOf
causes crashasInstanceOf
causes type mismatch error when used inline with method argument
asInstanceOf
causes type mismatch error when used inline with method argumentasInstanceOf
causes type mismatch error when used in method argument position
This has to do with the fact that
|
I don't quite understand this, as I would expect it to be compatible if Btw, in Scala 2 the 4th example is an error while the 3rd works... 😄 |
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 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 => |
This comment has been minimized.
This comment has been minimized.
Note that this isn't |
(For background on this remark Martin made, see https://docs.oracle.com/javase/specs/jls/se16/html/jls-5.html#jls-5.1.10 — it begins with formal language, but then following that is a nice informal motivation section, in the purple box.) |
Compiler version
3.0.0
Minimized code
Output
Expectation
No error.
The text was updated successfully, but these errors were encountered: