Skip to content

Commit 07a48e6

Browse files
celinvaladpaco-aws
andauthored
Fix infinite loop on stub resolution and improve error handling (rust-lang#2227)
Refactor the name resolution code to take into consideration loops added by loops in globs. Since I had to do some refactoring, I also ensured we now the reason why the resolution failed and print it as part of the error. Co-authored-by: Adrian Palacios <73246657+adpaco-aws@users.noreply.github.com>
1 parent 35b3653 commit 07a48e6

File tree

24 files changed

+497
-345
lines changed

24 files changed

+497
-345
lines changed

kani-compiler/src/kani_middle/attributes.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use tracing::{debug, trace};
1717

1818
use crate::kani_middle::CompilerHelpers;
1919

20+
use super::resolve;
21+
2022
#[derive(Debug, Clone, Copy, AsRefStr, EnumString, PartialEq, Eq, PartialOrd, Ord)]
2123
#[strum(serialize_all = "snake_case")]
2224
enum KaniAttributeKind {
@@ -83,7 +85,10 @@ pub fn is_test_harness_closure(tcx: TyCtxt, def_id: DefId) -> bool {
8385
}
8486

8587
/// Extract all Kani attributes for a given `def_id` if any exists.
88+
/// We only extract attributes for harnesses that are local to the current crate.
8689
pub fn extract_harness_attributes(tcx: TyCtxt, def_id: DefId) -> Option<HarnessAttributes> {
90+
// Abort if not local.
91+
def_id.as_local()?;
8792
let attributes = extract_kani_attributes(tcx, def_id);
8893
trace!(?def_id, ?attributes, "extract_harness_attributes");
8994
if attributes.contains_key(&KaniAttributeKind::Proof) {
@@ -96,7 +101,7 @@ pub fn extract_harness_attributes(tcx: TyCtxt, def_id: DefId) -> Option<HarnessA
96101
harness.solver = parse_solver(tcx, expect_single(tcx, kind, &attributes));
97102
}
98103
KaniAttributeKind::Stub => {
99-
harness.stubs = parse_stubs(tcx, attributes);
104+
harness.stubs = parse_stubs(tcx, def_id, attributes);
100105
}
101106
KaniAttributeKind::Unwind => {
102107
harness.unwind_value =
@@ -189,12 +194,21 @@ fn parse_unwind(tcx: TyCtxt, attr: &Attribute) -> Option<u32> {
189194
}
190195
}
191196

192-
fn parse_stubs(tcx: TyCtxt, attributes: Vec<&Attribute>) -> Vec<Stub> {
197+
fn parse_stubs(tcx: TyCtxt, harness: DefId, attributes: Vec<&Attribute>) -> Vec<Stub> {
198+
let current_module = tcx.parent_module_from_def_id(harness.expect_local());
199+
let check_resolve = |attr: &Attribute, name: &str| {
200+
let result = resolve::resolve_fn(tcx, current_module, name);
201+
if let Err(err) = result {
202+
tcx.sess.span_err(attr.span, format!("failed to resolve `{name}`: {err}"));
203+
}
204+
};
193205
attributes
194206
.iter()
195207
.filter_map(|attr| match parse_paths(attr) {
196208
Ok(paths) => match paths.as_slice() {
197209
[orig, replace] => {
210+
check_resolve(attr, orig);
211+
check_resolve(attr, replace);
198212
Some(Stub { original: orig.clone(), replacement: replace.clone() })
199213
}
200214
_ => {

0 commit comments

Comments
 (0)