Skip to content
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
11 changes: 4 additions & 7 deletions tools/hermes/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,14 @@ where

// If this is a trait impl, we use UFCS syntax (`<Type as Trait>`). If
// this is an inherent impl, we use the `Type: Type` syntax.
let self_ty = &node.self_ty;
let segment = if let Some((_, path, _)) = &node.trait_ {
let self_ty = &node.self_ty;
let tokens = quote! { <#self_ty as #path> };
tokens.to_string().replace(" ", "") // Remove spaces for cleaner paths
quote! { <#self_ty as #path> }
} else {
let self_ty = &node.self_ty;
let tokens = quote! { #self_ty };
tokens.to_string().replace(" ", "")
quote! { #self_ty }
};

self.current_path.push(segment);
self.current_path.push(segment.to_string());
syn::visit::visit_item_impl(self, node);
self.current_path.pop();
}
Expand Down
32 changes: 20 additions & 12 deletions tools/hermes/src/transform.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
use proc_macro2::Span;
use std::ops::Range;

use syn::spanned::Spanned;

use crate::parse::{ParsedItem, ParsedLeanItem};

/// Appends the spans of text that should be blanked out in the shadow crate.
/// Appends the byte ranges that should be blanked out in the shadow crate.
///
/// For `unsafe` functions with Hermes annotations, this targets:
/// 1. The `unsafe` keyword (to make the function signature "safe" for Aeneas).
/// 2. The entire function block (to remove the unverified implementation).
pub fn append_edits(item: &ParsedLeanItem, edits: &mut Vec<Span>) {
/// 2. The *contents* of the function block (preserving the braces `{}`).
pub fn append_edits(item: &ParsedLeanItem, edits: &mut Vec<Range<usize>>) {
if let ParsedItem::Fn(func) = &item.item {
if let Some(unsafety) = &func.sig.unsafety {
// 1. Mark the `unsafe` keyword for blanking.
// Result: `unsafe fn` -> ` fn`
edits.push(unsafety.span());
edits.push(unsafety.span().byte_range());

// TODO:
// - Only blank bodies for functions which are modeled.
// - Figure out what to replace these bodies with.
edits.push(func.block.span());

// 2. Mark the *inside* of the block for blanking.
// We use start+1 and end-1 to preserve the curly braces.
let block_range = func.block.span().byte_range();
if block_range.len() >= 2 {
edits.push(block_range.start + 1..block_range.end - 1);
}
}
}
}
Expand All @@ -34,9 +41,9 @@ pub fn append_edits(item: &ParsedLeanItem, edits: &mut Vec<Span>) {
/// # Panics
///
/// Panics if any span in `edits` is not in-bounds of `buffer`.
pub fn apply_edits(buffer: &mut [u8], edits: &[Span]) {
for span in edits {
for byte in &mut buffer[span.byte_range()] {
pub fn apply_edits(buffer: &mut [u8], edits: &[Range<usize>]) {
for range in edits {
for byte in &mut buffer[range.clone()] {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since Range<usize> is a Copy type, you can dereference range directly instead of calling clone(). This is slightly more idiomatic.

Suggested change
for byte in &mut buffer[range.clone()] {
for byte in &mut buffer[*range] {

if !matches!(*byte, b'\n' | b'\r') {
*byte = b' ';
}
Expand All @@ -59,7 +66,8 @@ mod tests {
_ => panic!("Expected function"),
};

let edits = vec![func.sig.unsafety.unwrap().span(), func.block.span()];
let edits =
vec![func.sig.unsafety.unwrap().span().byte_range(), func.block.span().byte_range()];

apply_edits(&mut buffer, &edits);

Expand Down Expand Up @@ -91,9 +99,9 @@ mod tests {
/// ```lean
/// theorem foo : True := by trivial
/// ```
fn foo()
fn foo() {

}
";
assert_eq!(std::str::from_utf8(&buffer).unwrap(), expected);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[[command]]
# Expected: crate::<*constFooasT1>::m1, crate::<[Foo;5]asT3>::m3, crate::<[Foo]asT2>::m2
args = ["--start-from", "crate::<*constFooasT1>::m1,crate::<[Foo;5]asT3>::m3,crate::<[Foo]asT2>::m2"]
args = ["--start-from", "crate::< * const Foo as T1 >::m1,crate::< [Foo ; 5] as T3 >::m3,crate::< [Foo] as T2 >::m2"]
should_not_exist = false
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
failure
4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/crlf_endings/expected/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
fn windows() {}

/// ```lean
/// ```
fn _hermes_dummy() {}
4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/crlf_endings/source/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
fn windows() {}

/// ```lean
/// ```
fn _hermes_dummy() {}
Comment on lines +3 to +5
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This dummy function with a lean block is being added to many test fixtures. While this likely fixes the tests, it seems like a workaround. It suggests that hermes might not handle crates with no annotated items gracefully. It might be better to fix hermes to support this case, which would allow removing these dummy functions from the test fixtures.

4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/custom_path_mod/source/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#[path = "sys/unix.rs"]
mod sys;


/// ```lean
/// ```
fn _hermes_dummy() {}
4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/deep_invocation/source/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
pub mod nested;


/// ```lean
/// ```
fn _hermes_dummy() {}
4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/dir_mod/source/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
mod bar;


/// ```lean
/// ```
fn _hermes_dummy() {}
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
fn clean() {}


/// ```lean
/// ```
fn _hermes_dummy() {}
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
fn clean() {}


/// ```lean
/// ```
fn _hermes_dummy() {}
4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/empty_file/expected/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

/// ```lean
/// ```
fn _hermes_dummy() {}
4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/empty_file/source/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

/// ```lean
/// ```
fn _hermes_dummy() {}
4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/file_mod/expected/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
mod foo;


/// ```lean
/// ```
fn _hermes_dummy() {}
4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/file_mod/source/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
mod foo;


/// ```lean
/// ```
fn _hermes_dummy() {}
4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/ignored_files/expected/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
fn keep() {}


/// ```lean
/// ```
fn _hermes_dummy() {}
4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/ignored_files/source/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
fn keep() {}


/// ```lean
/// ```
fn _hermes_dummy() {}
4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/inline_mod/expected/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ mod foo {
fn bar() {}
}


/// ```lean
/// ```
fn _hermes_dummy() {}
4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/inline_mod/source/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ mod foo {
fn bar() {}
}


/// ```lean
/// ```
fn _hermes_dummy() {}
4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/large_assets/source/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
fn code() {}


/// ```lean
/// ```
fn _hermes_dummy() {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#[cfg(target_os = "fake_os")]
mod fake;


/// ```lean
/// ```
fn _hermes_dummy() {}
4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/missing_cfg_mod/source/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#[cfg(target_os = "fake_os")]
mod fake;


/// ```lean
/// ```
fn _hermes_dummy() {}
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
pub fn shared() {}


/// ```lean
/// ```
fn _hermes_dummy() {}
4 changes: 4 additions & 0 deletions tools/hermes/tests/fixtures/multi_artifact/source/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
pub fn shared() {}


/// ```lean
/// ```
fn _hermes_dummy() {}
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
pub fn check() {}


/// ```lean
/// ```
fn _hermes_dummy() {}
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
pub fn check() {}


/// ```lean
/// ```
fn _hermes_dummy() {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/// ```lean
/// ```
fn main() {}
pub fn main() {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
failure
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/// ```lean
/// ```
fn main() {}
pub fn main() {}
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
fn main() {}


/// ```lean
/// ```
fn _hermes_dummy() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
failure
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
fn main() {}


/// ```lean
/// ```
fn _hermes_dummy() {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// ```lean
/// model foo
/// ```
fn foo() -> i32
fn foo() -> i32 {

}
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
pub fn foo() {}


/// ```lean
/// ```
fn _hermes_dummy() {}
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
pub fn foo() {}


/// ```lean
/// ```
fn _hermes_dummy() {}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/// ```lean
/// model async_foo
/// ```
async fn async_foo() -> i32
async fn async_foo() -> i32 { }

/// ```lean
/// model const_foo
/// ```
const fn const_foo() -> i32
const fn const_foo() -> i32 { }

/// ```lean
/// model extern_foo
/// ```
extern "C" fn extern_foo() -> i32
extern "C" fn extern_foo() -> i32 { }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
failure
7 changes: 7 additions & 0 deletions tools/hermes/tests/fixtures/weird_functions/source/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tools/hermes/tests/fixtures/weird_functions/source/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
name = "weird"
version = "0.1.0"
edition = "2021"

[workspace]
Loading