Skip to content

Commit

Permalink
Fix null ref in covered tokens (#5548)
Browse files Browse the repository at this point in the history
Fixes #5355

### Description
- Fix null reference exception that occurred when looking at the tokens
of a generated node

### How has this been tested?
- Add LSP test

<small>By submitting this pull request, I confirm that my contribution
is made under the terms of the [MIT
license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small>
  • Loading branch information
keyboardDrummer authored Jun 10, 2024
1 parent f0b25e0 commit 5ad2413
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
1 change: 0 additions & 1 deletion Source/DafnyCore/AST/Members/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public TailStatus
public readonly Formal Result;
public PreType ResultPreType;
public readonly Type ResultType;
public readonly Specification<FrameExpression> Reads;
public Expression Body; // an extended expression; Body is readonly after construction, except for any kind of rewrite that may take place around the time of resolution
public IToken /*?*/ ByMethodTok; // null iff ByMethodBody is null
public BlockStmt /*?*/ ByMethodBody;
Expand Down
1 change: 0 additions & 1 deletion Source/DafnyCore/AST/Members/Method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ static Method() {
public bool MustReverify;
public bool IsEntryPoint = false;
public readonly List<Formal> Outs;
public readonly Specification<FrameExpression> Reads;
public readonly Specification<FrameExpression> Mod;
[FilledInDuringResolution] public bool IsRecursive;
[FilledInDuringResolution] public bool IsTailRecursive;
Expand Down
6 changes: 1 addition & 5 deletions Source/DafnyCore/AST/Members/MethodOrFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,5 @@ public void ResolveMethodOrFunction(INewOrOldResolver resolver) {
protected MethodOrFunction(RangeToken tok, Name name, bool hasStaticKeyword, bool isGhost, Attributes attributes, bool isRefining) : base(tok, name, hasStaticKeyword, isGhost, attributes, isRefining) {
}

public Specification<FrameExpression> Reads => this switch {
Method m => m.Reads,
Function f => f.Reads,
_ => throw new cce.UnreachableException()
};
public Specification<FrameExpression> Reads { get; set; }
}
3 changes: 3 additions & 0 deletions Source/DafnyCore/Generic/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public abstract class Node : INode {
public IEnumerable<IToken> CoveredTokens {
get {
var token = StartToken;
if (token == Token.NoToken) {
yield break;
}
while (token.Prev != EndToken) {
yield return token;
token = token.Next;
Expand Down
36 changes: 36 additions & 0 deletions Source/DafnyLanguageServer.Test/CodeActions/CodeActionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ public async Task MethodKeywordCodeAction() {
Assert.Empty(completionList);
}

[Fact]
public async Task TypeTestCodeAction() {
await SetUp(o => o.Set(CommonOptionBag.RelaxDefiniteAssignment, true));

MarkupTestFile.GetPositionsAndAnnotatedRanges(@"module M {
trait Object {
ghost function typeId() : (id: string)
}
type A extends Object {
ghost function typeId() : (id: string) { ""A"" }
}
type B extends Object {
ghost function typeId() : (id: string) { ""B"" }
}
type C extends Object {
ghost function typeId() : (id: string) { ""C"" }
}
lemma test(x: Object)
ensures multiset{x is A, x is B, x is C}[true] == 1
{
var _ := x.typeId();
if (x is A) {
assert x !is B.><
}
}
}".TrimStart(), out var source, out var positions,
out var ranges);
var documentItem = await CreateOpenAndWaitForResolve(source);
var position = positions[0];
var completionList = await RequestCodeActionAsync(documentItem, new Range(position, position));
Assert.Empty(completionList);
}

[Fact]
public async Task GitIssue4401CorrectInsertionPlace() {
await TestCodeAction(@"
Expand Down

0 comments on commit 5ad2413

Please sign in to comment.