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

Linter rule: old should only refer to heap values #1989

Closed
mschlaipfer opened this issue Apr 8, 2022 · 2 comments · Fixed by #2610
Closed

Linter rule: old should only refer to heap values #1989

mschlaipfer opened this issue Apr 8, 2022 · 2 comments · Fixed by #2610
Assignees
Labels
kind: enhancement Enhancements, feature requests, etc. These are NOT bugs, but ways we can improve Dafny part: resolver Resolution and typechecking

Comments

@mschlaipfer
Copy link
Member

Dafny does not provide a warning when using the old keyword on immutable values. This doesn't make sense and can lead to misunderstanding. Pointing this out in an analysis exposed via the IDE would clear this up.

For example, the following code verifies, and it's not immediately obvious why.

module Test {

  class C {
  }

  function arrSet(a: array<C>) : set<object>
    reads a
  {
    set i : int | 0 <= i < a.Length :: a[i]
  }

  method test(a: array<C>, i : int, x : C)
    modifies a
    requires 0 <= i < a.Length
    requires x !in arrSet(a)
  {
    a[i] := x;
    assert(arrSet(a) == arrSet(old(a)));
  }
}

The reason that assert(arrSet(a) == arrSet(old(a))); verifies is that old(a) refers to the reference, and not to the values in a. This is documented, but a linter pass warning about using old on an immutable value would be helpful.

@mschlaipfer mschlaipfer added the kind: enhancement Enhancements, feature requests, etc. These are NOT bugs, but ways we can improve Dafny label Apr 8, 2022
@keyboardDrummer
Copy link
Member

I guess you want to receive a warning when old is used on an expression that does not contain a heap access, so old has no affect, is that right?

@keyboardDrummer keyboardDrummer added the part: resolver Resolution and typechecking label Apr 8, 2022
@mschlaipfer
Copy link
Member Author

Yes, exactly.

@RustanLeino RustanLeino self-assigned this Aug 15, 2022
keyboardDrummer pushed a commit that referenced this issue Sep 2, 2022
Fixes #1989

If the argument to `old` has no dereferences of the mutable heap, generate a warning saying that the `old` has no effect. This has been a common source of errors and confusion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement Enhancements, feature requests, etc. These are NOT bugs, but ways we can improve Dafny part: resolver Resolution and typechecking
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants