Skip to content

Commit

Permalink
Fix bad generated Java code when using --legacy-data-constructors (#5657
Browse files Browse the repository at this point in the history
)

### Description
- Fix bad generated Java code when using --legacy-data-constructors

### How has this been tested?
- Updated existing test

<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>
  • Loading branch information
keyboardDrummer authored Jul 31, 2024
1 parent 36691ee commit 8600061
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
24 changes: 15 additions & 9 deletions Source/DafnyCore/Backends/Java/JavaCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,18 @@ protected override void DeclareSubsetType(SubsetTypeDecl sst, ConcreteSyntaxTree
}
cw.StaticMemberWriter.Write($"public static {TypeParameters(sst.TypeArgs, " ")}{typeName} defaultValue(");
var typeDescriptorParams = sst.TypeArgs.Where(NeedsTypeDescriptor);
cw.StaticMemberWriter.Write(typeDescriptorParams.Comma(tp =>
$"{DafnyTypeDescriptor}<{tp.GetCompileName(Options)}> {FormatTypeDescriptorVariable(tp.GetCompileName(Options))}"));
cw.StaticMemberWriter.Write(typeDescriptorParams.Comma(TypeDescriptorVariableDeclaration));
var w = cw.StaticMemberWriter.NewBlock(")");
w.WriteLine($"return {witness};");
}

GenerateIsMethod(sst, cw.StaticMemberWriter);
}

private string TypeDescriptorVariableDeclaration(TypeParameter tp) {
return $"{DafnyTypeDescriptor}<{tp.GetCompileName(Options)}> {FormatTypeDescriptorVariable(tp.GetCompileName(Options))}";
}

protected class ClassWriter : IClassWriter {
public readonly JavaCodeGenerator CodeGenerator;
public readonly ConcreteSyntaxTree InstanceMemberWriter;
Expand Down Expand Up @@ -557,7 +560,8 @@ protected ConcreteSyntaxTree CreateMethod(Method m, List<TypeArgumentInstantiati
wr.Write("{0} {1}", targetReturnTypeReplacement ?? "void", IdName(m));
wr.Write("(");
var sep = "";
WriteRuntimeTypeDescriptorsFormals(ForTypeDescriptors(typeArgs, m.EnclosingClass, m, lookasideBody), wr, ref sep, tp => $"{DafnyTypeDescriptor}<{tp.GetCompileName(Options)}> {FormatTypeDescriptorVariable(tp)}");
WriteRuntimeTypeDescriptorsFormals(ForTypeDescriptors(typeArgs, m.EnclosingClass, m, lookasideBody), wr, ref sep,
TypeDescriptorVariableDeclaration);
if (customReceiver) {
DeclareFormal(sep, "_this", receiverType, m.tok, true, wr);
sep = ", ";
Expand Down Expand Up @@ -602,7 +606,7 @@ protected override ConcreteSyntaxTree EmitMethodReturns(Method m, ConcreteSyntax
wr.Write(TypeParameters(TypeArgumentInstantiation.ToFormals(ForTypeParameters(typeArgs, member, lookasideBody)), " "));
wr.Write($"{TypeName(resultType, wr, tok)} {name}(");
var sep = "";
var argCount = WriteRuntimeTypeDescriptorsFormals(ForTypeDescriptors(typeArgs, member.EnclosingClass, member, lookasideBody), wr, ref sep, tp => $"{DafnyTypeDescriptor}<{tp.GetCompileName(Options)}> {FormatTypeDescriptorVariable(tp)}");
var argCount = WriteRuntimeTypeDescriptorsFormals(ForTypeDescriptors(typeArgs, member.EnclosingClass, member, lookasideBody), wr, ref sep, TypeDescriptorVariableDeclaration);
if (customReceiver) {
DeclareFormal(sep, "_this", receiverType, tok, true, wr);
sep = ", ";
Expand Down Expand Up @@ -1944,6 +1948,9 @@ IClassWriter CompileDatatypeBase(DatatypeDecl dt, ConcreteSyntaxTree wr) {
if (Options.Get(JavaBackend.LegacyDataConstructors)) {
wr.WriteLine("@Deprecated()");
w = wr.NewBlock($"public static{justTypeArgs} {simplifiedTypeName} Default({typeParameters})");
foreach (var typeParameter in dt.TypeArgs) {
w.WriteLine(TypeDescriptorVariableDeclaration(typeParameter) + " = null;");
}
w.Write("return ");
wLegacyDefault = w.Fork();
w.WriteLine(";");
Expand All @@ -1960,12 +1967,11 @@ IClassWriter CompileDatatypeBase(DatatypeDecl dt, ConcreteSyntaxTree wr) {
EmitDatatypeValue(dt, groundingCtor,
dt.TypeArgs.ConvertAll(tp => (Type)new UserDefinedType(dt.tok, tp)),
dt is CoDatatypeDecl, $"{wDefaultTypeArguments}", args, wDefault);
}

if (dt.TypeArgs.Any() && Options.Get(JavaBackend.LegacyDataConstructors)) {
var nullTypeDescriptorArgs = Enumerable.Repeat("null", defaultMethodTypeDescriptorCount).Comma();
EmitDatatypeValue(dt, groundingCtor,
dt.TypeArgs.ConvertAll(tp => (Type)new UserDefinedType(dt.tok, tp)),
dt is CoDatatypeDecl, nullTypeDescriptorArgs, args, wLegacyDefault);
if (wLegacyDefault != null) {
foreach (var node in wDefault.Nodes) {
wLegacyDefault.Append(node);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module {:options "--function-syntax:4"} LibraryModule {
// Record type
datatype Pair<+T, +U> = Pair(first: T, second: U)

datatype NestedTypeConstructorRecord<+T> = First(value: Pair<T, T>)
datatype NestedTypeConstructorClass<+T> = First2(value: Pair<T, T>) | Second2(value: Pair<T, T>)

datatype NoTypeArgs = Success2 | Failure2

}

0 comments on commit 8600061

Please sign in to comment.