Skip to content

fix(analysis): stop module import aliases crossing a module boundary #3544

Closed

Description

@EffortlessSteven

Problem

Rust does not carry a use into a child module. ScopedModuleImportAlias::module_path_at resolves purely by line range, so a module alias imported in one module is still resolved for calls inside a nested mod that never imported it. That credits a relation against a target the code cannot reach — a fabricated cross-module reach, and a wrong actionable signal.

This is the same defect #3483 fixed on the direct function import path (ScopedImportedFunctionAlias::binding_at), still present on the module import path that #3487 owns. Both now share scan_lexical_use_statements and the same line_module_depths data, so the two paths are inconsistent: one honours the module boundary, the other does not.

Deterministic reproduction

Verified on main at dcb18a8 (Linux, rustc 1.95.0), by probing module_import_aliases / module_path_at directly:

use crate::alpha as a;
fn outer() { a::compute(); }
mod child {
    fn inner() { a::compute(); }   // E0433 in real Rust
}
fn tail() { a::compute(); }

Actual:

outer              line 2 -> alpha
inside mod child   line 4 -> alpha    <-- never imported the alias
tail               line 6 -> alpha

Expected: line 4 unresolved; lines 2 and 6 unchanged.

The line-4 call does not compile — use crate::alpha as a; is not in scope inside mod child, which is E0433 — so any relation credited through it names a target the code cannot reach.

Target seam

crates/ripr/src/analysis/test_grip_evidence/related_tests/context.rs only:

  • ScopedModuleImportBinding — carry the nested-module line ranges, as ScopedImportedFunctionBinding::nested_module_lines already does.
  • ScopedModuleImportAlias::module_path_at — skip a candidate whose nested-module ranges cover the queried line, alongside the existing end_is_partial guard.
  • module_import_aliases — populate the ranges from the shared scan via the existing nested_module_line_ranges helper and LexicalUseStatement::module_depth.

No new scanning machinery is needed; #3483 already added and merged all of it.

Acceptance criteria

  • A module alias imported in an enclosing module is not resolved for a call inside a nested mod that never imported it.
  • A nested module that imports its own module alias resolves to its own target.
  • The enclosing binding resumes after the nested module closes.
  • Blocks, impl bodies, and fn bodies remain unaffected — they are not module boundaries, so a function-local use still shadows the file-level one inside its own block and stops at its closing brace.
  • The existing end_is_partial mid-line scope-close guard and the same-scope ambiguity guard keep their current behaviour.
  • A discriminating regression fails if the exclusion is removed, and a positive control fails if the alias stops resolving where it legitimately should.

Proof

cargo test -p ripr analysis::test_grip_evidence::tests -- --nocapture
cargo test -p ripr --lib
cargo clippy -p ripr --all-targets -- -D warnings
cargo fmt --all -- --check
cargo xtask goldens check
cargo xtask dogfood
cargo xtask precommit

Note on goldens: when #3483 made the equivalent change on the direct-function path, goldens check, dogfood, and fixtures all came back clean with nothing re-blessed. That is the expected result here too, because the construct being declined does not compile and so cannot appear in a corpus that builds. A moved golden would be the interesting signal — it would mean a fixture had been credited through a binding it could not actually reach.

Non-goals

Risk and rollback

Fail-closed in direction: the change only ever withholds a binding that Rust itself would not provide, so it cannot invent a new relation. Rollback is reverting the one commit; no schema, stored state, fixture, or golden migration is involved.

Provenance

Found while reviewing the #3487 reconciliation on #3483, and raised there rather than folded in, because it widens that PR's single acceptance criterion onto a path #3487 owned and its red gate needed only a formatting fix. Filing it here so the finding is durable rather than living in a merged PR comment.

Related: #3413 (parent claim, acceptance complete), #3483, #3487.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status/needs-work

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions