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

Fix whitespace handling after where clause #98256

Merged
merged 2 commits into from
Jul 6, 2022

Conversation

GuillaumeGomez
Copy link
Member

Fixes #97733.

You can see the result here.

r? @jsha

@GuillaumeGomez GuillaumeGomez added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. A-rustdoc-ui Area: Rustdoc UI (generated HTML) labels Jun 19, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 19, 2022
src/etc/htmldocck.py Outdated Show resolved Hide resolved
@bors
Copy link
Contributor

bors commented Jun 19, 2022

☔ The latest upstream changes (presumably #98255) made this pull request unmergeable. Please resolve the merge conflicts.


// @has 'foo/trait.ToOwned2.html'
// @snapshot trait2 - '//*[@class="docblock item-decl"]'
// There should be a whitespace in this case!
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment is ambiguous. There should be whitespace in a variety of places! Where specifically is this test trying to confirm the presence of a whitespace?

let before_where = w.len();
write!(w, "{}", print_where_clause(&e.generics, cx, 0, true),);
// If there wasn't a `where` clause, we add a whitespace.
if before_where == w.len() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Determining whether there's a where clause based on the length of the emitted data is confusing and self-referential. Instead, the filter should print_where_clause should be extracted out to a method of clean::Generics:

        let mut where_predicates = gens.where_predicates.iter().filter(|pred| {
            !matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty())

For instance it could be fn where_predicates() -> impl Iterator<Item = WherePredicate> on Generics that returns all where predicates with non-empty bounds. Then this function would have two possible rendering paths: (a) when the where clause is present (where_predicates() returns a nonempty iterator), and (b) when the where clause is not present.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree that the code doesn't look great but i don't agree with the solution you propose. I have an idea so I'll update the PR and you can tell me if you find it better.

Copy link
Contributor

Choose a reason for hiding this comment

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

I still think it's not good style. You are still checking the length of the buffer to see whether print_where_clause did anything: https://github.com/rust-lang/rust/pull/98256/files#diff-c4e61d56ff695cfc762517d170672a9c8650ac815abee6a9e28267a2ff27c8f3R72-R74.

That strongly suggests to me that the logic for "will print_where_clause do anything" should be extracted into its own function. I'm not particularly attached to the iterator idea I proposed above; it could also be a has_where_clause() function.

That will make the code that chooses whether to add a space, newline, or semicolon clearer, since it can check if there is or is not a where clause, and call print_where_clause only in that case; and also emit a space, newline, or semicolon only in that case.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't like the idea of duplicating this logic though (duplicating in the sense of "checking it twice", not "copy/paste it in two places").

);
if !print_where_clause_and_check(w, &e.generics, cx, 0, true) {
// If there wasn't a `where` clause, we add a whitespace.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be better to make this whitespace emitted by print_where_clause. Then the role of print_where_clause is simple to explain and self contained. It either (a) prints a newline, a where clause, and another newline, or (b) prints a whitespace.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let me give it a try then!

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately it cannot work for cases like:

trait A {
    fn foo<T>(a: T)
    where T: Clone;
}

type F<T> where T: Clone = Vec<T>;
trait G<T> where T: Clone = A<T>;

If we handle the whitespace directly into print_where_clause, it'll need to be handled manually in these cases to "counter" the effect.

@GuillaumeGomez
Copy link
Member Author

r? @notriddle

src/test/rustdoc/whitespace-after-where-clause.enum.html Outdated Show resolved Hide resolved
src/librustdoc/html/render/print_item.rs Outdated Show resolved Hide resolved
@GuillaumeGomez
Copy link
Member Author

Cleaned up the code and fixed the tests.

@GuillaumeGomez
Copy link
Member Author

Updated all channel URLs.

@notriddle
Copy link
Contributor

I like that the unused indent and end_newline parameters were removed from print_where_clause_and_check.

But I was actually thinking that the end_newline parameter could be removed entirely. Its only purpose is to insert a newline at the end of the where clause if the where clause was non-empty, just like the way the trailing space is handled. Why can't both be done the same way?

@GuillaumeGomez
Copy link
Member Author

You meant for print_where_clause? I can do it too but in another PR to not mix fix and cleanup. I can open an issue for it if you want?

@notriddle
Copy link
Contributor

Yeah, makes sense.

@bors r+

@bors
Copy link
Contributor

bors commented Jul 5, 2022

📌 Commit c8a5b67 has been approved by notriddle

@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 5, 2022
@GuillaumeGomez
Copy link
Member Author

Will send a PR tomorrow then.

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

Rollup of 6 pull requests

Successful merges:

 - rust-lang#95503 (bootstrap: Allow building individual crates)
 - rust-lang#96814 (Fix repr(align) enum handling)
 - rust-lang#98256 (Fix whitespace handling after where clause)
 - rust-lang#98880 (Proper macOS libLLVM symlink when cross compiling)
 - rust-lang#98944 (Edit `rustc_mir_dataflow::framework::lattice::FlatSet` docs)
 - rust-lang#98951 (Update books)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 4986379 into rust-lang:master Jul 6, 2022
@rustbot rustbot added this to the 1.64.0 milestone Jul 6, 2022
@GuillaumeGomez GuillaumeGomez deleted the whitespace-where-clause branch July 6, 2022 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rustdoc-ui Area: Rustdoc UI (generated HTML) 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: curly brace in wrong place for Cow
6 participants