Skip to content

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

Merged
merged 4 commits into from
May 29, 2025

Conversation

paldepind
Copy link
Contributor

@paldepind paldepind commented May 27, 2025

Implements type inference for overloaded operators in Rust.

Note, that this does not implement the data flow side of things.

Shoutout to Copilot :copilot: for helping out with generating the tests 💪

@github-actions github-actions bot added the Rust Pull requests that update Rust code label May 27, 2025
@paldepind paldepind force-pushed the rust/operator-overloading branch from 46463ee to 70dbc83 Compare May 27, 2025 11:43
@paldepind paldepind force-pushed the rust/operator-overloading branch from 70dbc83 to d92d454 Compare May 27, 2025 12:03
@paldepind paldepind marked this pull request as ready for review May 27, 2025 12:29
@paldepind paldepind requested a review from a team as a code owner May 27, 2025 12:29
@paldepind paldepind requested a review from hvitved May 28, 2025 11:30
Copy link
Contributor

@hvitved hvitved left a 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()) }
Copy link
Contributor

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)
Copy link
Contributor

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.

Copy link
Contributor Author

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 {
Copy link
Contributor

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) {
Copy link
Contributor

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...

Copy link
Contributor Author

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).

@paldepind paldepind merged commit 437246f into github:main May 29, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants