Skip to content

Commit 756df82

Browse files
committed
Explain how nested generic args are represented
1 parent 9d494a8 commit 756df82

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

src/ty-module/generic-arguments.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,30 @@ For the `MyStruct<U>` written in the `Foo` type alias, we would represent it in
126126
- There would be an `AdtDef` (and corresponding `DefId`) for `MyStruct`.
127127
- There would be a `GenericArgs` containing the list `[GenericArgKind::Type(Ty(u32))]`
128128
- And finally a `TyKind::Adt` with the `AdtDef` and `GenericArgs` listed above.
129+
130+
## Nested generic args
131+
132+
Consider:
133+
134+
```rust,ignore
135+
struct MyStruct<T>(T);
136+
137+
impl<T> MyStruct<T> {
138+
fn func<T2>(t: T, t2: T2) {}
139+
}
140+
141+
fn main() {
142+
MyStruct::<u32>::func::<bool>(2, true);
143+
}
144+
```
145+
146+
The construct `MyStruct::<u32>::func::<bool>` is represented by a tuple: a DefId pointing at `func`, and then a
147+
`GenericArgs` list that "walks" all containing generic parameters - in this case, the list would be `[u32, bool]`.
148+
149+
The [`ty::Generics`] type (returned by the [`generics_of`] query) contains the information of how the nested hierarchy
150+
gets flattened down to a list, and lets you figure out which index in the `GenericArgs` list corresponds to which
151+
generic (handling complications such as `Self` sometimes being a generic parameter, and so whether it should be included
152+
in the list or not). Check how it works for exact specifics on how parameters get mapped to indices.
153+
154+
[`ty::Generics`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Generics.html
155+
[`generics_of`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.generics_of

0 commit comments

Comments
 (0)