-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Rust: Rework type inference for method calls #20282
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
642bfbd
fc9ce33
195a708
d11b22b
7662edf
77fa7b4
adfced0
5183918
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 |
---|---|---|
|
@@ -636,6 +636,8 @@ final class ImplItemNode extends ImplOrTraitItemNode instanceof Impl { | |
|
||
TraitItemNode resolveTraitTy() { result = resolvePath(this.getTraitPath()) } | ||
|
||
predicate isBlanket() { this.resolveSelfTy() instanceof TypeParam } | ||
|
||
override AssocItemNode getAnAssocItem() { result = this.getADescendant() } | ||
|
||
override string getName() { result = "(impl)" } | ||
|
@@ -721,7 +723,7 @@ final class ImplItemNode extends ImplOrTraitItemNode instanceof Impl { | |
} | ||
} | ||
|
||
final private class ImplTraitTypeReprItemNode extends TypeItemNode instanceof ImplTraitTypeRepr { | ||
final class ImplTraitTypeReprItemNode extends TypeItemNode instanceof ImplTraitTypeRepr { | ||
pragma[nomagic] | ||
Path getABoundPath() { | ||
result = super.getTypeBoundList().getABound().getTypeRepr().(PathTypeRepr).getPath() | ||
|
@@ -1425,24 +1427,43 @@ signature predicate relevantTraitVisibleSig(Element element, Trait trait); | |
* at a given element. | ||
*/ | ||
module TraitIsVisible<relevantTraitVisibleSig/2 relevantTraitVisible> { | ||
/** Holds if the trait might be looked up in `encl`. */ | ||
private predicate traitLookup(ItemNode encl, Element element, Trait trait) { | ||
// lookup in immediately enclosing item | ||
relevantTraitVisible(element, trait) and | ||
encl.getADescendant() = element | ||
private newtype TNode = | ||
TTrait(Trait t) or | ||
TItemNode(ItemNode i) or | ||
TElement(Element e) { relevantTraitVisible(e, _) } | ||
|
||
private predicate isTrait(TNode n) { n instanceof TTrait } | ||
|
||
private predicate step(TNode n1, TNode n2) { | ||
exists(Trait t1, ItemNode i2 | | ||
n1 = TTrait(t1) and | ||
n2 = TItemNode(i2) and | ||
t1 = i2.getASuccessor(_, _) | ||
) | ||
or | ||
// lookup in an outer scope, but only if the trait is not declared in inner scope | ||
exists(ItemNode mid | | ||
traitLookup(mid, element, trait) and | ||
not trait = mid.getASuccessor(_, _) and | ||
encl = getOuterScope(mid) | ||
exists(ItemNode i1, ItemNode i2 | | ||
n1 = TItemNode(i1) and | ||
n2 = TItemNode(i2) and | ||
i1 = getOuterScope(i2) | ||
) | ||
or | ||
exists(ItemNode i1, Element e2 | | ||
n1 = TItemNode(i1) and | ||
n2 = TElement(e2) and | ||
i1.getADescendant() = e2 | ||
) | ||
} | ||
|
||
private predicate isElement(TNode n) { n instanceof TElement } | ||
|
||
private predicate traitIsVisibleTC(TNode trait, TNode element) = | ||
doublyBoundedFastTC(step/2, isTrait/1, isElement/1)(trait, element) | ||
|
||
/** Holds if the trait `trait` is visible at `element`. */ | ||
pragma[nomagic] | ||
predicate traitIsVisible(Element element, Trait trait) { | ||
exists(ItemNode encl | traitLookup(encl, element, trait) and trait = encl.getASuccessor(_, _)) | ||
traitIsVisibleTC(TTrait(trait), TElement(element)) and | ||
relevantTraitVisible(element, trait) | ||
} | ||
} | ||
|
||
|
@@ -1740,7 +1761,7 @@ private module Debug { | |
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn | | ||
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and | ||
filepath.matches("%/main.rs") and | ||
startline = 52 | ||
startline = 167 | ||
Comment on lines
1763
to
+1764
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. This hardcoded line number (167) in the debug module appears to be a leftover from development/testing. Consider removing this specific line constraint or making it configurable to avoid issues when the test file structure changes. See below for a potential fix:
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback 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] This hardcoded line number in the debug module should be removed or made configurable. Having hardcoded debug coordinates in production code can cause confusion and makes the code less maintainable. See below for a potential fix:
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
) | ||
} | ||
|
||
|
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.
The replacement of the recursive
traitLookup
predicate with transitive closure usingdoublyBoundedFastTC
is a good optimization. However, ensure that the performance characteristics are tested with large codebases, as transitive closure can be expensive for deeply nested scopes.Copilot uses AI. Check for mistakes.