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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -1036,6 +1045,20 @@ pub(in crate::analysis::test_grip_evidence) fn module_import_aliases(
continue;
}
let end_line = scan.end_line(statement);
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)
Expand All @@ -1047,6 +1070,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),
});
}
Expand Down Expand Up @@ -1133,6 +1157,11 @@ struct LexicalUseScan {
/// and line coordinates alone cannot say which.
line_closes_scope_midline: Vec<bool>,
line_module_depths: Vec<usize>,
/// 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<usize>,
}

impl LexicalUseScan {
Expand Down Expand Up @@ -1168,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<usize> = Vec::new();
let mut line_module_depths = Vec::new();
let mut pending_use: Option<(usize, usize, usize, String)> = None;
let mut brace_depth = 0usize;
Expand Down Expand Up @@ -1237,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 => {
Expand Down Expand Up @@ -1265,6 +1299,7 @@ fn scan_lexical_use_statements(source: &str) -> LexicalUseScan {
line_depths_after,
line_closes_scope_midline,
line_module_depths,
module_open_lines,
}
}

Expand All @@ -1283,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)
Expand Down
25 changes: 23 additions & 2 deletions crates/ripr/src/analysis/test_grip_evidence/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
Loading