-
Notifications
You must be signed in to change notification settings - Fork 137
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
Disallow covariant subtyping in checker #2885
Comments
@SupunS Need a bit of help. I looked into the second part of the issue. Basically the following:
You are right that it is being allowed in the checker. The code: Lines 6312 to 6313 in 323b887
I have superficial understanding of this but it doesn't seem like a bug and more like a choice. Do we really want to disable covariant subtyping of parameterized types? It might also affect other existing parameterized types. I am wondering if we can rethink if it is possible to support the covariant sub-typing with InclusiveRange as well. The main constraint we need is that the static type of Else if we still want to disallow, then may be we need a hard-coded check for InclusiveRange for
cc: @turbolent |
Yes agreed, that it's a choice we made. The problem in this case is that covariance for I think there were a few reasons not to allow covariance, like for example, the runtime needed to check the types (do we do that already?), etc. I don't remember all the details / other reasons, need to dig up the previous discussions.
Don't think we can enforce this statically. In the example: let a: Integer = 0
let b: Integer = Int8(10)
var range: InclusiveRange<Integer> = InclusiveRange(a, b) The runtime type of |
Given that type parameters can still only be defined for built-in types, would it help add a notion of variance to type parameters? Something along the lines of e.g. Scala |
We check the equality of static types of cadence/runtime/stdlib/range.go Lines 95 to 106 in 7ab492f
Wouldn't |
Ah, there is a bit of confusion. When I said "can never be guaranteed statically", I meant during the checking time. i.e: just by using the static analysis of the program, without executing it. |
So yeah, given the run-time/interpreter already handles it, we can allow it in the checker as well. But a couple of things to be mindful here is that:
|
okay, then let me look into the variance idea. It is definitely desirable to catch errors in the checker if possible. |
@turbolent @SupunS I looked into the variance idea. IIUC, if we make
But I don't think it'll help with the following:
Here on both the sides, we have What we need is the ability to specify an exclusive type bound. Something that doesn't seem to be commonly supported. Scala does seem to have some workaround: https://stackoverflow.com/questions/48590195/in-scala-how-do-i-define-upper-type-bounds-that-are-exclusive-of-the-defined-cla Even with support for exclusive type bound, we would still need to disallow I believe a solution might be to just disallow creation of Lines 5402 to 5407 in 7ab492f
Something like below
|
Sorry, I missed your last suggestion. I like the last solution. i.e:
I think this requirement exists (at least currently) only for the |
Issue to be solved
Came out of: https://github.com/onflow/cadence/pull/2523/files#r1361276644
The text was updated successfully, but these errors were encountered: