Skip to content

Conversation

paldepind
Copy link
Contributor

@paldepind paldepind commented Sep 2, 2025

We currently infer certain type information for self in fn f(self: Self) but not for self in fn 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 on iced). 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.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Sep 2, 2025
@paldepind paldepind added the no-change-note-required This PR does not need a change note label Sep 2, 2025
@paldepind paldepind marked this pull request as ready for review September 2, 2025 14:50
@paldepind paldepind requested a review from a team as a code owner September 2, 2025 14:50
@Copilot Copilot AI review requested due to automatic review settings September 2, 2025 14:50
@paldepind paldepind marked this pull request as draft September 2, 2025 14:55
Copilot

This comment was marked as outdated.

@paldepind paldepind force-pushed the rust/certain-shorthand-self branch from 4df7945 to e610465 Compare September 3, 2025 10:53
@paldepind paldepind marked this pull request as ready for review September 3, 2025 17:22
@Copilot Copilot AI review requested due to automatic review settings September 3, 2025 17:22
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 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

@paldepind paldepind requested a review from hvitved September 4, 2025 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-change-note-required This PR does not need a change note Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant