-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Rust: Type inference for impl trait types with type parameters #20119
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: Type inference for impl trait types with type parameters #20119
Conversation
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 implements type inference for impl
trait types that contain type parameters from their enclosing function. The implementation allows proper handling of return position impl
types that reference function type parameters, such as fn get<T: Clone>(x: T) -> impl MyTrait<T>
.
Key changes:
- Adds support for type parameters within
impl
trait types by creatingImplTraitTypeParameter
objects - Updates type inference logic to properly handle and rank these new type parameters
- Extends test coverage with various scenarios of parameterized
impl
trait return types
Reviewed Changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
main.rs | Adds comprehensive test cases for impl trait types with type parameters |
PathResolutionConsistency.expected | Updates line number references due to test additions |
TypeMention.qll | Implements type resolution for ImplTraitTypeParameter |
TypeInference.qll | Extends type parameter ranking system to handle ImplTraitTypeParameter |
Type.qll | Core implementation of ImplTraitTypeParameter class and related logic |
ImplTraitTypeReprImpl.qll | Adds getFunctionReturnPos() method to locate containing function |
.gitattributes & .generated.list | Removes generated file markers for ImplTraitTypeReprImpl.qll |
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.
Mostly questions to aid my own understanding...
rust/ql/lib/codeql/rust/elements/internal/ImplTraitTypeReprImpl.qll
Outdated
Show resolved
Hide resolved
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.
All concerns addressed. 👍
We currently can't handle
impl
types with type parameters inside them, such as this example whereT
occurs within theimpl
:The example is from the draft PR: #19954
This PR fixes the problem by letting type parameters of the function where the return position
impl
occurs also induce a type parameter of theimpl
type.In the above example
get
has a type parameterT
which occurs in theimpl
in return position. Hence a type parameter corresponding toT
is added to theimpl
type. Whenget
is called an a specific type is known forT
then that type will be instantiated inside the returnedimpl
type.This implementation is a bit different from the draft one in #19954. That implementation relies on inferring trait types directly. I.e. elements of
impl Foo + Bar
will have both the typeFoo
andBar
. That is a perfectly fine implementation, but I believe this approach has two advantages:impl
type.