From a87b83a5a7d2dec100ee8e5637af8e23e4dfacd6 Mon Sep 17 00:00:00 2001 From: Steven Zimmerman Date: Fri, 28 Aug 2026 22:26:13 -0400 Subject: [PATCH 1/2] fix(analysis): isolate module import aliases by scope --- .../test_grip_evidence/related_tests/context.rs | 16 ++++++++++++++++ .../src/analysis/test_grip_evidence/tests.rs | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/crates/ripr/src/analysis/test_grip_evidence/related_tests/context.rs b/crates/ripr/src/analysis/test_grip_evidence/related_tests/context.rs index 0e6f5b85610..2ccf19e6c84 100644 --- a/crates/ripr/src/analysis/test_grip_evidence/related_tests/context.rs +++ b/crates/ripr/src/analysis/test_grip_evidence/related_tests/context.rs @@ -285,6 +285,8 @@ struct ScopedModuleImportBinding { module_path: String, start_line: usize, end_line: usize, + /// Lines inside nested module bodies are not covered by this import. + nested_module_lines: Vec<(usize, usize)>, /// The owning scope closes part-way through `end_line`, with code after /// the closing brace. See `LexicalUseScan::line_closes_scope_midline`. end_is_partial: bool, @@ -319,6 +321,13 @@ impl ScopedModuleImportAlias { // tell which, and guessing would credit the wrong owner. return None; } + if binding + .nested_module_lines + .iter() + .any(|(first, last)| *first <= line && line <= *last) + { + continue; + } match nearest { Some(current) if current.start_line > binding.start_line => {} Some(current) if current.start_line == binding.start_line => { @@ -1036,6 +1045,12 @@ pub(in crate::analysis::test_grip_evidence) fn module_import_aliases( continue; } let end_line = scan.end_line(statement); + let nested_module_lines = nested_module_line_ranges( + &scan.line_module_depths, + statement.start_line, + end_line, + statement.module_depth, + ); for (alias, module_path) in parsed { aliases .entry(alias) @@ -1047,6 +1062,7 @@ pub(in crate::analysis::test_grip_evidence) fn module_import_aliases( module_path, start_line: statement.start_line, end_line, + nested_module_lines: nested_module_lines.clone(), end_is_partial: scan.end_is_partial(end_line), }); } diff --git a/crates/ripr/src/analysis/test_grip_evidence/tests.rs b/crates/ripr/src/analysis/test_grip_evidence/tests.rs index 55d94ab2254..db589a4259c 100644 --- a/crates/ripr/src/analysis/test_grip_evidence/tests.rs +++ b/crates/ripr/src/analysis/test_grip_evidence/tests.rs @@ -585,8 +585,8 @@ fn outer_after() { aliases .get("unique") .and_then(|alias| alias.module_path_at(13)), - Some("only_here"), - "a unique outer alias stays credited inside a nested module" + None, + "an outer module alias must not leak into a nested module" ); let conflicts = module_import_aliases( From f3c508551507cb66a6f27a9f11f3d13017029a40 Mon Sep 17 00:00:00 2001 From: "Steven Zimmerman, CPA" <15812269+EffortlessSteven@users.noreply.github.com> Date: Fri, 28 Aug 2026 23:26:36 -0400 Subject: [PATCH 2/2] fix(analysis): fail closed on inline nested-module opening lines (#3544 review) A one-liner `mod nested { call(); }` records the parent module depth before its opening brace, so the depth-range nested check missed the line and module_path_at resolved the parent alias for a call that Rust would reject without an explicit child import. The scanner now records every module-opening line, and the import binding marks those lines nested so inline resolution fails closed instead of strengthening exposure with wrong-owner evidence. Regression: the inline one-liner case resolves None on the opening line and the parent alias after it; with the open-line recording disabled the new assertion fails (mutation-checked). --- .../related_tests/context.rs | 33 +++++++++++++++++-- .../src/analysis/test_grip_evidence/tests.rs | 21 ++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/crates/ripr/src/analysis/test_grip_evidence/related_tests/context.rs b/crates/ripr/src/analysis/test_grip_evidence/related_tests/context.rs index 2ccf19e6c84..e1d21f8da5e 100644 --- a/crates/ripr/src/analysis/test_grip_evidence/related_tests/context.rs +++ b/crates/ripr/src/analysis/test_grip_evidence/related_tests/context.rs @@ -1045,12 +1045,20 @@ pub(in crate::analysis::test_grip_evidence) fn module_import_aliases( continue; } let end_line = scan.end_line(statement); - let nested_module_lines = nested_module_line_ranges( + let mut nested_module_lines = nested_module_line_ranges( &scan.line_module_depths, statement.start_line, end_line, statement.module_depth, ); + // An inline `mod nested { call(); }` opening line records the + // parent depth before its brace, so the depth ranges alone miss it; + // mark it nested explicitly (fail closed, #3544 review). + for line in &scan.module_open_lines { + if statement.start_line <= *line && *line <= end_line { + nested_module_lines.push((*line, *line)); + } + } for (alias, module_path) in parsed { aliases .entry(alias) @@ -1149,6 +1157,11 @@ struct LexicalUseScan { /// and line coordinates alone cannot say which. line_closes_scope_midline: Vec, line_module_depths: Vec, + /// Lines where a `mod` scope opened. The depth vector records the + /// pre-line depth, so an inline `mod nested { call(); }` body keeps the + /// parent depth on its opening line; those lines are ambiguous for + /// line-based alias resolution (#3544 review). + module_open_lines: Vec, } impl LexicalUseScan { @@ -1184,6 +1197,7 @@ fn scan_lexical_use_statements(source: &str) -> LexicalUseScan { let mut line_closes_scope_midline = Vec::new(); let mut scope_starts = vec![1usize]; let mut module_scopes = vec![false]; + let mut module_open_lines: Vec = Vec::new(); let mut line_module_depths = Vec::new(); let mut pending_use: Option<(usize, usize, usize, String)> = None; let mut brace_depth = 0usize; @@ -1253,7 +1267,11 @@ fn scan_lexical_use_statements(source: &str) -> LexicalUseScan { '{' => { brace_depth = brace_depth.saturating_add(1); scope_starts.push(line_number); - module_scopes.push(segment_declares_module(&segment)); + let declares_module = segment_declares_module(&segment); + module_scopes.push(declares_module); + if declares_module && module_open_lines.last() != Some(&line_number) { + module_open_lines.push(line_number); + } segment.clear(); } '}' if brace_depth > 0 => { @@ -1281,6 +1299,7 @@ fn scan_lexical_use_statements(source: &str) -> LexicalUseScan { line_depths_after, line_closes_scope_midline, line_module_depths, + module_open_lines, } } @@ -1299,12 +1318,20 @@ pub(in crate::analysis::test_grip_evidence) fn direct_function_import_aliases( continue; } let end_line = scan.end_line(statement); - let nested_module_lines = nested_module_line_ranges( + let mut nested_module_lines = nested_module_line_ranges( &scan.line_module_depths, statement.start_line, end_line, statement.module_depth, ); + // An inline `mod nested { call(); }` opening line records the + // parent depth before its brace, so the depth ranges alone miss it; + // mark it nested explicitly (fail closed, #3544 review). + for line in &scan.module_open_lines { + if statement.start_line <= *line && *line <= end_line { + nested_module_lines.push((*line, *line)); + } + } for (alias, imported) in parsed { aliases .entry(alias) diff --git a/crates/ripr/src/analysis/test_grip_evidence/tests.rs b/crates/ripr/src/analysis/test_grip_evidence/tests.rs index db589a4259c..ecaca0526f8 100644 --- a/crates/ripr/src/analysis/test_grip_evidence/tests.rs +++ b/crates/ripr/src/analysis/test_grip_evidence/tests.rs @@ -600,6 +600,27 @@ fn outer_after() { "same-scope conflicting module aliases must fail closed" ); + // An inline one-liner `mod nested { ... }` records the parent module + // depth before its opening brace, so the alias must fail closed on that + // line rather than credit the parent binding (#3544 review). + let inline_nested = module_import_aliases( + "use crate::only_here as unique; +mod nested { fn call() { unique::compute(); } } +fn outer_call() { unique::compute(); }", + ); + let inline_unique = inline_nested + .get("unique") + .ok_or_else(|| "inline nested alias should be indexed".to_string())?; + assert!( + inline_unique.module_path_at(2).is_none(), + "an inline nested-module opening line must not resolve the parent alias" + ); + assert_eq!( + inline_unique.module_path_at(3), + Some("only_here"), + "calls after the inline module still resolve the parent alias" + ); + // The shared lexical scanner narrows a block-local module alias to its // own block instead of publishing it file-wide. let block_local = module_import_aliases(