Skip to content

Get rid of fake DefIds in rustdoc #83183

Closed
@jyn514

Description

@jyn514

Right now, rustdoc has a whole mess of awful hacks to be able to add DefIds for things that aren't definitions:

rust/src/librustdoc/core.rs

Lines 131 to 140 in 4c10c84

/// Create a new "fake" [`DefId`].
///
/// This is an ugly hack, but it's the simplest way to handle synthetic impls without greatly
/// refactoring either rustdoc or [`rustc_middle`]. In particular, allowing new [`DefId`]s
/// to be registered after the AST is constructed would require storing the [`DefId`] mapping
/// in a [`RefCell`], decreasing the performance for normal compilation for very little gain.
///
/// Instead, we construct "fake" [`DefId`]s, which start immediately after the last `DefId`.
/// In the [`Debug`] impl for [`clean::Item`], we explicitly check for fake `DefId`s,
/// as we'll end up with a panic if we use the `DefId` `Debug` impl for fake `DefId`s.

It's unfortunate that these are needed at all, but it's really unfortunate that these have the same type as normal DefIds - they are not valid and cannot be passed to rustc APIs; trying to do so will usually panic. It also leads to lots of other hacks, like giving primitives DefIds: #83083 (comment)

A better alternative is to add a new wrapper type, something like

// clean/type.rs
enum DefId {
  Real(rustc_span::def_id::DefId),
  Fake(usize),
}

That makes it clear that the two are very different, and doesn't require hacks in rustdoc or rustc.

Things that can be removed once this is implemented:

Metadata

Metadata

Assignees

Labels

C-cleanupCategory: PRs that clean code up or issues documenting cleanup.E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.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