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: Prevent diagnostics to "pass through" #2142

Merged
merged 8 commits into from
May 18, 2022
3 changes: 2 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Upcoming

- feat: The IDE will show verification errors for a method immediately after that method has been verified, instead of waiting for all methods to be verified. (https://github.com/dafny-lang/dafny/pull/1944)
- feat: The IDE will show verification errors for a method immediately after that method has been verified, instead of after all methods are verified.
- fix: No more display of previous obsolete verification diagnostics if newer are available. (https://github.com/dafny-lang/dafny/pull/2142)
- feat: Live verification diagnostics for the language server (https://github.com/dafny-lang/dafny/pull/1942)
- fix: Prevent the language server from crashing and not responding on resolution or ghost diagnostics errors (https://github.com/dafny-lang/dafny/pull/2080)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Dafny.LanguageServer.IntegrationTest.Extensions;
using Microsoft.Dafny.LanguageServer.IntegrationTest.Util;
using Microsoft.Dafny.LanguageServer.Workspace.Notifications;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using Range = OmniSharp.Extensions.LanguageServer.Protocol.Models.Range;

namespace Microsoft.Dafny.LanguageServer.IntegrationTest.Diagnostics;

[TestClass]
public class ConcurrentLinearVerificationGutterStatusTester : LinearVerificationGutterStatusTester {
private const int MaxSimultaneousVerificationTasks = 3;

protected TestNotificationReceiver<VerificationStatusGutter>[] verificationStatusGutterReceivers =
new TestNotificationReceiver<VerificationStatusGutter>[MaxSimultaneousVerificationTasks];

private void NotifyAllVerificationGutterStatusReceivers(VerificationStatusGutter request) {
foreach (var receiver in verificationStatusGutterReceivers) {
receiver.NotificationReceived(request);
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused by this. All verificationStatusGutterReceivers receive the same notifications, which would be the notifications for all the tasks. How does VerifyTrace that gets verificationStatusGutterReceivers[i] not get confused by the bogus notifications?

Copy link
Member Author

Choose a reason for hiding this comment

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

It checks that the filename matches and filters out notifications that do not match.
That way, it can rebuild the trace for every file 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 you explain that in a comment in the testing code?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a commit in #1946 to add a comment regarding this.

}
}

[TestInitialize]
public override async Task SetUp() {
for (var i = 0; i < verificationStatusGutterReceivers.Length; i++) {
verificationStatusGutterReceivers[i] = new();
}
verificationStatusGutterReceiver = new();
client = await InitializeClient(options =>
options
.AddHandler(DafnyRequestNames.VerificationStatusGutter,
NotificationHandler.For<VerificationStatusGutter>(NotifyAllVerificationGutterStatusReceivers))
);
}

[TestMethod]
public async Task EnsuresManyDocumentsCanBeVerifiedAtOnce() {
var result = new List<Task>();
for (var i = 0; i < MaxSimultaneousVerificationTasks; i++) {
result.Add(VerifyTrace(@"
. | | | I | | :predicate F(i: int) {
. | | | I | | : false // Should not be highlighted in gutter.
. | | | I | | :}
| | | I | | :
. S [S][ ][I][S][ ]:method H()
. S [=][=][-][~][O]: ensures F(1)
. S [=][=][-][~][=]:{//Next: { assert false;
. S [S][ ][I][S][ ]:}", $"testfile{i}.dfy", true, verificationStatusGutterReceivers[i]));
}

for (var i = 0; i < MaxSimultaneousVerificationTasks; i++) {
await result[i];
}

//await Task.WhenAll(result.ToArray());
Copy link
Member

Choose a reason for hiding this comment

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

Should this be removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes it can. I'll remove it in another PR.

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@

namespace Microsoft.Dafny.LanguageServer.IntegrationTest.Diagnostics;

public abstract class LinearVerificationDiagnosticTester : ClientBasedLanguageServerTest {
protected TestNotificationReceiver<VerificationStatusGutter> VerificationDiagnosticReceiver;
public abstract class LinearVerificationGutterStatusTester : ClientBasedLanguageServerTest {
protected TestNotificationReceiver<VerificationStatusGutter> verificationStatusGutterReceiver;
[TestInitialize]
public override async Task SetUp() {
diagnosticReceiver = new();
VerificationDiagnosticReceiver = new();
verificationStatusGutterReceiver = new();
client = await InitializeClient(options =>
options
.OnPublishDiagnostics(diagnosticReceiver.NotificationReceived)
.AddHandler(DafnyRequestNames.VerificationStatusGutter,
NotificationHandler.For<VerificationStatusGutter>(VerificationDiagnosticReceiver.NotificationReceived))
NotificationHandler.For<VerificationStatusGutter>(verificationStatusGutterReceiver.NotificationReceived))
);
}

Expand Down Expand Up @@ -108,27 +106,29 @@ public static string ExtractCode(string tracesAndCode) {

protected List<LineVerificationStatus[]> previousTraces = null;

protected async Task<List<LineVerificationStatus[]>> GetAllLineVerificationDiagnostics(TextDocumentItem documentItem) {
protected async Task<List<LineVerificationStatus[]>> GetAllLineVerificationStatuses(
TextDocumentItem documentItem,
TestNotificationReceiver<VerificationStatusGutter> verificationStatusGutterReceiver
) {
var traces = new List<LineVerificationStatus[]>();
var maximumNumberOfTraces = 50;
var previousPerLineDiagnostics
= previousTraces == null || previousTraces.Count == 0 ? null :
previousTraces[^1].ToList();
var nextDiagnostic = await diagnosticReceiver.AwaitNextNotificationAsync(CancellationToken);
for (; maximumNumberOfTraces > 0; maximumNumberOfTraces--) {
var verificationDiagnosticReport = await VerificationDiagnosticReceiver.AwaitNextNotificationAsync(CancellationToken);
if (documentItem.Uri != verificationDiagnosticReport.Uri || documentItem.Version != verificationDiagnosticReport.Version) {
var verificationStatusGutter = await verificationStatusGutterReceiver.AwaitNextNotificationAsync(CancellationToken);
if (documentItem.Uri != verificationStatusGutter.Uri || documentItem.Version != verificationStatusGutter.Version) {
continue;
}
var newPerLineDiagnostics = verificationDiagnosticReport.PerLineStatus.ToList();
var newPerLineDiagnostics = verificationStatusGutter.PerLineStatus.ToList();
if ((previousPerLineDiagnostics != null
&& previousPerLineDiagnostics.SequenceEqual(newPerLineDiagnostics)) ||
newPerLineDiagnostics.All(status => status == LineVerificationStatus.Nothing)) {
continue;
}

traces.Add(verificationDiagnosticReport.PerLineStatus);
if (NoMoreNotificationsToAwaitFrom(verificationDiagnosticReport)) {
traces.Add(verificationStatusGutter.PerLineStatus);
if (NoMoreNotificationsToAwaitFrom(verificationStatusGutter)) {
break;
}

Expand All @@ -139,10 +139,10 @@ var previousPerLineDiagnostics
return traces;
}

private static bool NoMoreNotificationsToAwaitFrom(VerificationStatusGutter verificationDiagnosticReport) {
return verificationDiagnosticReport.PerLineStatus.Contains(LineVerificationStatus.ResolutionError) ||
verificationDiagnosticReport.PerLineStatus.All(IsNotIndicatingProgress) ||
verificationDiagnosticReport.PerLineStatus.All(status => status == LineVerificationStatus.Nothing);
private static bool NoMoreNotificationsToAwaitFrom(VerificationStatusGutter verificationGutterStatus) {
return verificationGutterStatus.PerLineStatus.Contains(LineVerificationStatus.ResolutionError) ||
verificationGutterStatus.PerLineStatus.All(IsNotIndicatingProgress) ||
verificationGutterStatus.PerLineStatus.All(status => status == LineVerificationStatus.Nothing);
}

/// <summary>
Expand Down Expand Up @@ -218,19 +218,24 @@ Position IndexToPosition(int index) {
}

// If testTrace is false, codeAndTree should not contain a trace to test.
public async Task VerifyTrace(string codeAndTrace, bool testTrace = true) {
public async Task VerifyTrace(string codeAndTrace, string fileName = null, bool testTrace = true,
TestNotificationReceiver<VerificationStatusGutter> verificationStatusGutterReceiver = null
) {
if (verificationStatusGutterReceiver == null) {
verificationStatusGutterReceiver = this.verificationStatusGutterReceiver;
}
codeAndTrace = codeAndTrace[0] == '\n' ? codeAndTrace.Substring(1) :
codeAndTrace.Substring(0, 2) == "\r\n" ? codeAndTrace.Substring(2) :
codeAndTrace;
var codeAndChanges = testTrace ? ExtractCode(codeAndTrace) : codeAndTrace;
var (code, changes) = ExtractCodeAndChanges(codeAndChanges);
var documentItem = CreateTestDocument(code);
await client.OpenDocumentAndWaitAsync(documentItem, CancellationToken);
var documentItem = CreateTestDocument(code, fileName);
client.OpenDocument(documentItem);
var traces = new List<LineVerificationStatus[]>();
traces.AddRange(await GetAllLineVerificationDiagnostics(documentItem));
traces.AddRange(await GetAllLineVerificationStatuses(documentItem, verificationStatusGutterReceiver));
foreach (var (range, inserted) in changes) {
ApplyChange(ref documentItem, range, inserted);
traces.AddRange(await GetAllLineVerificationDiagnostics(documentItem));
traces.AddRange(await GetAllLineVerificationStatuses(documentItem, verificationStatusGutterReceiver));
}

if (testTrace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@ namespace Microsoft.Dafny.LanguageServer.IntegrationTest.Diagnostics;
/// methods priorities for verification being set automatically.
/// </summary>
[TestClass]
public class ReorderingVerificationDiagnosticTester : LinearVerificationDiagnosticTester {
public class ReorderingVerificationGutterStatusTester : LinearVerificationGutterStatusTester {
private ListeningTextDocumentLoader textDocumentLoader;
private const int MaxTestExecutionTimeMs = 10000;

[TestInitialize]
public override async Task SetUp() {
DafnyOptions.Install(DafnyOptions.Create("-proverOpt:SOLVER=noop"));
diagnosticReceiver = new();
VerificationDiagnosticReceiver = new();
verificationStatusGutterReceiver = new();
client = await InitializeClient(options =>
options
.OnPublishDiagnostics(diagnosticReceiver.NotificationReceived)
.AddHandler(DafnyRequestNames.VerificationStatusGutter,
NotificationHandler.For<VerificationStatusGutter>(VerificationDiagnosticReceiver.NotificationReceived))
NotificationHandler.For<VerificationStatusGutter>(verificationStatusGutterReceiver.NotificationReceived))
, serverOptions => {
serverOptions.Services.AddSingleton<ITextDocumentLoader>(serviceProvider => {
textDocumentLoader = new ListeningTextDocumentLoader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
namespace Microsoft.Dafny.LanguageServer.IntegrationTest.Diagnostics;

[TestClass]
public class SimpleLinearVerificationDiagnosticTester : LinearVerificationDiagnosticTester {
public class SimpleLinearVerificationGutterStatusTester : LinearVerificationGutterStatusTester {
private const int MaxTestExecutionTimeMs = 10000;

// To add a new test, just call VerifyTrace on a given program,
// the test will fail and give the correct output that can be use for the test
// Add '//Next<n>:' to edit a line multiple times

[TestMethod, Timeout(MaxTestExecutionTimeMs)]
public async Task EnsureVerificationDiagnosticsAreWorking() {
public async Task EnsureVerificationGutterStatusIsWorking() {
await VerifyTrace(@"
. | | | I I | | :predicate Ok() {
. | | | I I | | : true
Expand Down
Loading