Skip to content

Rust: resolve modules declared in include expansions - #23113

Draft
hvitved wants to merge 8 commits into
github:mainfrom
hvitved:hvitved-rust-include-module-paths
Draft

hvitved wants to merge 8 commits into
github:mainfrom
hvitved:hvitved-rust-include-module-paths

Conversation

@hvitved

@hvitved hvitved commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Preserve source locations in item expansions of include! calls, so module declarations resolve relative to the included file rather than the include invocation.

Use rust-analyzer's resolved include targets and source spans instead of parsing arguments or treating expansion offsets as source offsets. Cache each included file's location data, and extend QL crate membership and nested module lookup to follow included files.

Add regressions for nested includes, qualified and shadowed macro names, alternate delimiters, comments, trailing commas, computed include paths, crate-root references, exported macros, and derives in included files. Regenerate the affected location expectations.

Fixes #23009.

hvitved and others added 2 commits November 5, 2026 12:49
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added the Rust Pull requests that update Rust code label Nov 12, 2026
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Incorrect source-range mapping and resolving nested includes relative to the outer file produce wrong locations and can leave modules unresolved or incorrectly resolved.

6 open issues
What changed in this PR

This PR updates Rust extraction so item expansions from literal include! calls retain the included file’s location, allowing module declarations to resolve relative to that file rather than the invocation.

Changes:

  • Adds included-file context and file-aware location emission to the extractor.
  • Adds a regression for calling a function in a module declared by an included tree/mod.rs.
  • Updates location expectations and inline path-resolution checks. Computed include paths remain outside scope.
File Description
rust/​ql/​test/​library-tests/​path-resolution/​path-resolution.expected Updates expected module and path-resolution results.
rust/​ql/​test/​library-tests/​path-resolution/​main.rs Adds the include invocation and regression call.
rust/​ql/​test/​library-tests/​path-resolution/​included/​tree/​mod.rs Declares the included module.
rust/​ql/​test/​library-tests/​path-resolution/​included/​tree/​included_module.rs Provides the regression’s target function.
rust/​ql/​test/​extractor-tests/​macro-expansion/​test.expected Updates macro-expansion location expectations.
rust/​ql/​test/​extractor-tests/​macro-expansion/​PrintAst.expected Updates printed AST locations for included items.
rust/​ql/​test/​extractor-tests/​generated/​MacroItems/​MacroItems.expected Updates generated MacroItems location expectations.
rust/​ql/​lib/​utils/​test/​PathResolutionInlineExpectationsTest.qll Excludes unqualified include macro paths from inline checks.
rust/​extractor/​src/​translate/​base.rs Adds include-file contexts and file-aware location emission.
rust/​extractor/​src/​rust_analyzer.rs Passes virtual filesystem access into semantic file information.

Review effort: Max

Comment thread rust/extractor/src/translate/base.rs Resolved
Comment thread rust/extractor/src/translate/base.rs Resolved
Comment thread rust/extractor/src/translate/base.rs Resolved
Comment thread rust/extractor/src/translate/base.rs Resolved
Comment thread rust/extractor/src/translate/base.rs Resolved
Comment thread rust/extractor/src/translate/base.rs Resolved
Use rust-analyzer's resolved include targets and source spans, cache file locations, and follow included files during QL crate and module resolution. Cover nested, qualified, delimited, computed, and shadowed includes with regression tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@hvitved hvitved changed the title Rust: resolve modules declared in literal include expansions Rust: resolve modules declared in include expansions Nov 13, 2026
@hvitved
hvitved requested a max review from Copilot November 13, 2026 09:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread rust/extractor/src/translate/base.rs Resolved
Comment thread rust/extractor/src/translate/base.rs Resolved
Comment thread rust/extractor/src/translate/base.rs Resolved
Map ordinary macros through their include-site invocation, archive included targets and preserve extraction classification, and keep diagnostics file-aware. Track active crate items rather than physical files, and resolve non-mod.rs include modules relative to the included directory. Add regressions for generated output, disabled and duplicate exports, shared library roots, macro-generated modules, and included-file warnings.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Included-source handling incorrectly excludes compiled code from quality analysis and can omit generated sibling modules or their source control-flow graphs.

4 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Attribute include parse errors to the erroneous source line

rust/​extractor/​src/​translate/​base.rs:596

For a syntax error directly in the included text, this still passes the outer include! call as the diagnostic owner. emit_macro_expansion_parse_errors then maps the expansion's error range through that call, whose syntax belongs to the invoking file, so the warning is attributed to the include site instead of the erroneous included line. Map include-expansion parse errors through expanded.value and add a direct syntax-error case to the diagnostics fixture.

6 resolved since last review

Review effort: Max

Comment thread rust/extractor/src/main.rs Resolved
Comment thread rust/extractor/src/translate/base.rs Resolved
Comment thread rust/extractor/src/translate/base.rs Resolved
Track semantic availability for included files and isolate the library-root include regression from its parent test.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread rust/extractor/src/main.rs Resolved
Comment thread rust/extractor/src/main.rs Resolved
Comment thread rust/extractor/src/translate/base.rs Resolved
Comment thread rust/extractor/src/translate/base.rs Resolved
Comment thread rust/extractor/src/main.rs Resolved
Comment thread rust/ql/lib/codeql/files/FileSystem.qll Resolved
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread rust/extractor/src/main.rs Resolved
Comment thread rust/extractor/src/main.rs Resolved
Comment thread rust/extractor/src/main.rs Resolved
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
private predicate isNotInactiveStandaloneCopy(AstNode node) {
not exists(ExtractedFile file |
file = node.getFile() and
exists(AstNode included |

This branch has not been deployed

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

Labels

documentation Rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rust: modules declared in a file brought in by include! are not resolved (no canonical paths, unresolved calls), e.g. in windows-sys and windows

3 participants