-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Rust: Support non-universal impl
blocks
#19372
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
paldepind
wants to merge
6
commits into
github:main
Choose a base branch
from
paldepind:rust-ti-implementing-type-method
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
22407ca
Rust: Add type inference test for non-universal impl blocks
paldepind e45b5c5
Rust: Implement type inference support for non-universal impl blocks
paldepind 4513106
Rust: Add type inference test for inherent implementation shadowing t…
paldepind 3ee8989
Rust: Handle inherent implementations shadowing trait implementations
paldepind ecead2c
Rust: Workaround for method existing both as source and as dependency
paldepind a545361
Rust: Accept test changes
paldepind 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
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
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 |
---|---|---|
|
@@ -27,10 +27,6 @@ newtype TType = | |
* types, such as traits and implementation blocks. | ||
*/ | ||
abstract class Type extends TType { | ||
/** Gets the method `name` belonging to this type, if any. */ | ||
pragma[nomagic] | ||
abstract Function getMethod(string name); | ||
|
||
/** Gets the struct field `name` belonging to this type, if any. */ | ||
pragma[nomagic] | ||
abstract StructField getStructField(string name); | ||
|
@@ -45,25 +41,6 @@ abstract class Type extends TType { | |
/** Gets a type parameter of this type. */ | ||
final TypeParameter getATypeParameter() { result = this.getTypeParameter(_) } | ||
|
||
/** | ||
* Gets an AST node that mentions a base type of this type, if any. | ||
* | ||
* Although Rust doesn't have traditional OOP-style inheritance, we model trait | ||
* bounds and `impl` blocks as base types. Example: | ||
* | ||
* ```rust | ||
* trait T1 {} | ||
* | ||
* trait T2 {} | ||
* | ||
* trait T3 : T1, T2 {} | ||
* // ^^ `this` | ||
* // ^^ `result` | ||
* // ^^ `result` | ||
* ``` | ||
*/ | ||
abstract TypeMention getABaseTypeMention(); | ||
|
||
/** Gets a textual representation of this type. */ | ||
abstract string toString(); | ||
|
||
|
@@ -73,21 +50,6 @@ abstract class Type extends TType { | |
|
||
abstract private class StructOrEnumType extends Type { | ||
abstract ItemNode asItemNode(); | ||
|
||
final override Function getMethod(string name) { | ||
result = this.asItemNode().getASuccessor(name) and | ||
exists(ImplOrTraitItemNode impl | result = impl.getAnAssocItem() | | ||
impl instanceof Trait | ||
or | ||
impl.(ImplItemNode).isFullyParametric() | ||
) | ||
} | ||
|
||
/** Gets all of the fully parametric `impl` blocks that target this type. */ | ||
final override ImplMention getABaseTypeMention() { | ||
this.asItemNode() = result.resolveSelfTy() and | ||
result.isFullyParametric() | ||
} | ||
} | ||
|
||
/** A struct type. */ | ||
|
@@ -138,8 +100,6 @@ class TraitType extends Type, TTrait { | |
|
||
TraitType() { this = TTrait(trait) } | ||
|
||
override Function getMethod(string name) { result = trait.(ItemNode).getASuccessor(name) } | ||
|
||
override StructField getStructField(string name) { none() } | ||
|
||
override TupleField getTupleField(int i) { none() } | ||
|
@@ -151,14 +111,6 @@ class TraitType extends Type, TTrait { | |
any(AssociatedTypeTypeParameter param | param.getTrait() = trait and param.getIndex() = i) | ||
} | ||
|
||
pragma[nomagic] | ||
private TypeReprMention getABoundMention() { | ||
result = trait.getTypeBoundList().getABound().getTypeRepr() | ||
} | ||
|
||
/** Gets any of the trait bounds of this trait. */ | ||
override TypeMention getABaseTypeMention() { result = this.getABoundMention() } | ||
|
||
override string toString() { result = trait.toString() } | ||
|
||
override Location getLocation() { result = trait.getLocation() } | ||
|
@@ -220,8 +172,6 @@ class ImplType extends Type, TImpl { | |
|
||
ImplType() { this = TImpl(impl) } | ||
|
||
override Function getMethod(string name) { result = impl.(ItemNode).getASuccessor(name) } | ||
|
||
override StructField getStructField(string name) { none() } | ||
|
||
override TupleField getTupleField(int i) { none() } | ||
|
@@ -230,9 +180,6 @@ class ImplType extends Type, TImpl { | |
result = TTypeParamTypeParameter(impl.getGenericParamList().getTypeParam(i)) | ||
} | ||
|
||
/** Get the trait implemented by this `impl` block, if any. */ | ||
override TypeMention getABaseTypeMention() { result = impl.getTrait() } | ||
|
||
override string toString() { result = impl.toString() } | ||
|
||
override Location getLocation() { result = impl.getLocation() } | ||
|
@@ -247,8 +194,6 @@ class ImplType extends Type, TImpl { | |
class ArrayType extends Type, TArrayType { | ||
ArrayType() { this = TArrayType() } | ||
|
||
override Function getMethod(string name) { none() } | ||
|
||
override StructField getStructField(string name) { none() } | ||
|
||
override TupleField getTupleField(int i) { none() } | ||
|
@@ -257,8 +202,6 @@ class ArrayType extends Type, TArrayType { | |
none() // todo | ||
} | ||
|
||
override TypeMention getABaseTypeMention() { none() } | ||
|
||
override string toString() { result = "[]" } | ||
|
||
override Location getLocation() { result instanceof EmptyLocation } | ||
|
@@ -273,8 +216,6 @@ class ArrayType extends Type, TArrayType { | |
class RefType extends Type, TRefType { | ||
RefType() { this = TRefType() } | ||
|
||
override Function getMethod(string name) { none() } | ||
|
||
override StructField getStructField(string name) { none() } | ||
|
||
override TupleField getTupleField(int i) { none() } | ||
|
@@ -284,17 +225,13 @@ class RefType extends Type, TRefType { | |
i = 0 | ||
} | ||
|
||
override TypeMention getABaseTypeMention() { none() } | ||
|
||
override string toString() { result = "&" } | ||
|
||
override Location getLocation() { result instanceof EmptyLocation } | ||
} | ||
|
||
/** A type parameter. */ | ||
abstract class TypeParameter extends Type { | ||
override TypeMention getABaseTypeMention() { none() } | ||
|
||
override StructField getStructField(string name) { none() } | ||
|
||
override TupleField getTupleField(int i) { none() } | ||
|
@@ -318,19 +255,9 @@ class TypeParamTypeParameter extends TypeParameter, TTypeParamTypeParameter { | |
|
||
TypeParam getTypeParam() { result = typeParam } | ||
|
||
override Function getMethod(string name) { | ||
// NOTE: If the type parameter has trait bounds, then this finds methods | ||
// on the bounding traits. | ||
result = typeParam.(ItemNode).getASuccessor(name) | ||
} | ||
|
||
override string toString() { result = typeParam.toString() } | ||
|
||
override Location getLocation() { result = typeParam.getLocation() } | ||
|
||
final override TypeMention getABaseTypeMention() { | ||
result = typeParam.getTypeBoundList().getABound().getTypeRepr() | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -377,19 +304,13 @@ class AssociatedTypeTypeParameter extends TypeParameter, TAssociatedTypeTypePara | |
|
||
int getIndex() { traitAliasIndex(_, result, typeAlias) } | ||
|
||
override Function getMethod(string name) { none() } | ||
|
||
override string toString() { result = typeAlias.getName().getText() } | ||
|
||
override Location getLocation() { result = typeAlias.getLocation() } | ||
|
||
override TypeMention getABaseTypeMention() { none() } | ||
} | ||
|
||
/** An implicit reference type parameter. */ | ||
class RefTypeParameter extends TypeParameter, TRefTypeParameter { | ||
override Function getMethod(string name) { none() } | ||
|
||
override string toString() { result = "&T" } | ||
|
||
override Location getLocation() { result instanceof EmptyLocation } | ||
|
@@ -409,15 +330,34 @@ class SelfTypeParameter extends TypeParameter, TSelfTypeParameter { | |
|
||
Trait getTrait() { result = trait } | ||
|
||
override TypeMention getABaseTypeMention() { result = trait } | ||
override string toString() { result = "Self [" + trait.toString() + "]" } | ||
|
||
override Location getLocation() { result = trait.getLocation() } | ||
} | ||
|
||
/** A type abstraction. */ | ||
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'd appreciate a bit more QLDoc here. What is a "type abstraction" in this context? (assuming it's not a Rust term) |
||
abstract class TypeAbstraction extends AstNode { | ||
abstract TypeParameter getATypeParameter(); | ||
} | ||
|
||
override Function getMethod(string name) { | ||
// The `Self` type parameter is an implementation of the trait, so it has | ||
// all the trait's methods. | ||
result = trait.(ItemNode).getASuccessor(name) | ||
final class ImplTypeAbstraction extends TypeAbstraction, Impl { | ||
override TypeParamTypeParameter getATypeParameter() { | ||
result.getTypeParam() = this.getGenericParamList().getATypeParam() | ||
} | ||
} | ||
|
||
override string toString() { result = "Self [" + trait.toString() + "]" } | ||
final class TraitTypeAbstraction extends TypeAbstraction, Trait { | ||
override TypeParamTypeParameter getATypeParameter() { | ||
result.getTypeParam() = this.getGenericParamList().getATypeParam() | ||
} | ||
} | ||
|
||
override Location getLocation() { result = trait.getLocation() } | ||
final class TypeBoundTypeAbstraction extends TypeAbstraction, TypeBound { | ||
override TypeParamTypeParameter getATypeParameter() { none() } | ||
} | ||
|
||
final class SelfTypeBoundTypeAbstraction extends TypeAbstraction, Name { | ||
SelfTypeBoundTypeAbstraction() { any(Trait trait).getName() = this } | ||
|
||
override TypeParamTypeParameter getATypeParameter() { none() } | ||
} |
Oops, something went wrong.
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.
Why do we prioritize results with
fromSource
above everything else?