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

Enum discriminant completeness #1512

Open
nishanthkarthik opened this issue Apr 10, 2024 · 0 comments
Open

Enum discriminant completeness #1512

nishanthkarthik opened this issue Apr 10, 2024 · 0 comments

Comments

@nishanthkarthik
Copy link

This might be related to #1391. Prusti does not generate an axiom to prove the bijection between an enum's constructor and its discriminant in case of the simple no-argument enum.

#[derive(PartialEq, Eq, Copy, Clone)]
#[repr(u16)]
enum T {
    I1 = 1,
    I2 = 2,
    I3 = 3,
}

impl T {
    #[pure]
    #[ensures(1 <= result && result <= 3)]
    #[ensures(matches!(self, T::I1) <==> result == 1)]
    #[ensures(matches!(self, T::I2) <==> result == 2)]
    #[ensures(matches!(self, T::I3) <==> result == 3)]
    fn f(&self) -> u16 {
        match self {
            T::I1 => 1,
            T::I2 => 2,
            T::I3 => 3,
        }
    }

    #[ensures(a.f() == b.f() <==> a == b)]
    fn test_biject_2(a: T, b: T) {}

    #[ensures(forall(|t: T| a == t <==> a.f() == t.f()))]
    fn test_biject_1(a: T) {
        let _ = T::I1; // added to generate axioms about discriminants
        let _ = T::I2;
        let _ = T::I3;
    }
}

Expected: Both test_biject_2 and test_biject_1
Actual: Only test_biject_2 verifies.

Adding

  axiom oneof {
    forall self: Snap$m_T$_beg_$_end_ :: {discriminant$__$TY$__Snap$m_T$_beg_$_end_$Snap$m_T$_beg_$_end_$$int$(self)}
      self == cons$0$__$TY$__Snap$m_T$_beg_$_end_$Snap$m_T$_beg_$_end_() ||
      self == cons$1$__$TY$__Snap$m_T$_beg_$_end_$Snap$m_T$_beg_$_end_() ||
      self == cons$2$__$TY$__Snap$m_T$_beg_$_end_$Snap$m_T$_beg_$_end_()
  }

makes test_biject_1 verify. Credits to @zgrannan for the tip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant