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

fix: Generate proper default string value for JavaScript #5239

Merged
merged 7 commits into from
Mar 23, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,11 @@ protected override string TypeInitializationValue(Type type, ConcreteSyntaxTree
return $"{DafnyMultiSetClass}.Empty";
} else if (xType is SeqType seq) {
if (seq.Arg.IsCharType) {
return "''";
if (UnicodeCharEnabled) {
return "_dafny.Seq.UnicodeFromString(\"\")";
} else {
return "\"\"";
}
}
return $"{DafnySeqClass}.of()";
} else if (xType is MapType) {
Expand Down
33 changes: 22 additions & 11 deletions Source/DafnyCore/Backends/Python/PythonCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,13 @@ protected override string TypeInitializationValue(Type type, ConcreteSyntaxTree
case RealType:
return $"{DafnyRuntimeModule}.BigRational()";
case CollectionType:
return $"{TypeHelperName(xType)}({{}})";
if (xType is SeqType { Arg: { IsCharType: true } }) {
var wrString = new ConcreteSyntaxTree();
StringLiteralWrapper(wrString).Write("\"\"");
return wrString.ToString();
} else {
return $"{TypeHelperName(xType)}({{}})";
}
case UserDefinedType udt: {
var cl = udt.ResolvedClass;
Contract.Assert(cl != null);
Expand Down Expand Up @@ -1027,16 +1033,7 @@ protected override void EmitLiteralExpr(ConcreteSyntaxTree wr, LiteralExpr e) {
}
break;
case StringLiteralExpr str:
if (UnicodeCharEnabled) {
wr.Write($"{DafnySeqMakerFunction}(map({DafnyRuntimeModule}.CodePoint, ");
TrStringLiteral(str, wr);
wr.Write("))");
} else {
wr.Write($"{DafnySeqMakerFunction}(");
TrStringLiteral(str, wr);
wr.Write(")");
}

TrStringLiteral(str, StringLiteralWrapper(wr));
break;
case StaticReceiverExpr:
wr.Write(TypeName(e.Type, wr, e.tok));
Expand All @@ -1063,6 +1060,20 @@ protected override void EmitLiteralExpr(ConcreteSyntaxTree wr, LiteralExpr e) {
}
}

private ConcreteSyntaxTree StringLiteralWrapper(ConcreteSyntaxTree wr) {
ConcreteSyntaxTree wrStringGoesHere;
if (UnicodeCharEnabled) {
wr.Write($"{DafnySeqMakerFunction}(map({DafnyRuntimeModule}.CodePoint, ");
wrStringGoesHere = wr.Fork();
wr.Write("))");
} else {
wr.Write($"{DafnySeqMakerFunction}(");
wrStringGoesHere = wr.Fork();
wr.Write(")");
}
return wrStringGoesHere;
}

protected override void EmitStringLiteral(string str, bool isVerbatim, ConcreteSyntaxTree wr) {
if (!isVerbatim) {
wr.Write($"\"{TranslateEscapes(str)}\"");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %testDafnyForEachCompiler "%s"
// RUN: %testDafnyForEachCompiler "%s" -- --unicode-char=false

method Main() {
var s: seq<char>;
s := *;
print "(", s, ") ", s == "", " ", "" == s, " ", |s|, "\n";
s := "";
print "(", s, ") ", s == "", " ", "" == s, " ", |s|, "\n";
s := "hello";
print "(", s, ") ", s == "", " ", "" == s, " ", |s|, "\n";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
() true true 0
() true true 0
(hello) false false 5
1 change: 1 addition & 0 deletions docs/dev/news/5239.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the default string value emitted for JavaScript