From bdc191cd22ce3aa80a4dfe2cfc672f8d3212b518 Mon Sep 17 00:00:00 2001 From: khyperia <953151+khyperia@users.noreply.github.com> Date: Sat, 28 Mar 2026 10:36:14 +0100 Subject: [PATCH 1/2] Explain how nested generic args are represented --- src/ty-module/generic-arguments.md | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/ty-module/generic-arguments.md b/src/ty-module/generic-arguments.md index 3339d31142..3d6124c2ec 100644 --- a/src/ty-module/generic-arguments.md +++ b/src/ty-module/generic-arguments.md @@ -126,3 +126,35 @@ For the `MyStruct` written in the `Foo` type alias, we would represent it in - There would be an `AdtDef` (and corresponding `DefId`) for `MyStruct`. - There would be a `GenericArgs` containing the list `[GenericArgKind::Type(Ty(u32))]` - And finally a `TyKind::Adt` with the `AdtDef` and `GenericArgs` listed above. + +## Nested generic args + +```rust +struct MyStruct(T); + +impl MyStruct { + fn func() {} +} + +fn main() { + MyStruct::::func::(); +} +``` + +The construct `MyStruct::::func::` is represented by a tuple: a DefId pointing at `func`, and then a +`GenericArgs` list that "walks" all containing generic parameters - in this case, the list would be `[u32, bool, char]`. + +The [`ty::Generics`] type (returned by the [`generics_of`] query) contains the information of how a nested hierarchy +gets flattened down to a list, and lets you figure out which index in the `GenericArgs` list corresponds to which +generic. The general theme of how it works is outermost to innermost (`T` before `T2` in the example), left to right +(`T2` before `T3`), but there are several complications: + +- `Self` is sometimes a generic parameter, and should sometimes be included in the list. +- Only early-bound generic parameters are included, [late-bound generics] are not. +- ... and more... + +Check out [`ty::Generics`] for exact specifics on how the flattening works. + +[`ty::Generics`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Generics.html +[`generics_of`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.generics_of +[late-bound generics]: ../early-late-parameters.md From 383636a98b771a41fbac96b1c0344966a4fe9c0d Mon Sep 17 00:00:00 2001 From: Boxy Date: Mon, 30 Mar 2026 11:20:31 +0100 Subject: [PATCH 2/2] reword `Self` parameter bullet point --- src/ty-module/generic-arguments.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ty-module/generic-arguments.md b/src/ty-module/generic-arguments.md index 3d6124c2ec..cbf7b2c682 100644 --- a/src/ty-module/generic-arguments.md +++ b/src/ty-module/generic-arguments.md @@ -149,7 +149,7 @@ gets flattened down to a list, and lets you figure out which index in the `Gener generic. The general theme of how it works is outermost to innermost (`T` before `T2` in the example), left to right (`T2` before `T3`), but there are several complications: -- `Self` is sometimes a generic parameter, and should sometimes be included in the list. +- Traits have an implicit `Self` generic parameter which is the first (i.e. 0th) generic parameter. Note that `Self` doesn't mean a generic parameter in all situations, see [Res::SelfTyAlias](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def/enum.Res.html#variant.SelfTyAlias) and [Res::SelfCtor](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def/enum.Res.html#variant.SelfCtor). - Only early-bound generic parameters are included, [late-bound generics] are not. - ... and more...