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

disallow #[trigger <expr>], fixes #1456 #1458

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 41 additions & 29 deletions source/builtin_macros/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3161,10 +3161,47 @@ impl VisitMut for Visitor {
}

fn visit_attribute_mut(&mut self, attr: &mut Attribute) {
fn path_verifier(
span: Span,
) -> Punctuated<syn_verus::PathSegment, syn_verus::token::PathSep> {
let mut path_segments = Punctuated::new();
path_segments.push(syn_verus::PathSegment {
ident: Ident::new("verifier", span),
arguments: syn_verus::PathArguments::None,
});
path_segments
}
fn invalid_attribute(span: Span, trigger: bool) -> Attribute {
let mut path_segments = path_verifier(span);
path_segments.push(syn_verus::PathSegment {
ident: if trigger {
Ident::new("invalid_trigger_attribute", span)
} else {
Ident::new("invalid_attribute", span)
},
arguments: syn_verus::PathArguments::None,
});
let path = Path { leading_colon: None, segments: path_segments };
Attribute {
pound_token: token::Pound { spans: [span] },
style: syn_verus::AttrStyle::Outer,
bracket_token: token::Bracket { span: into_spans(span) },
meta: syn_verus::Meta::Path(path),
}
}
if let syn_verus::AttrStyle::Outer = attr.style {
match &attr.path().segments.iter().map(|x| &x.ident).collect::<Vec<_>>()[..] {
[attr_name] if attr_name.to_string() == "trigger" => {
*attr = mk_verus_attr(attr.span(), quote! { trigger });
let mut valid = true;
if let syn_verus::Meta::List(list) = &attr.meta {
if !list.tokens.is_empty() {
*attr = invalid_attribute(attr.span(), true);
valid = false;
}
}
if valid {
*attr = mk_verus_attr(attr.span(), quote! { trigger });
}
}
[attr_name] if attr_name.to_string() == "via_fn" => {
*attr = mk_verus_attr(attr.span(), quote! { via });
Expand All @@ -3174,34 +3211,9 @@ impl VisitMut for Visitor {
let Ok(parsed) = attr.parse_args_with(
Punctuated::<syn_verus::Meta, Token![,]>::parse_terminated,
) else {
*attr = invalid_attribute(span);
*attr = invalid_attribute(span, false);
return;
};
fn path_verifier(
span: Span,
) -> Punctuated<syn_verus::PathSegment, syn_verus::token::PathSep>
{
let mut path_segments = Punctuated::new();
path_segments.push(syn_verus::PathSegment {
ident: Ident::new("verifier", span),
arguments: syn_verus::PathArguments::None,
});
path_segments
}
fn invalid_attribute(span: Span) -> Attribute {
let mut path_segments = path_verifier(span);
path_segments.push(syn_verus::PathSegment {
ident: Ident::new("invalid_attribute", span),
arguments: syn_verus::PathArguments::None,
});
let path = Path { leading_colon: None, segments: path_segments };
Attribute {
pound_token: token::Pound { spans: [span] },
style: syn_verus::AttrStyle::Outer,
bracket_token: token::Bracket { span: into_spans(span) },
meta: syn_verus::Meta::Path(path),
}
}
match parsed {
meta_list if meta_list.len() == 1 => {
let (second_segment, nested) = match &meta_list[0] {
Expand All @@ -3211,7 +3223,7 @@ impl VisitMut for Visitor {
}
syn_verus::Meta::Path(meta_path) => (&meta_path.segments[0], None),
_ => {
*attr = invalid_attribute(span);
*attr = invalid_attribute(span, false);
return;
}
};
Expand All @@ -3237,7 +3249,7 @@ impl VisitMut for Visitor {
};
}
_ => {
*attr = invalid_attribute(span);
*attr = invalid_attribute(span, false);
return;
}
}
Expand Down
6 changes: 6 additions & 0 deletions source/rust_verify/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,12 @@ pub(crate) fn parse_attrs(
AttrTree::Fun(_, arg, None) if arg == "type_invariant" => {
v.push(Attr::TypeInvariantFn)
}
AttrTree::Fun(_, arg, None) if arg == "invalid_trigger_attribute" => {
return err_span(
span,
"invalid trigger attribute: to provide a trigger expression, use the #![trigger <expr>] attribute",
);
}
_ => return err_span(span, "unrecognized verifier attribute"),
},
AttrPrefix::Verus(verus_prefix) => match verus_prefix {
Expand Down
18 changes: 18 additions & 0 deletions source/rust_verify_test/tests/quantifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,21 @@ test_verify_one_file! {
}
} => Ok(())
}

test_verify_one_file! {
#[test] test_trigger_with_expression_should_be_disallowed_regression_1456 verus_code! {
spec fn a(x: int) -> bool;
spec fn b(x: int) -> bool;
spec fn c(x: int) -> bool;

proof fn test(i: int)
requires
forall|x: int| #[trigger a(x)] c(x) && (a(x) ==> b(x)),
a(i),
ensures
b(i),
{

}
} => Err(err) => assert_vir_error_msg(err, "invalid trigger attribute: to provide a trigger expression, use the #![trigger <expr>] attribute")
}