Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust: Take prelude into account when resolving paths #19157

Merged
merged 5 commits into from
Apr 4, 2025

Conversation

hvitved
Copy link
Contributor

@hvitved hvitved commented Mar 31, 2025

This PR started out with me wanting to get rid of the VariantInLib workaround, which was needed prior to extracting the crate graph.

Removing VariantLib means we will be using our QL logic for resolving option::Option and result::Result paths, instead of relying on getExtendedCanonicalPath provided by the extractor. However, in order to resolve those paths, we need to take the standard library prelude into account, which is exactly what this PR does.

Since we don't know which edition of Rust is being used, we simply include all the editions; 2015, 2018, 2021, and 2024.

Removing VariantLib also revealed that we need to handle $crate paths inside expanded macros. For now, we over-approximate resolution of $crate paths by considering all transitive dependencies.

DCA shows that, while we mostly maintain performance, we gain an additional 10% true positive call edges (up 475,373 from 430,130) and an additional 14% true positive resolved paths (up 368,369 from 324,251), all numbers computed across the entire DCA suite.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Mar 31, 2025
@hvitved hvitved force-pushed the rust/path-resolution-prelude branch 7 times, most recently from f5a7f97 to 905ee47 Compare April 2, 2025 09:39
@hvitved hvitved marked this pull request as ready for review April 2, 2025 13:00
@hvitved hvitved requested review from Copilot and paldepind April 2, 2025 13:00
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances path resolution in Rust by incorporating the standard library prelude and improved handling of $crate macro paths.

  • Introduces additional test cases in a new module (m21) that exercise the resolution of unit variant enums and unit structs.
  • Adjusts usage patterns in the test code to reflect QL resolution expectations regarding the Rust prelude and macro expansion.
Files not reviewed (19)
  • rust/ql/integration-tests/hello-project/summary.expected: Language not supported
  • rust/ql/integration-tests/hello-workspace/summary.cargo.expected: Language not supported
  • rust/ql/integration-tests/hello-workspace/summary.rust-project.expected: Language not supported
  • rust/ql/lib/codeql/rust/dataflow/internal/Content.qll: Language not supported
  • rust/ql/lib/codeql/rust/dataflow/internal/DataFlowImpl.qll: Language not supported
  • rust/ql/lib/codeql/rust/dataflow/internal/FlowSummaryImpl.qll: Language not supported
  • rust/ql/lib/codeql/rust/elements/internal/EnumImpl.qll: Language not supported
  • rust/ql/lib/codeql/rust/elements/internal/PathImpl.qll: Language not supported
  • rust/ql/lib/codeql/rust/frameworks/stdlib/Stdlib.qll: Language not supported
  • rust/ql/lib/codeql/rust/internal/CachedStages.qll: Language not supported
  • rust/ql/lib/codeql/rust/internal/PathResolution.qll: Language not supported
  • rust/ql/lib/codeql/rust/internal/PathResolutionConsistency.qll: Language not supported
  • rust/ql/lib/codeql/rust/internal/Type.qll: Language not supported
  • rust/ql/lib/codeql/rust/internal/TypeInference.qll: Language not supported
  • rust/ql/test/extractor-tests/canonical_path/CONSISTENCY/PathResolutionConsistency.expected: Language not supported
  • rust/ql/test/extractor-tests/canonical_path_disabled/CONSISTENCY/PathResolutionConsistency.expected: Language not supported
  • rust/ql/test/library-tests/path-resolution/CONSISTENCY/PathResolutionConsistency.expected: Language not supported
  • rust/ql/test/library-tests/path-resolution/path-resolution.expected: Language not supported
  • rust/ql/test/library-tests/path-resolution/path-resolution.ql: Language not supported

Tip: If you use Visual Studio Code, you can request a review from Copilot before you push from the "Source Control" tab. Learn more


fn f() {
let _ = MyEnum::A; // $ item=I104
let _ = MyStruct {}; // $ item=I106
Copy link
Preview

Copilot AI Apr 2, 2025

Choose a reason for hiding this comment

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

MyStruct is defined as a unit struct; therefore, it should be instantiated without braces. Please update the instantiation to 'let _ = MyStruct;'.

Suggested change
let _ = MyStruct {}; // $ item=I106
let _ = MyStruct; // $ item=I106

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

@hvitved hvitved force-pushed the rust/path-resolution-prelude branch from 905ee47 to 02e9795 Compare April 2, 2025 17:15
@hvitved hvitved requested a review from paldepind April 3, 2025 11:33
@hvitved hvitved force-pushed the rust/path-resolution-prelude branch from 02e9795 to f4e9382 Compare April 3, 2025 14:07
Copy link
Contributor

@paldepind paldepind left a comment

Choose a reason for hiding this comment

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

Looks great. I only have one small comment and a question.

@@ -171,17 +173,22 @@ abstract class ItemNode extends Locatable {
}

/** Gets a successor named `name` of this item, if any. */
pragma[nomagic]
cached
Copy link
Contributor

Choose a reason for hiding this comment

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

Did something change in this PR that means getASuccessor should now be cached? Or would it already have made sense to cache it prior to this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it already made sense prior to this PR since the type inference stage relies on the predicate.

Co-authored-by: Simon Friis Vindum <paldepind@github.com>
@hvitved hvitved requested a review from paldepind April 4, 2025 09:42
@hvitved hvitved merged commit 6f704f0 into github:main Apr 4, 2025
17 checks passed
@hvitved hvitved deleted the rust/path-resolution-prelude branch April 4, 2025 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants