@@ -36,7 +36,6 @@ use syn_verus::{
3636} ;
3737
3838const VERUS_SPEC : & str = "VERUS_SPEC__" ;
39- const VERUS_UNERASED_PROXY : & str = "VERUS_UNERASED_PROXY__" ;
4039
4140fn take_expr ( expr : & mut Expr ) -> Expr {
4241 let dummy: Expr = Expr :: Verbatim ( TokenStream :: new ( ) ) ;
@@ -69,8 +68,8 @@ enum InsideArith {
6968 Fixed ,
7069}
7170
72- struct Visitor {
73- erase_ghost : EraseGhost ,
71+ pub ( crate ) struct Visitor {
72+ pub ( crate ) erase_ghost : EraseGhost ,
7473 // TODO: this should always be true
7574 use_spec_traits : bool ,
7675 // inside_ghost > 0 means we're currently visiting ghost code
@@ -94,7 +93,7 @@ struct Visitor {
9493 assign_to : bool ,
9594
9695 // Add extra verus signature information to the docstring
97- rustdoc : bool ,
96+ pub ( crate ) rustdoc : bool ,
9897}
9998
10099// For exec "let pat = init" declarations, recursively find Tracked(x), Ghost(x), x in pat
@@ -463,10 +462,6 @@ fn merge_default_ensures(
463462}
464463
465464impl Visitor {
466- fn needs_unerased_proxies ( & self ) -> bool {
467- self . erase_ghost . keep ( ) && !self . rustdoc
468- }
469-
470465 fn take_ghost < T : Default > ( & self , dest : & mut T ) -> T {
471466 take_ghost ( self . erase_ghost , dest)
472467 }
@@ -511,8 +506,7 @@ impl Visitor {
511506 spec : & mut SignatureSpec ,
512507 ret_pat : Option < ( Pat , TType ) > ,
513508 final_ret_pat : Option < Pat > , // Some(pat) if different from ret_pat,
514- is_const : bool ,
515- span : Span ,
509+ _span : Span ,
516510 ) -> Vec < Stmt > {
517511 let requires = self . take_ghost ( & mut spec. requires ) ;
518512 let recommends = self . take_ghost ( & mut spec. recommends ) ;
@@ -741,16 +735,7 @@ impl Visitor {
741735 }
742736 }
743737
744- if is_const {
745- vec ! [ Stmt :: Expr (
746- Expr :: Verbatim (
747- quote_spanned!( span => #[ verus:: internal( const_header_wrapper) ] || { #( #spec_stmts) * } ; ) ,
748- ) ,
749- None ,
750- ) ]
751- } else {
752- spec_stmts
753- }
738+ spec_stmts
754739 }
755740
756741 fn visit_fn (
@@ -945,8 +930,7 @@ impl Visitor {
945930 self . inside_ghost += 1 ; // for requires, ensures, etc.
946931
947932 let sig_span = sig. span ( ) . clone ( ) ;
948- let spec_stmts =
949- self . take_sig_specs ( & mut sig. spec , ret_pat, None , sig. constness . is_some ( ) , sig_span) ;
933+ let spec_stmts = self . take_sig_specs ( & mut sig. spec , ret_pat, None , sig_span) ;
950934 if !self . erase_ghost . erase ( ) {
951935 stmts. extend ( spec_stmts) ;
952936 }
@@ -992,36 +976,14 @@ impl Visitor {
992976 } ) ) ) ;
993977 }
994978 } else {
995- let ensures = self . take_ghost ( con_ensures) ;
996- if let Some ( Ensures { token, mut exprs, attrs } ) = ensures {
997- self . inside_ghost += 1 ;
998- let mut stmts: Vec < Stmt > = Vec :: new ( ) ;
999- if attrs. len ( ) > 0 {
1000- let err = "outer attributes only allowed on function's ensures" ;
1001- let expr = Expr :: Verbatim ( quote_spanned ! ( token. span => compile_error!( #err) ) ) ;
1002- stmts. push ( Stmt :: Expr ( expr, Some ( Semi { spans : [ token. span ] } ) ) ) ;
1003- } else if exprs. exprs . len ( ) > 0 {
1004- for expr in exprs. exprs . iter_mut ( ) {
1005- self . visit_expr_mut ( expr) ;
1006- }
1007- // Use a closure in the ensures to avoid circular const definition.
1008- // Note: we can't use con.ident as the closure pattern,
1009- // because Rust would treat this as a const path pattern.
1010- // So we use a 0-parameter closure.
1011- stmts. push ( stmt_with_semi ! ( builtin, token. span => #[ verus:: internal( const_header_wrapper) ] || { #builtin:: ensures( || [ #exprs] ) ; } ) ) ;
1012- }
1013- let mut block = std:: mem:: take ( con_block) . expect ( "const-with-ensures block" ) ;
1014- block. stmts . splice ( 0 ..0 , stmts) ;
1015- * con_block = Some ( block) ;
1016- self . inside_ghost -= 1 ;
1017- }
1018979 if let Some ( block) = std:: mem:: take ( con_block) {
1019980 let expr_block = syn_verus:: ExprBlock { attrs : vec ! [ ] , label : None , block : * block } ;
1020981 * con_expr = Some ( Box :: new ( Expr :: Block ( expr_block) ) ) ;
1021982 * con_eq_token = Some ( syn_verus:: token:: Eq { spans : [ con_span] } ) ;
1022983 * con_semi_token = Some ( Semi { spans : [ con_span] } ) ;
1023984 }
1024985 }
986+ * con_ensures = None ;
1025987 }
1026988
1027989 fn visit_const_or_static (
@@ -1840,161 +1802,9 @@ impl Visitor {
18401802 }
18411803 }
18421804
1843- fn item_needs_unerased_proxy ( & self , item : & Item ) -> bool {
1844- match item {
1845- Item :: Const ( item_const) => !is_external ( & item_const. attrs ) ,
1846- Item :: Fn ( item_fn) => item_fn. sig . constness . is_some ( ) && !is_external ( & item_fn. attrs ) ,
1847- _ => false ,
1848- }
1849- }
1850-
1851- fn item_translate_const_to_0_arg_fn ( & self , item : Item ) -> Item {
1852- match item {
1853- Item :: Const ( item_const) => {
1854- let span = item_const. span ( ) ;
1855- let ItemConst {
1856- mut attrs,
1857- vis,
1858- const_token,
1859- ident,
1860- generics,
1861- colon_token,
1862- ty,
1863- eq_token : _,
1864- expr,
1865- semi_token : _,
1866- publish,
1867- mode,
1868- ensures,
1869- block,
1870- } = item_const;
1871- attrs. push ( mk_verus_attr ( span, quote ! { encoded_const } ) ) ;
1872-
1873- let publish = match ( publish, & mode, & vis) {
1874- ( publish, _, Visibility :: Inherited ) => publish,
1875- (
1876- Publish :: Default ,
1877- FnMode :: Spec ( _) | FnMode :: SpecChecked ( _) | FnMode :: Default ,
1878- _,
1879- ) => Publish :: Open ( syn_verus:: Open { token : token:: Open { span } } ) ,
1880- ( publish, _, _) => publish,
1881- } ;
1882-
1883- Item :: Fn ( ItemFn {
1884- attrs,
1885- vis,
1886- sig : Signature {
1887- spec : SignatureSpec {
1888- prover : None ,
1889- requires : None ,
1890- recommends : None ,
1891- ensures,
1892- default_ensures : None ,
1893- returns : None ,
1894- decreases : None ,
1895- invariants : None ,
1896- unwind : None ,
1897- with : None ,
1898- } ,
1899- publish,
1900- constness : None ,
1901- asyncness : None ,
1902- unsafety : None ,
1903- abi : None ,
1904- broadcast : None ,
1905- mode : mode,
1906- fn_token : token:: Fn { span : const_token. span } ,
1907- ident : ident. clone ( ) ,
1908- generics,
1909- paren_token : Paren { span : into_spans ( colon_token. span ) } ,
1910- inputs : Punctuated :: new ( ) ,
1911- variadic : None ,
1912- output : ReturnType :: Type (
1913- token:: RArrow { spans : [ colon_token. span , colon_token. span ] } ,
1914- None ,
1915- None , /*
1916- Some(Box::new((
1917- Paren { span: into_spans(colon_token.span) },
1918- Pat::Verbatim(quote!{ #ident }),
1919- colon_token,
1920- ))), */
1921- ty,
1922- ) ,
1923- } ,
1924- block : ( match block {
1925- Some ( block) => block,
1926- _ => {
1927- let expr = expr. unwrap ( ) ;
1928- Box :: new ( Block {
1929- brace_token : Brace { span : into_spans ( expr. span ( ) ) } ,
1930- stmts : vec ! [ Stmt :: Expr ( * expr, None ) ] ,
1931- } )
1932- }
1933- } ) ,
1934- semi_token : None ,
1935- } )
1936- }
1937- item => item,
1938- }
1939- }
1940-
1941- fn item_make_proxy ( & self , item : Item ) -> Item {
1942- let mut item = item;
1943- item = self . item_translate_const_to_0_arg_fn ( item) ;
1944- match & mut item {
1945- Item :: Fn ( item_fn) => {
1946- item_fn. sig . ident = Ident :: new (
1947- & format ! ( "{}{}" , VERUS_UNERASED_PROXY , & item_fn. sig. ident) ,
1948- item_fn. sig . span ( ) ,
1949- ) ;
1950- item_fn. attrs . push ( mk_verus_attr ( item_fn. span ( ) , quote ! { unerased_proxy } ) ) ;
1951- }
1952- _ => unreachable ! ( ) ,
1953- }
1954- item
1955- }
1956-
1957- fn item_make_external_and_erased ( & mut self , item : Item ) -> Item {
1958- let mut item = item;
1959-
1960- self . erase_ghost = EraseGhost :: Erase ;
1961- self . visit_item_mut ( & mut item) ;
1962- self . erase_ghost = EraseGhost :: Keep ;
1963-
1964- let span = item. span ( ) ;
1965- let attributes = match & mut item {
1966- Item :: Fn ( item_fn) => & mut item_fn. attrs ,
1967- Item :: Const ( item_const) => & mut item_const. attrs ,
1968- _ => unreachable ! ( ) ,
1969- } ;
1970- attributes. push ( mk_verifier_attr ( span, quote ! { external } ) ) ;
1971- attributes. push ( mk_verus_attr ( span, quote ! { uses_unerased_proxy } ) ) ;
1972-
1973- item
1974- }
1975-
1976- fn visit_items_make_unerased_proxies ( & mut self , items : & mut Vec < Item > ) {
1977- if !self . needs_unerased_proxies ( ) {
1978- return ;
1979- }
1980-
1981- let mut new_items = vec ! [ ] ;
1982-
1983- for item in std:: mem:: take ( items) . into_iter ( ) {
1984- if self . item_needs_unerased_proxy ( & item) {
1985- let proxy = self . item_make_proxy ( item. clone ( ) ) ;
1986- let erased = self . item_make_external_and_erased ( item) ;
1987- new_items. push ( proxy) ;
1988- new_items. push ( erased) ;
1989- } else {
1990- new_items. push ( item) ;
1991- }
1992- }
1993-
1994- * items = new_items;
1995- }
1996-
19971805 fn visit_impl_items_prefilter ( & mut self , items : & mut Vec < ImplItem > , for_trait : bool ) {
1806+ self . visit_impl_items_make_unerased_proxies ( items, for_trait) ;
1807+
19981808 if self . erase_ghost . erase_all ( ) {
19991809 items. retain ( |item| match item {
20001810 ImplItem :: Fn ( fun) => match fun. sig . mode {
@@ -4865,13 +4675,7 @@ pub(crate) fn sig_specs_attr(
48654675 rustdoc : env_rustdoc ( ) ,
48664676 } ;
48674677 let sig_span = sig. span ( ) . clone ( ) ;
4868- spec_stmts. extend ( visitor. take_sig_specs (
4869- & mut spec,
4870- ret_pat,
4871- final_ret_pat,
4872- sig. constness . is_some ( ) ,
4873- sig_span,
4874- ) ) ;
4678+ spec_stmts. extend ( visitor. take_sig_specs ( & mut spec, ret_pat, final_ret_pat, sig_span) ) ;
48754679 spec_stmts
48764680}
48774681
0 commit comments