Skip to content
Merged
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
2 changes: 2 additions & 0 deletions changelog.d/14552-test-local-binder-owner-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- section: Fixed -->
- Analysis: a test that only calls a local of its own body named like the changed function (`let dt = |x| x + 1;`, `let dt = other;`, a closure parameter or a nested `fn dt`) no longer counts as a `direct_owner_call` of that function; the seam now reads `reach: weak` through `same_test_file`. The parsed body decides each mention block by block, so a call before the binding, inside the binding's own initializer, or after the binding's block ends still credits the owner. A qualified call, an import or glob in an enclosing block, another binding pattern (`match`, `if let`, `for`) or a non-std macro keeps the earlier reading. Pinned in the evidence-promotion honesty corpus with a block-scoped control (#14552).
109 changes: 104 additions & 5 deletions crates/ripr/src/analysis/classify/call_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3943,11 +3943,7 @@ fn bare_name_renamed_in_nested_blocks(
else {
return false;
};
let tree_of = |path: &std::path::Path| index.source_syntax.get(index, path)?.tree();
let ancestors = crate::analysis::syntax::enclosing_module_files(&test.file, tree_of, |root| {
manifest_confirms_crate_root(index, root)
})
.filter(|_| !crate_may_export_a_std_macro(index));
let ancestors = macro_scope_ancestors(index, test);
let Some(paths) = index
.source_syntax
.get(index, &test.file)
Expand All @@ -3974,6 +3970,21 @@ fn bare_name_renamed_in_nested_blocks(
})
}

/// The files of the modules enclosing `test`'s file, nearest first, up to
/// the crate root, for settling which macros reach its body; `None` when
/// that chain is not known or a crate file may export a macro under a std
/// macro's name.
fn macro_scope_ancestors(
index: &RustIndex,
test: &TestSummary,
) -> Option<Vec<ra_ap_syntax::ast::SourceFile>> {
let tree_of = |path: &std::path::Path| index.source_syntax.get(index, path)?.tree();
crate::analysis::syntax::enclosing_module_files(&test.file, tree_of, |root| {
manifest_confirms_crate_root(index, root)
})
.filter(|_| !crate_may_export_a_std_macro(index))
}

/// Whether the module scope a bare call of the owner's name resolves in
/// can only bind that name through globs of the standard library (`use
/// std::cmp::*;` around a bare `max(..)` beside a changed crate-level `max`,
Expand Down Expand Up @@ -4119,6 +4130,75 @@ pub(in crate::analysis) fn test_calls_only_a_renamed_other_function(
.all(|qualifier| matches!(qualifier, Some(segments) if segments.is_empty()))
}

/// #14552: whether every mention of the free owner's name in `test` is
/// bound by a local of the test body (`let dt = |x| x + 1;`, `let dt =
/// other;`, a closure parameter, a nested `fn dt`), so the test never calls
/// the owner. The parsed tree decides each mention block by block; anything
/// it cannot settle as a local (a qualified call, an import or glob in an
/// enclosing block, a mention before the binding or past its block, a
/// non-std macro) keeps the prior relation.
pub(in crate::analysis) fn test_calls_only_a_local_binding(
index: &RustIndex,
owner: &FunctionSummary,
test: &TestSummary,
) -> bool {
if owner.item.container != FunctionContainer::Free {
return false;
}
let name = owner.name.trim_start_matches("r#");
if !spells_a_local_binder(&test.body, name) {
return false;
}
// A generated test is read from its expanded body, so it reads as its
// hand-written twin does; an expansion that declares its own items may
// import the name and keeps the earlier reading.
let generated_body = match test.generated.as_ref() {
None => None,
Some(origin) if origin.body_declares_items => return false,
Some(_) => match index.source_syntax.parse_fragment(&test.body) {
Some(parse) => Some(parse.tree()),
None => return false,
},
};
let ancestors = macro_scope_ancestors(index, test);
index
.source_syntax
.get(index, &test.file)
.is_some_and(|snapshot| {
snapshot.every_mention_bound_locally(
test,
ancestors.as_deref(),
generated_body.as_ref(),
name,
)
})
}

/// Text prefilter for [`test_calls_only_a_local_binding`]: whether `body`
/// spells `name` where a binder may stand (after `let`, `mut`, `ref`, `fn`,
/// `|`, `,`, `(`, `{`, `[`, `&` or `:`, as in tuple, field, slice and
/// reference patterns), not
/// followed by a call or path. Most tests spell their owner only as a call
/// and stop here.
fn spells_a_local_binder(body: &str, name: &str) -> bool {
let masked = mask_comments_and_strings(body);
masked.match_indices(name).any(|(start, _)| {
let end = start + name.len();
if ident_char_before(&masked, start) || ident_char_at(&masked, end) {
return false;
}
let before = &masked[..start];
let head = before.strip_suffix("r#").unwrap_or(before).trim_end();
if head_word(head) == "fn" {
return true;
}
let tail = masked[end..].trim_start();
let binder_head = matches!(head_word(head), "let" | "mut" | "ref")
|| head.ends_with(['|', ',', '(', '{', '[', '&', ':']);
binder_head && !tail.starts_with('(') && !tail.starts_with("::")
Comment thread
EffortlessSteven marked this conversation as resolved.
Resolved
})
}

/// RIPR-SPEC-0186: a boundary oracle must observe the changed definition,
/// not merely another call with its name in a test that also reaches it.
/// Qualified calls use activation's existing identity authority. When the
Expand Down Expand Up @@ -5217,6 +5297,25 @@ mod tests {
use super::*;
use crate::analysis::rust_index::summarize_file;

#[test]
fn the_local_binder_prefilter_admits_every_supported_pattern_spelling() {
for body in [
"let dt = |x: i32| x; dt(1);",
"let mut dt = other; dt(1);",
"let (a, dt) = (1, other); dt(a);",
"let Pair { dt } = pair; dt(1);",
"let Pair { callback: dt } = pair; dt(1);",
"let [dt] = [other]; dt(1);",
"let &dt = &other; dt(1);",
"let apply = |dt: fn(i32) -> i32| dt(1);",
"fn dt(x: i32) -> i32 { x } dt(1);",
] {
assert!(spells_a_local_binder(body, "dt"), "{body}");
}
assert!(!spells_a_local_binder("assert_eq!(dt(1), 2);", "dt"));
assert!(!spells_a_local_binder("let x = super::dt(1);", "dt"));
}

#[test]
fn std_impl_source_computes_each_trait_and_head_once() {
let memos = CallIdentityMemos::default();
Expand Down
6 changes: 3 additions & 3 deletions crates/ripr/src/analysis/classify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ pub(in crate::analysis) use builder_override::{
pub(in crate::analysis) use call_identity::{
CallIdentityMemos, CallTarget, OwnerCallIdentity, bare_owner_call_settles_on_owner,
bound_free_owner_calls, qualified_owner_calls_resolve, shared_owner_call_identity,
test_call_settles_on_owner, test_calls_only_a_local_value_item,
test_calls_only_a_renamed_other_function, test_calls_only_through_local_rival_module,
text_calls_owner,
test_call_settles_on_owner, test_calls_only_a_local_binding,
test_calls_only_a_local_value_item, test_calls_only_a_renamed_other_function,
test_calls_only_through_local_rival_module, text_calls_owner,
};
pub(in crate::analysis) use caller_loop_selector::withhold_caller_loop_selector_gap;
pub(in crate::analysis) use compile_time_guard::constrained_by_compile_time_assertion;
Expand Down
6 changes: 6 additions & 0 deletions crates/ripr/src/analysis/classify/related_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8797,6 +8797,12 @@ fn owner_call_relation_reason_gated(
if super::call_identity::test_calls_only_a_renamed_other_function(index, owner, test) {
return RelationReason::WeakTokenSubstring;
}
// #14552: a test whose every mention of the name is bound by a
// local of its body (`let dt = |x| x + 1;`, a nested `fn dt`) calls
// that local, not the owner.
if super::call_identity::test_calls_only_a_local_binding(index, owner, test) {
return RelationReason::WeakTokenSubstring;
}
// RIPR-SPEC-0005: a free function whose name another module also
// defines is not reached when the test's own `use` binds the bare
// name to that other module's twin.
Expand Down
21 changes: 13 additions & 8 deletions crates/ripr/src/analysis/seam_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,9 +801,11 @@ pub(crate) struct CachedSeamLimitInfo {
/// weak, not exact (#16286).
/// `1.201`: a renamed call in a test's own module file (`src/tests.rs`) reads
/// the enclosing module files, so a nested-block rename settles there too (#16277).
/// `1.202`: `assert_eq!(matches!(..), true)` reads only as strong as its
/// pattern (#16290).
pub(crate) const CACHE_SCHEMA_VERSION: &str = "1.202";
/// `1.204`: a test whose every mention of the owner's name is bound by a
/// local of its body (`let dt = |x| x + 1;`, a nested `fn dt`) no longer
/// relates by `direct_owner_call` (#14552).
pub(crate) const CACHE_SCHEMA_VERSION: &str = "1.204";
/// `0.2` → `0.3`: same semantic transition as the outer cache (#3273 /
/// #3286) — sharded entries derive from the same facts and cannot bypass
/// the outer generation bump.
Expand Down Expand Up @@ -1043,7 +1045,8 @@ pub(crate) const CACHE_SCHEMA_VERSION: &str = "1.202";
/// `0.202`: same or-pattern oracle reading as full `1.200` (#16286).
/// `0.203`: same file-module nested-block rename as full `1.201` (#16277).
/// `0.204`: same compared-pattern oracle reading as full `1.202` (#16290).
const SHARDED_CLASSIFIED_SEAM_CACHE_SCHEMA_VERSION: &str = "0.204";
/// `0.206`: same local-binder refusal as full `1.204` (#14552).
const SHARDED_CLASSIFIED_SEAM_CACHE_SCHEMA_VERSION: &str = "0.206";

/// Compact-classified seam cache schema. This cache stores the same
/// `ClassifiedSeam` envelope shape as the full repo exposure cache, but
Expand Down Expand Up @@ -1285,7 +1288,8 @@ const SHARDED_CLASSIFIED_SEAM_CACHE_SCHEMA_VERSION: &str = "0.204";
/// `0.202`: same or-pattern oracle reading as full `1.200` (#16286).
/// `0.203`: same file-module nested-block rename as full `1.201` (#16277).
/// `0.204`: same compared-pattern oracle reading as full `1.202` (#16290).
pub(crate) const COMPACT_CLASSIFIED_SEAM_CACHE_SCHEMA_VERSION: &str = "0.204";
/// `0.206`: same local-binder refusal as full `1.204` (#14552).
pub(crate) const COMPACT_CLASSIFIED_SEAM_CACHE_SCHEMA_VERSION: &str = "0.206";

/// Compact class-count cache used by repo badge rendering. It keys off
/// the same workspace state as the full fact cache, but stores only
Expand Down Expand Up @@ -5894,8 +5898,8 @@ mod tests {
// 1.198 -> 1.199: include_str!/include_bytes! wrappers forward their helper result (#10357).
// 1.199 -> 1.200: matches! or-patterns of values read weak (#16286).
// 1.200 -> 1.201: nested-block renames settle in a test's own module file (#16277).
// 1.201 -> 1.202: assert_eq!(matches!(..), true) follows its pattern (#16290).
assert_eq!(CACHE_SCHEMA_VERSION, "1.202");
// 1.202 -> 1.204: a test-local binder of the name withholds direct credit (#14552).
assert_eq!(CACHE_SCHEMA_VERSION, "1.204");
// 1.168 -> 1.169: plain `assert_eq!` literal withhold in the audit (#13005).
// 0.12 -> 0.13 through 0.14 / 0.15 / 0.16 / 0.17 / 0.18: same
// #3731 semantic transition as the outer classified-seam cache,
Expand Down Expand Up @@ -6075,8 +6079,9 @@ mod tests {
// 0.201 -> 0.202: same or-pattern oracle reading as full 1.200 (#16286).
// 0.202 -> 0.203: same file-module nested-block rename as full 1.201 (#16277).
// 0.203 -> 0.204: same compared-pattern oracle reading as full 1.202 (#16290).
assert_eq!(SHARDED_CLASSIFIED_SEAM_CACHE_SCHEMA_VERSION, "0.204");
assert_eq!(COMPACT_CLASSIFIED_SEAM_CACHE_SCHEMA_VERSION, "0.204");
// 0.204 -> 0.206: same local-binder refusal as full 1.204 (#14552).
assert_eq!(SHARDED_CLASSIFIED_SEAM_CACHE_SCHEMA_VERSION, "0.206");
assert_eq!(COMPACT_CLASSIFIED_SEAM_CACHE_SCHEMA_VERSION, "0.206");
// 0.170 -> 0.171: same plain `assert_eq!` literal withhold as full 1.169 (#13005).
}

Expand Down
5 changes: 5 additions & 0 deletions crates/ripr/src/analysis/syntax/name_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ use std::sync::Arc;

#[cfg(test)]
mod index_tests;
mod local_binding;

pub(crate) use local_binding::{
every_mention_bound_locally_in_fragment, every_mention_bound_locally_in_tree,
};

/// The scopes around one function, innermost first: the function body (and
/// any enclosing function bodies), then each enclosing inline module, then
Expand Down
Loading
Loading