Skip to content

Commit

Permalink
Auto merge of #93766 - petrochenkov:doclinkregr, r=camelid,GuillaumeG…
Browse files Browse the repository at this point in the history
…omez

rustdoc: Collect traits in scope for lang items

Inherent impls on primitive types are not included in the list of all inherent impls in the crate (`inherent_impls_in_crate_untracked`), they are taken from the list of lang items instead, but such impls can also be inlined by rustdoc, e.g. if something derefs to a primitive type.

r? `@camelid`
Fixes #93698
  • Loading branch information
bors committed Feb 18, 2022
2 parents feac2ec + 0da7adc commit b8c56fa
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
12 changes: 5 additions & 7 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,13 +1032,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}

/// Iterates over the language items in the given crate.
fn get_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, usize)] {
tcx.arena.alloc_from_iter(
self.root
.lang_items
.decode(self)
.map(|(def_index, index)| (self.local_def_id(def_index), index)),
)
fn get_lang_items(self) -> impl Iterator<Item = (DefId, usize)> + 'a {
self.root
.lang_items
.decode(self)
.map(move |(def_index, index)| (self.local_def_id(def_index), index))
}

/// Iterates over the diagnostic items in the given crate.
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
tcx.arena.alloc_slice(&result)
}
defined_lib_features => { cdata.get_lib_features(tcx) }
defined_lang_items => { cdata.get_lang_items(tcx) }
defined_lang_items => { tcx.arena.alloc_from_iter(cdata.get_lang_items()) }
diagnostic_items => { cdata.get_diagnostic_items() }
missing_lang_items => { cdata.get_missing_lang_items(tcx) }

Expand Down Expand Up @@ -501,6 +501,11 @@ impl CStore {
) -> impl Iterator<Item = (DefId, DefId)> + '_ {
self.get_crate_data(cnum).get_inherent_impls()
}

/// Decodes all lang items in the crate (for rustdoc).
pub fn lang_items_untracked(&self, cnum: CrateNum) -> impl Iterator<Item = DefId> + '_ {
self.get_crate_data(cnum).get_lang_items().map(|(def_id, _)| def_id)
}
}

impl CrateStore for CStore {
Expand Down
4 changes: 4 additions & 0 deletions src/librustdoc/passes/collect_intra_doc_links/early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ impl IntraLinkCrateLoader<'_, '_> {
Vec::from_iter(self.resolver.cstore().trait_impls_in_crate_untracked(cnum));
let all_inherent_impls =
Vec::from_iter(self.resolver.cstore().inherent_impls_in_crate_untracked(cnum));
let all_lang_items = Vec::from_iter(self.resolver.cstore().lang_items_untracked(cnum));

// Querying traits in scope is expensive so we try to prune the impl and traits lists
// using privacy, private traits and impls from other crates are never documented in
Expand All @@ -141,6 +142,9 @@ impl IntraLinkCrateLoader<'_, '_> {
self.add_traits_in_parent_scope(impl_def_id);
}
}
for def_id in all_lang_items {
self.add_traits_in_parent_scope(def_id);
}

self.all_traits.extend(all_traits);
self.all_trait_impls.extend(all_trait_impls.into_iter().map(|(_, def_id, _)| def_id));
Expand Down
29 changes: 29 additions & 0 deletions src/test/rustdoc/intra-doc/auxiliary/extern-lang-item-impl-dep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// no-prefer-dynamic

#![feature(lang_items)]

#![crate_type = "rlib"]
#![no_std]

pub struct DerefsToF64(f64);

impl core::ops::Deref for DerefsToF64 {
type Target = f64;
fn deref(&self) -> &Self::Target {
&self.0
}
}

mod inner {
#[lang = "f64_runtime"]
impl f64 {
/// [f64::clone]
pub fn method() {}
}
}

#[lang = "eh_personality"]
fn foo() {}

#[panic_handler]
fn bar(_: &core::panic::PanicInfo) -> ! { loop {} }
11 changes: 11 additions & 0 deletions src/test/rustdoc/intra-doc/extern-lang-item-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Reexport of a structure that derefs to a type with lang item impls having doc links in their
// comments. The doc link points to an associated item, so we check that traits in scope for that
// link are populated.

// aux-build:extern-lang-item-impl-dep.rs

#![no_std]

extern crate extern_lang_item_impl_dep;

pub use extern_lang_item_impl_dep::DerefsToF64;

0 comments on commit b8c56fa

Please sign in to comment.