-
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
base: main
Are you sure you want to change the base?
Changes from all commits
22407ca
e45b5c5
4513106
3ee8989
ecead2c
a545361
06cfa9a
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 |
---|---|---|
|
@@ -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) 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. A tried to explain this over in the shared module. Would it make sense to repeat the Rust specific part of that here? |
||
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() } | ||
} |
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?