Skip to content

Fix generics where bounds order #89098

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Sep 30, 2021

Conversation

GuillaumeGomez
Copy link
Member

@GuillaumeGomez GuillaumeGomez commented Sep 19, 2021

Fixes #88809.

Like said on the above issue, the issue is that we were expecting Symbol comparisons to be string-based but they are integer-based (because Symbol is an integer), messing up the bounds order. To fix it, I simply stored into a FxIndexMap instead.

r? @jyn514

@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 Sep 19, 2021
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 19, 2021
@GuillaumeGomez
Copy link
Member Author

I implemented @Mark-Simulacrum's suggestion.

@camelid
Copy link
Member

camelid commented Sep 22, 2021

@GuillaumeGomez This bug seems to be cross-crate-export related, so you'll need to re-export a type implementing a trait to reproduce this. I came up with a repro that should work as a src/test/rustdoc/ test (with an auxiliary file) if you add the appropriate @has checks:

// In lib1.rs

pub struct Struct<A, B, C>(A, B, C);

// NOTE: would it make sense to define `struct B`, `struct A`, `struct C`, in that order,
// to make sure the symbol indices are in a random order?

pub trait Trait<Other> {}

impl<A, B, C> Trait<Struct<A, B, C>> for Struct<A, B, C>
where
    A: Trait<A>,
    B: Trait<B>,
    C: Trait<C>,
{
}
// In lib2.rs

// aux-build:lib1.rs

pub use lib1::*;

// Add `@has` checks here, either for `Struct`'s page, or for `Trait`'s page

@camelid camelid added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 22, 2021
@camelid camelid assigned camelid and unassigned jyn514 Sep 22, 2021
@camelid
Copy link
Member

camelid commented Sep 22, 2021

Like said on the above issue, the issue is that we were expecting Symbol comparisons to be string-based but they are integer-based (because Symbol is an integer), messing up the bounds order. To fix it, I simply stored the original order and then iterate over this one instead.

About the implementation: one idea I had was to simply switch from a BTreeMap to a Vec instead of having both.

Also, could you update the PR description to reflect that this now uses FxIndexMap?

@GuillaumeGomez
Copy link
Member Author

@camelid: Just realized that we can't check that in test/rustdoc because the implementors content is generated through HTML.

@camelid
Copy link
Member

camelid commented Sep 23, 2021

@camelid: Just realized that we can't check that in test/rustdoc because the implementors content is generated through HTML.

Ah, that's unfortunate. I think it might not have been possible anyway because I don't know if there's a way to check ordering of elements with @has.

@camelid
Copy link
Member

camelid commented Sep 23, 2021

@camelid: Just realized that we can't check that in test/rustdoc because the implementors content is generated through HTML.

Actually, I don't think it is. You can check the implementors section of lib2::Trait or the implementations of lib2::Struct – they both work with JS disabled AFAICT. Both the trait and the struct are re-exported, so if you use the re-exported versions, it should work.

However, there's still the challenge of checking the order of the HTML elements in the where clause. Is there a way to do that?

@GuillaumeGomez
Copy link
Member Author

We could add this check with a better XPath support:

// @has	- '//*[@id="implementors-list"]//span[@class="where fmt-newline"]/text()[1]' 'B:'

Unfortunately, it's not supported:

Traceback (most recent call last):
  File "/usr/lib/python3.9/xml/etree/ElementPath.py", line 354, in iterfind
    selector = _cache[cache_key]
KeyError: ('.//*[@id="implementors-list"]//span[@class="where fmt-newline"]/text()[1]',)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/imperio/rust/rust/src/etc/htmldocck.py", line 490, in <module>
    check(sys.argv[1], get_commands(sys.argv[2]))
  File "/home/imperio/rust/rust/src/etc/htmldocck.py", line 482, in check
    check_command(c, cache)
  File "/home/imperio/rust/rust/src/etc/htmldocck.py", line 439, in check_command
    ret = check_tree_text(cache.get_tree(c.args[0]), pat, c.args[2], regexp)
  File "/home/imperio/rust/rust/src/etc/htmldocck.py", line 370, in check_tree_text
    for e in tree.findall(path):
  File "/usr/lib/python3.9/xml/etree/ElementPath.py", line 395, in findall
    return list(iterfind(elem, path, namespaces))
  File "/usr/lib/python3.9/xml/etree/ElementPath.py", line 368, in iterfind
    selector.append(ops[token[0]](next, token))
KeyError: '()'

@GuillaumeGomez
Copy link
Member Author

The python module we use has been deprecated a while ago. We should switch to another parser like BeautifulSoup which supports XPath entirely, that would make such checks possible.

What do you think @jyn514 ? If we agree on that, let's approve this one, open an issue to add a test whenever it's possible and I'll send a PR to switch the HTML parser we use.

@GuillaumeGomez GuillaumeGomez added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 29, 2021
@camelid
Copy link
Member

camelid commented Sep 29, 2021

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Sep 29, 2021

📌 Commit b226d17 has been approved by camelid

@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 Sep 29, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Sep 29, 2021
…r=camelid

Fix generics where bounds order

Fixes rust-lang#88809.

Like said on the above issue, the issue is that we were expecting `Symbol` comparisons to be string-based but they are integer-based (because `Symbol` is an integer), messing up the bounds order. To fix it, I simply stored into a `FxIndexMap` instead.

r? `@jyn514`
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 30, 2021
Rollup of 13 pull requests

Successful merges:

 - rust-lang#87428 (Fix union keyword highlighting in rustdoc HTML sources)
 - rust-lang#88412 (Remove ignore-tidy-undocumented-unsafe from core::slice::sort)
 - rust-lang#89098 (Fix generics where bounds order)
 - rust-lang#89232 (Improve help for recursion limit errors)
 - rust-lang#89294 (:arrow_up: rust-analyzer)
 - rust-lang#89297 (Remove Never variant from clean::Type enum)
 - rust-lang#89311 (Add unit assignment to MIR for `asm!()`)
 - rust-lang#89313 (PassWrapper: handle function rename from upstream D36850)
 - rust-lang#89315 (Clarify that `CString::from_vec_unchecked` appends 0 byte.)
 - rust-lang#89335 (Optimize is_sorted for Range and RangeInclusive)
 - rust-lang#89366 (rustdoc: Remove lazy_static dependency)
 - rust-lang#89377 (Update cargo)
 - rust-lang#89378 (Update books)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 7c23ff2 into rust-lang:master Sep 30, 2021
@rustbot rustbot added this to the 1.57.0 milestone Sep 30, 2021
@GuillaumeGomez GuillaumeGomez deleted the where-bounds-order branch September 30, 2021 08:03
# for free to join this conversation on GitHub. Already have an account? # 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.

PartialOrd docs for generic tuples show the where clauses in a seemingly random order
7 participants