Skip to content

HACKY/INCOMPLETE: Add implied bounds to rustdoc-json #142264

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

Closed

Conversation

aDotInTheVoid
Copy link
Member

@aDotInTheVoid aDotInTheVoid commented Jun 9, 2025

A hackattempt at #142226. CC @obi1kenobi @workingjubilee @fmease

This is a meant to show it's possible. The version that lands will probably be a new PR that learns from this.

How

Hir doesn't have implied bounds, so we need to get generics from ty. Rustdoc already has a codepath for that, but it's not the default because ty is far more normalized, so the HTML output is farther from what the user wrote.

Limitations

  • Only works on structs. Fixable by replacing all calls to clean_generics with calls to clean_ty_generics, so we only get generics from ty. Also would require replacing all calls to tcx.explicit_predicates_of to just predicates_of.

    I don't think this isn't fundamental, but should be doable with more work.

  • Doesn't differentiate implied vs explicit bounds. This is necessary for json (rustdoc-json: Show implied outlives-bounds #142226 (comment)), and pretty desirable for HTML

  • Makes the HTML output """worse""" (for some definition of worse)

@rustbot
Copy link
Collaborator

rustbot commented Jun 9, 2025

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-compiletest Area: The compiletest test runner A-rustdoc-json Area: Rustdoc JSON backend A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Jun 9, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 9, 2025

Some changes occurred in src/tools/compiletest

cc @jieyouxu

These commits modify tests/rustdoc-json.
rustdoc-json is a public (but unstable) interface.

Please ensure that if you've changed the output:

  • It's intentional.
  • The FORMAT_VERSION in src/librustdoc-json-types is bumped if necessary.

cc @aDotInTheVoid, @obi1kenobi

@aDotInTheVoid
Copy link
Member Author

r? @ghost

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@fmease fmease Jun 9, 2025

Choose a reason for hiding this comment

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

Only works on structs. […]
I don't think this isn't fundamental, but should be doable with more work.

It is "fundamental" in that it doesn't work for functions which is the real question here. CC Jubilee's mention of implicit implied outlives-bounds.

For functions, predicates_of (more specifically, inferred_outlives_of which is called under the hood) does not include implied outlives-bounds. CC https://rustc-dev-guide.rust-lang.org/traits/implied-bounds.html#implicit-implied-bounds

generics: clean_ty_generics(
cx,
cx.tcx.generics_of(def_id),
cx.tcx.predicates_of(def_id),
Copy link
Member

@fmease fmease Jun 9, 2025

Choose a reason for hiding this comment

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

Right. predicates_of contains the (explicit) implied bounds because it's explicit_predicates_of + inferred_outlives_of.

I would just call inferred_outlives_of separately and not call predicates_of here. This way, implied bounds can be marked as implied.

Copy link
Member Author

Choose a reason for hiding this comment

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

d'oh that's much nicer, and solves all the problems with this approach, as we also get to keep using HIR for HTML!

@rust-cloud-vms rust-cloud-vms bot force-pushed the rustdoc-ty-generics branch from a5cf688 to 1d077d3 Compare June 9, 2025 21:04
@aDotInTheVoid aDotInTheVoid removed T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-compiletest Area: The compiletest test runner labels Jun 10, 2025
@aDotInTheVoid
Copy link
Member Author

Closing, as not the right approach.

workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jun 10, 2025
rustdoc: Refractor `clean_ty_generics`

Refactoring towards rust-lang#142226

[Zulip Discussion](https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/Why.20sometimes.20.60predicates_of.60.20.28vs.20.60explicit_predicates_.2E.2E.2E/near/523182529)

The old `clean_ty_generics` was almost always called with the same args, so rename it to `clean_ty_generics_inner`, and add a new wrapper that generates those args from a `DefId`. Having this be the main entrypoint to `clean_ty_generics` should make it easier to start calling `inferred_outlives_of` rust-lang#142264 (comment) (and is more readable even if we don't)

Also, replaces all calls in rustdoc to `tcx.predicates_of` to `tcx.explicit_predicates_of`, which lets us remove `filter_non_trait_generics`

r? `@fmease`
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jun 10, 2025
rustdoc: Refractor `clean_ty_generics`

Refactoring towards rust-lang#142226

[Zulip Discussion](https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/Why.20sometimes.20.60predicates_of.60.20.28vs.20.60explicit_predicates_.2E.2E.2E/near/523182529)

The old `clean_ty_generics` was almost always called with the same args, so rename it to `clean_ty_generics_inner`, and add a new wrapper that generates those args from a `DefId`. Having this be the main entrypoint to `clean_ty_generics` should make it easier to start calling `inferred_outlives_of` rust-lang#142264 (comment) (and is more readable even if we don't)

Also, replaces all calls in rustdoc to `tcx.predicates_of` to `tcx.explicit_predicates_of`, which lets us remove `filter_non_trait_generics`

r? ``@fmease``
fmease added a commit to fmease/rust that referenced this pull request Jun 10, 2025
rustdoc: Refractor `clean_ty_generics`

Refactoring towards rust-lang#142226

[Zulip Discussion](https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/Why.20sometimes.20.60predicates_of.60.20.28vs.20.60explicit_predicates_.2E.2E.2E/near/523182529)

The old `clean_ty_generics` was almost always called with the same args, so rename it to `clean_ty_generics_inner`, and add a new wrapper that generates those args from a `DefId`. Having this be the main entrypoint to `clean_ty_generics` should make it easier to start calling `inferred_outlives_of` rust-lang#142264 (comment) (and is more readable even if we don't)

Also, replaces all calls in rustdoc to `tcx.predicates_of` to `tcx.explicit_predicates_of`, which lets us remove `filter_non_trait_generics`

r? ```@fmease```
rust-timer added a commit that referenced this pull request Jun 10, 2025
Rollup merge of #142275 - aDotInTheVoid:gen-ty-of, r=fmease

rustdoc: Refractor `clean_ty_generics`

Refactoring towards #142226

[Zulip Discussion](https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/Why.20sometimes.20.60predicates_of.60.20.28vs.20.60explicit_predicates_.2E.2E.2E/near/523182529)

The old `clean_ty_generics` was almost always called with the same args, so rename it to `clean_ty_generics_inner`, and add a new wrapper that generates those args from a `DefId`. Having this be the main entrypoint to `clean_ty_generics` should make it easier to start calling `inferred_outlives_of` #142264 (comment) (and is more readable even if we don't)

Also, replaces all calls in rustdoc to `tcx.predicates_of` to `tcx.explicit_predicates_of`, which lets us remove `filter_non_trait_generics`

r? ```@fmease```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rustdoc-json Area: Rustdoc JSON backend A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants