Skip to content

Commit 0556c73

Browse files
committed
[hermes] Clean up after removing shadow crate support
gherrit-pr-id: Gldfbyjdbfd3uolcvhjysepgbx472jv5a
1 parent 398dc36 commit 0556c73

File tree

19 files changed

+72
-110
lines changed

19 files changed

+72
-110
lines changed

tools/hermes/src/resolve.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ pub struct HermesTarget {
110110
pub kind: HermesTargetKind,
111111
/// Path to the main source file for this target.
112112
pub src_path: PathBuf,
113+
/// Path to the `Cargo.toml` for this target.
114+
pub manifest_path: PathBuf,
113115
}
114116

115117
#[derive(Debug)]
@@ -172,6 +174,7 @@ pub fn resolve_roots(args: &Args) -> Result<Roots> {
172174
},
173175
kind,
174176
src_path: target.src_path.as_std_path().to_owned(),
177+
manifest_path: package.manifest_path.as_std_path().to_owned(),
175178
});
176179
}
177180
}
@@ -187,15 +190,15 @@ fn resolve_run_root(metadata: &Metadata) -> PathBuf {
187190
let hermes_global = target_dir.join("hermes");
188191

189192
// Used by integration tests to ensure deterministic shadow dir names.
190-
if let Ok(name) = std::env::var("HERMES_TEST_SHADOW_NAME") {
193+
if let Ok(name) = std::env::var("HERMES_TEST_DIR_NAME") {
191194
return hermes_global.join(name);
192195
}
193196

194197
// Hash the path to the workspace root to avoid collisions between different
195198
// workspaces using the same target directory.
196199
let workspace_root_hash = {
197200
let mut hasher = std::hash::DefaultHasher::new();
198-
hasher.write(b"hermes_shadow_salt");
201+
hasher.write(b"hermes_build_salt");
199202
metadata.workspace_root.hash(&mut hasher);
200203
hasher.finish()
201204
};
@@ -331,7 +334,7 @@ fn resolve_targets<'a>(
331334
}
332335

333336
// TODO: Eventually, we'll want to support external path dependencies by
334-
// rewriting the path in the `Cargo.toml` in the shadow copy.
337+
// analyzing them in-place or ensuring they are accessible to Charon.
335338

336339
/// Scans the package graph to ensure all local dependencies are contained
337340
/// within the workspace root. Returns an error if an external path dependency

tools/hermes/src/scanner.rs

Lines changed: 53 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
sync::mpsc::{self, Sender},
66
};
77

8-
use anyhow::{Context, Result};
8+
use anyhow::Result;
99
use dashmap::DashSet;
1010

1111
use crate::{
@@ -43,8 +43,8 @@ impl HermesArtifact {
4343
}
4444
}
4545

46-
/// 1. Scans and rules all reachable source files, printing any errors
47-
/// encountered. Collects all items with Hermes annotations.
46+
/// Scans the workspace to identify Hermes entry points (`/// ```lean` blocks)
47+
/// and collects targets for verification.
4848
pub fn scan_workspace(roots: &Roots) -> Result<Vec<HermesArtifact>> {
4949
log::trace!("scan_workspace({:?})", roots);
5050

@@ -117,7 +117,7 @@ pub fn scan_workspace(roots: &Roots) -> Result<Vec<HermesArtifact>> {
117117
.roots
118118
.iter()
119119
.filter_map(|target| {
120-
let manifest_path = find_package_root(&target.src_path).ok()?.join("Cargo.toml");
120+
let manifest_path = target.manifest_path.clone();
121121
let start_from = entry_points.remove(&target.name)?;
122122

123123
Some(HermesArtifact {
@@ -130,20 +130,6 @@ pub fn scan_workspace(roots: &Roots) -> Result<Vec<HermesArtifact>> {
130130
.collect())
131131
}
132132

133-
/// Walks up the directory tree from the source file to find the directory
134-
/// containing Cargo.toml.
135-
fn find_package_root(src_path: &Path) -> Result<PathBuf> {
136-
let mut current = src_path;
137-
while let Some(parent) = current.parent() {
138-
if parent.join("Cargo.toml").exists() {
139-
return Ok(parent.to_path_buf());
140-
}
141-
current = parent;
142-
}
143-
// This is highly unlikely if the path came from cargo metadata.
144-
anyhow::bail!("Could not find Cargo.toml in any parent of {:?}", src_path)
145-
}
146-
147133
fn process_file_recursive<'a>(
148134
scope: &rayon::Scope<'a>,
149135
src_path: &Path,
@@ -160,78 +146,61 @@ fn process_file_recursive<'a>(
160146
}
161147

162148
// Walking the AST is enough to collect new modules.
163-
let result = (|| -> Result<Vec<(PathBuf, String)>> {
164-
// Walk the AST, collecting new modules to process.
165-
let (_source_code, unloaded_modules) =
166-
parse::read_file_and_scan_compilation_unit(src_path, |_src, item_result| {
167-
match item_result {
168-
Ok(parsed_item) => {
169-
if let Some(item_name) = parsed_item.item.name() {
170-
// Calculate the full path to this item.
171-
let mut full_path = module_prefix.clone();
172-
full_path.extend(parsed_item.module_path);
173-
full_path.push(item_name);
174-
175-
let _ = path_tx.send((name.clone(), full_path.join("::")));
176-
}
177-
}
178-
Err(e) => {
179-
// A parsing error means we either:
180-
// 1. Lost the module graph, causing missing files later.
181-
// 2. Lost a specification, causing unsound verification.
182-
// We must abort the build.
183-
let _ = err_tx.send(anyhow::anyhow!(e));
149+
// Walk the AST, collecting new modules to process.
150+
let (_source_code, unloaded_modules) =
151+
match parse::read_file_and_scan_compilation_unit(src_path, |_src, item_result| {
152+
match item_result {
153+
Ok(parsed_item) => {
154+
if let Some(item_name) = parsed_item.item.name() {
155+
// Calculate the full path to this item.
156+
let mut full_path = module_prefix.clone();
157+
full_path.extend(parsed_item.module_path);
158+
full_path.push(item_name);
159+
160+
let _ = path_tx.send((name.clone(), full_path.join("::")));
184161
}
185162
}
186-
})
187-
.context(format!("Failed to parse {:?}", src_path))?;
188-
189-
// Resolve and queue child modules for processing.
190-
let base_dir = src_path.parent().unwrap();
191-
let mut next_paths = Vec::new();
192-
for module in unloaded_modules {
193-
if let Some(mod_path) =
194-
resolve_module_path(base_dir, &module.name, module.path_attr.as_deref())
195-
{
196-
next_paths.push((mod_path, module.name));
197-
} else {
198-
// This is an expected condition – it shows up when modules are
199-
// conditionally compiled. Instead of implementing conditional
200-
// compilation ourselves, we can just let rustc error later if
201-
// this is actually an error.
202-
log::debug!("Could not resolve module '{}' in {:?}", module.name, src_path);
163+
Err(e) => {
164+
// A parsing error means we either:
165+
// 1. Lost the module graph, causing missing files later.
166+
// 2. Lost a specification, causing unsound verification.
167+
// We must abort the build.
168+
let _ = err_tx.send(anyhow::anyhow!(e));
169+
}
203170
}
204-
}
205-
206-
Ok(next_paths)
207-
})();
208-
209-
match result {
210-
Ok(children) => {
171+
}) {
172+
Ok(res) => res,
173+
Err(e) => {
174+
let _ = err_tx
175+
.send(anyhow::anyhow!(e).context(format!("Failed to parse {:?}", src_path)));
176+
return;
177+
}
178+
};
179+
180+
// Resolve and queue child modules for processing.
181+
let base_dir = src_path.parent().unwrap();
182+
for module in unloaded_modules {
183+
if let Some(mod_path) =
184+
resolve_module_path(base_dir, &module.name, module.path_attr.as_deref())
185+
{
211186
// Spawn new tasks for discovered modules.
212-
for (child_path, mod_name) in children {
213-
let (err_tx, path_tx) = (err_tx.clone(), path_tx.clone());
187+
let (err_tx, path_tx) = (err_tx.clone(), path_tx.clone());
214188

215-
let mut new_prefix = module_prefix.clone();
216-
new_prefix.push(mod_name);
189+
let mut new_prefix = module_prefix.clone();
190+
new_prefix.push(module.name);
217191

218-
let name = name.clone();
219-
scope.spawn(move |s| {
220-
process_file_recursive(
221-
s,
222-
&child_path,
223-
config,
224-
visited,
225-
err_tx,
226-
path_tx,
227-
new_prefix,
228-
name,
229-
)
230-
})
231-
}
232-
}
233-
Err(e) => {
234-
let _ = err_tx.send(e);
192+
let name = name.clone();
193+
scope.spawn(move |s| {
194+
process_file_recursive(
195+
s, &mod_path, config, visited, err_tx, path_tx, new_prefix, name,
196+
)
197+
})
198+
} else {
199+
// This is an expected condition – it shows up when modules are
200+
// conditionally compiled. Instead of implementing conditional
201+
// compilation ourselves, we can just let rustc error later if
202+
// this is actually an error.
203+
log::debug!("Could not resolve module '{}' in {:?}", module.name, src_path);
235204
}
236205
}
237206
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Line 1 from charon
2-
{"reason": "compiler-message", "package_id": "test 0.1.0 (path+file://[PROJECT_ROOT])", "target": {"kind": ["lib"], "crate_types": ["lib"], "name": "test", "src_path": "[PROJECT_ROOT]/src/lib.rs", "edition": "2021", "doc": true, "doctest": true, "test": true}, "message": {"rendered": "error: interleaved\n", "children": [], "code": null, "level": "error", "message": "interleaved error", "spans": [{"byte_end": 10, "byte_start": 0, "column_end": 11, "column_start": 1, "line_end": 1, "line_start": 1, "file_name": "[SHADOW_ROOT]/src/lib.rs", "is_primary": true, "label": "label", "text": [], "expansion": null}]}}
2+
{"reason": "compiler-message", "package_id": "test 0.1.0 (path+file://[PROJECT_ROOT])", "target": {"kind": ["lib"], "crate_types": ["lib"], "name": "test", "src_path": "[PROJECT_ROOT]/src/lib.rs", "edition": "2021", "doc": true, "doctest": true, "test": true}, "message": {"rendered": "error: interleaved\n", "children": [], "code": null, "level": "error", "message": "interleaved error", "spans": [{"byte_end": 10, "byte_start": 0, "column_end": 11, "column_start": 1, "line_end": 1, "line_start": 1, "file_name": "[PROJECT_ROOT]/src/lib.rs", "is_primary": true, "label": "label", "text": [], "expansion": null}]}}
33
Line 2 from charon
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"reason": "compiler-message", "package_id": "test 0.1.0 (path+file://[PROJECT_ROOT])", "target": {"kind": ["lib"], "crate_types": ["lib"], "name": "test", "src_path": "[PROJECT_ROOT]/src/lib.rs", "edition": "2021", "doc": true, "doctest": true, "test": true}, "message": {"rendered": "error: missing source\n", "children": [], "code": null, "level": "error", "message": "missing source", "spans": [{"byte_end": 10, "byte_start": 0, "column_end": 11, "column_start": 1, "line_end": 1, "line_start": 1, "file_name": "[SHADOW_ROOT]/src/missing.rs", "is_primary": true, "label": "label", "text": [], "expansion": null}]}}
1+
{"reason": "compiler-message", "package_id": "test 0.1.0 (path+file://[PROJECT_ROOT])", "target": {"kind": ["lib"], "crate_types": ["lib"], "name": "test", "src_path": "[PROJECT_ROOT]/src/lib.rs", "edition": "2021", "doc": true, "doctest": true, "test": true}, "message": {"rendered": "error: missing source\n", "children": [], "code": null, "level": "error", "message": "missing source", "spans": [{"byte_end": 10, "byte_start": 0, "column_end": 11, "column_start": 1, "line_end": 1, "line_start": 1, "file_name": "[PROJECT_ROOT]/src/missing.rs", "is_primary": true, "label": "label", "text": [], "expansion": null}]}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"reason": "compiler-message", "package_id": "test 0.1.0 (path+file://[PROJECT_ROOT])", "target": {"kind": ["lib"], "name": "test", "src_path": "[PROJECT_ROOT]/src/lib.rs", "crate_types": ["lib"], "edition": "2021", "doc": true, "doctest": true, "test": true}, "message": {"rendered": "error: ignore this rendered text\n", "children": [], "code": null, "level": "error", "message": "real error", "spans": [{"byte_end": 10, "byte_start": 0, "column_end": 11, "column_start": 1, "expansion": null, "file_name": "[SHADOW_ROOT]/src/lib.rs", "is_primary": true, "label": "real label", "line_end": 1, "line_start": 1, "text": []}]}}
1+
{"reason": "compiler-message", "package_id": "test 0.1.0 (path+file://[PROJECT_ROOT])", "target": {"kind": ["lib"], "name": "test", "src_path": "[PROJECT_ROOT]/src/lib.rs", "crate_types": ["lib"], "edition": "2021", "doc": true, "doctest": true, "test": true}, "message": {"rendered": "error: ignore this rendered text\n", "children": [], "code": null, "level": "error", "message": "real error", "spans": [{"byte_end": 10, "byte_start": 0, "column_end": 11, "column_start": 1, "expansion": null, "file_name": "[PROJECT_ROOT]/src/lib.rs", "is_primary": true, "label": "real label", "line_end": 1, "line_start": 1, "text": []}]}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"reason": "compiler-message", "package_id": "test 0.1.0 (path+file://[PROJECT_ROOT])", "target": {"kind": ["lib"], "name": "test", "src_path": "[PROJECT_ROOT]/src/lib.rs", "crate_types": ["lib"], "edition": "2021", "doc": true, "doctest": true, "test": true}, "message": {"rendered": "error: parent error\n", "children": [{"children": [], "code": null, "level": "help", "message": "help me", "rendered": null, "spans": []}], "code": null, "level": "error", "message": "parent error", "spans": [{"byte_end": 10, "byte_start": 0, "column_end": 11, "column_start": 1, "expansion": null, "file_name": "[SHADOW_ROOT]/src/lib.rs", "is_primary": true, "label": "oops", "line_end": 1, "line_start": 1, "text": []}]}}
1+
{"reason": "compiler-message", "package_id": "test 0.1.0 (path+file://[PROJECT_ROOT])", "target": {"kind": ["lib"], "name": "test", "src_path": "[PROJECT_ROOT]/src/lib.rs", "crate_types": ["lib"], "edition": "2021", "doc": true, "doctest": true, "test": true}, "message": {"rendered": "error: parent error\n", "children": [{"children": [], "code": null, "level": "help", "message": "help me", "rendered": null, "spans": []}], "code": null, "level": "error", "message": "parent error", "spans": [{"byte_end": 10, "byte_start": 0, "column_end": 11, "column_start": 1, "expansion": null, "file_name": "[PROJECT_ROOT]/src/lib.rs", "is_primary": true, "label": "oops", "line_end": 1, "line_start": 1, "text": []}]}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"reason": "compiler-message", "message": {"message": "mixed span error", "level": "error", "code": null, "spans": [{"file_name": "[SHADOW_ROOT]/src/rewritten.rs", "byte_start": 10, "byte_end": 20, "line_start": 1, "line_end": 1, "column_start": 11, "column_end": 21, "is_primary": true, "text": [], "label": "called here"}, {"file_name": "[PROJECT_ROOT]/src/symlinked.rs", "byte_start": 50, "byte_end": 60, "line_start": 2, "line_end": 2, "column_start": 1, "column_end": 11, "is_primary": false, "text": [], "label": "defined here"}], "children": [], "rendered": "Rendered mixed error"}}
1+
{"reason": "compiler-message", "message": {"message": "mixed span error", "level": "error", "code": null, "spans": [{"file_name": "[PROJECT_ROOT]/src/rewritten.rs", "byte_start": 10, "byte_end": 20, "line_start": 1, "line_end": 1, "column_start": 11, "column_end": 21, "is_primary": true, "text": [], "label": "called here"}, {"file_name": "[PROJECT_ROOT]/src/symlinked.rs", "byte_start": 50, "byte_end": 60, "line_start": 2, "line_end": 2, "column_start": 1, "column_end": 11, "is_primary": false, "text": [], "label": "defined here"}], "children": [], "rendered": "Rendered mixed error"}}

tools/hermes/tests/fixtures/map_rewritten_file/mock_charon_output.json

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"reason": "compiler-message", "package_id": "test 0.1.0 (path+file://[PROJECT_ROOT])", "target": {"kind": ["lib"], "crate_types": ["lib"], "name": "test", "src_path": "[PROJECT_ROOT]/src/lib.rs", "edition": "2021", "doc": true, "doctest": true, "test": true}, "message": {"rendered": "error: something bad\n --> src/untransformed.rs:1:1\n", "children": [], "code": null, "level": "error", "message": "something bad", "spans": [{"byte_end": 10, "byte_start": 0, "column_end": 11, "column_start": 1, "expansion": null, "file_name": "[SHADOW_ROOT]/src/untransformed.rs", "is_primary": true, "label": "oops", "line_end": 1, "line_start": 1, "suggested_replacement": null, "suggestion_applicability": null, "text": [{"highlight_end": 11, "highlight_start": 1, "text": "pub fn untouched() {}"}]}]}}
1+
{"reason": "compiler-message", "package_id": "test 0.1.0 (path+file://[PROJECT_ROOT])", "target": {"kind": ["lib"], "crate_types": ["lib"], "name": "test", "src_path": "[PROJECT_ROOT]/src/lib.rs", "edition": "2021", "doc": true, "doctest": true, "test": true}, "message": {"rendered": "error: something bad\n --> src/untransformed.rs:1:1\n", "children": [], "code": null, "level": "error", "message": "something bad", "spans": [{"byte_end": 10, "byte_start": 0, "column_end": 11, "column_start": 1, "expansion": null, "file_name": "[PROJECT_ROOT]/src/untransformed.rs", "is_primary": true, "label": "oops", "line_end": 1, "line_start": 1, "suggested_replacement": null, "suggestion_applicability": null, "text": [{"highlight_end": 11, "highlight_start": 1, "text": "pub fn untouched() {}"}]}]}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"reason": "compiler-message", "package_id": "test 0.1.0 (path+file://[PROJECT_ROOT])", "target": {"kind": ["lib"], "name": "test", "src_path": "[PROJECT_ROOT]/src/lib.rs", "crate_types": ["lib"], "edition": "2021", "doc": true, "doctest": true, "test": true}, "message": {"rendered": "error: unsafe\n", "children": [], "code": null, "level": "error", "message": "unsafe", "spans": [{"byte_end": 18, "byte_start": 9, "column_end": 19, "column_start": 10, "expansion": null, "file_name": "[SHADOW_ROOT]/src/lib.rs", "is_primary": true, "label": "danger", "line_end": 1, "line_start": 1, "text": []}]}}
1+
{"reason": "compiler-message", "package_id": "test 0.1.0 (path+file://[PROJECT_ROOT])", "target": {"kind": ["lib"], "name": "test", "src_path": "[PROJECT_ROOT]/src/lib.rs", "crate_types": ["lib"], "edition": "2021", "doc": true, "doctest": true, "test": true}, "message": {"rendered": "error: unsafe\n", "children": [], "code": null, "level": "error", "message": "unsafe", "spans": [{"byte_end": 18, "byte_start": 9, "column_end": 19, "column_start": 10, "expansion": null, "file_name": "[PROJECT_ROOT]/src/lib.rs", "is_primary": true, "label": "danger", "line_end": 1, "line_start": 1, "text": []}]}}

0 commit comments

Comments
 (0)