Skip to content

Avoid java compilation error when user code defines Get in a codatatype #6054

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions Source/DafnyCore/Backends/Java/JavaCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public JavaCodeGenerator(DafnyOptions options, ErrorReporter reporter) : base(op
const string DafnyMultiArrayClassPrefix = "dafny.Array";
const string DafnyTupleClassPrefix = "dafny.Tuple";

// Reserved name for Get method in co-datatypes. Starts with _ to avoid name clash
// with user-defined method
const string CoDatatypeGet = "_Get";

string DafnyMultiArrayClass(int dim) => DafnyMultiArrayClassPrefix + dim;
string DafnyTupleClass(int size) => DafnyTupleClassPrefix + size;

Expand Down Expand Up @@ -1995,7 +1999,7 @@ IClassWriter CompileDatatypeBase(DatatypeDecl dt, ConcreteSyntaxTree wr) {
}
}
if (dt is CoDatatypeDecl) {
wr.WriteLine($"public abstract {DtT_protected} Get();");
wr.WriteLine($"public abstract {DtT_protected} {CoDatatypeGet}();");
}
if (dt.HasFinitePossibleValues) {
Contract.Assert(dt.TypeArgs.Count == 0);
Expand Down Expand Up @@ -2029,7 +2033,7 @@ IClassWriter CompileDatatypeBase(DatatypeDecl dt, ConcreteSyntaxTree wr) {
if (dt.IsRecordType) {
wDtor.WriteLine($"return this.{FieldName(arg, 0)};");
} else {
wDtor.WriteLine("{0} d = this{1};", DtT_protected, dt is CoDatatypeDecl ? ".Get()" : "");
wDtor.WriteLine("{0} d = this{1};", DtT_protected, dt is CoDatatypeDecl ? "." + CoDatatypeGet + "()" : "");
var compiledConstructorsProcessed = 0;
for (var i = 0; i < dtor.EnclosingCtors.Count; i++) {
var ctor_i = dtor.EnclosingCtors[i];
Expand Down Expand Up @@ -2095,8 +2099,8 @@ void CompileDatatypeConstructors(DatatypeDecl dt, ConcreteSyntaxTree wrx) {
var wCtorBody = w.NewBlock($"public {dt.GetCompileName(Options)}__Lazy({wCtorParams}{sep}Computer c)");
wCtorBody.WriteLine($"super({wBaseCallArguments});");
wCtorBody.WriteLine("this.c = c;");
w.WriteLine($"public {dt.GetCompileName(Options)}{typeParams} Get() {{ if (c != null) {{ d = c.run(); c = null; }} return d; }}");
w.WriteLine("public String toString() { return Get().toString(); }");
w.WriteLine($"public {dt.GetCompileName(Options)}{typeParams} {CoDatatypeGet}() {{ if (c != null) {{ d = c.run(); c = null; }} return d; }}");
w.WriteLine("public String toString() { return " + CoDatatypeGet + "().toString(); }");
}
}

Expand Down Expand Up @@ -2137,7 +2141,7 @@ void DatatypeFieldsAndConstructor(DatatypeCtor ctor, int constructorIndex, Concr
}
if (dt is CoDatatypeDecl) {
string typeParams = TypeParameters(dt.TypeArgs);
wr.WriteLine($"public {dt.GetCompileName(Options)}{typeParams} Get() {{ return this; }}");
wr.WriteLine($"public {dt.GetCompileName(Options)}{typeParams} {CoDatatypeGet}() {{ return this; }}");
}
// Equals method
wr.WriteLine();
Expand Down Expand Up @@ -3352,7 +3356,7 @@ protected override void EmitDestructor(Action<ConcreteSyntaxTree> source, Formal
}
wr.Write("(({0})", DtCtorName(ctor, getTypeArgs(), wr));
source(wr);
wr.Write("{0}).{1}", ctor.EnclosingDatatype is CoDatatypeDecl ? ".Get()" : "", dtorName);
wr.Write("{0}).{1}", ctor.EnclosingDatatype is CoDatatypeDecl ? "." + CoDatatypeGet + "()" : "", dtorName);
}

private void CreateLambdaFunctionInterface(int i, ConcreteSyntaxTree outputWr) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %testDafnyForEachCompiler --refresh-exit-code=0 "%s"

codatatype NameclashCo = CoCtor(x: int)
{
method Get() returns (u: int) { return 79; }
}


method Main() {
print "Hello!\n";
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello!
1 change: 1 addition & 0 deletions docs/news/fix.4153
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid name clashes when compiling a codatatype with a Get method in Java
Loading