Rust: Type inference for operator overloading#19593
Merged
paldepind merged 4 commits intogithub:mainfrom May 29, 2025
Merged
Conversation
46463ee to
70dbc83
Compare
70dbc83 to
d92d454
Compare
aibaars
reviewed
May 27, 2025
hvitved
requested changes
May 28, 2025
Contributor
hvitved
left a comment
There was a problem hiding this comment.
Very nice! A couple of trivial remarks.
| * This is either 1 for prefix operations, or 2 for binary operations. | ||
| */ | ||
| abstract string getOperatorName(); | ||
| final int getNumberOfOperands() { result = count(this.getAnOperand()) } |
| */ | ||
| abstract Expr getAnOperand(); | ||
| predicate isOverloaded(Trait trait, string methodName) { | ||
| isOverloaded(this.getOperatorName(), trait.getCanonicalPath(), methodName) |
Contributor
There was a problem hiding this comment.
Great to see that the QL computed canonical paths work here.
| } | ||
| } | ||
|
|
||
| private class OperationAccess extends Access instanceof Operation { |
Contributor
There was a problem hiding this comment.
Add charpred with super.isOverloaded(_, _)?
| override Trait getTrait() { result = trait } | ||
|
|
||
| pragma[nomagic] | ||
| override Type getTypeAt(TypePath path) { |
Contributor
There was a problem hiding this comment.
I know it was already so before this PR, but perhaps call this predicate getReceiverTypeAt instead? I have mistakenly thought it meant the type of the method call itself a couple of times...
Contributor
Author
There was a problem hiding this comment.
I definitely get that, but I can't just change it as we need it to match the signature when it is given to IsInstantiationOf (which is generic and isn't just used for receivers).
hvitved
approved these changes
May 28, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements type inference for overloaded operators in Rust.
Note, that this does not implement the data flow side of things.
Shoutout to Copilot
for helping out with generating the tests 💪