Skip to content
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

Java support for variance on type parameters #3196

Open
robin-aws opened this issue Dec 15, 2022 · 0 comments
Open

Java support for variance on type parameters #3196

robin-aws opened this issue Dec 15, 2022 · 0 comments
Labels
kind: bug Crashes, unsoundness, incorrect output, etc. If possible, add a `part:` label

Comments

@robin-aws
Copy link
Member

Dafny version

master, not yet released (edab6cc)

Code to produce this issue

module Repro {

    trait Foo {}
    class Bar extends Foo {
        constructor() {}
    }

    datatype Option<+T> = Some(value: T) | None
    datatype D = D(Option<Foo>)

    method Main() {
        var b: Bar := new Bar();
        var d := D(Some(b));
    }
}

Command to run and resulting output

% dafny run -t:java src/Scratch.dfy

Dafny program verifier finished with 1 verified, 0 errors
/Users/salkeldr/Documents/GitHub/libraries/src/Scratch-java/Repro_Compile/__default.java:17: error: incompatible types: Option<Bar> cannot be converted to Option<Foo>
    _1_d = Repro_Compile.D.create(Repro_Compile.Option.<Bar>create_Some(_0_b));
                                                                       ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
Error while compiling Java files. Process exited with exit code 1

What happened?

Root case is that the D data constructor creation method D.create has the following signature:

public static D create(Option<Foo> _a0) {
    return new D(_a0);
}

But given Option's type parameter is covariant, it should be:

public static D create(Option<? extends Foo> _a0) {
    return new D(_a0);
}

Or perhaps the intent was to insert an explicit unchecked (Option<Foo>) cast before passing the Option<Bar> value to the constructor.

This works correctly if the data constructor parameter is a seq<T> instead, which is also implicitly covariant.

@RustanLeino This looks like a missing case from #3072?

What type of operating system are you experiencing the problem on?

Mac

@robin-aws robin-aws added the kind: bug Crashes, unsoundness, incorrect output, etc. If possible, add a `part:` label label Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug Crashes, unsoundness, incorrect output, etc. If possible, add a `part:` label
Projects
None yet
Development

No branches or pull requests

1 participant