Skip to content

Rustdoc json: Associated constants in impl have wrong representation #81340

Closed

Description

This affects rustdoc json output

struct Simple;

/// Compare output for this inherent impl:
impl Simple {
    pub const CONSTANT: usize = 0;
}

pub trait EasyToImpl {
    type ToDeclare;
    const An_ATTRIBTE: usize;
}

/// versus this trait impl
impl EasyToImpl for Simple {
    type ToDeclare = usize;
    const An_ATTRIBTE: usize = 0;
}

I expected to see this happen: The constant in the first impl is printed with ItemEnum::Constant, the type and constant declaration in EasyToImpl with ItemEnum::{AssocTypeItem, AssocConstItem}, and the corresponding type definition in the second impl with ItemEnum::{Typedef, Constant}.

Instead, this happened: The constant definitions in both impls are the odd one out. They uses the
ItemEnum::AssocConstItem form instead and their definitions are in the default attribute.

The raw json produced
{"root":"0:0","crate_version":null,"includes_private":false,"index":{"0:0":{"id":"0:0","crate_id":0,"name":"example_crate","source":{"filename":"src/lib.rs","begin":[1,0],"end":[21,1]},"visibility":"public","docs":"","links":{},"attrs":["#![no_std]","#![feature(no_core)]","#![no_core]"],"deprecation":null,"kind":"module","inner":{"is_crate":true,"items":["0:1","0:5"]}},"0:7":{"id":"0:7","crate_id":0,"name":"An_ATTRIBTE","source":{"filename":"src/lib.rs","begin":[14,4],"end":[14,29]},"visibility":"public","docs":"","links":{},"attrs":[],"deprecation":null,"kind":"assoc_const","inner":{"type":{"kind":"primitive","inner":"usize"},"default":null}},"0:10":{"id":"0:10","crate_id":0,"name":"An_ATTRIBTE","source":{"filename":"src/lib.rs","begin":[20,4],"end":[20,33]},"visibility":"public","docs":"","links":{},"attrs":[],"deprecation":null,"kind":"assoc_const","inner":{"type":{"kind":"primitive","inner":"usize"},"default":"0"}},"0:4":{"id":"0:4","crate_id":0,"name":"CONSTANT","source":{"filename":"src/lib.rs","begin":[9,4],"end":[9,34]},"visibility":"public","docs":"","links":{},"attrs":[],"deprecation":null,"kind":"assoc_const","inner":{"type":{"kind":"primitive","inner":"usize"},"default":"0"}},"0:1":{"id":"0:1","crate_id":0,"name":"Simple","source":{"filename":"src/lib.rs","begin":[5,0],"end":[5,18]},"visibility":"public","docs":"","links":{},"attrs":[],"deprecation":null,"kind":"struct","inner":{"struct_type":"unit","generics":{"params":[],"where_predicates":[]},"fields_stripped":false,"fields":[],"impls":["0:3","0:8"]}},"0:5":{"id":"0:5","crate_id":0,"name":"EasyToImpl","source":{"filename":"src/lib.rs","begin":[12,0],"end":[15,1]},"visibility":"public","docs":"","links":{},"attrs":[],"deprecation":null,"kind":"trait","inner":{"is_auto":false,"is_unsafe":false,"items":["0:6","0:7"],"generics":{"params":[],"where_predicates":[]},"bounds":[],"implementors":["0:8"]}},"0:8":{"id":"0:8","crate_id":0,"name":null,"source":{"filename":"src/lib.rs","begin":[18,0],"end":[21,1]},"visibility":"crate","docs":"versus this trait impl","links":{},"attrs":[],"deprecation":null,"kind":"impl","inner":{"is_unsafe":false,"generics":{"params":[],"where_predicates":[]},"provided_trait_methods":[],"trait":{"kind":"resolved_path","inner":{"name":"EasyToImpl","id":"0:5","args":{"angle_bracketed":{"args":[],"bindings":[]}},"param_names":[]}},"for":{"kind":"resolved_path","inner":{"name":"Simple","id":"0:1","args":{"angle_bracketed":{"args":[],"bindings":[]}},"param_names":[]}},"items":["0:9","0:10"],"negative":false,"synthetic":false,"blanket_impl":null}},"0:9":{"id":"0:9","crate_id":0,"name":"ToDeclare","source":{"filename":"src/lib.rs","begin":[19,4],"end":[19,27]},"visibility":"public","docs":"","links":{},"attrs":[],"deprecation":null,"kind":"typedef","inner":{"type":{"kind":"primitive","inner":"usize"},"generics":{"params":[],"where_predicates":[]}}},"0:3":{"id":"0:3","crate_id":0,"name":null,"source":{"filename":"src/lib.rs","begin":[8,0],"end":[10,1]},"visibility":"crate","docs":"Compare output for this inherent impl:","links":{},"attrs":[],"deprecation":null,"kind":"impl","inner":{"is_unsafe":false,"generics":{"params":[],"where_predicates":[]},"provided_trait_methods":[],"trait":null,"for":{"kind":"resolved_path","inner":{"name":"Simple","id":"0:1","args":{"angle_bracketed":{"args":[],"bindings":[]}},"param_names":[]}},"items":["0:4"],"negative":false,"synthetic":false,"blanket_impl":null}},"0:6":{"id":"0:6","crate_id":0,"name":"ToDeclare","source":{"filename":"src/lib.rs","begin":[13,4],"end":[13,19]},"visibility":"public","docs":"","links":{},"attrs":[],"deprecation":null,"kind":"assoc_type","inner":{"bounds":[],"default":null}}},"paths":{"0:5":{"crate_id":0,"path":["example_crate","EasyToImpl"],"kind":"trait"},"0:9":{"crate_id":0,"path":["example_crate","ToDeclare"],"kind":"typedef"},"0:0":{"crate_id":0,"path":["example_crate"],"kind":"module"},"0:1":{"crate_id":0,"path":["example_crate","Simple"],"kind":"struct"}},"external_crates":{},"format_version":1}
Debug print of the item kinds in the impls
[src/frontend/rustdoc/mod.rs:786] krate.index.get(item_id) = Some(
    Item {
        id: Id(
            "0:38",
        ),
        crate_id: 0,
        name: Some(
            "CONSTANT",
        ),
        source: Some(
            Span {
                filename: "src/lib.rs",
                begin: (
                    69,
                    4,
                ),
                end: (
                    69,
                    34,
                ),
            },
        ),
        visibility: Public,
        docs: "",
        links: {},
        attrs: [],
        deprecation: None,
        kind: AssocConst,
        inner: AssocConstItem {
            type_: Primitive(
                "usize",
            ),
            default: Some(
                "0",
            ),
        },
    },
)
[src/frontend/rustdoc/mod.rs:786] krate.index.get(item_id) = Some(
    Item {
        id: Id(
            "0:41",
        ),
        crate_id: 0,
        name: Some(
            "ToDeclare",
        ),
        source: Some(
            Span {
                filename: "src/lib.rs",
                begin: (
                    79,
                    4,
                ),
                end: (
                    79,
                    27,
                ),
            },
        ),
        visibility: Public,
        docs: "",
        links: {},
        attrs: [],
        deprecation: None,
        kind: Typedef,
        inner: TypedefItem(
            Typedef {
                type_: Primitive(
                    "usize",
                ),
                generics: Generics {
                    params: [],
                    where_predicates: [],
                },
            },
        ),
    },
)
[src/frontend/rustdoc/mod.rs:786] krate.index.get(item_id) = Some(
    Item {
        id: Id(
            "0:42",
        ),
        crate_id: 0,
        name: Some(
            "An_ATTRIBTE",
        ),
        source: Some(
            Span {
                filename: "src/lib.rs",
                begin: (
                    80,
                    4,
                ),
                end: (
                    80,
                    33,
                ),
            },
        ),
        visibility: Public,
        docs: "",
        links: {},
        attrs: [],
        deprecation: None,
        kind: AssocConst,
        inner: AssocConstItem {
            type_: Primitive(
                "usize",
            ),
            default: Some(
                "0",
            ),
        },
    },
)

Meta

rustc --version --verbose:

cargo 1.51.0-nightly (a73e5b7d5 2021-01-12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    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.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions