-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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: Fix inconsistencies in const
value rendering
#83035
Comments
PR is merged! |
Do you want to work on it? Otherwise I'll do it. :) |
I'm hoping to work on it, but if I don't get to it I'll try to let you know so you can do it :) |
Well, please tell me if you change your mind. A lot of other things to do. ;) |
I'm working on this now, and I noticed that statics use only a |
I think the code would be greatly simplified if we always evaluated constants. I.e., currently something like const FOO: usize = 1 + 2; // 3usize I don't think showing the const FOO: usize = 3; I think this is the difference between |
One weird thing is that we print the values of some constants, but not others. E.g., we seem to print |
Wow, we literally store rendered constant initializers in crate metadata just for rustdoc. So, not showing the pretty-printed version, or only showing it for local items, would probably reduce crate metadata size somewhat significantly. |
I think as a team we should have a discussion about if we want to change constant rendering. I think the current logic is confusing both to end users and to people who work on the code. For example, the initializer of As another example, Furthermore, a re-export of A re-export of |
I agree, printing the value directly would be more interesting. If they want to see the original code, they can see the source. Overall, there is a lot of cleanup to do apparently. I'm surprised we didn't detect all those weird behaviours before but I'm glad you did. So except for keeping the original value in the const and show the computed value as a code comment, I think you can just ahead for the rest. |
👍
Hmm, what do you mean? Are you saying I should show only the computed value, or should not? |
The computed value, you said:
So I guess it's up to debate and not decided yet. ;) |
Yeah, perhaps let's cc @rust-lang/rustdoc for discussion. |
I think we should normalize the constant wherever possible. Ideally I'd like the show the original source as a comment, but I would rather omit that than have it be inconsistent if it's a cross crate reexport. |
Normalization sounds good to me too, except for byte strings, these do have some major utility from being shown as their written form. const FOO: &[u8] = [102, 111, 111];
const BAR: &[u8] = b"bar"; |
Showing evaluated strings and slices seems to be trickier since you have to deal with rustc
Yeah, I agree. One issue is that we can't get the information of whether a |
Could we instead show both by using |
I feel somewhat opposed to showing both computed and source/pretty-printed constants. In many cases, it's redundant, and it also adds complexity to both the implementation and the behavior of rustdoc. |
I would rather it be complicated than inconsistent, using HIR means it will be different if you re-export it. If you want rustdoc could hide the comment if it's the same as the pretty-printed expression? |
Hmm, HIR will be have different behavior but the source map will have the same behavior? How does the source map interact with
The issue with that is that floating-point numbers will have different precision depending on whether they came from source or from const-eval: https://doc.rust-lang.org/nightly/std/f32/consts/constant.PI.html. Also, it seems weird that changing the way the constant is computed will change the display in the docs. |
Interestingly, we seem to never show the value of statics, re-export or not. So, another option is to just never show the value of a constant—though I'm guessing we wouldn't want to do this. |
It doesn't. html_no_source is about the HTML file rustdoc generates, it has no relation to metadata (right now rustdoc doesn't even store it across crates: #55957).
I feel like that's useful information though? Hiding that the behavior is inconsistent doesn't make it any less inconsistent: that's something that affects the end-behavior, as opposed to just displaying the docs inconsistentluy. |
Following up on this: I'd like to make rustdoc consistent about always using There are basically three ways we can show constants, and each have pros and cons for both implementation and UX:
My understanding is that currently, rustdoc uses a combination of Does switching to always use |
That seems ok to me but we should try to display byte strings as strings instead of arrays if possible - maybe we could cheat by looking at the original source to see if it used |
That won't work if the byte string is computed (e.g., the constant's body is a call to a In the future, we could maybe record in the metadata whether a constant was written as a byte string. |
Another inconsistency: free-standing consts have their value shown, but (inherent, not sure about trait) associated consts don't: |
What if we just displayed them as hex arrays? It would be a lot simpler than trying to recover whether the byte slice was originally a byte string. Although then user-written arrays will be displayed in hex... maybe we can just display byte strings and arrays as decimal arrays? I imagine in many cases seeing the raw byte values for a byte string might actually be good enough. |
One issue is that for, e.g., const generics, what happens if we don't know how to pretty-print the |
ConstantKind::Extern
and ConstantKind::Local
const
value rendering
This should improve performance, clean up the code, and help pave the way for rust-lang#83035.
…Gomez rustdoc: Pretty-print assoc const defaults on-demand This should improve performance, clean up the code, and help pave the way for rust-lang#83035.
Showing
Currently there are also inconsistencies between how module level constants are rendered and how associated constants are rendered. For example, pub const CONSTANT: u16 = 0x103040;
pub struct Testing;
impl Testing {
pub const CONSTANT: u16 = 0x103040;
}
pub trait Test {
const CONSTANT: u16 = 0x103040;
}
impl Test for Testing {
const CONSTANT: u16 = 0x111111;
} gives the following renders: |
The topic of this issue has changed since it was first opened. See #83035 (comment) for a more up-to-date summary.
The way they're displayed is both different (one use a
BodyId
whereas the other is forced to use itsDefId
. But ultimately, it'd be nice to be able to merge them.Originally posted by @GuillaumeGomez in #82873 (comment)
This will be done after #82873 is merged.
The text was updated successfully, but these errors were encountered: