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-json: Don't ignore impls for primitive types #88234

Merged
merged 1 commit into from
Oct 5, 2021

Conversation

hkmatsumoto
Copy link
Member

Fix the issue discussed at Zulip

r? @jyn514

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 22, 2021
src/librustdoc/json/mod.rs Outdated Show resolved Hide resolved
src/librustdoc/json/mod.rs Outdated Show resolved Hide resolved
@jyn514 jyn514 added A-rustdoc-json Area: Rustdoc JSON backend T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Aug 22, 2021
@jyn514 jyn514 changed the title Don't ignore impls for primitive types Don't ignore impls for primitive types if they come from non-local crate Aug 22, 2021
@jyn514
Copy link
Member

jyn514 commented Aug 22, 2021

Looking back it's unclear to me what problem this is solving. get_impls is only meant to show impls from the current crate, right? Why do you need it to be shown for non-local crates? Can you add a test showing the change in behavior?

@jyn514 jyn514 changed the title Don't ignore impls for primitive types if they come from non-local crate rustdoc-json: Don't ignore impls for primitive types if they come from a non-local crate Aug 22, 2021
@jyn514 jyn514 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 Aug 22, 2021
@hkmatsumoto
Copy link
Member Author

get_impls in json backend returns list of id linked to the impls for local items, and inserts the impls to the index. Using the latter feature here we have the information of impls of structs and enums in the resulting json file.

@jyn514 jyn514 closed this Aug 26, 2021
@jyn514 jyn514 reopened this Aug 26, 2021
@jyn514 jyn514 added S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 6, 2021
@jyn514
Copy link
Member

jyn514 commented Sep 25, 2021

#87073 has been merged, so this is no longer blocked :)

@jyn514 jyn514 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. labels Sep 25, 2021
@hkmatsumoto hkmatsumoto force-pushed the rustdoc-impls-for-primitive branch 3 times, most recently from 850b851 to 5177fe7 Compare September 26, 2021 12:35
@hkmatsumoto hkmatsumoto 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 26, 2021
@hkmatsumoto
Copy link
Member Author

hkmatsumoto commented Sep 26, 2021

I found a weird behavior with doc(primitive).

By running cargo +stage1 rustdoc -- --output-format json -Z unstable-options on the following code (using stage1 here because this part of change is needed to generate json file without panic):

#![feature(doc_primitive)]
#[doc(primitive = "char")]
mod char {}

cat target/doc/foo.json | jq '.index[] | select(.id == "0:0")' emits:

{
  "id": "0:0",
  "crate_id": 0,
  "name": "foo",
  "span": {
    "filename": "src/lib.rs",
    "begin": [
      1,
      0
    ],
    "end": [
      3,
      11
    ]
  },
  "visibility": "public",
  "docs": null,
  "links": {},
  "attrs": [
    "#![feature(doc_primitive)]"
  ],
  "deprecation": null,
  "kind": "module",
  "inner": {
    "is_crate": true,
    "items": [
      "0:3" // I assume this refers to `char` module but it's private
    ]
  }
}

while cat target/doc/issue_87073.json | jq '.index[] | select(.id == "0:3")' | wc -l emits 0, i.e., an item with the id is not present in index.

@hkmatsumoto hkmatsumoto force-pushed the rustdoc-impls-for-primitive branch 2 times, most recently from aaad690 to ce9ab9e Compare September 29, 2021 04:50
@hkmatsumoto
Copy link
Member Author

I'm not sure exactly what's going wrong, but it's likely because the module itself is private. I think we may need to add a new type of item to the index to allow representing this? I don't think it has a type for primitives currently.

Added ItemEnum::Primitive(String) variant and it works like a charm. I wonder if we need to upgrade Crate::format_version due to this change.

@rust-log-analyzer

This comment has been minimized.


#![feature(doc_primitive)]
#[doc(primitive = "usize")]
mod usize {}
Copy link
Member

Choose a reason for hiding this comment

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

Can you also test the behavior when the primitive is only defined in libstd, not the current crate?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added tests to check crate_id of methods of usize does not match to local crate's. We cannot check if they come from libstd because the std module item is not in the index, but hope it suffices.

Copy link
Member

Choose a reason for hiding this comment

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

Added tests to check crate_id of methods of usize does not match to local crate's.

In that case, this mod usize is doing nothing and should be removed.

Copy link
Member Author

@hkmatsumoto hkmatsumoto Sep 29, 2021

Choose a reason for hiding this comment

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

doc(primitive) requires an item to be defined right after itself, so we need this meaningless mod usize like we do in library/std/src/primitive_docs.rs.

Copy link
Member

@jyn514 jyn514 Sep 29, 2021

Choose a reason for hiding this comment

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

No, my point is that doc(primitive) is doing nothing. If it's using the methods in libstd then whether the docs come from this crate or libstd doesn't matter.

Copy link
Member

@jyn514 jyn514 Sep 29, 2021

Choose a reason for hiding this comment

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

Actually, I'm not sure this is the right approach at all - the original problem was that documenting libcore wouldn't show the primitive impls, right? but we don't want to show them on all crates unconditionally, only crates where the primitive is documented.

@jyn514
Copy link
Member

jyn514 commented Sep 29, 2021

Added ItemEnum::Primitive(String) variant and it works like a charm. I wonder if we need to upgrade Crate::format_version due to this change.

Sure, that seems useful.

How were primitives being handled before? I'm surprised it didn't ICE trying to document the primitives in libstd.

@hkmatsumoto
Copy link
Member Author

How were primitives being handled before? I'm surprised it didn't ICE trying to document the primitives in libstd.

Definitions of primitive types themselves were not indexed, unlike those of user-defined ADTs. Impls for primitive types were not indexed, too (They'll be indexed after this patch).

@jyn514
Copy link
Member

jyn514 commented Sep 29, 2021

Definitions of primitive types themselves were not indexed, unlike those of user-defined ADTs.

Ok. Shouldn't those definitions be indexed too? It seems strange to show only the impls and not the primitives themselves.

@hkmatsumoto
Copy link
Member Author

IIRC lang_items are not explicitly defined because the compiler does some injections instead.

@jyn514
Copy link
Member

jyn514 commented Sep 29, 2021

@hkmatsumoto right, but primitives aren't documented through lang_items. They're documented through doc(primitive) modules, like the one you wrote in the test. I'm asking what happens to those doc(primitive) modules. The correct behavior is for them to show up in the JSON index.

@hkmatsumoto
Copy link
Member Author

hkmatsumoto commented Sep 29, 2021

With this code:

#![feature(doc_primitive)]

#[doc(primitive = "usize")]
mod foo {}

cat target/doc/issue_88234.json | jq '.index[] | select(.name == "primitive")' emits:

{
  "id": "0:0",
  "crate_id": 0,
  "name": "primitive",
  "span": {
    "filename": "src/lib.rs",
    "begin": [
      1,
      0
    ],
    "end": [
      4,
      11
    ]
  },
  "visibility": "public",
  "docs": null,
  "links": {},
  "attrs": [
    "#![feature(doc_primitive)]"
  ],
  "deprecation": null,
  "kind": "module",
  "inner": {
    "is_crate": true,
    "items": [
      "0:3"
    ]
  }
}

cat target/doc/issue_88234.json | jq '.index[] | select(.id == "0:3")' emits:

{
  "id": "0:3",
  "crate_id": 0,
  "name": "usize",
  "span": {
    "filename": "src/lib.rs",
    "begin": [
      4,
      0
    ],
    "end": [
      4,
      10
    ]
  },
  "visibility": "crate",
  "docs": null,
  "links": {},
  "attrs": [
    "#[doc(primitive = \"usize\")]"
  ],
  "deprecation": null,
  "kind": "primitive_type",
  "inner": "usize"
}

The modules are showing up.

@hkmatsumoto hkmatsumoto changed the title rustdoc-json: Don't ignore impls for primitive types if they come from a non-local crate rustdoc-json: Don't ignore impls for primitive types Sep 29, 2021
@jyn514
Copy link
Member

jyn514 commented Oct 3, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Oct 3, 2021

📌 Commit e18a8ef has been approved by jyn514

@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 Oct 3, 2021
Manishearth added a commit to Manishearth/rust that referenced this pull request Oct 5, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 5, 2021
…arth

Rollup of 12 pull requests

Successful merges:

 - rust-lang#87631 (os current_exe using same approach as linux to get always the full ab…)
 - rust-lang#88234 (rustdoc-json: Don't ignore impls for primitive types)
 - rust-lang#88651 (Use the 64b inner:monotonize() implementation not the 128b one for aarch64)
 - rust-lang#88816 (Rustdoc migrate to table so the gui can handle >2k constants)
 - rust-lang#89244 (refactor: VecDeques PairSlices fields to private)
 - rust-lang#89364 (rustdoc-json: Encode json files with UTF-8)
 - rust-lang#89423 (Fix ICE caused by non_exaustive_omitted_patterns struct lint)
 - rust-lang#89426 (bootstrap: add config option for nix patching)
 - rust-lang#89462 (haiku thread affinity build fix)
 - rust-lang#89482 (Follow the diagnostic output style guide)
 - rust-lang#89504 (Don't suggest replacing region with 'static in NLL)
 - rust-lang#89535 (fix busted JavaScript in error index generator)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 7a09755 into rust-lang:master Oct 5, 2021
@rustbot rustbot added this to the 1.57.0 milestone Oct 5, 2021
@hkmatsumoto hkmatsumoto deleted the rustdoc-impls-for-primitive branch October 6, 2021 04:33
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 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.

6 participants