-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Rust: Improve handling of where clauses in type inference and path resolution #20140
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
base: main
Are you sure you want to change the base?
Changes from all commits
ce60fb9
2313011
c6fb8c1
69ca7ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -36,5 +36,36 @@ module Impl { | |||||
not this.hasGenericParamList() and | ||||||
result = 0 | ||||||
} | ||||||
|
||||||
private int nrOfDirectTypeBounds() { | ||||||
result = this.getTypeBoundList().getNumberOfBounds() | ||||||
or | ||||||
not this.hasTypeBoundList() and | ||||||
result = 0 | ||||||
} | ||||||
|
||||||
/** | ||||||
* Gets the `index`th type bound of this trait, if any. | ||||||
* | ||||||
* This includes type bounds directly on the trait and bounds from any | ||||||
* `where` clauses for `Self`. | ||||||
*/ | ||||||
TypeBound getTypeBound(int index) { | ||||||
result = this.getTypeBoundList().getBound(index) | ||||||
or | ||||||
exists(WherePred wp | | ||||||
wp = this.getWhereClause().getAPredicate() and | ||||||
wp.getTypeRepr().(PathTypeRepr).getPath().getText() = "Self" and | ||||||
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. [nitpick] The hardcoded string comparison
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
result = wp.getTypeBoundList().getBound(index - this.nrOfDirectTypeBounds()) | ||||||
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. It appears to be legal to have two where predicates with
I think at the moment this will break your index numbering. 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. You're right, that is legal and the implementation currently does not account for that. 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. Did this one get fixed? If not can you write it up as an issue and lets get this PR merged! |
||||||
) | ||||||
} | ||||||
|
||||||
/** | ||||||
* Gets a type bound of this trait. | ||||||
* | ||||||
* This includes type bounds directly on the trait and bounds from any | ||||||
* `where` clauses for `Self`. | ||||||
*/ | ||||||
TypeBound getATypeBound() { result = this.getTypeBound(_) } | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ private import codeql.rust.elements.internal.generated.TypeParam | |
*/ | ||
module Impl { | ||
private import rust | ||
private import codeql.rust.internal.PathResolution | ||
|
||
// the following QLdoc is generated: if you need to edit it, do it in the schema file | ||
/** | ||
|
@@ -27,6 +28,41 @@ module Impl { | |
/** Gets the position of this type parameter. */ | ||
int getPosition() { this = any(GenericParamList l).getTypeParam(result) } | ||
|
||
private TypeBound getTypeBoundAt(int i, int j) { | ||
exists(TypeBoundList tbl | result = tbl.getBound(j) | | ||
tbl = this.getTypeBoundList() and i = 0 | ||
or | ||
exists(WherePred wp | | ||
wp = this.(TypeParamItemNode).getAWherePred() and | ||
tbl = wp.getTypeBoundList() and | ||
wp = any(WhereClause wc).getPredicate(i) | ||
) | ||
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. I feel like this case should simplify to 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. I'll give it a try. I had that predicate before, but ran into non-monotonic recursion. But it should be doable as long as the 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. I agree that what you propose is a lot cleaner, but the 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. No problem, don't spend any more time on it! |
||
) | ||
} | ||
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. [nitpick] The Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
|
||
/** | ||
* Gets the `index`th type bound of this type parameter, if any. | ||
* | ||
* This includes type bounds directly on this type parameter and bounds from | ||
* any `where` clauses for this type parameter. | ||
*/ | ||
TypeBound getTypeBound(int index) { | ||
|
||
result = rank[index + 1](int i, int j | | this.getTypeBoundAt(i, j) order by i, j) | ||
} | ||
|
||
/** | ||
* Gets a type bound of this type parameter. | ||
* | ||
* This includes type bounds directly on this type parameter and bounds from | ||
* any `where` clauses for this type parameter. | ||
*/ | ||
TypeBound getATypeBound() { | ||
// NOTE: This predicate is used in path resolution, so it can not be | ||
// defined using `getTypeBound` as that would cause non-monotonic | ||
// recursion due to the `rank`. | ||
result = this.getTypeBoundAt(_, _) | ||
} | ||
|
||
override string toAbbreviatedString() { result = this.getName().getText() } | ||
|
||
override string toStringImpl() { result = this.getName().getText() } | ||
|
Uh oh!
There was an error while loading. Please reload this page.