-
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
Superfluous asInstanceOf breaking singleton types #11220
Comments
Indeed, if I comment out the usage of |
@Ang9876 thanks for opening the issue! Could you also upload the traces that you've shown me to Github Gist or something similar? They will be very useful for solving the issue. @smarter I already took a look at this code in more detail (thanks to @Ang9876's traces). IIRC, it indeed looked like what needed to be changed was the place in which we looked up GADT constraints, because we looked at them before looking at the "normal" bounds (where normal == not refined by GADTs). |
It seems wrong that GADT lookup would break singleton types in any case. So am I guessing correctly that pushing the problem into I've also had |
The problem is that when we conclude that a subtype check succeeded by looking up GADT constraints, we add a type ascription to, I guess, satisfy the JVM. In this particular case, we look up GADTs too early, and using GADTs we conclude something we could've concluded w/o using them. So it seems to me that checking GADTs "later" should be considered a "definitive" solution. |
@abgruszecki But what happens when the GADT reasoning is needed, and the singleton type is also needed? For instance, imagine if we were calling some method |
Ah, right, it's this case you're talking about. I think what could work is to instead cast to singleton type, and then in the backend emit a JVM cast to the type we originally applied to .asInstanceOf. We should keep that issue in mind when fixing this. |
These are the traces for two similar test cases. I am not sure whether they are useful now. |
- Register class type parameters from outer contexts in typedDefdef. - Remove a condition to support gadt approximation for class type parameters. - Add test cases in tests/pos/class-gadt. - Ignore test cases tests/pos/i4345.scala and i5735.scala (issue scala#11220 and scala#11221)
- keep the singleton type in the cast target - assert cast target stability if original was stable - fix isStable test for TypeParamRefs - double check if GADT logic is really needed before inserting a cast Fixes scala#11220 Fixes scala#11955
Compiler version
Scala compiler version 3.0.0-M3
Minimized code
This code is inspired by
tests/pos/i4345.scala
.Output
Result after
typer
Expectation
This code should be compiled but not.
c
is converted toc.$asInstanceOf$[A]
in methodadapt
when the compilertestSubType
(c:A)
andA
.testSubType
returnsOKwithGADTused
, but GADT usage inthirdTryNamed
is unnecessary.https://github.com/lampepfl/dotty/blob/a3b7ed601061e3a309fa23f09a740b7759c61bf6/compiler/src/dotty/tools/dotc/typer/Typer.scala#L3418
The text was updated successfully, but these errors were encountered: