-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Rust: Improve handling of trait bounds #19062
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
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 improves type inference for trait bounds on type parameters by refining the handling of base type mentions and tweaking the evaluation of type parameters in trait bounds. It also adds new test modules to validate the changes in trait bound resolution and type alias handling.
- Introduces the module "type_parameter_bounds" with various functions to test trait method resolution.
- Adds the module "type_aliases" to test generic and non-generic type alias inference.
Files not reviewed (3)
- rust/ql/lib/codeql/rust/internal/Type.qll: Language not supported
- rust/ql/lib/codeql/rust/internal/TypeInference.qll: Language not supported
- shared/typeinference/codeql/typeinference/internal/TypeInference.qll: Language not supported
Comments suppressed due to low confidence (1)
rust/ql/test/library-tests/type-inference/main.rs:546
- The variable 'p3' is declared twice in the same scope in function 'f'; using distinct names may clarify the test cases.
let p3: AnotherPair<S3> = PairOption::PairNone(); // type for `Snd` missing, spurious `S3` for `Fst`
Tip: Leave feedback on Copilot's review comments with the 👎 and 👍 buttons to help improve review quality. Learn more
| type AnotherPair<Thr> = PairOption<S2, Thr>; | ||
|
|
||
| pub fn f() { | ||
| // Type can be infered from the constructor |
Copilot
AI
Mar 19, 2025
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.
The word 'infered' appears in this comment; it should be spelled 'inferred'.
| // Type can be infered from the constructor | |
| // Type can be inferred from the constructor |
| let p1: MyPair = PairOption::PairBoth(S1, S2); | ||
| println!("{:?}", p1); | ||
|
|
||
| // Type can be only infered from the type alias |
Copilot
AI
Mar 19, 2025
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.
The word 'infered' appears in this comment; it should be spelled 'inferred'.
| // Type can be only infered from the type alias | |
| // Type can be only inferred from the type alias |
hvitved
left a comment
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.
Very nice! Let's wait for DCA before merging.
|
DCA is looking fine. |
This PR improves type inference for trait bounds on type parameters. For instance
where
methodis a trait method.This is done by implementing
getABaseTypeMentionon type parameter types s.t. their bounds are base type mentions and by a small tweak tobaseTypeMentionHasNonTypeParameterAt.To explain and motivate the change to
baseTypeMentionHasNonTypeParameterAtconsider the C# example below.I'm not sure if this can actually happen in C#, but we suppose that
Tis some type parameter in scope and we have:Here the meaning of
SandTinSuperare different.Tis a fixed type at the class declaration andSis a type parameter of the class and the subclassing (Rust makes this more clear as the type quantification is explicit). Hence theTshould actually be handled just likeInt, butbaseTypeMentionHasNonTypeParameterAtwhich gives us the "concrete" types in the type mention, would only produceIntand notS, due tonot t instanceof TypeParameter.The above can maybe not happen in C#, but in Rust it does in the following, if we consider
SecondTrait<I>to be a base type mention ofT:Here we'd like
Tand it's mentionSecondTrait<I>to have at the pathAthe typeI. We make this possible only excluding type parameters of the "subtype" and not all type parameters.The PR also adds some comments and some tests for type aliases, that are unrelated to the above.