33use proc_macro2:: Span ;
44use quote:: { ToTokens , quote} ;
55use syn:: {
6- parenthesized,
6+ Block , Expr , ExprClosure , Ident , ItemFn , Meta , Pat , Token , parenthesized,
77 parse:: { Parse , ParseStream , Result } ,
88 parse_quote,
99 punctuated:: Punctuated ,
1010 spanned:: Spanned ,
11- Block , Expr , ExprClosure , Ident , ItemFn , Meta , Pat , Token ,
1211} ;
1312
1413/// A contract specifies the intended behavior of a function or method.
@@ -22,17 +21,21 @@ pub struct Contract {
2221 pub ensures : Vec < ConditionClosure > ,
2322}
2423
25- /// An expression guarded by a `cfg` attribute .
24+ /// A Condition represented by a `bool`-valued expression .
2625#[ derive( Debug ) ]
2726pub struct Condition {
27+ /// A setting to control when the condition should be present via a `#[cfg]` annotation.
2828 pub cfg : Option < Meta > ,
29+ /// The expression.
2930 pub expr : Expr ,
3031}
3132
32- /// A closure expression guarded by a `cfg` attribute .
33+ /// A Condition represented by a `bool`-valued closure .
3334#[ derive( Debug ) ]
3435pub struct ConditionClosure {
36+ /// A setting to control when the condition should be present via a `#[cfg]` annotation.
3537 pub cfg : Option < Meta > ,
38+ // The closure.
3639 pub closure : ExprClosure ,
3740}
3841
@@ -69,24 +72,20 @@ impl TryFrom<ContractArgs> for Contract {
6972 match arg {
7073 ContractArg :: Requires { cfg, expr } => {
7174 if let Expr :: Array ( conditions) = expr {
72- requires. extend (
73- conditions
74- . elems
75- . into_iter ( )
76- . map ( |expr| Condition { cfg : cfg. clone ( ) , expr } ) ,
77- ) ;
75+ requires. extend ( conditions. elems . into_iter ( ) . map ( |expr| Condition {
76+ cfg : cfg. clone ( ) ,
77+ expr,
78+ } ) ) ;
7879 } else {
7980 requires. push ( Condition { cfg, expr } ) ;
8081 }
8182 }
8283 ContractArg :: Maintains { cfg, expr } => {
8384 if let Expr :: Array ( conditions) = expr {
84- maintains. extend (
85- conditions
86- . elems
87- . into_iter ( )
88- . map ( |expr| Condition { cfg : cfg. clone ( ) , expr } ) ,
89- ) ;
85+ maintains. extend ( conditions. elems . into_iter ( ) . map ( |expr| Condition {
86+ cfg : cfg. clone ( ) ,
87+ expr,
88+ } ) ) ;
9089 } else {
9190 maintains. push ( Condition { cfg, expr } ) ;
9291 }
@@ -102,12 +101,10 @@ impl TryFrom<ContractArgs> for Contract {
102101 }
103102 ContractArg :: Ensures { cfg, expr } => {
104103 if let Expr :: Array ( conditions) = expr {
105- ensures_exprs. extend (
106- conditions
107- . elems
108- . into_iter ( )
109- . map ( |expr| Condition { cfg : cfg. clone ( ) , expr } ) ,
110- ) ;
104+ ensures_exprs. extend ( conditions. elems . into_iter ( ) . map ( |expr| Condition {
105+ cfg : cfg. clone ( ) ,
106+ expr,
107+ } ) ) ;
111108 } else {
112109 ensures_exprs. push ( Condition { cfg, expr } ) ;
113110 }
@@ -297,7 +294,10 @@ pub fn instrument_function_body(contract: &Contract, func: &ItemFn) -> Result<Bl
297294 . chain ( contract. ensures . iter ( ) . map ( |condition_closure| {
298295 let closure = & condition_closure. closure ;
299296 let msg = format ! ( "Postcondition failed: {}" , closure. to_token_stream( ) ) ;
300- let cfg = condition_closure. cfg . as_ref ( ) . map ( |c| quote ! { #[ cfg( #c) ] } ) ;
297+ let cfg = condition_closure
298+ . cfg
299+ . as_ref ( )
300+ . map ( |c| quote ! { #[ cfg( #c) ] } ) ;
301301 quote ! { #cfg assert!( ( #closure) ( #binding_ident) , #msg) ; }
302302 } ) ) ;
303303
0 commit comments