Skip to content
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: Censor certain complex unevaluated const exprs #98814

Merged
merged 1 commit into from
Jul 5, 2022

Conversation

fmease
Copy link
Member

@fmease fmease commented Jul 2, 2022

Fixes #97933.

This is more of a hotfix for the aforementioned issue. By that, I mean that my proposed patch is
not the best solution but one that does not change as much existing code.
It treats symptoms rather than the root cause.

This PR “censors” certain complex unevaluated constant expressions like matches, blocks, function calls, struct literals etc. by pretty-printing them as _ / { _ } (number and string literals, paths and () are still printed as one would expect).
Resorting to this placeholder is preferable to printing the full expression verbatim since
they can be quite large and verbose resulting in an unreadable mess in the generated documentation.
Further, mindlessly printing the const would leak private and doc(hidden) struct fields (#97933), at least in the current
stable & nightly implementations which rely on span_to_snippet (!) and rustc_hir_pretty::id_to_string.

The censoring of verbose expressions is probably going to stay longer term.
However, in regards to private and doc(hidden) struct fields, I have a more proper fix in mind
which I have already partially implemented locally and for which I am going to open a separate PR sometime soon.
For that, I was already in contact with @GuillaumeGomez.
The proper fix involves rustdoc not falling back on pretty-printing unevaluated consts so easily (what this PR is concerned about)
and instead preferring to print evaluated consts which contain more information allowing it to selectively hide private and doc(hidden) fields, create hyperlinks etc. generally making the output more granular and precise (compared to the brutal _ placeholder).

Unfortunately, I was a bit too late and the issue just hit stable (1.62).
Should this be backported to beta or even a potential 1.62.1?

r? @GuillaumeGomez

@rustbot rustbot added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Jul 2, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 2, 2022
@GuillaumeGomez
Copy link
Member

I think it should be backported for beta so it'll be in 1.63 stable.

// FIXME: Omit the curly braces if the enclosing expression is an array literal
// with a repeated element (an `ExprKind::Repeat`) as in such case it
// would not actually need any disambiguation.
"{_}".to_owned()
Copy link
Member

Choose a reason for hiding this comment

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

Maybe { _ }? (with whitespaces)

// FIXME: `.value()` uses `clean::utils::format_integer_with_underscore_sep` under the
// hood which adds noisy underscores and a type suffix to number literals.
// This hurts readability in this context especially when more complex expressions
// are involved and it doesn't add much of value.
// Find a way to print constants here without all that jazz.
write!(w, " = {}", default.value(cx.tcx()).unwrap_or_else(|| default.expr(cx.tcx())));
match default.value(cx.tcx()) {
Some(value) => write!(w, "{}", value),
Copy link
Member

Choose a reason for hiding this comment

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

Why only the None is being Escaped?

@GuillaumeGomez
Copy link
Member

Looks like a nice improvement, thanks for working on this! What does a type with only public fields looks like btw? Could you add a test for this case please?

Also cc @notriddle

@fmease fmease force-pushed the minimal-fix-for-issue-97933 branch from dcc11ae to 2ad6fb3 Compare July 4, 2022 15:17
@fmease
Copy link
Member Author

fmease commented Jul 4, 2022

I've addressed all of your comments (except for the “typo“ one).

@fmease fmease force-pushed the minimal-fix-for-issue-97933 branch from 2ad6fb3 to 71b0ed9 Compare July 4, 2022 15:32
@fmease fmease force-pushed the minimal-fix-for-issue-97933 branch from 71b0ed9 to d3181a9 Compare July 4, 2022 16:41
@GuillaumeGomez
Copy link
Member

I think it's good enough for a first step. Please open issues (if not done already) for the next steps.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jul 4, 2022

📌 Commit d3181a9 has been approved by GuillaumeGomez

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 4, 2022
@fmease
Copy link
Member Author

fmease commented Jul 4, 2022

Great! I am going to open issues for things that need to be done.
@ GuillaumeGomez, since you agreed to my idea to backport this patch to beta, would you mind adding the beta-nominated label to this PR? I don't think I have the permission for that, do you?

@GuillaumeGomez
Copy link
Member

For the beta nomination, I need other rustdoc people to accept too.

Are you ok with backporting this fix bet @rust-lang/rustdoc ?

bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 4, 2022
…askrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#98738 (Clarify MIR semantics of checked binary operations)
 - rust-lang#98782 (Improve spans for specialization error)
 - rust-lang#98793 (Lint against executable files in the root directory)
 - rust-lang#98814 (rustdoc: Censor certain complex unevaluated const exprs)
 - rust-lang#98878 (add more `rustc_pass_by_value`)
 - rust-lang#98879 (Fix "wrap closure in parenthesis" suggestion for `async` closure)
 - rust-lang#98886 (incr.comp.: Make split-dwarf commandline options [TRACKED].)
 - rust-lang#98898 (Add "no-div-regex" eslint rule)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 9ad3ef1 into rust-lang:master Jul 5, 2022
@rustbot rustbot added this to the 1.64.0 milestone Jul 5, 2022
@fmease fmease deleted the minimal-fix-for-issue-97933 branch July 5, 2022 07:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rustdoc displays private internals of associated constants (regression)
6 participants