Skip to content

Explain how nested generic args are represented#2809

Merged
BoxyUwU merged 2 commits intorust-lang:mainfrom
khyperia:fix-2776
Mar 30, 2026
Merged

Explain how nested generic args are represented#2809
BoxyUwU merged 2 commits intorust-lang:mainfrom
khyperia:fix-2776

Conversation

@khyperia
Copy link
Copy Markdown
Contributor

fixes #2776

fyi @BoxyUwU (but I'll let the auto-review pick someone to not throw more on your plate)

r? rustc-dev-guide

@rustbot rustbot added the S-waiting-on-review Status: this PR is waiting for a reviewer to verify its content label Mar 28, 2026

The [`ty::Generics`] type (returned by the [`generics_of`] query) contains the information of how the nested hierarchy
gets flattened down to a list, and lets you figure out which index in the `GenericArgs` list corresponds to which
generic (handling complications such as `Self` sometimes being a generic parameter).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does “handling“ mean here? Will it normalize the Self type to a concrete Ty?

```

The construct `MyStruct::<u32>::func::<bool>` is represented by a tuple: a DefId pointing at `func`, and then a
`GenericArgs` list that "walks" from the outermost to innermost, left to right, of all containing generic arguments - in
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“outermost” refers to the tcx.parent chain? and left-to-right refers to the order of the list?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I presume these are rhetorical questions, asking from the perspective of a reader, let me know if they're actual questions)

Hmm, my intent here was to be a bit vague and using simple imprecise "everyday" wording giving the general theme of how things work, rather than getting lost in the weeds of what specifically "outermost to innermost, left to right" means - especially as it quickly gets nontrivial, with things like the Self generic parameter, late-bound parameters, and the like. I was thinking the link to the ty::Generics would serve as a "ok, here's the weeds, if you want to know exactly how this works". Do you have a suggestion on how this should be worded? Perhaps just deleting this bit, to not imply specificity where there is none. (Pushed an update with an attempt at rewording)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, just spent a bit more time rewriting, hopefully that's clearer

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not aware of any authoritative reference for how this works, not even in the code. If you know one, please let me know. Otherwise it would be nice if we could make this section of the guide the reference.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately the current wording is my understanding from reading the code - if you'd prefer this PR's explanation is expanded, would you be okay with the current wording as is (something is better than nothing), then later expanding on it to be a more in-depth/authoritative reference later?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, seems good 👍

@khyperia khyperia force-pushed the fix-2776 branch 2 times, most recently from 1f6f165 to 756df82 Compare March 28, 2026 09:08
Comment on lines +156 to +158
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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any info about flattening here. Where did you get all this info from?

Copy link
Copy Markdown
Contributor Author

@khyperia khyperia Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```

The construct `MyStruct::<u32>::func::<bool>` is represented by a tuple: a DefId pointing at `func`, and then a
`GenericArgs` list that "walks" from the outermost to innermost, left to right, of all containing generic arguments - in
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not aware of any authoritative reference for how this works, not even in the code. If you know one, please let me know. Otherwise it would be nice if we could make this section of the guide the reference.

- 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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ideally there would be something better we could link but I'm not sure off the top of my head if such a place exists.

@BoxyUwU
Copy link
Copy Markdown
Member

BoxyUwU commented Mar 30, 2026

I'm going to merge this as I think it's good enough to be in the guide :) I think in general the overlap between the generic parameter definitions section and the ADTs section (this chapter) is unfortunate and should be some kind of unified whole somehow. Ah well :)

thx all for the reviews

@BoxyUwU BoxyUwU merged commit 058cec6 into rust-lang:main Mar 30, 2026
1 check passed
@rustbot rustbot removed the S-waiting-on-review Status: this PR is waiting for a reviewer to verify its content label Mar 30, 2026
@khyperia khyperia deleted the fix-2776 branch March 30, 2026 10:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generic arguments page doesn't mention how nested generics are represented

5 participants