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: structs and functions deserialize incorrect #82299

Closed
HeroicKatora opened this issue Feb 19, 2021 · 3 comments · Fixed by #82613
Closed

Rustdoc Json: structs and functions deserialize incorrect #82299

HeroicKatora opened this issue Feb 19, 2021 · 3 comments · Fixed by #82613
Assignees
Labels
A-rustdoc-json Area: Rustdoc JSON backend C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@HeroicKatora
Copy link
Contributor

HeroicKatora commented Feb 19, 2021

I tried this code:

pub struct Foobar {
    field: u8,
}

Running rustdoc on this this happily produces the following json which I would judge to be correct.

{"root":"0:0","crate_version":null,"includes_private":true,"index":{"0:2":{"id":"0:2","crate_id":0,"name":"field","source":{"filename":"src/main.rs","begin":[4,4],"end":[4,13]},"visibility":"default","docs":null,"links":{},"attrs":[],"deprecation":null,"kind":"struct_field","inner":{"kind":"primitive","inner":"u8"}},"0:1":{"id":"0:1","crate_id":0,"name":"Foobar","source":{"filename":"src/main.rs","begin":[3,0],"end":[5,1]},"visibility":"public","docs":null,"links":{},"attrs":[],"deprecation":null,"kind":"struct","inner":{"struct_type":"plain","generics":{"params":[],"where_predicates":[]},"fields_stripped":false,"fields":["0:2"],"impls":[]}},"0:0":{"id":"0:0","crate_id":0,"name":"rustdoc_issue","source":{"filename":"src/main.rs","begin":[1,0],"end":[5,1]},"visibility":"public","docs":null,"links":{},"attrs":["#![feature(no_core)]","#![no_core]"],"deprecation":null,"kind":"module","inner":{"is_crate":true,"items":["0:1"]}}},"paths":{"0:0":{"crate_id":0,"path":["rustdoc_issue"],"kind":"module"},"0:1":{"crate_id":0,"path":["rustdoc_issue","Foobar"],"kind":"struct"}},"external_crates":{},"format_version":4}

However, deserializing this with the types defined in src/rustdoc-json-types/lib.rs leads to the incoherent representation:

Item {
    kind: ItemKind::Struct,
    inner: ItemEnum::Union {}
}

This happens because the ItemEnum is an untagged enum, the UnionItem variant appears before the StructItem variant and the struct Union has a subset of the fields of struct Struct, therefore it matches first. The same issue happens with FunctionItem/Function and MethodItem/Method.

It could be fixed by reordering the variants but that's quite brittle and non-obvious.

Meta

rustc --version --verbose:

 rustc 1.52.0-nightly (a143517d4 2021-02-16)

cc: @jyn514

@HeroicKatora HeroicKatora added the C-bug Category: This is a bug. label Feb 19, 2021
@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 Feb 19, 2021
@jyn514
Copy link
Member

jyn514 commented Feb 19, 2021

hey @CraftSpider ;) are you interested in taking a look?

@CraftSpider
Copy link
Contributor

Yes, I'll take a look at this

@CraftSpider
Copy link
Contributor

@rustbot claim

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 8, 2021
Remove Item::kind, use tagged enum. Rename variants to match

Fixes rust-lang#82299, by making the ItemEnum tagged. Doesn't remove ItemKind as it's still used in other places.

r? `@jyn514`
`@rustbot` label: +A-rustdoc-json +T-rustdoc
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 8, 2021
Remove Item::kind, use tagged enum. Rename variants to match

Fixes rust-lang#82299, by making the ItemEnum tagged. Doesn't remove ItemKind as it's still used in other places.

r? ``@jyn514``
``@rustbot`` label: +A-rustdoc-json +T-rustdoc
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 8, 2021
Remove Item::kind, use tagged enum. Rename variants to match

Fixes rust-lang#82299, by making the ItemEnum tagged. Doesn't remove ItemKind as it's still used in other places.

r? ```@jyn514```
```@rustbot``` label: +A-rustdoc-json +T-rustdoc
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 8, 2021
Remove Item::kind, use tagged enum. Rename variants to match

Fixes rust-lang#82299, by making the ItemEnum tagged. Doesn't remove ItemKind as it's still used in other places.

r? ````@jyn514````
````@rustbot```` label: +A-rustdoc-json +T-rustdoc
@bors bors closed this as completed in 0681951 Mar 8, 2021
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 C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants