-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
hvitved
wants to merge
2
commits into
github:main
Choose a base branch
from
hvitved:rust/type-inference-for-range
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+352
−38
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
rust/ql/lib/change-notes/2025-07-07-type-inference-for-loops.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: minorAnalysis | ||
--- | ||
* Type inference has been improved for `for` loops and range expressions, which improves call resolution and may ultimately lead to more query results. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* This module provides sub classes of the `RangeExpr` class. | ||
*/ | ||
|
||
private import rust | ||
|
||
/** | ||
* A range-from expression. For example: | ||
* ```rust | ||
* let x = 10..; | ||
* ``` | ||
*/ | ||
final class RangeFromExpr extends RangeExpr { | ||
RangeFromExpr() { | ||
this.getOperatorName() = ".." and | ||
not this.hasEnd() | ||
} | ||
} | ||
|
||
/** | ||
* A range-to expression. For example: | ||
* ```rust | ||
* let x = ..10; | ||
* ``` | ||
*/ | ||
final class RangeToExpr extends RangeExpr { | ||
RangeToExpr() { | ||
this.getOperatorName() = ".." and | ||
not this.hasStart() | ||
} | ||
} | ||
|
||
/** | ||
* A range-from-to expression. For example: | ||
* ```rust | ||
* let x = 10..20; | ||
* ``` | ||
*/ | ||
final class RangeFromToExpr extends RangeExpr { | ||
RangeFromToExpr() { | ||
this.getOperatorName() = ".." and | ||
this.hasStart() and | ||
this.hasEnd() | ||
} | ||
} | ||
|
||
/** | ||
* A range-inclusive expression. For example: | ||
* ```rust | ||
* let x = 1..=10; | ||
* ``` | ||
*/ | ||
final class RangeInclusiveExpr extends RangeExpr { | ||
RangeInclusiveExpr() { | ||
this.getOperatorName() = "..=" and | ||
this.hasStart() and | ||
this.hasEnd() | ||
} | ||
} | ||
|
||
/** | ||
* A range-to-inclusive expression. For example: | ||
* ```rust | ||
* let x = ..=10; | ||
* ``` | ||
*/ | ||
final class RangeToInclusiveExpr extends RangeExpr { | ||
RangeToInclusiveExpr() { | ||
this.getOperatorName() = "..=" and | ||
not this.hasStart() and | ||
this.hasEnd() | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -421,21 +421,25 @@ final class ImplTypeAbstraction extends TypeAbstraction, Impl { | |
} | ||
|
||
final class TraitTypeAbstraction extends TypeAbstraction, Trait { | ||
override TypeParamTypeParameter getATypeParameter() { | ||
result.getTypeParam() = this.getGenericParamList().getATypeParam() | ||
override TypeParameter getATypeParameter() { | ||
result.(TypeParamTypeParameter).getTypeParam() = this.getGenericParamList().getATypeParam() | ||
or | ||
result.(AssociatedTypeTypeParameter).getTrait() = this | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this change permit? |
||
} | ||
} | ||
|
||
final class TypeBoundTypeAbstraction extends TypeAbstraction, TypeBound { | ||
override TypeParamTypeParameter getATypeParameter() { none() } | ||
override TypeParameter getATypeParameter() { none() } | ||
} | ||
|
||
final class SelfTypeBoundTypeAbstraction extends TypeAbstraction, Name { | ||
SelfTypeBoundTypeAbstraction() { any(Trait trait).getName() = this } | ||
private TraitTypeAbstraction trait; | ||
|
||
SelfTypeBoundTypeAbstraction() { trait.getName() = this } | ||
|
||
override TypeParamTypeParameter getATypeParameter() { none() } | ||
override TypeParameter getATypeParameter() { none() } | ||
} | ||
|
||
final class ImplTraitTypeReprAbstraction extends TypeAbstraction, ImplTraitTypeRepr { | ||
override TypeParamTypeParameter getATypeParameter() { none() } | ||
override TypeParameter getATypeParameter() { none() } | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -296,6 +296,27 @@ private predicate typeEquality(AstNode n1, TypePath prefix1, AstNode n2, TypePat | |
n1.(ArrayRepeatExpr).getRepeatOperand() = n2 and | ||
prefix1 = TypePath::singleton(TArrayTypeParameter()) and | ||
prefix2.isEmpty() | ||
or | ||
exists(Struct s | | ||
n2 = [n1.(RangeExpr).getStart(), n1.(RangeExpr).getEnd()] and | ||
prefix1 = TypePath::singleton(TTypeParamTypeParameter(s.getGenericParamList().getATypeParam())) and | ||
prefix2.isEmpty() | ||
| | ||
n1 instanceof RangeFromExpr and | ||
s instanceof RangeFromStruct | ||
or | ||
n1 instanceof RangeToExpr and | ||
s instanceof RangeToStruct | ||
or | ||
n1 instanceof RangeFromToExpr and | ||
s instanceof RangeStruct | ||
or | ||
n1 instanceof RangeInclusiveExpr and | ||
s instanceof RangeInclusiveStruct | ||
or | ||
n1 instanceof RangeToInclusiveExpr and | ||
s instanceof RangeToInclusiveStruct | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use |
||
) | ||
} | ||
|
||
pragma[nomagic] | ||
|
@@ -1062,7 +1083,7 @@ private TraitType inferAsyncBlockExprRootType(AsyncBlockExpr abe) { | |
result = getFutureTraitType() | ||
} | ||
|
||
final class AwaitTarget extends Expr { | ||
final private class AwaitTarget extends Expr { | ||
AwaitTarget() { this = any(AwaitExpr ae).getExpr() } | ||
|
||
Type getTypeAt(TypePath path) { result = inferType(this, path) } | ||
|
@@ -1098,6 +1119,29 @@ private class Vec extends Struct { | |
pragma[nomagic] | ||
private Type inferArrayExprType(ArrayExpr ae) { exists(ae) and result = TArrayType() } | ||
|
||
/** | ||
* Gets the root type of the range expression `re`. | ||
*/ | ||
pragma[nomagic] | ||
private Type inferRangeExprType(RangeExpr re) { | ||
exists(Struct s | result = TStruct(s) | | ||
re instanceof RangeFromExpr and | ||
s instanceof RangeFromStruct | ||
or | ||
re instanceof RangeToExpr and | ||
s instanceof RangeToStruct | ||
or | ||
re instanceof RangeFromToExpr and | ||
s instanceof RangeStruct | ||
or | ||
re instanceof RangeInclusiveExpr and | ||
s instanceof RangeInclusiveStruct | ||
or | ||
re instanceof RangeToInclusiveExpr and | ||
s instanceof RangeToInclusiveStruct | ||
) | ||
} | ||
|
||
/** | ||
* According to [the Rust reference][1]: _"array and slice-typed expressions | ||
* can be indexed with a `usize` index ... For other types an index expression | ||
|
@@ -1134,23 +1178,49 @@ private Type inferIndexExprType(IndexExpr ie, TypePath path) { | |
) | ||
} | ||
|
||
final private class ForIterableExpr extends Expr { | ||
ForIterableExpr() { this = any(ForExpr fe).getIterable() } | ||
|
||
Type getTypeAt(TypePath path) { result = inferType(this, path) } | ||
} | ||
|
||
private module ForIterableSatisfiesConstraintInput implements | ||
SatisfiesConstraintInputSig<ForIterableExpr> | ||
{ | ||
predicate relevantConstraint(ForIterableExpr term, Type constraint) { | ||
exists(term) and | ||
exists(Trait t | t = constraint.(TraitType).getTrait() | | ||
// TODO: Remove the line below once we can handle the `impl<I: Iterator> IntoIterator for I` implementation | ||
t instanceof IteratorTrait or | ||
t instanceof IntoIteratorTrait | ||
) | ||
} | ||
} | ||
|
||
pragma[nomagic] | ||
private AssociatedTypeTypeParameter getIteratorItemTypeParameter() { | ||
result.getTypeAlias() = any(IteratorTrait t).getItemType() | ||
} | ||
|
||
pragma[nomagic] | ||
private AssociatedTypeTypeParameter getIntoIteratorItemTypeParameter() { | ||
result.getTypeAlias() = any(IntoIteratorTrait t).getItemType() | ||
} | ||
|
||
pragma[nomagic] | ||
private Type inferForLoopExprType(AstNode n, TypePath path) { | ||
// type of iterable -> type of pattern (loop variable) | ||
exists(ForExpr fe, Type iterableType, TypePath iterablePath | | ||
exists(ForExpr fe, TypePath exprPath, AssociatedTypeTypeParameter tp | | ||
n = fe.getPat() and | ||
iterableType = inferType(fe.getIterable(), iterablePath) and | ||
result = iterableType and | ||
( | ||
iterablePath.isCons(any(Vec v).getElementTypeParameter(), path) | ||
or | ||
iterablePath.isCons(any(ArrayTypeParameter tp), path) | ||
or | ||
iterablePath | ||
.stripPrefix(TypePath::cons(TRefTypeParameter(), | ||
TypePath::singleton(any(SliceTypeParameter tp)))) = path | ||
// TODO: iterables (general case for containers, ranges etc) | ||
) | ||
SatisfiesConstraint<ForIterableExpr, ForIterableSatisfiesConstraintInput>::satisfiesConstraintType(fe.getIterable(), | ||
_, exprPath, result) and | ||
exprPath.isCons(tp, path) | ||
| | ||
tp = getIntoIteratorItemTypeParameter() | ||
or | ||
// TODO: Remove once we can handle the `impl<I: Iterator> IntoIterator for I` implementation | ||
tp = getIteratorItemTypeParameter() and | ||
inferType(fe.getIterable()) != TArrayType() | ||
) | ||
} | ||
|
||
|
@@ -1589,6 +1659,9 @@ private module Cached { | |
result = inferArrayExprType(n) and | ||
path.isEmpty() | ||
or | ||
result = inferRangeExprType(n) and | ||
path.isEmpty() | ||
or | ||
result = inferIndexExprType(n, path) | ||
or | ||
result = inferForLoopExprType(n, path) | ||
|
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
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
Oops, something went wrong.
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.
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.
There is also a
RangeFullExpr
which has neither a start or end. At present it would match bothRangeFromExpr
andRangeToExpr
.