Skip to content

Commit 09c4885

Browse files
committed
fix(extract): a Rust generic impl's call scope drops its type arguments
The def side names a Rust impl method after the implementing type with its generic arguments stripped (`impl<T> Gen<T>` -> `Gen`, extract_rust_impl). The call-scope QN in extract_unified.c used the impl's type text verbatim, `Gen<T>` / `Life<'a>`. So every method of a generic or lifetime-parameterised impl had a scope QN that no node carries, and every call in its body was sourced from the File node instead of the method. Base census, 9f85f10 par (lane L7's DeusData#763 measurement): 87,855 Rust CALLS start at a File/Module node, 84,331 of them (96%) on a line inside a function span. Go is 0%. Strip the generic arguments on the call side exactly as the def side does. Plain impls, trait impls, free functions and inline modules were already consistent (checked on a fixture with the base binary). Test: extract_rust_generic_impl_call_scope_matches_def (plain, type-generic and lifetime-generic impls; def QN == call scope). Refs DeusData#763 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 9f85f10 commit 09c4885

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

‎internal/cbm/extract_unified.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,20 @@ static const char *compute_class_qn(CBMExtractCtx *ctx, TSNode node, const WalkS
11591159
if (!name || !name[0]) {
11601160
return NULL;
11611161
}
1162+
/* Rust: the impl's type text carries its generic arguments (`impl<T> Gen<T>`,
1163+
* `impl<'a> Parser<'a>`), which the def side strips before naming the type
1164+
* (extract_defs.c extract_rust_impl: `Buffer<T>` -> `Buffer`). Strip them the
1165+
* same way, or every method of a generic impl gets a scope QN no node carries
1166+
* and all its calls fall back to the File node. */
1167+
if (ctx->language == CBM_LANG_RUST && strcmp(ts_node_type(node), "impl_item") == 0) {
1168+
char *lt = strchr(name, '<');
1169+
if (lt) {
1170+
*lt = '\0';
1171+
}
1172+
if (!name[0]) {
1173+
return NULL;
1174+
}
1175+
}
11621176

11631177
/* Nested class: prefix with the enclosing class QN (Outer.Inner) so this
11641178
* scope QN matches the def-side class QN (extract_defs.c compute_class_qn /

‎tests/test_extraction.c‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6886,6 +6886,63 @@ TEST(extract_perl_method_call_flags_is_method) {
68866886
PASS();
68876887
}
68886888

6889+
/* The def QN of `name` and the enclosing-function QN of the call `callee` inside
6890+
* it, from one extraction result. */
6891+
static void rust_def_and_scope(const CBMFileResult *r, const char *name, const char *callee,
6892+
const char **def_qn, const char **scope_qn) {
6893+
*def_qn = NULL;
6894+
*scope_qn = NULL;
6895+
for (int i = 0; i < r->defs.count; i++) {
6896+
const CBMDefinition *d = &r->defs.items[i];
6897+
if (d->name && d->qualified_name && strcmp(d->name, name) == 0) {
6898+
*def_qn = d->qualified_name;
6899+
}
6900+
}
6901+
for (int i = 0; i < r->calls.count; i++) {
6902+
const CBMCall *c = &r->calls.items[i];
6903+
if (c->callee_name && strcmp(c->callee_name, callee) == 0 && c->enclosing_func_qn &&
6904+
strstr(c->enclosing_func_qn, name)) {
6905+
*scope_qn = c->enclosing_func_qn;
6906+
}
6907+
}
6908+
}
6909+
6910+
/* F-R4: a method of a generic impl (`impl<T> Gen<T>`, `impl<'a> Life<'a>`) got a
6911+
* call scope that kept the generic arguments ("...Gen<T>.in_generic") while the
6912+
* def side strips them ("...Gen.in_generic"), so no node carried the scope and
6913+
* every call in the body fell back to the File node (base census: 84,331 Rust
6914+
* CALLS sourced from a File although inside a function, 96% of all File-sourced
6915+
* Rust CALLS). The def QN and the call scope must agree. */
6916+
TEST(extract_rust_generic_impl_call_scope_matches_def) {
6917+
CBMFileResult *r = extract("pub struct Plain;\n"
6918+
"impl Plain {\n"
6919+
" pub fn in_plain(&self) -> u32 { helper() }\n"
6920+
"}\n"
6921+
"pub struct Gen<T> { v: T }\n"
6922+
"impl<T> Gen<T> {\n"
6923+
" pub fn in_generic(&self) -> u32 { helper() }\n"
6924+
"}\n"
6925+
"pub struct Life<'a> { s: &'a str }\n"
6926+
"impl<'a> Life<'a> {\n"
6927+
" pub fn in_lifetime(&self) -> u32 { helper() }\n"
6928+
"}\n"
6929+
"pub fn helper() -> u32 { 1 }\n",
6930+
CBM_LANG_RUST, "t", "src/lib.rs");
6931+
ASSERT_NOT_NULL(r);
6932+
ASSERT_FALSE(r->has_error);
6933+
static const char *const methods[] = {"in_plain", "in_generic", "in_lifetime", NULL};
6934+
for (int m = 0; methods[m]; m++) {
6935+
const char *def_qn = NULL;
6936+
const char *scope_qn = NULL;
6937+
rust_def_and_scope(r, methods[m], "helper", &def_qn, &scope_qn);
6938+
ASSERT_NOT_NULL(def_qn);
6939+
ASSERT_NOT_NULL(scope_qn);
6940+
ASSERT_STR_EQ(scope_qn, def_qn);
6941+
}
6942+
cbm_free_result(r);
6943+
PASS();
6944+
}
6945+
68896946
/* Languages OUTSIDE the is_method flag set (only Perl and TS/JS/TSX set it) must
68906947
* be unaffected: a Go method call never sets is_method. */
68916948
TEST(extract_flag_exempt_method_call_not_flagged_is_method) {
@@ -12220,6 +12277,7 @@ SUITE(extraction) {
1222012277
RUN_TEST(extract_perl_config_string_not_a_callee);
1222112278
RUN_TEST(extract_perl_builtin_call_is_function_not_method);
1222212279
RUN_TEST(extract_perl_method_call_flags_is_method);
12280+
RUN_TEST(extract_rust_generic_impl_call_scope_matches_def);
1222312281
RUN_TEST(extract_flag_exempt_method_call_not_flagged_is_method);
1222412282
RUN_TEST(extract_python_member_call_flags_is_method);
1222512283
RUN_TEST(extract_python_bare_call_flags_locally_bound_callee);

0 commit comments

Comments
 (0)