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: Support for user-defined module fmt for all compilers #5441

Merged
merged 17 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
72 changes: 69 additions & 3 deletions Source/DafnyCore/Backends/GoLang/GoCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2573,11 +2573,77 @@ public override string PublicIdProtect(string name) {
}
}

/* Obtained by running the following on the console on the page https://pkg.go.dev/std :
var lineLength = 0; copy([...document.querySelectorAll(
"td > div.UnitDirectories-pathCell > div > span, "+
"td > div.UnitDirectories-pathCell > div > a")]
.map((e, i) => {
var res = JSON.stringify(e.textContent);
if(lineLength + res.length > 94) {
lineLength = res.length;
res = ",\n " + res;
} else if(i > 0) {
res = "," + res;
lineLength += res.length;
}
return res;
}).join(""))
*/
public readonly HashSet<string> ReservedModuleNames = new() {
"c",
"archive",
"bufio",
"builtin",
"bytes",
"cmp",
"compress",
"container",
"context",
"crypto",
"database",
"debug",
"embed",
"encoding",
"errors",
"expvar",
"flag",
"fmt",
"go",
"hash",
"html",
"image",
"index",
"internal",
"io",
"log",
"maps",
"math",
"mime",
"net",
"os",
"path",
"plugin",
"reflect",
"regexp",
"runtime",
"slices",
"sort",
"strconv",
"strings",
"sync",
"syscall",
"testing",
"text",
"time",
"unicode",
"unsafe"
};

public string PublicModuleIdProtect(string name) {
if (name == "C") {
return "_C";
if (ReservedModuleNames.Contains(name.ToLower())) {
return "_" + name;
} else {
return name;
return IdProtect(name);
}
}

Expand Down
23 changes: 19 additions & 4 deletions Source/DafnyCore/Backends/Python/PythonCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected override ConcreteSyntaxTree CreateStaticMain(IClassWriter cw, string a

protected override ConcreteSyntaxTree CreateModule(string moduleName, bool isDefault, ModuleDefinition externModule,
string libraryName, ConcreteSyntaxTree wr) {
moduleName = IdProtect(moduleName);
moduleName = PublicModuleIdProtect(moduleName);
var file = wr.NewFile($"{moduleName}.py");
EmitImports(moduleName, file);
return file;
Expand Down Expand Up @@ -170,7 +170,7 @@ protected override IClassWriter CreateTrait(string name, bool isExtern, List<Typ
}

protected override ConcreteSyntaxTree CreateIterator(IteratorDecl iter, ConcreteSyntaxTree wr) {
var cw = (ClassWriter)CreateClass(IdProtect(iter.EnclosingModuleDefinition.GetCompileName(Options)), IdName(iter), false,
var cw = (ClassWriter)CreateClass(PublicModuleIdProtect(iter.EnclosingModuleDefinition.GetCompileName(Options)), IdName(iter), false,
iter.FullName, iter.TypeArgs, iter, null, iter.tok, wr);
var constructorWriter = cw.ConstructorWriter;
var w = cw.MethodWriter;
Expand Down Expand Up @@ -686,8 +686,11 @@ internal override string TypeName(Type type, ConcreteSyntaxTree wr, IToken tok,
}

private string FullName(TopLevelDecl decl) {
var localDefinition = decl.EnclosingModuleDefinition == enclosingModule;
return IdProtect(localDefinition ? decl.GetCompileName(Options) : decl.GetFullCompileName(Options));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I messed with this a little. Maybe you can have double check it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to evaluate if this is good or not. I'll let the CI test this new behavior.

var segments = new List<string> { IdProtect(decl.GetCompileName(Options)) };
if (decl.EnclosingModuleDefinition != enclosingModule) {
segments = decl.EnclosingModuleDefinition.GetCompileName(Options).Split('.').Select(PublicModuleIdProtect).Concat(segments).ToList();
}
return string.Join('.', segments);
}

protected override string TypeInitializationValue(Type type, ConcreteSyntaxTree wr, IToken tok,
Expand Down Expand Up @@ -1154,6 +1157,18 @@ public override string PublicIdProtect(string name) {
};
}


private readonly HashSet<string> ReservedModuleNames = new() {
"itertools", "math", "typing", "sys"
};

private string PublicModuleIdProtect(string name) {
if (ReservedModuleNames.Contains(name)) {
return "_" + name;
}
return IdProtect(name);
}

protected override string FullTypeName(UserDefinedType udt, MemberDecl member = null) {
if (udt is ArrowType) {
//TODO: Add deeper types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ and then right-click the test you wish to debug and select
so you may wish to remove the calls you are not interested in first, e.g. if you are debugging an issue with a specific compiler.

## Updating test expect files
If you set the static field `DiffCommand.UpdateExpectFile` to true instead of false, then the `diff` command in tests will not do a comparison but instead overwrite the expected file with the actual contents. This can be useful when you need to update expect files.
If you set the environment variable `DAFNY_INTEGRATION_TESTS_UPDATE_EXPECT_FILE` to `true`, then the `diff` command in tests will not do a comparison but instead overwrite the expected file with the actual contents. This can be useful when you need to update expect files.

## Updating compiler-specific test expect files
To update compiler-specific error-aware output files for tests running with `%forEachDafnyCompiler`,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// RUN: %testDafnyForEachCompiler "%s"

/* Obtained from
https://pkg.go.dev/std
by running on the console
copy([...document.querySelectorAll(
"td > div.UnitDirectories-pathCell > div > span, "+
"td > div.UnitDirectories-pathCell > div > a")]
.map(e => "module " + e.textContent + " {}").join("\n"))
*/
module archive {}
module bufio {}
module builtin {}
module bytes {}
module cmp {}
module compress {}
module container {}
module context {}
module crypto {}
module database {}
module debug {}
module embed {}
module encoding {}
module errors {}
module expvar {}
module flag {}
module fmt {}
module go {}
module hash {}
module html {}
module image {}
module index {}
module internal {}
module io {}
module log {}
module maps {}
module math {}
module mime {}
module net {}
module os {}
module path {}
module plugin {}
module reflect {}
module regexp {}
module runtime {}
module slices {}
module sort {}
module strconv {}
module strings {}
module sync {}
module syscall {}
module testing {}
module text {}
module time {}
module unicode {}
module unsafe {}

method Main(){
print "done\n";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
done
1 change: 1 addition & 0 deletions docs/dev/news/5283.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support for user-defined module with the same name as Go built-in modules, checked for all compilers