-
Notifications
You must be signed in to change notification settings - Fork 260
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
Use dafny:
Uris for standard library files
#4832
Changes from all commits
c4805a9
8ce5fb0
788ef9f
e38e097
88e721d
c769a16
f7004a3
72ff748
1a0d9d0
a05bc07
f12acb6
b5878f4
97bd28c
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Reflection.Metadata; | ||
using System.Reflection.PortableExecutable; | ||
|
@@ -16,13 +18,30 @@ public class DafnyFile { | |
public string BaseName { get; private set; } | ||
public bool IsPreverified { get; set; } | ||
public bool IsPrecompiled { get; set; } | ||
public bool IsPrerefined { get; private set; } | ||
public Func<TextReader> GetContent { get; set; } | ||
public Uri Uri { get; } | ||
public Uri Uri { get; private set; } | ||
[CanBeNull] public IToken Origin { get; } | ||
|
||
private static readonly Dictionary<Uri, Uri> ExternallyVisibleEmbeddedFiles = new(); | ||
|
||
public static Uri ExposeInternalUri(string externalName, Uri internalUri) { | ||
var externalUri = new Uri("dafny:" + externalName); | ||
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. Why not just use 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. Because I didn't want to expose the implementation details of 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. No real objection to the complexity/work, but a little worried about exposing "DafnyStandardLibraries.dfy" when that string/file name isn't actually meaningful. Eventually I expect the IDE will be able to actually open the doo file, and we shouldn't hide the fact that the source is coming from a doo file (especially to be clear it's read-only) 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. Actually I added the dot dfy at the end so the IDE can recognize it's Dafny code and provide ide tooling on it. I think from the user perspective, opening a dotdfy file to view the sources inside a dotdoo file, seems good 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. Okay, I can live with it :) 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. We can still change the part after the I think, as part of adding Thinking about dotdoo support in the IDE more, and given the way dotdoo files are currently structured, the 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.
I definitely intended to iterate on the file format as we added features, and hence why the manifest has a
Yup, this is one of the reasons I thought |
||
ExternallyVisibleEmbeddedFiles[externalUri] = internalUri; | ||
return externalUri; | ||
} | ||
|
||
public static DafnyFile CreateAndValidate(ErrorReporter reporter, IFileSystem fileSystem, | ||
DafnyOptions options, Uri uri, IToken origin) { | ||
|
||
var embeddedFile = ExternallyVisibleEmbeddedFiles.GetValueOrDefault(uri); | ||
if (embeddedFile != null) { | ||
var result = CreateAndValidate(reporter, fileSystem, options, embeddedFile, origin); | ||
if (result != null) { | ||
result.Uri = uri; | ||
} | ||
return result; | ||
} | ||
|
||
var filePath = uri.LocalPath; | ||
|
||
origin ??= Token.NoToken; | ||
|
@@ -32,7 +51,6 @@ public static DafnyFile CreateAndValidate(ErrorReporter reporter, IFileSystem fi | |
Func<TextReader> getContent = null; | ||
bool isPreverified; | ||
bool isPrecompiled; | ||
var isPrerefined = false; | ||
var extension = ".dfy"; | ||
if (uri.IsFile) { | ||
extension = Path.GetExtension(uri.LocalPath).ToLower(); | ||
|
@@ -98,7 +116,7 @@ public static DafnyFile CreateAndValidate(ErrorReporter reporter, IFileSystem fi | |
dooFile = DooFile.Read(filePath); | ||
} | ||
|
||
if (!dooFile.Validate(reporter, filePathForErrors, options, options.CurrentCommand, origin)) { | ||
if (!dooFile.Validate(reporter, filePathForErrors, options, origin)) { | ||
return null; | ||
} | ||
|
||
|
@@ -109,7 +127,6 @@ public static DafnyFile CreateAndValidate(ErrorReporter reporter, IFileSystem fi | |
// the DooFile class should encapsulate the serialization logic better | ||
// and expose a Program instead of the program text. | ||
getContent = () => new StringReader(dooFile.ProgramText); | ||
isPrerefined = true; | ||
} else if (extension == ".dll") { | ||
isPreverified = true; | ||
// Technically only for C#, this is for backwards compatability | ||
|
@@ -127,7 +144,6 @@ public static DafnyFile CreateAndValidate(ErrorReporter reporter, IFileSystem fi | |
return new DafnyFile(extension, canonicalPath, baseName, getContent, uri, origin) { | ||
IsPrecompiled = isPrecompiled, | ||
IsPreverified = isPreverified, | ||
IsPrerefined = isPrerefined | ||
}; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,11 +116,10 @@ public DooFile(Program dafnyProgram) { | |
private DooFile() { | ||
} | ||
|
||
public bool Validate(ErrorReporter reporter, string filePath, DafnyOptions options, Command currentCommand, | ||
IToken origin) { | ||
if (currentCommand == null) { | ||
public bool Validate(ErrorReporter reporter, string filePath, DafnyOptions options, IToken origin) { | ||
if (!options.UsingNewCli) { | ||
reporter.Error(MessageSource.Project, origin, | ||
$"Cannot load {filePath}: .doo files cannot be used with the legacy CLI"); | ||
$"cannot load {filePath}: .doo files cannot be used with the legacy CLI"); | ||
return false; | ||
} | ||
|
||
|
@@ -131,7 +130,7 @@ public bool Validate(ErrorReporter reporter, string filePath, DafnyOptions optio | |
} | ||
|
||
var success = true; | ||
var relevantOptions = currentCommand.Options.ToHashSet(); | ||
var relevantOptions = options.Options.OptionArguments.Keys.ToHashSet(); | ||
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. Is this still correct, given the comment immediately below? i.e. is 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. This is still correct, yes |
||
foreach (var (option, check) in OptionChecks) { | ||
// It's important to only look at the options the current command uses, | ||
// because other options won't be initialized to the correct default value. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,21 +5,18 @@ | |
// SPDX-License-Identifier: MIT | ||
// | ||
//----------------------------------------------------------------------------- | ||
using System.Collections.Generic; | ||
|
||
using System.Linq; | ||
using System; | ||
using System.Diagnostics.Contracts; | ||
using JetBrains.Annotations; | ||
using Microsoft.Boogie; | ||
using static Microsoft.Dafny.RewriterErrors; | ||
|
||
namespace Microsoft.Dafny { | ||
|
||
public class PrecedenceLinter : IRewriter { | ||
private CompilationData compilation; | ||
private readonly CompilationData compilation; | ||
// Don't perform linting on doo files in general, since the source has already been processed. | ||
internal override void PostResolve(ModuleDefinition moduleDefinition) { | ||
if (moduleDefinition.tok.Uri != null && moduleDefinition.tok.Uri.LocalPath.EndsWith(".doo")) { | ||
if (moduleDefinition.tok.Uri != null && !moduleDefinition.ShouldVerify(compilation)) { | ||
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. For the record I think this is a step backwards in terms of accuracy: not linting doo files is more about the fact that the content is read-only to consumers and not their responsibility to lint, nothing to do with verification. I don't want to block this change on it, but perhaps we could at least add a comment in the next related PR. 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. I'm wondering whether The reasoning behind not verifying a file seem to apply to other correctness checks as well. |
||
return; | ||
} | ||
foreach (var topLevelDecl in moduleDefinition.TopLevelDecls.OfType<TopLevelDeclWithMembers>()) { | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Dafny.LanguageServer.IntegrationTest.Util; | ||
using Microsoft.Extensions.Logging; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Models; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.Dafny.LanguageServer.IntegrationTest.Various; | ||
|
||
public class StandardLibraryTest : ClientBasedLanguageServerTest { | ||
[Fact] | ||
public async Task CanUseWrappers() { | ||
var source = @" | ||
import opened DafnyStdLibs.Wrappers | ||
|
||
const triggerSemicolonWarning := 3; | ||
|
||
method Foo() returns (s: Option<int>) { | ||
return Some(3); | ||
}".TrimStart(); | ||
|
||
var projectSource = @" | ||
[options] | ||
standard-libraries = true"; | ||
|
||
var withoutStandardLibraries = CreateAndOpenTestDocument(source); | ||
var diagnostics1 = await GetLastDiagnostics(withoutStandardLibraries, DiagnosticSeverity.Error); | ||
Assert.Single(diagnostics1); | ||
|
||
var directory = Path.GetTempFileName(); | ||
CreateAndOpenTestDocument(projectSource, Path.Combine(directory, DafnyProject.FileName)); | ||
var document = CreateAndOpenTestDocument(source, Path.Combine(directory, "document.dfy")); | ||
var diagnostics2 = await GetLastDiagnostics(document); | ||
Assert.Single(diagnostics2); | ||
Assert.Equal(DiagnosticSeverity.Warning, diagnostics2[0].Severity); | ||
} | ||
|
||
[Fact] | ||
public async Task GotoDefinition() { | ||
var source = @" | ||
import opened DafnyStdLibs.Wrappers | ||
|
||
method Foo() returns (s: ><Option<int>) { | ||
return Some(3); | ||
}".TrimStart(); | ||
|
||
var projectSource = @" | ||
[options] | ||
standard-libraries = true"; | ||
|
||
var directory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
Directory.CreateDirectory(directory); | ||
await File.WriteAllTextAsync(Path.Combine(directory, DafnyProject.FileName), projectSource); | ||
|
||
MarkupTestFile.GetPositionsAndNamedRanges(source, out var cleanSource, | ||
out var positions, out var ranges); | ||
|
||
var filePath = Path.Combine(directory, "StandardLibraryGotoDefinition.dfy"); | ||
var documentItem = CreateAndOpenTestDocument(cleanSource, filePath); | ||
await AssertNoDiagnosticsAreComing(CancellationToken); | ||
var result = await RequestDefinition(documentItem, positions[0]); | ||
Assert.Equal(new Uri("dafny:DafnyStandardLibraries.dfy"), result.Single().Location.Uri); | ||
} | ||
|
||
public StandardLibraryTest(ITestOutputHelper output, LogLevel dafnyLogLevel = LogLevel.Information) : base(output, dafnyLogLevel) { | ||
} | ||
} |
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.
Off topic but when did this appear and what was it used for?
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.
It was needed when .doo files still contained resolved code instead of just parsed code.
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.
Okay good, I figured it was related but wasn't sure if was actually in sync before.