Rust: Infer certain type for shorthand self#20348
Merged
paldepind merged 5 commits intogithub:mainfrom Sep 9, 2025
Merged
Conversation
4df7945 to
e610465
Compare
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR improves type inference for Rust's shorthand self parameters by treating them consistently with explicit self: Self annotations. The change extends type inference to cover implicit self parameter syntax, which is syntactic sugar for explicit self type annotations.
Key changes:
- Unifies type inference logic for both shorthand and explicit self parameter syntax
- Moves shorthand self type inference into the
inferAnnotatedTypefunction - Removes duplicate logic for implicit self parameter handling
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| rust/ql/test/library-tests/type-inference/type-inference.expected | Test expectations updated to reflect removal of spurious type inferences |
| rust/ql/test/library-tests/type-inference/main.rs | Added certainType annotations to test shorthand self parameters |
| rust/ql/lib/codeql/rust/internal/TypeInference.qll | Refactored self parameter type inference logic and consolidated duplicate functions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We currently infer certain type information for
selfinfn f(self: Self)but not forselfinfn f(self)even though the latter is just syntactic sugar for the former. This PR removes that distinction. Just in our tests, that removes 4 spurious types.The PR uses the word "shorthand" which comes from the Rust reference which uses the terms "shorthand self" and "shorthand syntax".
The logic for inferring the type of a shorthand self parameters is now included inside
inferAnnotatedTypewhich I think makes sense as there's explicit annotations right beneath the sugar.The first DCA run showed a performance regression that I can't reproduce locally (I tried quick evaling
inferTypeoniced). The second DCA run shows only a tiny performance regression, and doesn't really agree with the first. For instance, neon-empty-diff was the biggest regression in the first run but shows a speedup in the second. So I think it's just noice, and the rest of the DCA report looks as expected.