-
Notifications
You must be signed in to change notification settings - Fork 13k
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
rustdoc: rich-text and visibility-aware formatting for constant values #98929
Comments
This seems like a good idea to me! Can you link to some real world crates with examples of complex const values (especially structs) so we can see if the proposed display works well for them? I'm thinking maybe structs should be split out onto multiple lines. AFAIK there is nowhere else in rustdoc that we try to cram them onto a single line and it seems like it could get crowded fast. |
Oh, also, I think this is the main question:
Or to put it another way: should we display what the doc author wrote, or what the compiler sees? For instance, say the doc author writes: pub struct MyThing {
pub x: i32,
pub y: i32,
pub z: i32,
pub name: String,
... many fields ...
}
impl const Default for MyThing { ... }
pub const EMPTY: MyThing = MyThing::default() Should the docs show:
|
I'd say the second. |
Sadly I don't know of any such crate. Maybe
This is a very fair question. Obviously I lean towards printing evaluated constants but deep down I wish constants could be handled in a more nuanced way: I imagine a system where Re. Now, I think I will go ahead and bring my stalled WIP PR up to speed and just open it for you to discuss. Enough time has passed I think (sorry). Esp. since apparently the hotfix wasn't enough to cover it all: #99630. |
Yes. #97974 is the WIP PR.
I think it's a good approach. That will at least allow us to have a UI we can actually go through that can be used to be debated upon. |
I can't claim to have the definitive answer here but I lean towards displaying what the doc author wrote. This makes it possible to express either semantic. If the doc author wants to hide the details of a complex object, they write a core::f64::consts provides some useful examples to think about. Would |
While types in rustdoc are richly formatted using HTML (most notably for hyperlinks to definitions),
constant expressions and values do not enjoy any such treatment and are printed as plain text (or
sometimes even entirely omitted from the documentation).
With the continuously growing language support for “
const
” (const fn
, const generics,adt_const_params
,generic_const_exprs
,const_trait_impl
) which allows ever-more-complex constant expressions inmore and more places, it seems very fitting to improve rustdoc's output to help users navigate this complexity.
I propose the following:
tcx.const_eval_poly
as much as possible for pretty-printing and only fall back onhir::Expr
(unevaluated expressions) as a last resort. Currently for free and assoc. consts, rustdoc evaluates arbitrary const expressions of any type (arbitrary ADTs) but throws away the result if the type is not an integer (or similar) and instead prints the unevaluated expression.hir::Expr
.ConstValue
/ValTree
s eventually (?)):S { f: 0 }
,Ok(())
doc(hidden)
fields.In non-tuple structs, we skip those fields and add the symbol
..
(mirroring the syntax of struct patterns).In tuple structs, we replace them with
_
(just how it's already done today in the code blocks at the top of tuple struct pages)[/* N elements */]
for large arrays to not clutter the documentation/* large string */
for large stringsUnfortunately rustdoc's current reliance on
const_eval_poly
means that in a quite a lot of cases – namely those where the const expr contains type or const parameters (including the implicit type parameterSelf
) –, we aren't gonna get the benefits of this proposed richer pretty-printing since the evaluation will fail on those with the error “too generic“ and we'll have to fall back on printing the unevaluatedhir::Expr
. This is a known limitation of rustdoc and tracked in #82852.Parts of this proposal aim to solve #97933 properly whereas #98814 is but a hotfix.
I have already partially implemented this proposal in a local patch that is yet to be published as a PR. You can get a visual taste of it below:
@rustbot label T-rustdoc A-rustdoc-ui C-enhancement
@rustbot claim
The text was updated successfully, but these errors were encountered: