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

Use dafny: Uris for standard library files #4832

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 11 additions & 1 deletion Source/DafnyCore/DafnyFile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
Expand All @@ -23,6 +24,13 @@ public class DafnyFile {

public static DafnyFile CreateAndValidate(ErrorReporter reporter, IFileSystem fileSystem,
DafnyOptions options, Uri uri, IToken origin) {

if (uri.Scheme == "doo") {
Copy link
Member Author

Choose a reason for hiding this comment

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

VSCode only allows you to have custom handlers for custom schemes, which seems fair to me, but it means we have to introduce a doo: scheme, which is at odds with that we already handle the .doo extension as being special, even if no doo: scheme is used. That's why the code looks a bit funky now.

If we were more strict, only allowing doo files being used as libraries, and only allowing dfy files being used as sources, then we wouldn't need to rely on the extension checking, but that seems like something to consider for the future.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, this seems wrong enough to me to not do it at all - it really is abusing the URI scheme concept. It seems like letting VSCode understand the dllresource: scheme is part of the solution, but it also needs to understand that .doo is different content format independently.

Copy link
Member

Choose a reason for hiding this comment

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

Could we have a custom handler for dllresource: that says "if the URI ends with .doo ... else open like a normal Dafny file"? That would at least handle the standard libraries, even if we'll need to make more changes to handle doo files in general.

var uriString = uri.ToString();
var dots = uriString.Substring("doo://".Length).Split(".");
uri = new Uri(dots[0] + "://" + string.Join('.', dots.Skip(1)));
}

var filePath = uri.LocalPath;

origin ??= Token.NoToken;
Expand Down Expand Up @@ -98,10 +106,12 @@ 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;
}

uri = new Uri("doo://" + uri.ToString().Replace("://", "."));

// For now it's simpler to let the rest of the pipeline parse the
// program text back into the AST representation.
// At some point we'll likely want to serialize a program
Expand Down
1 change: 0 additions & 1 deletion Source/DafnyCore/DafnyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public class DafnyOptions : Bpl.CommandLineOptions {
public IList<Uri> CliRootSourceUris = new List<Uri>();

public DafnyProject DafnyProject { get; set; }
public Command CurrentCommand { get; set; }

public static void ParseDefaultFunctionOpacity(Option<CommonOptionBag.DefaultFunctionOpacityOptions> option, Bpl.CommandLineParseState ps, DafnyOptions options) {
if (ps.ConfirmArgumentCount(1)) {
Expand Down
9 changes: 4 additions & 5 deletions Source/DafnyCore/DooFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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();
Copy link
Member

Choose a reason for hiding this comment

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

Is this still correct, given the comment immediately below? i.e. is OptionArguments.Keys the set of valid parameters for the current command, or just the options explicitly provided on the CLI or in project files?

Copy link
Member Author

Choose a reason for hiding this comment

The 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.
Expand Down
2 changes: 1 addition & 1 deletion Source/DafnyCore/Generic/Reporting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected override bool MessageCore(MessageSource source, ErrorLevel level, stri

Options.OutputWriter.Write(errorLine);

if (Options.Get(DafnyConsolePrinter.ShowSnippets) && tok != Token.NoToken) {
if (Options.Get(DafnyConsolePrinter.ShowSnippets) && tok.Uri != null) {
TextWriter tw = new StringWriter();
new DafnyConsolePrinter(Options).WriteSourceCodeSnippet(tok.ToRange(), tw);
Options.OutputWriter.Write(tw.ToString());
Expand Down
1 change: 0 additions & 1 deletion Source/DafnyDriver/Commands/DafnyCli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ async Task Handle(InvocationContext context) {
}
}

dafnyOptions.CurrentCommand = command;
dafnyOptions.ApplyDefaultOptionsWithoutSettingsDefault();
dafnyOptions.UsingNewCli = true;
context.ExitCode = await continuation(dafnyOptions, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<ItemGroup>
<ProjectReference Include="..\DafnyCore.Test\DafnyCore.Test.csproj" />
<ProjectReference Include="..\DafnyLanguageServer\DafnyLanguageServer.csproj" />
<ProjectReference Include="..\DafnyPipeline\DafnyPipeline.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ private Action<LanguageServerOptions> GetServerOptionsAction(Action<DafnyOptions
var dafnyOptions = DafnyOptions.Create(output);
dafnyOptions.Set(ProjectManager.UpdateThrottling, 0);
modifyOptions?.Invoke(dafnyOptions);
Microsoft.Dafny.LanguageServer.LanguageServer.ConfigureDafnyOptionsForServer(dafnyOptions);
dafnyOptions.UsingNewCli = true;
LanguageServer.ConfigureDafnyOptionsForServer(dafnyOptions);
ApplyDefaultOptionValues(dafnyOptions);
return options => {
options.WithDafnyLanguageServer(() => { });
Expand Down
18 changes: 0 additions & 18 deletions Source/DafnyLanguageServer.Test/Lookup/GoToDefinitionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,6 @@ requires Err?
await AssertPositionsLineUpWithRanges(source);
}

/// <summary>
/// Given <paramref name="source"/> with N positions, for each K from 0 to N exclusive,
/// assert that a RequestDefinition at position K
/// returns either the Kth range, or the range with key K (as a string).
/// </summary>
private async Task AssertPositionsLineUpWithRanges(string source) {
MarkupTestFile.GetPositionsAndNamedRanges(source, out var cleanSource,
out var positions, out var ranges);

var documentItem = await CreateOpenAndWaitForResolve(cleanSource);
for (var index = 0; index < positions.Count; index++) {
var position = positions[index];
var range = ranges.ContainsKey(string.Empty) ? ranges[string.Empty][index] : ranges[index.ToString()].Single();
var result = (await RequestDefinition(documentItem, position)).Single();
Assert.Equal(range, result.Location!.Range);
}
}

[Fact]
public async Task StaticFunctionCall() {
var source = @"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,22 @@ protected async Task<TextDocumentItem> GetDocumentItem(string source, string fil
await client.OpenDocumentAndWaitAsync(documentItem, CancellationToken);
return documentItem;
}

/// <summary>
/// Given <paramref name="source"/> with N positions, for each K from 0 to N exclusive,
/// assert that a RequestDefinition at position K
/// returns either the Kth range, or the range with key K (as a string).
/// </summary>
protected async Task AssertPositionsLineUpWithRanges(string source, string filePath = null) {
MarkupTestFile.GetPositionsAndNamedRanges(source, out var cleanSource,
out var positions, out var ranges);

var documentItem = await CreateOpenAndWaitForResolve(cleanSource, filePath);
for (var index = 0; index < positions.Count; index++) {
var position = positions[index];
var range = ranges.ContainsKey(string.Empty) ? ranges[string.Empty][index] : ranges[index.ToString()].Single();
var result = (await RequestDefinition(documentItem, position)).Single();
Assert.Equal(range, result.Location!.Range);
}
}
}
37 changes: 0 additions & 37 deletions Source/DafnyLanguageServer.Test/Various/StandardLibrary.cs

This file was deleted.

70 changes: 70 additions & 0 deletions Source/DafnyLanguageServer.Test/Various/StandardLibraryTest.cs
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("doo://dllresource.dafnypipeline/DafnyStandardLibraries.doo"), result.Single().Location.Uri);
}

public StandardLibraryTest(ITestOutputHelper output, LogLevel dafnyLogLevel = LogLevel.Information) : base(output, dafnyLogLevel) {
}
}
15 changes: 1 addition & 14 deletions Source/DafnyLanguageServer/Language/DafnyLangParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,7 @@ public Program Parse(Compilation compilation, CancellationToken cancellationToke

var beforeParsing = DateTime.Now;
try {
var rootFiles = compilation.RootFiles!;
List<DafnyFile> dafnyFiles = new();
foreach (var rootFile in rootFiles) {
try {
dafnyFiles.Add(rootFile);
if (logger.IsEnabled(LogLevel.Trace)) {
logger.LogTrace($"Parsing file with uri {rootFile.Uri} and content\n{rootFile.GetContent().ReadToEnd()}");
}
} catch (IOException) {
logger.LogError($"Tried to parse file {rootFile} that could not be found");
}
}

return programParser.ParseFiles(compilation.Project.ProjectName, dafnyFiles, compilation.Reporter, cancellationToken);
return programParser.ParseFiles(compilation.Project.ProjectName, compilation.RootFiles, compilation.Reporter, cancellationToken);
}
finally {
telemetryPublisher.PublishTime("Parse", compilation.Project.Uri.ToString(), DateTime.Now - beforeParsing);
Expand Down
1 change: 0 additions & 1 deletion Source/DafnyLanguageServer/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ related locations

public static void ConfigureDafnyOptionsForServer(DafnyOptions dafnyOptions) {
dafnyOptions.Set(DafnyConsolePrinter.ShowSnippets, true);

dafnyOptions.PrintIncludesMode = DafnyOptions.IncludesModes.None;

// TODO This may be subject to change. See Microsoft.Boogie.Counterexample
Expand Down
Loading