Skip to content

Rustdoc Json: structs and functions deserialize incorrect #82299

Closed
@197g

Description

@197g

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

Metadata

Metadata

Assignees

Labels

A-rustdoc-jsonArea: Rustdoc JSON backendC-bugCategory: This is a bug.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions