Skip to content

Commit

Permalink
fix spans for binary ops
Browse files Browse the repository at this point in the history
while preserving the safety of parenthesizing incoming LHS/RHS
  • Loading branch information
juliand665 committed Jan 7, 2023
1 parent 0916b01 commit b0db31e
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 92 deletions.
19 changes: 12 additions & 7 deletions prusti-contracts/prusti-specs/src/specifications/preparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,20 +878,25 @@ impl PrustiBinaryOp {
}
}

fn translate(&self, span: Span, lhs: TokenStream, rhs: TokenStream) -> TokenStream {
fn translate(&self, span: Span, raw_lhs: TokenStream, raw_rhs: TokenStream) -> TokenStream {
let lhs = quote_spanned! { raw_lhs.span() => (#raw_lhs) };
let rhs = quote_spanned! { raw_rhs.span() => (#raw_rhs) };
match self {
Self::Rust(op) => op.translate(span, lhs, rhs),
Self::Rust(op) => op.translate(span, raw_lhs, raw_rhs),
// implication is desugared into this form to avoid evaluation
// order issues: `f(a, b)` makes Rust evaluate both `a` and `b`
// before the `f` call
Self::Implies => {
// preserve span of LHS
let not_lhs = quote_spanned! { lhs.span() => !(#lhs) };
quote_spanned! { span => (#not_lhs || (#rhs)) }
let not_lhs = quote_spanned! { lhs.span() => !#lhs };
quote_spanned! { span => #not_lhs || #rhs }
}
Self::Or => quote_spanned! { span => #lhs || #rhs },
Self::And => quote_spanned! { span => #lhs && #rhs },
Self::SnapEq => {
let joined_span = join_spans(lhs.span(), rhs.span());
quote_spanned! { joined_span => snapshot_equality(&#lhs, &#rhs) }
}
Self::Or => quote_spanned! { span => (#lhs) || (#rhs) },
Self::And => quote_spanned! { span => (#lhs) && (#rhs) },
Self::SnapEq => quote_spanned! { span => snapshot_equality(&(#lhs), &(#rhs)) },
}
}
}
Expand Down
98 changes: 49 additions & 49 deletions prusti-tests/tests/parse/ui/composite.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use prusti_contracts::*;
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test1_$(NUM_UUID)() -> bool {
!!(((!((true) && (true)) || ((true) && (true)))): bool)
!!((!((true) && (true)) || ((true) && (true))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -51,7 +51,7 @@ fn test1() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test2_$(NUM_UUID)() -> bool {
!!(((((true) && (((!(true) || (true))))) && (((true) || (true)))) &&
!!(((((true) && ((!(true) || (true)))) && (((true) || (true)))) &&
(true)): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
Expand All @@ -61,7 +61,7 @@ fn test2() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test3_$(NUM_UUID)() -> bool {
!!(((!(((true) && (true))) || (true))): bool)
!!((!(((true) && (true))) || (true)): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -70,7 +70,7 @@ fn test3() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test4_$(NUM_UUID)() -> bool {
!!(((!((((!(true) || (true)))) && (true)) || (true))): bool)
!!((!(((!(true) || (true))) && (true)) || (true)): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -79,8 +79,8 @@ fn test4() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test5_$(NUM_UUID)() -> bool {
!!(((((!(true) || (true)))) &&
(((!(true) || ((true) && (((true) || (true)))))))): bool)
!!((((!(true) || (true))) &&
((!(true) || ((true) && (((true) || (true))))))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -89,8 +89,8 @@ fn test5() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test6_$(NUM_UUID)() -> bool {
!!(((!(((true) && (true))) ||
((!(true) || ((!(true) || ((!(true) || (true))))))))): bool)
!!((!(((true) && (true))) ||
(!(true) || (!(true) || (!(true) || (true))))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -99,8 +99,8 @@ fn test6() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test7_$(NUM_UUID)() -> bool {
!!(((!(((true) && (true))) ||
((!(((true) && (true))) || (((true) && (true))))))): bool)
!!((!(((true) && (true))) ||
(!(((true) && (true))) || (((true) && (true))))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -109,7 +109,7 @@ fn test7() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test8_$(NUM_UUID)() -> bool {
!!(((!(((true) || (true))) || (((true) || (true))))): bool)
!!((!(((true) || (true))) || (((true) || (true)))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -118,8 +118,8 @@ fn test8() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test9_$(NUM_UUID)() -> bool {
!!(((!(((true) || (true))) ||
(((true) || (((true) && (((true) || (true))))))))): bool)
!!((!(((true) || (true))) ||
(((true) || (((true) && (((true) || (true)))))))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand Down Expand Up @@ -151,10 +151,10 @@ fn test12() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test13_$(NUM_UUID)() -> bool {
!!(((!(true) ||
((!(forall((),
#[prusti::spec_only] |a: i32, b: i32| -> bool
{ ((a == 5): bool) })) || (true))))): bool)
!!((!(true) ||
(!(forall((),
#[prusti::spec_only] |a: i32, b: i32| -> bool
{ ((a == 5): bool) })) || (true))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -163,10 +163,10 @@ fn test13() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test14_$(NUM_UUID)() -> bool {
!!(((!(true) ||
(forall((),
#[prusti::spec_only] |a: i32| -> bool
{ ((a == 5): bool) })))): bool)
!!((!(true) ||
(forall((),
#[prusti::spec_only] |a: i32| -> bool
{ ((a == 5): bool) }))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -175,9 +175,9 @@ fn test14() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test15_$(NUM_UUID)() -> bool {
!!(((!(forall((),
#[prusti::spec_only] |a: i32| -> bool { ((a == 5): bool) }))
|| (true))): bool)
!!((!(forall((),
#[prusti::spec_only] |a: i32| -> bool { ((a == 5): bool) }))
|| (true)): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -186,13 +186,13 @@ fn test15() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test16_$(NUM_UUID)() -> bool {
!!(((!(forall((),
#[prusti::spec_only] |b: i32| -> bool
{ ((b == 10): bool) })) ||
((!(true) ||
(forall((),
#[prusti::spec_only] |a: u32, b: u32| -> bool
{ ((a == 5): bool) })))))): bool)
!!((!(forall((),
#[prusti::spec_only] |b: i32| -> bool
{ ((b == 10): bool) })) ||
(!(true) ||
(forall((),
#[prusti::spec_only] |a: u32, b: u32| -> bool
{ ((a == 5): bool) })))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand Down Expand Up @@ -224,10 +224,10 @@ fn test19() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test20_$(NUM_UUID)() -> bool {
!!(((!(true) ||
((!(exists((),
#[prusti::spec_only] |a: i32, b: i32| -> bool
{ ((a == 5): bool) })) || (true))))): bool)
!!((!(true) ||
(!(exists((),
#[prusti::spec_only] |a: i32, b: i32| -> bool
{ ((a == 5): bool) })) || (true))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -236,10 +236,10 @@ fn test20() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test21_$(NUM_UUID)() -> bool {
!!(((!(true) ||
(exists((),
#[prusti::spec_only] |a: i32| -> bool
{ ((a == 5): bool) })))): bool)
!!((!(true) ||
(exists((),
#[prusti::spec_only] |a: i32| -> bool
{ ((a == 5): bool) }))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -248,9 +248,9 @@ fn test21() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test22_$(NUM_UUID)() -> bool {
!!(((!(exists((),
#[prusti::spec_only] |a: i32| -> bool { ((a == 5): bool) }))
|| (true))): bool)
!!((!(exists((),
#[prusti::spec_only] |a: i32| -> bool { ((a == 5): bool) }))
|| (true)): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -259,13 +259,13 @@ fn test22() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test23_$(NUM_UUID)() -> bool {
!!(((!(exists((),
#[prusti::spec_only] |b: i32| -> bool
{ ((b == 10): bool) })) ||
((!(true) ||
(exists((),
#[prusti::spec_only] |a: u32, b: u32| -> bool
{ ((a == 5): bool) })))))): bool)
!!((!(exists((),
#[prusti::spec_only] |b: i32| -> bool
{ ((b == 10): bool) })) ||
(!(true) ||
(exists((),
#[prusti::spec_only] |a: u32, b: u32| -> bool
{ ((a == 5): bool) })))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand Down
6 changes: 2 additions & 4 deletions prusti-tests/tests/parse/ui/exists.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ fn test2() {}
fn prusti_pre_item_test3_$(NUM_UUID)() -> bool {
!!((exists((),
#[prusti::spec_only] |a: i32, b: i32| -> bool
{ (((!(a + b == a + b) || (a + b == a + b))): bool) })):
bool)
{ ((!(a + b == a + b) || (a + b == a + b)): bool) })): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand Down Expand Up @@ -92,8 +91,7 @@ fn prusti_pre_item_test6_$(NUM_UUID)() -> bool {
#[prusti::spec_only] |a: i32, b: i32| (2)),
(#[prusti::spec_only] |a: i32, b: i32| (1),)),
#[prusti::spec_only] |a: i32, b: i32| -> bool
{ (((!(a + b == a + b) || (a + b == a + b))): bool) })):
bool)
{ ((!(a + b == a + b) || (a + b == a + b)): bool) })): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand Down
6 changes: 2 additions & 4 deletions prusti-tests/tests/parse/ui/forall.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ fn test2() {}
fn prusti_pre_item_test3_$(NUM_UUID)() -> bool {
!!((forall((),
#[prusti::spec_only] |a: i32, b: i32| -> bool
{ (((!(a + b == a + b) || (a + b == a + b))): bool) })):
bool)
{ ((!(a + b == a + b) || (a + b == a + b)): bool) })): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand Down Expand Up @@ -92,8 +91,7 @@ fn prusti_pre_item_test6_$(NUM_UUID)() -> bool {
#[prusti::spec_only] |a: i32, b: i32| (2)),
(#[prusti::spec_only] |a: i32, b: i32| (1),)),
#[prusti::spec_only] |a: i32, b: i32| -> bool
{ (((!(a + b == a + b) || (a + b == a + b))): bool) })):
bool)
{ ((!(a + b == a + b) || (a + b == a + b)): bool) })): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand Down
20 changes: 10 additions & 10 deletions prusti-tests/tests/parse/ui/implies.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use prusti_contracts::*;
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test1_$(NUM_UUID)() -> bool {
!!(((!(true) || (true))): bool)
!!((!(true) || (true)): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -37,7 +37,7 @@ fn test1() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test2_$(NUM_UUID)() -> bool {
!!(((!(true) || ((!(true) || (true))))): bool)
!!((!(true) || (!(true) || (true))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -46,7 +46,7 @@ fn test2() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test3_$(NUM_UUID)() -> bool {
!!(((!(true) || (((!(true) || (true)))))): bool)
!!((!(true) || ((!(true) || (true)))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -55,7 +55,7 @@ fn test3() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test4_$(NUM_UUID)() -> bool {
!!(((!(((!(true) || (true)))) || (true))): bool)
!!((!((!(true) || (true))) || (true)): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -64,7 +64,7 @@ fn test4() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test5_$(NUM_UUID)() -> bool {
!!(((!(((!(true) || (true)))) || (((!(true) || (true)))))): bool)
!!((!((!(true) || (true))) || ((!(true) || (true)))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -73,7 +73,7 @@ fn test5() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test21_$(NUM_UUID)() -> bool {
!!(((!(true) || (true))): bool)
!!((!(true) || (true)): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -82,7 +82,7 @@ fn test21() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test22_$(NUM_UUID)() -> bool {
!!(((!(true) || ((!(true) || (true))))): bool)
!!((!(true) || (!(true) || (true))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -91,7 +91,7 @@ fn test22() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test23_$(NUM_UUID)() -> bool {
!!(((!(true) || (((!(true) || (true)))))): bool)
!!((!(true) || ((!(true) || (true)))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -100,7 +100,7 @@ fn test23() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test24_$(NUM_UUID)() -> bool {
!!(((!(((!(true) || (true)))) || (true))): bool)
!!((!((!(true) || (true))) || (true)): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand All @@ -109,7 +109,7 @@ fn test24() {}
#[prusti::spec_only]
#[prusti::spec_id = "$(NUM_UUID)"]
fn prusti_pre_item_test25_$(NUM_UUID)() -> bool {
!!(((!(((!(true) || (true)))) || (((!(true) || (true)))))): bool)
!!((!((!(true) || (true))) || ((!(true) || (true)))): bool)
}
#[prusti::pre_spec_id_ref = "$(NUM_UUID)"]
#[prusti::specs_version = $(SPECS_VERSION)]
Expand Down
4 changes: 2 additions & 2 deletions prusti-tests/tests/parse/ui/predicates.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn prusti_pred_item_forall_implication_$(NUM_UUID)()
-> bool {
!!((forall((),
#[prusti::spec_only] |x: usize| -> bool
{ (((!((x != 0)) || (x * 2 != 0))): bool) })): bool)
{ ((!((x != 0)) || (x * 2 != 0)): bool) })): bool)
}
#[allow(unused_must_use, unused_variables, dead_code)]
#[prusti::pred_spec_id_ref = "$(NUM_UUID)"]
Expand All @@ -96,7 +96,7 @@ fn prusti_pred_item_exists_implication_$(NUM_UUID)()
-> bool {
!!((exists((),
#[prusti::spec_only] |x: usize| -> bool
{ (((!((x != 0)) || (x * 2 != 0))): bool) })): bool)
{ ((!((x != 0)) || (x * 2 != 0)): bool) })): bool)
}
#[allow(unused_must_use, unused_variables, dead_code)]
#[prusti::pred_spec_id_ref = "$(NUM_UUID)"]
Expand Down
Loading

0 comments on commit b0db31e

Please sign in to comment.