-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for traits as type arguments with variance on datatypes…
… in Java (#3072) This PR removes that pesky "not supported" error for Java where it wouldn't allow a `trait` to be passed in as a variant type parameter of a (co)datatype. Fixes #2013 The solution is pretty simple. Basically, it just emits casts when needed. To be accepted by Java's static type checker, these casts first upcast to `Object` and then downcast to the desired type. Since the JVM doesn't keep track of type parameters, it'll never know what hit it. (In particular, this means that we don't need the expensive `DowncastClone` mechanism that we're using for C# to satisfy .NET's more informed runtime.) To make it work, this PR also makes more precise the `IsTargetSupertype` method for Java. In particular, to check if `A<X, Y>` is a supertype of `B<U, V, W>`, first keep converting the latter up the `trait` parents of `B` until it gets to `A` (if ever). This is already done for all the compilers. Call the result `A<X', Y'>`. For the other compilers, one would now start to see if the parameters `X, Y` are target supertypes of `X', Y'` with variance in mind (for example, if the first type parameter is contravariant, we would actually check if `X'` is a supertype of `X`). For Java, however, we continue with variance in mind only if `A` is a collection type (because those already have run-time support for variance, using Java's bounded existential types). For non-collection types, we instead check if `X, Y` are target-type equal to `X', Y'`. If they are not, the compiler calls `EmitDowncast`. (To read these in the code, look at `EmitDowncastIfNecessary` in `SinglePassCompiler.cs`.) We could have used simple casts like this for collection types, too. But since I already have those, I left those alone. <small>By submitting this pull request, I confirm that my contribution is made under the terms of the [MIT license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small> Co-authored-by: Fabio Madge <[email protected]>
- Loading branch information
1 parent
29f7e3a
commit 50c2c99
Showing
17 changed files
with
385 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.