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

Disjunctive pattern matching not correctly handled #5572

Closed
MikaelMayer opened this issue Jun 25, 2024 · 0 comments · Fixed by #5574
Closed

Disjunctive pattern matching not correctly handled #5572

MikaelMayer opened this issue Jun 25, 2024 · 0 comments · Fixed by #5574
Labels
during 4: bad execution of correct program A bug in the Dafny compiler that causes a correct Dafny program to execute incorrectly kind: bug Crashes, unsoundness, incorrect output, etc. If possible, add a `part:` label

Comments

@MikaelMayer
Copy link
Member

Dafny version

latest-nightly

Code to produce this issue

datatype D = A(int) | C(int) {
  function Test(): D {
    match this{
      case A(_) | C(_) =>
        this
    }
  }
}
method Main() {
  print C(0).Test();
}

Command to run and resulting output

dafny run -t:cs file.dfy

What happened?

Dafny program verifier finished with 0 verified, 0 errors
Unhandled exception. System.InvalidCastException: Unable to cast object of type '_module.D_C' to type '
_module.D_A'.
   at _module.D.get_dtor_A_a0()
   at _module.D.Test()
   at _module.__default._Main(ISequence`1 __noArgsParameter)
   at __CallToMain.<>c__DisplayClass0_0.<Main>b__0()
   at Dafny.Helpers.WithHaltHandling(Action action)
   at __CallToMain.Main(String[] args)

The translation is the following:

      bool unmatched0 = true;
      if (unmatched0) {
        bool disjunctiveMatch0 = false;
        BigInteger _0___v2 = _source0.dtor_A_a0;
        disjunctiveMatch0 = true;
        BigInteger _1___v3 = _source0.dtor_C_a0;
        disjunctiveMatch0 = true;
        if (disjunctiveMatch0) {
          unmatched0 = false;
          return this;
        }
      }

Since disjunctive patterns may not bind variables, we should simply not translate the patterns "_" into variable extraction.

What type of operating system are you experiencing the problem on?

Windows

@MikaelMayer MikaelMayer added kind: bug Crashes, unsoundness, incorrect output, etc. If possible, add a `part:` label during 4: bad execution of correct program A bug in the Dafny compiler that causes a correct Dafny program to execute incorrectly labels Jun 25, 2024
MikaelMayer added a commit that referenced this issue Jun 25, 2024
keyboardDrummer added a commit that referenced this issue Jun 26, 2024
…5574)

This PR fixes #5572
I added the corresponding 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>

---------

Co-authored-by: Remy Willems <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
during 4: bad execution of correct program A bug in the Dafny compiler that causes a correct Dafny program to execute incorrectly kind: bug Crashes, unsoundness, incorrect output, etc. If possible, add a `part:` label
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant