Repository navigation
Various clean-ups around LayoutCalculator - #163384
Conversation
This whole section of the code deals with constructing an alternative layout, but large nesting makes the control flow seem more complex than it actually is.
Constructing these error variants is basically free
Having the generic list span multiple lines is quite jarring, and imo easy to confuse with the parameter list at a glance.
|
r? @nnethercote rustbot has assigned @nnethercote. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
|
I'm afraid I don't have capacity for anything that's touching the code, not just the comments. |
951ffa7 to
593633d
Compare
|
No worries, thank you for taking a look:) |
This comment has been minimized.
This comment has been minimized.
593633d to
dc2f701
Compare
This comment has been minimized.
This comment has been minimized.
dc2f701 to
4bb015b
Compare
Improves consistency, and hopefully clarifies the purpose of previously-mysteriosly named methods like `univariant`.
4bb015b to
f598021
Compare
|
|
||
| /// Compute the layout for a univariant. | ||
| /// | ||
| /// For structs and univariant enums, prefer [`Self::layout_of_struct`]. |
There was a problem hiding this comment.
What is a univariant? Is it different to a univeriant enum? I don't understand how the first and second sentences in this comment relate.
There was a problem hiding this comment.
AFAICT it's basically everything in Variants::Single; this function is supposed to be used directly for non-ADTs (tuples, closures, ..), and is used internally by layout_of_struct, which takes care of structs and single-variant (or univariant, which indeed seems to be the same thing) enums.
That's the thing with this crate: there's a lot of jargon which is mentioned in a lot of places, but explained nowhere (afaict) – and I'm not sure what the place to introduce it would be. Based on my explanations above (which may or may not be correct), "univariant" could probably be defined in the docs for Variants::Single.
There was a problem hiding this comment.
That helps, but... aren't "structs and univariant enums" a subset of "univariants"? In which case the second sentence is surprising to see immediately after the first. E.g. this function can be used for structs but you should prefer layout_of_struct? Then what is this function good for? Just unions and non-closure non-ADTs?
There was a problem hiding this comment.
That helps, but... aren't "structs and univariant enums" a subset of "univariants"?
I think they are, yes.
In which case the second sentence is surprising to see immediately after the first. E.g. this function can be used for structs but you should prefer
layout_of_struct?
Well, kind of... this arguably shouldn't be used for structs, because it doesn't account for NicheOptimizations, for example. But it does seem to be used for struct variants of enums -- see
rust/compiler/rustc_abi/src/layout.rs
Line 616 in b373574
Then what is this function good for? Just unions and non-closure non-ADTs?
Pretty much, yes.
I'm struggling to come up with a way to describe this function which is helpful yet concise (i.e. doesn't duplicate a lot of the docs for Variants::Single)...
There was a problem hiding this comment.
OTOH, grepping for "univariant" in the codebase, this method seems to be the main source of use of this term, so maybe adding verbose and redundant docs to it would be worth it after all.
| } | ||
|
|
||
| /// single-variant enums are just structs, if you think about it | ||
| /// Calculate the layout for a struct, or a single-variant enum. |
There was a problem hiding this comment.
Is a single-variant enum different to a univariant enum?
There was a problem hiding this comment.
One could argue that it would make sense to stick with one of the terms, for consistency. But "univariant" is imo less intuitive (unless you know a bit of Latin), and so I'd prefer to use "single-variant" for surface-level docs at least... WDYT?
There was a problem hiding this comment.
Added a mention of "univariant enum" to Variants::Single
|
|
||
| /// Whether niche optimizations are should be performed during layout calculation. | ||
| /// | ||
| /// UnsafeCell and UnsafePinned both disable niche optimizations |
There was a problem hiding this comment.
Add . at the end of the sentence.
There was a problem hiding this comment.
Done. Added some intra-doc links as well while at it.
The name makes some sense at the callsite, but inside the function, the parameter refers to the `VariantIdx` that should be use for the newly-created layout, so name accordingly. Also move closer to the related `variants` param (Note: could consider passing `variants[present_first]` instead of `variants`, as that's the only way `variants` is used -- at the same type, passing `variants` seems more consistent with the rest of the file.)
This is a more type-safe alternative for the `niche_optimizations: bool` param
f598021 to
6668485
Compare
|
Thanks for the clarifications. I think:
But this is beyond the scope of this PR and can be an optional follow-up. Your added documentation has improved the status quo, thank you. @bors r+ rollup |
…cote Various clean-ups around `LayoutCalculator` Details in individual commits. The commits are roughly in the order of increasing controversiality. The changes are small overall imo, and I don't see a clear splitting point, but if you do want me to split up this PR, let me know. cc @RalfJung (sorry I keep pinging you despite you not being on a review rotation.. others seem not too keen to look at this code)
I mean... "univariant" means "has a single variant", and so this term ends up covering most of TyKinds, since having a single variant is basically the default, outside enums (and coroutines, as you note1). Ralf did mention that Imo the confusion here is caused by the fact the enums can end up with all of the
I agree Footnotes |
|
"Variant" has a standard, well-known meaning that is only relevant for enums. This code introduces a non-standard, subtly different meaning of "variant" that does make a certain sense. I think introducing the second meaning is the original sin here. New words might be better:
If "zero" is part of "multiple", yes... though defining something in terms of what it's not is rarely satisfying, especially when "single" is the most common case. |
Ah, sorry, I misunderstood you earlier. This does make a lot of sense – when expanding the docs on
That's what the "inhabited" in the definition would be for 😉 Uninhabited types end up with |
Rollup of 11 pull requests Successful merges: - #163085 (Suggest similarly named modules in import paths) - #163098 ([debugger visualizers] Add workaround to read `Rc` strong/weak counts) - #163120 (ci: make musl.sh look for patches next to the script) - #163301 (Fix unused_must_use for scenario which may need to keep value) - #163307 (Less `SpanData` in diagnostics) - #152972 (implement PartialEq<VecDeque<U>> for Vec<T>, &[T], &mut [T], [T; N], &[T; N] and &mut [T; N]) - #162536 (Implement Default for NumBuffer) - #163141 (Document safety requirements for intrinsic fallbacks) - #163384 (Various clean-ups around `LayoutCalculator`) - #163405 (Remove some #[linkage] options) - #163413 (mailmap: add Matilde Morrone)
Rollup of 11 pull requests Successful merges: - #163085 (Suggest similarly named modules in import paths) - #163098 ([debugger visualizers] Add workaround to read `Rc` strong/weak counts) - #163120 (ci: make musl.sh look for patches next to the script) - #163301 (Fix unused_must_use for scenario which may need to keep value) - #163307 (Less `SpanData` in diagnostics) - #163389 (`rustc_builtin_macros` cleanup, part 7) - #152972 (implement PartialEq<VecDeque<U>> for Vec<T>, &[T], &mut [T], [T; N], &[T; N] and &mut [T; N]) - #162536 (Implement Default for NumBuffer) - #163141 (Document safety requirements for intrinsic fallbacks) - #163384 (Various clean-ups around `LayoutCalculator`) - #163413 (mailmap: add Matilde Morrone)
Rollup merge of #163384 - ada4a:push-xlkootrrlxkt, r=nnethercote Various clean-ups around `LayoutCalculator` Details in individual commits. The commits are roughly in the order of increasing controversiality. The changes are small overall imo, and I don't see a clear splitting point, but if you do want me to split up this PR, let me know. cc @RalfJung (sorry I keep pinging you despite you not being on a review rotation.. others seem not too keen to look at this code)
Rollup of 11 pull requests Successful merges: - rust-lang/rust#163085 (Suggest similarly named modules in import paths) - rust-lang/rust#163098 ([debugger visualizers] Add workaround to read `Rc` strong/weak counts) - rust-lang/rust#163120 (ci: make musl.sh look for patches next to the script) - rust-lang/rust#163301 (Fix unused_must_use for scenario which may need to keep value) - rust-lang/rust#163307 (Less `SpanData` in diagnostics) - rust-lang/rust#163389 (`rustc_builtin_macros` cleanup, part 7) - rust-lang/rust#152972 (implement PartialEq<VecDeque<U>> for Vec<T>, &[T], &mut [T], [T; N], &[T; N] and &mut [T; N]) - rust-lang/rust#162536 (Implement Default for NumBuffer) - rust-lang/rust#163141 (Document safety requirements for intrinsic fallbacks) - rust-lang/rust#163384 (Various clean-ups around `LayoutCalculator`) - rust-lang/rust#163413 (mailmap: add Matilde Morrone)
View all comments
Details in individual commits. The commits are roughly in the order of increasing controversiality. The changes are small overall imo, and I don't see a clear splitting point, but if you do want me to split up this PR, let me know.
cc @RalfJung (sorry I keep pinging you despite you not being on a review rotation.. others seem not too keen to look at this code)