Skip to content

Commit d40ebf6

Browse files
authored
Merge pull request #2932 from kasheeesh08/fix/rust-grouped-imports-2922
fix(rust): expand grouped use imports
2 parents 6df7fcb + 0df1074 commit d40ebf6

3 files changed

Lines changed: 214 additions & 20 deletions

File tree

‎internal/cbm/extract_imports.c‎

Lines changed: 127 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
/* Local constants for magic number elimination. */
1515
enum {
16-
USE_PREFIX_LEN = 4, /* strlen("use ") */
1716
MIN_WOLFRAM_CHILDREN = 2,
1817
/* Nested `#if` branches followed while collecting C includes. */
1918
C_INCLUDE_NEST_LIMIT = 64,
@@ -1897,9 +1896,134 @@ static void parse_java_imports(CBMExtractCtx *ctx) {
18971896
// --- Rust imports ---
18981897
// use_declaration -> use_list or scoped_use_list
18991898

1900-
static void parse_rust_imports(CBMExtractCtx *ctx) {
1899+
static TSNode rust_import_field(TSNode node, const char *field) {
1900+
return ts_node_child_by_field_name(node, field, (uint32_t)strlen(field));
1901+
}
1902+
1903+
static char *rust_import_node_text_compact(CBMArena *a, TSNode node, const char *source) {
1904+
if (ts_node_is_null(node)) {
1905+
return NULL;
1906+
}
1907+
uint32_t start = ts_node_start_byte(node);
1908+
uint32_t end = ts_node_end_byte(node);
1909+
if (end <= start) {
1910+
return cbm_arena_strdup(a, "");
1911+
}
1912+
char *out = (char *)cbm_arena_alloc(a, (size_t)(end - start) + SKIP_ONE);
1913+
if (!out) {
1914+
return NULL;
1915+
}
1916+
size_t w = 0;
1917+
for (uint32_t i = start; i < end; i++) {
1918+
if (!isspace((unsigned char)source[i])) {
1919+
out[w++] = source[i];
1920+
}
1921+
}
1922+
out[w] = '\0';
1923+
return out;
1924+
}
1925+
1926+
static char *rust_import_join(CBMArena *a, const char *prefix, const char *suffix) {
1927+
if (!prefix || !prefix[0]) {
1928+
return cbm_arena_strdup(a, suffix ? suffix : "");
1929+
}
1930+
if (!suffix || !suffix[0]) {
1931+
return cbm_arena_strdup(a, prefix);
1932+
}
1933+
return cbm_arena_sprintf(a, "%s::%s", prefix, suffix);
1934+
}
1935+
1936+
static bool rust_import_exists(const CBMImportArray *imports, const char *local,
1937+
const char *module_path) {
1938+
for (int i = 0; i < imports->count; i++) {
1939+
const CBMImport *imp = &imports->items[i];
1940+
if (imp->local_name && imp->module_path && strcmp(imp->local_name, local) == 0 &&
1941+
strcmp(imp->module_path, module_path) == 0) {
1942+
return true;
1943+
}
1944+
}
1945+
return false;
1946+
}
1947+
1948+
static void rust_import_push(CBMExtractCtx *ctx, const char *full, const char *alias) {
1949+
static const char self_suffix[] = "::self";
19011950
CBMArena *a = ctx->arena;
1951+
if (!full || !full[0]) {
1952+
return;
1953+
}
1954+
1955+
const char *module_path = full;
1956+
size_t len = strlen(full);
1957+
size_t self_len = sizeof(self_suffix) - SKIP_ONE;
1958+
if (len > self_len && strcmp(full + len - self_len, self_suffix) == 0) {
1959+
module_path = cbm_arena_strndup(a, full, len - self_len);
1960+
}
19021961

1962+
const char *local = alias && alias[0] ? alias : path_last(a, module_path);
1963+
if (!local || !local[0]) {
1964+
return;
1965+
}
1966+
bool keyword =
1967+
strcmp(local, "crate") == 0 || strcmp(local, "super") == 0 || strcmp(local, "self") == 0;
1968+
if (keyword && (!alias || !alias[0])) {
1969+
return;
1970+
}
1971+
if (rust_import_exists(&ctx->result->imports, local, module_path)) {
1972+
return;
1973+
}
1974+
1975+
CBMImport imp = {.local_name = local, .module_path = module_path};
1976+
cbm_imports_push(&ctx->result->imports, a, imp);
1977+
}
1978+
1979+
static void rust_import_walk(CBMExtractCtx *ctx, TSNode node, const char *prefix) {
1980+
if (ts_node_is_null(node)) {
1981+
return;
1982+
}
1983+
1984+
CBMArena *a = ctx->arena;
1985+
const char *kind = ts_node_type(node);
1986+
if (strcmp(kind, "use_list") == 0) {
1987+
uint32_t count = ts_node_named_child_count(node);
1988+
for (uint32_t i = 0; i < count; i++) {
1989+
rust_import_walk(ctx, ts_node_named_child(node, i), prefix);
1990+
}
1991+
return;
1992+
}
1993+
if (strcmp(kind, "scoped_use_list") == 0) {
1994+
char *path = rust_import_node_text_compact(a, rust_import_field(node, "path"), ctx->source);
1995+
char *next_prefix = rust_import_join(a, prefix, path);
1996+
rust_import_walk(ctx, rust_import_field(node, "list"), next_prefix);
1997+
return;
1998+
}
1999+
if (strcmp(kind, "use_as_clause") == 0) {
2000+
char *path = rust_import_node_text_compact(a, rust_import_field(node, "path"), ctx->source);
2001+
char *alias =
2002+
rust_import_node_text_compact(a, rust_import_field(node, "alias"), ctx->source);
2003+
char *full = rust_import_join(a, prefix, path);
2004+
rust_import_push(ctx, full, alias);
2005+
return;
2006+
}
2007+
if (strcmp(kind, "use_wildcard") == 0) {
2008+
TSNode inner =
2009+
ts_node_named_child_count(node) > 0 ? ts_node_named_child(node, 0) : (TSNode){0};
2010+
char *path = rust_import_node_text_compact(a, inner, ctx->source);
2011+
char *base = rust_import_join(a, prefix, path);
2012+
char *full =
2013+
base && base[0] ? cbm_arena_sprintf(a, "%s::*", base) : cbm_arena_strdup(a, "*");
2014+
rust_import_push(ctx, full, NULL);
2015+
return;
2016+
}
2017+
if (strcmp(kind, "line_comment") == 0 || strcmp(kind, "block_comment") == 0) {
2018+
return;
2019+
}
2020+
2021+
char *path = rust_import_node_text_compact(a, node, ctx->source);
2022+
char *full = rust_import_join(a, prefix, path);
2023+
rust_import_push(ctx, full, NULL);
2024+
}
2025+
2026+
static void parse_rust_imports(CBMExtractCtx *ctx) {
19032027
TSTreeCursor cursor = ts_tree_cursor_new(ctx->root);
19042028
if (!ts_tree_cursor_goto_first_child(&cursor)) {
19052029
ts_tree_cursor_delete(&cursor);
@@ -1911,21 +2035,7 @@ static void parse_rust_imports(CBMExtractCtx *ctx) {
19112035
continue;
19122036
}
19132037

1914-
char *full = cbm_node_text(a, node, ctx->source);
1915-
if (!full) {
1916-
continue;
1917-
}
1918-
// Strip "use " prefix and trailing ";"
1919-
if (strncmp(full, "use ", USE_PREFIX_LEN) == 0) {
1920-
full += USE_PREFIX_LEN;
1921-
}
1922-
size_t len = strlen(full);
1923-
if (len > 0 && full[len - SKIP_ONE] == ';') {
1924-
full[len - SKIP_ONE] = '\0';
1925-
}
1926-
1927-
CBMImport imp = {.local_name = path_last(a, full), .module_path = full};
1928-
cbm_imports_push(&ctx->result->imports, a, imp);
2038+
rust_import_walk(ctx, rust_import_field(node, "argument"), "");
19292039
} while (ts_tree_cursor_goto_next_sibling(&cursor));
19302040
ts_tree_cursor_delete(&cursor);
19312041
}

‎tests/test_extraction.c‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5764,15 +5764,47 @@ TEST(java_imports) {
57645764
PASS();
57655765
}
57665766

5767+
static const CBMImport *rust_find_import(const CBMFileResult *r, const char *local) {
5768+
for (int i = 0; i < r->imports.count; i++) {
5769+
if (r->imports.items[i].local_name && strcmp(r->imports.items[i].local_name, local) == 0) {
5770+
return &r->imports.items[i];
5771+
}
5772+
}
5773+
return NULL;
5774+
}
5775+
5776+
static int assert_rust_import(const CBMFileResult *r, const char *local,
5777+
const char *module_path) {
5778+
const CBMImport *imp = rust_find_import(r, local);
5779+
ASSERT_NOT_NULL(imp);
5780+
ASSERT_NOT_NULL(imp->module_path);
5781+
ASSERT_STR_EQ(imp->module_path, module_path);
5782+
return 0;
5783+
}
5784+
57675785
TEST(rust_imports) {
57685786
CBMFileResult *r = extract(
5769-
"use std::collections::HashMap;\nuse std::io::{self, Write};\nuse serde::Serialize;\n"
5787+
"use std::collections::HashMap;\n"
5788+
"use std::io::{self, Write};\n"
5789+
"use serde::Serialize;\n"
5790+
"use crate::h::{Handler, Other as O};\n"
5791+
"use a::{b::{C, D}, E};\n"
5792+
"use crate::ops::*;\n"
57705793
"fn main() {}\n",
57715794
CBM_LANG_RUST, "t", "main.rs");
57725795
ASSERT_NOT_NULL(r);
57735796
ASSERT_FALSE(r->has_error);
5774-
ASSERT_GT(r->imports.count, 0);
5775-
ASSERT(has_import(r, "std::collections::HashMap"));
5797+
ASSERT_EQ(r->imports.count, 10);
5798+
ASSERT_EQ(assert_rust_import(r, "HashMap", "std::collections::HashMap"), 0);
5799+
ASSERT_EQ(assert_rust_import(r, "io", "std::io"), 0);
5800+
ASSERT_EQ(assert_rust_import(r, "Write", "std::io::Write"), 0);
5801+
ASSERT_EQ(assert_rust_import(r, "Serialize", "serde::Serialize"), 0);
5802+
ASSERT_EQ(assert_rust_import(r, "Handler", "crate::h::Handler"), 0);
5803+
ASSERT_EQ(assert_rust_import(r, "O", "crate::h::Other"), 0);
5804+
ASSERT_EQ(assert_rust_import(r, "C", "a::b::C"), 0);
5805+
ASSERT_EQ(assert_rust_import(r, "D", "a::b::D"), 0);
5806+
ASSERT_EQ(assert_rust_import(r, "E", "a::E"), 0);
5807+
ASSERT_EQ(assert_rust_import(r, "*", "crate::ops::*"), 0);
57765808
cbm_free_result(r);
57775809
PASS();
57785810
}

‎tests/test_pipeline.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12779,6 +12779,9 @@ TEST(pipeline_go_rw_usage_never_cross_into_c) {
1277912779
* the generic-engine lane on its 12-language fixture). A Rust call never
1278012780
* targets another language's definition. pad_files > 0 forces the parallel
1278112781
* resolver, whose Rust registry is the shared all-defs build. */
12782+
static int imports_binding_count(cbm_store_t *s, const char *project, const char *file_path,
12783+
const char *local_name);
12784+
1278212785
/* Pads a Rust fixture with pad_files filler modules (pad_files > 0 forces the
1278312786
* parallel resolver: CBM_WORKERS=4 for the run), runs the full pipeline into
1278412787
* <tmp>/<db_name> and opens the store. NULL store = the run failed. */
@@ -12880,6 +12883,54 @@ TEST(pipeline_rust_call_never_binds_another_language_parallel) {
1288012883
return rust_registry_rust_only_case(52);
1288112884
}
1288212885

12886+
TEST(pipeline_rust_grouped_import_resolves_assoc_call_issue2922) {
12887+
enum { MANY_BYTES = 64 * 1024 };
12888+
char tmp[256];
12889+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_rust_grouped_XXXXXX");
12890+
if (!cbm_mkdtemp(tmp)) {
12891+
FAIL("tmpdir");
12892+
}
12893+
write_temp_file(tmp, "Cargo.toml", "[package]\nname = \"app\"\nversion = \"0.1.0\"\n");
12894+
write_temp_file(tmp, "src/h.rs",
12895+
"pub struct Handler { pub n: u32 }\n"
12896+
"impl Handler { pub fn new() -> Handler { Handler { n: 1 } } }\n"
12897+
"pub struct Other;\n");
12898+
12899+
char *many = (char *)malloc(MANY_BYTES);
12900+
ASSERT_NOT_NULL(many);
12901+
size_t off = 0;
12902+
for (int i = 0; i < 300; i++) {
12903+
int n = snprintf(many + off, MANY_BYTES - off,
12904+
"pub struct S%d;\nimpl S%d { pub fn new() -> S%d { S%d } }\n", i, i, i,
12905+
i);
12906+
ASSERT_TRUE(n > 0 && (size_t)n < MANY_BYTES - off);
12907+
off += (size_t)n;
12908+
}
12909+
write_temp_file(tmp, "src/many.rs", many);
12910+
free(many);
12911+
12912+
write_temp_file(tmp, "src/main.rs",
12913+
"mod h;\n"
12914+
"mod many;\n"
12915+
"use crate::h::{Handler, Other};\n"
12916+
"fn grouped() -> u32 { let _o = Other; Handler::new().n }\n"
12917+
"fn main() { grouped(); }\n");
12918+
12919+
cbm_pipeline_t *p = NULL;
12920+
cbm_store_t *s = rust_fixture_run(tmp, 0, "rust_grouped.db", &p);
12921+
ASSERT_NOT_NULL(s);
12922+
const char *project = cbm_pipeline_project_name(p);
12923+
12924+
ASSERT_EQ(imports_binding_count(s, project, "src/main.rs", "Handler"), 1);
12925+
ASSERT_EQ(imports_binding_count(s, project, "src/main.rs", "Other"), 1);
12926+
ASSERT_GTE(named_edge_to_file_count(s, project, "CALLS", "grouped", "new", "src/h.rs"), 1);
12927+
12928+
cbm_store_close(s);
12929+
cbm_pipeline_free(p);
12930+
th_rmtree(tmp);
12931+
PASS();
12932+
}
12933+
1288312934
/* `Opts::write(true)` is an associated function. The LSP leaves it to the
1288412935
* registry (import_map -> fsx.Opts.write), but its free-function short-name
1288512936
* fallback bound it to the project's only free `write` (cache.write) first, and
@@ -32066,6 +32117,7 @@ SUITE(pipeline) {
3206632117
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c_parallel);
3206732118
RUN_TEST(pipeline_rust_call_never_binds_another_language);
3206832119
RUN_TEST(pipeline_rust_call_never_binds_another_language_parallel);
32120+
RUN_TEST(pipeline_rust_grouped_import_resolves_assoc_call_issue2922);
3206932121
RUN_TEST(pipeline_rust_assoc_call_never_binds_free_namesake);
3207032122
RUN_TEST(pipeline_rust_assoc_call_never_binds_free_namesake_parallel);
3207132123
RUN_TEST(pipeline_rust_generic_impl_method_sources_its_calls);

0 commit comments

Comments
 (0)