1- use proc_macro2:: Span ;
1+ use std:: ops:: Range ;
2+
23use syn:: spanned:: Spanned ;
34
45use crate :: parse:: { ParsedItem , ParsedLeanItem } ;
56
6- /// Appends the spans of text that should be blanked out in the shadow crate.
7+ /// Appends the byte ranges that should be blanked out in the shadow crate.
78///
89/// For `unsafe` functions with Hermes annotations, this targets:
910/// 1. The `unsafe` keyword (to make the function signature "safe" for Aeneas).
10- /// 2. The entire function block (to remove the unverified implementation ).
11- pub fn append_edits ( item : & ParsedLeanItem , edits : & mut Vec < Span > ) {
11+ /// 2. The *contents* of the function block (preserving the braces `{}` ).
12+ pub fn append_edits ( item : & ParsedLeanItem , edits : & mut Vec < Range < usize > > ) {
1213 if let ParsedItem :: Fn ( func) = & item. item {
1314 if let Some ( unsafety) = & func. sig . unsafety {
1415 // 1. Mark the `unsafe` keyword for blanking.
1516 // Result: `unsafe fn` -> ` fn`
16- edits. push ( unsafety. span ( ) ) ;
17+ edits. push ( unsafety. span ( ) . byte_range ( ) ) ;
1718
1819 // TODO:
1920 // - Only blank bodies for functions which are modeled.
2021 // - Figure out what to replace these bodies with.
21- edits. push ( func. block . span ( ) ) ;
22+
23+ // 2. Mark the *inside* of the block for blanking.
24+ // We use start+1 and end-1 to preserve the curly braces.
25+ let block_range = func. block . span ( ) . byte_range ( ) ;
26+ if block_range. len ( ) >= 2 {
27+ edits. push ( block_range. start + 1 ..block_range. end - 1 ) ;
28+ }
2229 }
2330 }
2431}
@@ -34,9 +41,9 @@ pub fn append_edits(item: &ParsedLeanItem, edits: &mut Vec<Span>) {
3441/// # Panics
3542///
3643/// Panics if any span in `edits` is not in-bounds of `buffer`.
37- pub fn apply_edits ( buffer : & mut [ u8 ] , edits : & [ Span ] ) {
38- for span in edits {
39- for byte in & mut buffer[ span . byte_range ( ) ] {
44+ pub fn apply_edits ( buffer : & mut [ u8 ] , edits : & [ Range < usize > ] ) {
45+ for range in edits {
46+ for byte in & mut buffer[ range . clone ( ) ] {
4047 if * byte != b'\n' && * byte != b'\r' {
4148 * byte = b' ' ;
4249 }
@@ -59,7 +66,8 @@ mod tests {
5966 _ => panic ! ( "Expected function" ) ,
6067 } ;
6168
62- let edits = vec ! [ func. sig. unsafety. unwrap( ) . span( ) , func. block. span( ) ] ;
69+ let edits =
70+ vec ! [ func. sig. unsafety. unwrap( ) . span( ) . byte_range( ) , func. block. span( ) . byte_range( ) ] ;
6371
6472 apply_edits ( & mut buffer, & edits) ;
6573
@@ -91,9 +99,9 @@ mod tests {
9199 /// ```lean
92100 /// theorem foo : True := by trivial
93101 /// ```
94- fn foo()
102+ fn foo() {
95103
96-
104+ }
97105 " ;
98106 assert_eq ! ( std:: str :: from_utf8( & buffer) . unwrap( ) , expected) ;
99107 }
0 commit comments