Skip to content

Conversation

@paldepind
Copy link
Contributor

@paldepind paldepind commented Jan 19, 2026

The goal of this PR is to handle paths that access an associated type on a concrete type (concrete meaning not a type parameter) in TypeMention.

These paths fall into one of two cases:

  1. <Foo as Trait>::Assoc where Foo can be an arbitrarily complex type.
  2. Self::Assoc inside an impl block. For an impl block of the form impl Trait for Foo such a path is basically sugar for the above.

A key point is that resolving the associated type relies on how the concrete type implements the trait that the associated type is from. For instance, the added tests have examples where <Odd<i32> as GetSet>::Output is equal to bool but <Odd<bool> as GetSet>::Output is equal to char.

Changes to path resolution

Today case 2 is somewhat handled inside path resolution. But path resolution doesn't understand how types implement traits: For a Self::Assoc path we find all associated types with the name Assoc across all traits that Self implement. This leads to spurious path resolutions as seen in the tests.

To address this, the PR tweaks path resolution such that a Self::Assoc path resolves to the associated type in the trait where Assoc is from (except when Assoc is defined directly in the impl block that Self belongs to). This does not depend on type information, so it can be done correctly in path resolution.

Changes to type inference

After that we use the SatisfiesConstraint module in TypeMention to find the correct trait implementations and read the associated types off of those. When implementing this I ran into non-monotonic recursion. The problem as well as the workaround is documented in the comment for the MkTypeMention module.

Non-unique certain type inconsistency

The last commit adds an example of a non-unique certain type inconsistency introduced by the PR caused by a type mention that resolves to two paths.

In this case the two types obviously can't both be "certain", but they still represent certain information in the sense that any other types would certainly be incorrect. Hence it's still beneficial to include those types as certain, even though it breaks the inconsistency rule.

I tried to come up with an exclusion to the rule, but it's not trivial as the certain types originate in a type mention and can propagate many places from there.

DCA

The DCA report shows more types and a 0.2% point increase in resolved calls.

There is a small slowdown. I'm not seeing any bad joins, so I think it's from now constructing two instances of TypeMention. If that is indeed the cause it might be possible to limit the impact in follow up work by restricting PreTypeMention to the (small) subset of type mentions that are actually used to construct the type hierarchy (those used in conditionSatisfiesConstraint).

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Jan 19, 2026
@paldepind paldepind force-pushed the rust/self-path-assoc branch 5 times, most recently from eca7559 to 8a465bd Compare January 23, 2026 12:40
@paldepind paldepind force-pushed the rust/self-path-assoc branch from 8a465bd to 7e74844 Compare January 26, 2026 14:15
typePath = TypePath::singleton(TSelfTypeParameter(this)) and
result = TSelfTypeParameter(this)
or
exists(TypeAlias alias |

Check warning

Code scanning / CodeQL

Omittable 'exists' variable Warning

This exists variable can be omitted by using a don't-care expression
in this argument
.
@paldepind paldepind force-pushed the rust/self-path-assoc branch 2 times, most recently from 4dcb7c1 to 9d10607 Compare January 27, 2026 12:21
@paldepind paldepind changed the title Rust: Paths to associated types resolve to the associated type if implementation is unclear Rust: Implement type inference for associated types for concrete types Jan 27, 2026
@paldepind paldepind force-pushed the rust/self-path-assoc branch 2 times, most recently from daeed4e to ddd5b63 Compare January 27, 2026 13:10
@paldepind paldepind marked this pull request as ready for review January 27, 2026 15:00
@paldepind paldepind requested a review from a team as a code owner January 27, 2026 15:00
Copilot AI review requested due to automatic review settings January 27, 2026 15:00
Copy link
Contributor

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 implements type inference for associated types on concrete types in Rust. It handles paths like <Foo as Trait>::Assoc and Self::Assoc in impl blocks by using a two-phase instantiation approach to avoid non-monotonic recursion. The key innovation is parameterizing TypeMention with a predicate for resolving additional path types, creating PreTypeMention first (with empty predicate) for building the type hierarchy, then the full TypeMention (with trait implementation knowledge) for complete type resolution.

Changes:

  • Two-phase type mention instantiation to resolve non-monotonic recursion between type mentions and the type hierarchy
  • Path resolution improvements for Self::Assoc paths in impl blocks to correctly resolve to the trait's associated type
  • New test cases demonstrating associated type resolution for concrete types like <Odd<i32> as GetSet>::Output = bool and <Odd<bool> as GetSet>::Output = char

Reviewed changes

Copilot reviewed 9 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
rust/ql/test/library-tests/type-inference/type-inference.expected Updated test expectations with additional type inferences for concrete associated types
rust/ql/test/library-tests/type-inference/associated_types.rs Added comprehensive test cases for concrete type associated type access
rust/ql/test/library-tests/type-inference/CONSISTENCY/TypeInferenceConsistency.expected Documents expected non-unique certain type inconsistency
rust/ql/test/library-tests/path-resolution/path-resolution.expected Updated path resolution expectations for improved Self path handling
rust/ql/test/library-tests/path-resolution/main.rs Added test for Self path resolution within impl blocks
rust/ql/lib/codeql/rust/internal/typeinference/TypeMention.qll Core implementation of two-phase type mention instantiation
rust/ql/lib/codeql/rust/internal/typeinference/TypeInferenceConsistency.qll Updated consistency checks to filter source-only inconsistencies
rust/ql/lib/codeql/rust/internal/typeinference/TypeInference.qll Changed to use PreTypeMention for type hierarchy construction
rust/ql/lib/codeql/rust/internal/typeinference/Type.qll Updated type parameter default handling
rust/ql/lib/codeql/rust/internal/PathResolution.qll Significant refactoring of Self path and associated type resolution logic

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}

TypeItemNode getResolved() { result = resolved }
class AdditionalPathTypeMention extends PathTypeMention {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately the indentation change caused from making this a module shows a very annoying diff 😢. This is the only spot where there are actual changes.

@paldepind paldepind added the no-change-note-required This PR does not need a change note label Jan 27, 2026
@paldepind paldepind force-pushed the rust/self-path-assoc branch from 2256e24 to 2cb0e81 Compare January 27, 2026 15:18
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