-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
C-cleanupCategory: PRs that clean code up or issues documenting cleanup.Category: PRs that clean code up or issues documenting cleanup.E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.Call 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.Call 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.Relevant to the rustdoc team, which will review and decide on the PR/issue.
Description
Right now, rustdoc has a whole mess of awful hacks to be able to add DefIds for things that aren't definitions:
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
DefId
s - 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:
Line 68 in 4c10c84
crate fake_def_ids: FxHashMap<CrateNum, DefIndex>, Line 145 in 4c10c84
crate fn next_def_id(&mut self, crate_num: CrateNum) -> DefId { Line 172 in 4c10c84
crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: DefId) -> Option<HirId> { rust/src/librustdoc/clean/types.rs
Line 98 in 4c10c84
impl fmt::Debug for Item { rust/src/librustdoc/clean/types.rs
Line 316 in 4c10c84
crate fn is_fake(&self) -> bool { pub fn num_def_ids(&self) -> usize { pub fn num_def_ids(&self, cnum: CrateNum) -> usize {
Metadata
Metadata
Assignees
Labels
C-cleanupCategory: PRs that clean code up or issues documenting cleanup.Category: PRs that clean code up or issues documenting cleanup.E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.Call 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.Call 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.Relevant to the rustdoc team, which will review and decide on the PR/issue.