-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Rust: Type inference for operator overloading #19593
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
46463ee
to
70dbc83
Compare
70dbc83
to
d92d454
Compare
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! A couple of trivial remarks.
*/ | ||
abstract string getOperatorName(); | ||
final int getNumberOfOperands() { result = count(this.getAnOperand()) } |
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.
Use strictcount
.
*/ | ||
abstract Expr getAnOperand(); | ||
predicate isOverloaded(Trait trait, string methodName) { | ||
isOverloaded(this.getOperatorName(), trait.getCanonicalPath(), methodName) |
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.
Great to see that the QL computed canonical paths work here.
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.
Indeed 🕺
result = CallExprImpl::getResolvedFunction(this) | ||
or | ||
result = inferMethodCallTarget(this) // mutual recursion; resolving method calls requires resolving types and vice versa | ||
} | ||
} | ||
|
||
private class OperationAccess extends Access instanceof Operation { |
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.
Add charpred with super.isOverloaded(_, _)
?
override Trait getTrait() { result = trait } | ||
|
||
pragma[nomagic] | ||
override Type getTypeAt(TypePath path) { |
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.
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...
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.
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).
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 💪