rustdoc_json: improve handling of generic args by nnethercote · Pull Request #142502 · rust-lang/rust

aDotInTheVoid

@rustbot rustbot 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

Jun 16, 2025

@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

Jun 17, 2025

jhpratt added a commit to jhpratt/rust that referenced this pull request

Jun 18, 2025
…gs, r=aDotInTheVoid

rustdoc_json: improve handling of generic args

This PR fixes some inconsistencies and inefficiencies in how generic args are handled by rustdoc-json-types.

r? `@aDotInTheVoid`

bors added a commit that referenced this pull request

Jun 18, 2025
Rollup of 10 pull requests

Successful merges:

 - #135656 (Add `-Z hint-mostly-unused` to tell rustc that most of a crate will go unused)
 - #138237 (Get rid of `EscapeDebugInner`.)
 - #140772 ({aarch64,x86_64}-pc-windows-gnullvm: build host tools)
 - #140774 (Affirm `-Cforce-frame-pointers=off` does not override)
 - #141610 (Stabilize `feature(generic_arg_infer)`)
 - #141864 (Handle win32 separator for cygwin paths)
 - #142384 (Bringing `rustc_rayon_core` in tree as `rustc_thread_pool`)
 - #142502 (rustdoc_json: improve handling of generic args)
 - #142571 (Reason about borrowed classes in CopyProp.)
 - #142591 (Add spawn APIs for BootstrapCommand to support deferred command execution)

r? `@ghost`
`@rustbot` modify labels: rollup

@bors bors 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-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

labels

Jun 18, 2025

@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-author

Status: This is awaiting some action (such as code changes or more information) from the author.

labels

Jun 20, 2025

@bors bors 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-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

labels

Jun 21, 2025
A lot of these are large! Lots of room for improvement in the future.

@nnethercote

A path without generic args, like `Reader`, currently has JSON produced
like this:
```
{"path":"Reader","id":286,"args":{"angle_bracketed":{"args":[],"constraints":[]}}}
```
Even though `types::Path::args` is `Option` and allows for "no args",
instead it gets represented as "empty args". (More like `Reader<>` than
`Reader`.)

This is due to a problem in `clean::Path::from_clean`. It only produces
`None` if the path is an empty string. This commit changes it to also
produce `None` if there are no generic args. The example above becomes:
```
{"path":"Reader","id":286,"args":null}
```
I looked at a few examples and saw this reduce the size of the JSON
output by 3-9%.

The commit also adds an assertion that non-final segments don't have any
generics; something the old code was implicitly relying on.

Note: the original sin here is that `clean::PathSegment::args` is not an
`Option`, unlike `{ast,hir}::PathSegment::args`. I want to fix that, but
it can be done separately.
As per the previous commit, generic args here can only appear on the
final segment. So make the comments obey that constraint.
They show up in three places: once as `Option<Box<GenericArgs>>`, once
as `Box<GenericArgs>`, and once as `GenericArgs`. The first option is
best. It is more compact because generic args are often missing. This
commit changes the latter two to the former.

Example output, before and after, for the `AssocItemConstraint` change:
```
{"name":"Offset","args":{"angle_bracketed":{"args":[],"constraints":[]}},"binding":{...}}
{"name":"Offset","args":null,"binding":{...}}
```
Example output, before and after, for the `Type::QualifiedPath` change:
```
{"qualified_path":{"name":"Offset","args":{"angle_bracketed":{"args":[],"constraints":[]}}, ...}}
{"qualified_path":{"name":"Offset","args":null, ...}}
```
This reduces JSON output size, but not by much (e.g. 0.5%), because
`AssocItemConstraint` and `Type::QualifiedPath` are uncommon.

@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-author

Status: This is awaiting some action (such as code changes or more information) from the author.

labels

Jun 21, 2025

bors added a commit that referenced this pull request

Jun 21, 2025
Rollup of 7 pull requests

Successful merges:

 - #142502 (rustdoc_json: improve handling of generic args)
 - #142597 (error on calls to ABIs that cannot be called)
 - #142785 (fix(linkcheck): Build using the lockfile)
 - #142787 (Add diagnostic items for Clippy)
 - #142788 (add doc(alias("AsciiChar")) to core::ascii::Char)
 - #142801 (Use gen blocks in the compiler instead of `from_coroutine`)
 - #142804 (Rename `LayoutS` to `LayoutData` in comments)

r? `@ghost`
`@rustbot` modify labels: rollup

rust-timer added a commit that referenced this pull request

Jun 21, 2025
Rollup merge of #142502 - nnethercote:rustdoc-json-GenericArgs, r=aDotInTheVoid

rustdoc_json: improve handling of generic args

This PR fixes some inconsistencies and inefficiencies in how generic args are handled by rustdoc-json-types.

r? `@aDotInTheVoid`

lnicola pushed a commit to lnicola/rust-analyzer that referenced this pull request

Jun 23, 2025

workingjubilee added a commit to workingjubilee/rustc that referenced this pull request

Jun 24, 2025
… r=aDotInTheVoid

rustdoc-json: Keep empty generic args if parenthesized

Because in the case of for example

    pub fn my_fn3(f: impl FnMut()) {}

we want to keep `()` even if it is empty since that matches e.g. Rust syntax requirements.

This is an amendment to rust-lang#142502, so:
r? `@aDotInTheVoid`
cc `@nnethercote`

cc cargo-public-api/cargo-public-api#798

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request

Jun 24, 2025
… r=aDotInTheVoid

rustdoc-json: Keep empty generic args if parenthesized

Because in the case of for example

    pub fn my_fn3(f: impl FnMut()) {}

we want to keep `()` even if it is empty since that matches e.g. Rust syntax requirements.

This is an amendment to rust-lang#142502, so:
r? ``@aDotInTheVoid``
cc ``@nnethercote``

cc cargo-public-api/cargo-public-api#798

rust-timer added a commit that referenced this pull request

Jun 24, 2025
Rollup merge of #142932 - Enselic:keep-empty-generic-params, r=aDotInTheVoid

rustdoc-json: Keep empty generic args if parenthesized

Because in the case of for example

    pub fn my_fn3(f: impl FnMut()) {}

we want to keep `()` even if it is empty since that matches e.g. Rust syntax requirements.

This is an amendment to #142502, so:
r? ``@aDotInTheVoid``
cc ``@nnethercote``

cc cargo-public-api/cargo-public-api#798