-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Rust: Type inference for tuples #20041
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 1 commit
21c030f
03a9a16
7c04c9f
97e7794
8858f21
a508089
bbd7ed5
7f8829a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Type parameters are required to belong to a single type only. Since we store the arity for tuple types, we need to store the arity in tuple type parameters as well such that we can associate them to the tuple type of the same arity.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,8 @@ private import codeql.rust.elements.internal.generated.Synth | |
cached | ||
newtype TType = | ||
TTuple(int arity) { | ||
exists(any(TupleTypeRepr t).getField(arity)) and Stages::TypeInferenceStage::ref() | ||
arity = any(TupleTypeRepr t).getNumberOfFields() and | ||
Stages::TypeInferenceStage::ref() | ||
} or | ||
TStruct(Struct s) or | ||
TEnum(Enum e) or | ||
|
@@ -19,7 +20,7 @@ newtype TType = | |
TRefType() or // todo: add mut? | ||
TImplTraitType(ImplTraitTypeRepr impl) or | ||
TSliceType() or | ||
TTupleTypeParameter(int i) { exists(TTuple(i)) } or | ||
TTupleTypeParameter(int arity, int i) { exists(TTuple(arity)) and i in [0 .. arity - 1] } or | ||
TTypeParamTypeParameter(TypeParam t) or | ||
TAssociatedTypeTypeParameter(TypeAlias t) { any(TraitItemNode trait).getAnAssocItem() = t } or | ||
TArrayTypeParameter() or | ||
|
@@ -83,7 +84,7 @@ class TupleType extends Type, TTuple { | |
|
||
override TupleField getTupleField(int i) { none() } | ||
|
||
override TypeParameter getTypeParameter(int i) { result = TTupleTypeParameter(i) and i < arity } | ||
override TypeParameter getTypeParameter(int i) { result = TTupleTypeParameter(arity, i) } | ||
|
||
int getArity() { result = arity } | ||
|
||
|
@@ -358,12 +359,20 @@ class AssociatedTypeTypeParameter extends TypeParameter, TAssociatedTypeTypePara | |
* their positional index. | ||
*/ | ||
class TupleTypeParameter extends TypeParameter, TTupleTypeParameter { | ||
override string toString() { result = this.getIndex().toString() } | ||
private int arity; | ||
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. Is it really necessary to have 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. Unintuitively the answer is yes. Before 7c04c9f arity was not in I've noted this in the QLdoc for 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. Thanks, I was wondering this as well. |
||
private int index; | ||
|
||
TupleTypeParameter() { this = TTupleTypeParameter(arity, index) } | ||
|
||
override string toString() { result = index.toString() + "(" + arity + ")" } | ||
|
||
override Location getLocation() { result instanceof EmptyLocation } | ||
|
||
/** Gets the index of this tuple type parameter. */ | ||
int getIndex() { this = TTupleTypeParameter(result) } | ||
int getIndex() { result = index } | ||
|
||
/** Gets the arity of this tuple type parameter. */ | ||
int getArity() { result = arity } | ||
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. TypeParameters don't really have an arity, right? Do we need this method for anything? Perhaps it should be replaced by TypeType getTupleType() { result = TTuple (arity) } |
||
} | ||
|
||
/** An implicit array type parameter. */ | ||
|
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.
This way of defining
arity
would makeTType
's definition recursive, right? Not sure if that is a problem, but we might want to avoid it.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.
Good question. No recursion markers show up in VScode, so I think what's happening is that
TTuple
is materialized first and thenTTupleTypeParameter
is materialized. SoTTupleTypeParameter
depends onTTuple
but there is no recursion. I'm just guessing though so maybe I'm wrong.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.
It's fine with me, @hvitved once commented on a case where I defined a recursive type where it wasn't necessary. Not sure if it is a problem here though. In any case if it's easy to rewrite if it turns out to be a problem.
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.
Agree. Let's keep it and change it if it's actually suboptimal.