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 compiler produces constructors that won't play well with native Java code #502

Open
davidcok opened this issue Jan 14, 2020 · 7 comments
Labels
area: ffi The {:extern} attribute and otherwise interfacing with code in other languages kind: bug Crashes, unsoundness, incorrect output, etc. If possible, add a `part:` label lang: java Dafny's Java transpiler and its runtime

Comments

@davidcok
Copy link
Collaborator

davidcok commented Jan 14, 2020

The Java compiler produces wrong output regarding constructors for a current test (comp/Class.dfy). In particular the code below produces a zero-argument constructor that initializes a, b, c, but then produces a method named __ctor that is the translation of the given Dafny constructor

class MyClass {
  var a: int
  const b: int
  const c := 17
  static const d: int
  static const e := 18
  constructor (x: int) {
    a := 100 + x;
    b := 200 + x;
  }

function method F(): int { 8 }
  static function method G(): int { 9 }
  method M() returns (r: int) { r := 69; }
  static method N() returns (r: int) { return 70; }
}

method Main() {
  var m := new MyClass.MyClass(10);
  print m.a, " ", m.b, "\n";
}

@seanmcl seanmcl added kind: bug Crashes, unsoundness, incorrect output, etc. If possible, add a `part:` label lang: java Dafny's Java transpiler and its runtime labels Jan 14, 2020
@RustanLeino
Copy link
Collaborator

Yes? That is one possible way to implement object construction, right? Or, is the __ctor not called when an object is allocated?

@robin-aws
Copy link
Member

I submit that this indicates a disagreement about the compiler requirements: currently the code it outputs just has to be functionally correct, but some folks may expect that it should output "logical" or "idiomatic" code, such as directly mapping a Dafny constructor to a Java constructor.

I think this needs to written down explicitly somewhere in the source and/or the documentation.

@davidcok
Copy link
Collaborator Author

davidcok commented Jan 31, 2020 via email

@richardlford
Copy link
Collaborator

richardlford commented Jan 31, 2020 via email

@davidcok
Copy link
Collaborator Author

davidcok commented Jan 31, 2020 via email

@robin-aws
Copy link
Member

@davidcok To play devil's advocate, given your requirements would it perhaps make sense to just write Java code with JML markup? I worry that if the output Java code needs to be understood and developed like a "normal" Java code base, the benefits of using Dafny will be largely lost, because changes will have to either be back-ported to Dafny, or the connection to Dafny will ultimately have to be severed at some point.

Coming from the perspective of another [Amazon internal] project :) I would love for the compiler to produce idiomatic code, but I'm also wary of how far the current compilers are from that goal. For us your fourth requirement about having an idiomatic API is more relevant, and allows us to encapsulate the compiler-generated source and not target idiomatic output for now. I think it's fine to make requests for improvement like this but I don't think it's fair to label them as bugs.

As for the third, functionally correct code “ought” to be provable, but Java-based tools will likely be more successful if usual Java idioms are used.

I'd actually argue that using fewer constructs in a target language might make it easier for Java-based tools to successfully prove assertions. For example, if it's possible to compile a block of code without using object references somehow, you could eliminate any possibility of aliasing and probably make proofs much easier, even if the idiomatic implementation using objects is far simpler and understandable.

@acioc acioc added this to the Dafny 3.0 milestone Aug 20, 2020
@acioc acioc added the area: ffi The {:extern} attribute and otherwise interfacing with code in other languages label Aug 20, 2020
@davidcok
Copy link
Collaborator Author

Reviewed again. Current behavior works, but needs to be reevaluated for FFI. In particular

  • If the anonymous constructor is given the attribute {:extern MyClass}, the compilation fails.
  • If a Java programmer wants to use the constructor as Java: new MyClass(10), such constructor is not defined.

@davidcok davidcok changed the title Java compiler produces erroneous constructors (current tests incorrect) Java compiler produces constructors that won't play well with native Java code Aug 24, 2020
@davidcok davidcok removed their assignment Aug 24, 2020
@robin-aws robin-aws modified the milestones: Dafny 3.0, Post 3.0 Dec 2, 2020
@robin-aws robin-aws removed this from the Post 3.1 milestone Sep 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: ffi The {:extern} attribute and otherwise interfacing with code in other languages kind: bug Crashes, unsoundness, incorrect output, etc. If possible, add a `part:` label lang: java Dafny's Java transpiler and its runtime
Projects
None yet
Development

No branches or pull requests

6 participants