-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Rust: Infer certain type for shorthand self
#20348
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
base: main
Are you sure you want to change the base?
Conversation
4df7945
to
e610465
Compare
There was a problem hiding this 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 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
inferAnnotatedType
function - 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 |
We currently infer certain type information for
self
infn f(self: Self)
but not forself
infn 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
inferAnnotatedType
which 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
inferType
oniced
). 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.