Skip to content

Rust: Improve type inference for for loops and range expressions #19971

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

hvitved
Copy link
Contributor

@hvitved hvitved commented Jul 3, 2025

This PR generalizes the existing logic for inferring types of for loops to be based on the IntoIterator trait, cf. how for loops are desugared. Additionally, this PR also adds type inference for range expressions like 1..10.

DCA looks good; more resolved calls, no significant slowdown.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Jul 3, 2025
@hvitved hvitved force-pushed the rust/type-inference-for-range branch from 2e9addd to 2c8f0b8 Compare July 3, 2025 16:01
@hvitved hvitved force-pushed the rust/type-inference-for-range branch from 2c8f0b8 to a36c9ec Compare July 4, 2025 12:47
@hvitved hvitved force-pushed the rust/type-inference-for-range branch from a36c9ec to 7647bc3 Compare July 4, 2025 12:48
@hvitved hvitved marked this pull request as ready for review July 7, 2025 07:35
@Copilot Copilot AI review requested due to automatic review settings July 7, 2025 07:35
@hvitved hvitved requested a review from a team as a code owner July 7, 2025 07:35
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few questions, but otherwise looks great, as does the DCA run.

this.getOperatorName() = ".." and
not this.hasStart()
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also a RangeFullExpr which has neither a start or end. At present it would match both RangeFromExpr and RangeToExpr.

override TypeParameter getATypeParameter() {
result.(TypeParamTypeParameter).getTypeParam() = this.getGenericParamList().getATypeParam()
or
result.(AssociatedTypeTypeParameter).getTrait() = this
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this change permit?

s instanceof RangeInclusiveStruct
or
n1 instanceof RangeToInclusiveExpr and
s instanceof RangeToInclusiveStruct
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use inferRangeExprType here?

let path = e.path(); // $ Alert[rust/summary/taint-sources]
let file_name = e.file_name(); // $ Alert[rust/summary/taint-sources]
sink(path); // $ hasTaintFlow
sink(file_name); // $ hasTaintFlow
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really happy to see these! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants