-
Notifications
You must be signed in to change notification settings - Fork 258
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
Support all option types in the project file #4506
Changes from 6 commits
0beaffb
4946ac6
7a6914d
81c7863
8b3b1ee
32c61ba
e913591
c89fd32
327817f
8df76c8
9e29a2a
7dde794
19ca042
177f6cb
3146dc2
e6a4a50
d52dd2c
e7c0a23
282a0ec
5e499c2
bbab8ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Tomlyn.Model; | ||
|
||
namespace DafnyCore.Generic; | ||
|
||
public static class TomlUtil { | ||
|
||
public static bool TryGetValueFromToml(TextWriter errorWriter, string sourceDir, string tomlPath, Type type, object tomlValue, out object value) { | ||
if (tomlValue == null) { | ||
value = null; | ||
return false; | ||
} | ||
|
||
if (type.IsAssignableFrom(typeof(List<string>))) { | ||
return TryGetListValueFromToml<string>(errorWriter, sourceDir, tomlPath, (TomlArray)tomlValue, out value); | ||
} | ||
if (type.IsAssignableFrom(typeof(List<FileInfo>))) { | ||
return TryGetListValueFromToml<FileInfo>(errorWriter, sourceDir, tomlPath, (TomlArray)tomlValue, out value); | ||
} | ||
|
||
if (tomlValue is string tomlString) { | ||
if (type == typeof(FileInfo)) { | ||
// Need to make sure relative paths are interpreted relative to the source of the value, | ||
// not the current directory. | ||
var fullPath = sourceDir != null ? Path.GetFullPath(tomlString, sourceDir) : tomlString; | ||
value = new FileInfo(fullPath); | ||
return true; | ||
} | ||
|
||
if (typeof(Enum).IsAssignableFrom(type)) { | ||
try { | ||
value = Enum.Parse(type, tomlString); | ||
return true; | ||
} catch (ArgumentException) { | ||
value = null; | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
if (!type.IsInstanceOfType(tomlValue)) { | ||
if (type == typeof(string)) { | ||
value = tomlValue.ToString(); | ||
return true; | ||
} | ||
errorWriter.WriteLine( | ||
$"Error: property '{tomlPath}' is of type '{tomlValue.GetType()}' but should be of type '{type}'"); | ||
value = null; | ||
return false; | ||
} | ||
|
||
value = tomlValue; | ||
return true; | ||
} | ||
|
||
private static bool TryGetListValueFromToml<T>(TextWriter errorWriter, string sourceDir, string tomlPath, TomlArray tomlValue, out object value) { | ||
var success = true; | ||
value = tomlValue.Select((e, i) => { | ||
if (TryGetValueFromToml(errorWriter, sourceDir, $"{tomlPath}[{i}]", typeof(T), e, out var elementValue)) { | ||
return (T)elementValue; | ||
} | ||
success = false; | ||
return default(T); | ||
}).ToList(); | ||
return success; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,7 @@ public async Task ClosestProjectFileIsFound() { | |
public async Task ProjectFileOverridesOptions() { | ||
await SetUp(options => { | ||
options.Set(Function.FunctionSyntaxOption, "3"); | ||
options.Set(CommonOptionBag.QuantifierSyntax, QuantifierSyntaxOptions.Version3); | ||
options.Set(CommonOptionBag.WarnShadowing, true); | ||
}); | ||
var source = @" | ||
|
@@ -110,13 +111,18 @@ method Foo() { | |
} | ||
} | ||
|
||
function Zoo(): set<(int,int)> { | ||
set x: int | 0 <= x < 5, y | 0 <= y < 6 :: (x,y) | ||
} | ||
|
||
ghost function Bar(): int { 3 }".TrimStart(); | ||
|
||
var projectFileSource = @" | ||
includes = [""**/*.dfy""] | ||
|
||
[options] | ||
warn-shadowing = false | ||
quantifier-syntax = 4 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In your test, could you please add an option that was otherwise translated to an enum or otherwise processed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh great, you got my point and that's perfect then. |
||
function-syntax = 4"; | ||
|
||
var directory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
|
@@ -127,7 +133,9 @@ method Foo() { | |
Assert.Single(diagnostics1); // Stops after parsing | ||
|
||
await File.WriteAllTextAsync(Path.Combine(directory, DafnyProject.FileName), projectFileSource); | ||
await CreateAndOpenTestDocument(source, Path.Combine(directory, "source.dfy")); | ||
var sourceFile = await CreateAndOpenTestDocument(source, Path.Combine(directory, "source.dfy")); | ||
var diagnostics2 = await GetLastDiagnostics(sourceFile, CancellationToken); | ||
Assert.Empty(diagnostics2.Where(d => d.Severity == DiagnosticSeverity.Error)); | ||
await AssertNoDiagnosticsAreComing(CancellationToken); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Support all types of options in the Dafny project file (dfyconfig.toml) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will parse the project file options in the same way as the CLI parses them, so there won't be any discrepancy any more.