Skip to content

Weird representation of trait const of associated type #150312

@talshorer

Description

@talshorer

Consider the following code as lib.rs:

pub trait Foo {
    type AssocType;
    const THE_CONST: Self::AssocType;
}

pub struct Bar;

impl Foo for Bar {
    type AssocType = u32;

    const THE_CONST: Self::AssocType = 1;
}

Running cargo doc and opening the generated html, one sees

Image

Note

const THE_CONST: Self::AssocType = {transmute(0x00000001): <Bar as Foo>::AssocType}

Instead of the more reasonably expected

const THE_CONST: Self::AssocType = 1u32

or, barring that, just nothing instead of the confusing transmute string.

Meta

rustc --version --verbose:

rustc 1.91.0 (f8297e351 2025-10-28)
binary: rustc
commit-hash: f8297e351a40c1439a467bbbb6879088047f50b3
commit-date: 2025-10-28
host: x86_64-unknown-linux-gnu
release: 1.91.0
LLVM version: 21.1.2

See also


Possible workaround

Explicitly setting the const's concrete type instead of using the trait's associated type

impl Foo for Bar {
    type AssocType = u32;

    const THE_CONST: u32 = 1;
}

yields a more reasonable representation in the doc:
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-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