Skip to content

Commit

Permalink
Add support for !is/!has syntax (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaybosamiya-ms authored Feb 7, 2025
2 parents d2c3a9f + ae4c428 commit fe40973
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

* Add support for `!is`/`!has` syntax (see [verus#1435](https://github.com/verus-lang/verus/pull/1435))

# v0.5.2

* Fix parser for `assume_specification` to improve support
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ fn to_doc<'a>(
| Rule::arrow_expr_str => s,
Rule::fn_traits | Rule::impl_str => s,
Rule::calc_str | Rule::seq_str => s,
Rule::has_str | Rule::is_str => s,
Rule::pipe_str => docs!(arena, arena.line(), s, arena.space()),
// Rule::triple_and |
// Rule::triple_or =>
Expand Down Expand Up @@ -755,9 +756,8 @@ fn to_doc<'a>(

Rule::as_str
| Rule::by_str_inline
| Rule::has_str
| Rule::implies_str
| Rule::is_str
| Rule::spaced_is_str
| Rule::matches_str => arena.space().append(s).append(arena.space()),

Rule::by_str | Rule::via_str | Rule::when_str => arena
Expand Down Expand Up @@ -1091,6 +1091,10 @@ fn to_doc<'a>(
Rule::expr_inner_no_struct => map_to_doc(ctx, arena, pair),
Rule::expr_with_block => map_to_doc(ctx, arena, pair),
Rule::expr_as => map_to_doc(ctx, arena, pair),
Rule::is_or_notis | Rule::has_or_nothas => arena
.space()
.append(map_to_doc(ctx, arena, pair))
.append(arena.space()),
Rule::expr_has => map_to_doc(ctx, arena, pair),
Rule::expr_is => map_to_doc(ctx, arena, pair),
Rule::expr_matches => map_to_doc(ctx, arena, pair),
Expand Down
18 changes: 14 additions & 4 deletions src/verus.pest
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ seq_macro_call = {
macro_call = {
calc_macro_call
| seq_macro_call
| attr* ~ path ~ bang_str ~ !"=" ~ token_tree
| attr* ~ path ~ bang_str ~ delim_token_tree
}

// NOTE: By convention, stmt-like or item-like macros (e.g., calc!{}) tend to
Expand Down Expand Up @@ -613,8 +613,10 @@ global_sizeof = {
sizeof_str ~ type ~ eq_eq_str ~ expr
}

spaced_is_str = { is_str }

global_layout = {
layout_str ~ type ~ is_str ~ identifier ~ eq_eq_str ~ literal
layout_str ~ type ~ spaced_is_str ~ identifier ~ eq_eq_str ~ literal
~ (spaced_comma_str ~ identifier ~ eq_eq_str ~ literal)?
}

Expand Down Expand Up @@ -1116,12 +1118,20 @@ expr_as = {
as_str ~ type
}

has_or_nothas = {
bang_str? ~ has_str
}

expr_has = {
has_str ~ expr
has_or_nothas ~ expr
}

is_or_notis = {
bang_str? ~ is_str
}

expr_is = {
is_str ~ type
is_or_notis ~ type
}

expr_matches = {
Expand Down
39 changes: 39 additions & 0 deletions tests/verus-consistency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2780,3 +2780,42 @@ verus!{
} // verus!
")
}

#[test]
fn verus_support_is_has_notis_nothas() {
let file = r#"
verus!{
proof fn uses_spec_has(s: Set<int>)
requires
s has 3,
s !has 4,
{
}
proof fn uses_spec_is(t: ThisOrThat)
requires
t is This,
t !is That
{
}
}
"#;
assert_snapshot!(parse_and_format(file).unwrap(), @r"
verus! {
proof fn uses_spec_has(s: Set<int>)
requires
s has 3,
s !has 4,
{
}
proof fn uses_spec_is(t: ThisOrThat)
requires
t is This,
t !is That,
{
}
} // verus!
")
}

0 comments on commit fe40973

Please sign in to comment.