#[is-variant] #41
Replies: 2 comments 6 replies
-
thoughts on doing I do realize it's a little inconsistent, which is a minus. |
Beta Was this translation helpful? Give feedback.
-
The _0 syntax is a bit hideous, but I understand that's a hackaround for the world in which we can't change Rust to have named fields. From the discussion, it looks like there's a path to a prettier future. Probably what we should really be doing is playing with richer examples:
I'm using the Hypothetical Future Syntax. With Present Syntax, we'd have n.get_Index_0()[3], which is pretty nasty; I'd vastly rather read n.get_Index_pivots()[3]. (And as Chris points out, n.get_pivots()[3] would be even better.) I'd most prefer n.pivots[3] -- treat 'pivots' as a field, as Dafny does. Is there a reason that's not rustacious? I'm honestly a little surprised that Rust doesn't have a way to name struct fields? That can't be right! Positionally-identified arguments are reasonable for short argument lists to procedures, but pretty gross for datatypes. |
Beta Was this translation helpful? Give feedback.
-
I've implemented an equivalent to Dafny’s adt.Variant? and (limited support) for variant accessors:
#[is-variant]
is an attribute macro that makes some #[spec] methods available for Result:r.is_Ok()
andr.is_Err()
, similar to Dafny’s r.Ok? and r.Err?r.get_Ok_0()
andr.get_Err_0()
(in general r.get_VariantName_FieldIndex()) to access the field(s)Sample usage:
There are some more examples of how to use these in the tests for the feature https://github.com/secure-foundations/verus/blob/d2e8869aa85bde74dfbce9c55cb86fec7b90f589/source/rust_verify/tests/adts.rs#L233-L349
This scheme, we believe, has the cleanest encoding. It can directly leverage the SMT field accessors and
(is-Variant)
primitive.The naming scheme also remains consistent if and when, in the future, we add support for enum variants with named fields, e.g.
where
r.get_Err_message()
will encode themessage
field.It is however a departure from Dafny's
.field
accessors (in the previous example, one could have doner.message
if they had the fact thatr.Err?
). This seems necessary to avoid an artificial restriction on Rust's rules or typechecking issues, as Rust (contrary do Dafny) allows fields with the same name but different types:Beta Was this translation helpful? Give feedback.
All reactions