Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow arbitrary inherent impls for builtin types in core #94963

Merged
merged 13 commits into from
Mar 30, 2022
Prev Previous commit
Next Next commit
fix rustdoc wrt builtin impls switch
lcnr committed Mar 30, 2022
commit ee62514b16b610870e001b14f15e7e71b15e54e7
19 changes: 14 additions & 5 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
@@ -1033,11 +1033,13 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}

/// Iterates over the language items in the given crate.
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))
fn get_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, usize)] {
tcx.arena.alloc_from_iter(
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.
@@ -1343,6 +1345,13 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
})
}

fn get_all_incoherent_impls(self) -> impl Iterator<Item = DefId> + 'a {
self.cdata
.incoherent_impls
.values()
.flat_map(move |impls| impls.decode(self).map(move |idx| self.local_def_id(idx)))
}

fn get_incoherent_impls(self, tcx: TyCtxt<'tcx>, simp: SimplifiedType) -> &'tcx [DefId] {
if let Some(impls) = self.cdata.incoherent_impls.get(&simp) {
tcx.arena.alloc_from_iter(impls.decode(self).map(|idx| self.local_def_id(idx)))
11 changes: 7 additions & 4 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
@@ -223,7 +223,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
tcx.arena.alloc_slice(&result)
}
defined_lib_features => { cdata.get_lib_features(tcx) }
defined_lang_items => { tcx.arena.alloc_from_iter(cdata.get_lang_items()) }
defined_lang_items => { cdata.get_lang_items(tcx) }
diagnostic_items => { cdata.get_diagnostic_items() }
missing_lang_items => { cdata.get_missing_lang_items(tcx) }

@@ -523,9 +523,12 @@ impl CStore {
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)
/// Decodes all incoherent inherent impls in the crate (for rustdoc).
pub fn incoherent_impls_in_crate_untracked(
&self,
cnum: CrateNum,
) -> impl Iterator<Item = DefId> + '_ {
self.get_crate_data(cnum).get_all_incoherent_impls()
}
}

5 changes: 3 additions & 2 deletions src/librustdoc/passes/collect_intra_doc_links/early.rs
Original file line number Diff line number Diff line change
@@ -113,7 +113,8 @@ 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));
let all_incoherent_impls =
Vec::from_iter(self.resolver.cstore().incoherent_impls_in_crate_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
@@ -137,7 +138,7 @@ impl IntraLinkCrateLoader<'_, '_> {
self.add_traits_in_parent_scope(impl_def_id);
}
}
for def_id in all_lang_items {
for def_id in all_incoherent_impls {
self.add_traits_in_parent_scope(def_id);
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// no-prefer-dynamic

#![feature(lang_items)]

#![feature(lang_items, rustc_attrs)]
#![crate_type = "rlib"]
#![no_std]

@@ -15,9 +14,9 @@ impl core::ops::Deref for DerefsToF64 {
}

mod inner {
#[lang = "f64_runtime"]
impl f64 {
/// [f64::clone]
#[rustc_allow_incoherent_impl]
pub fn method() {}
}
}
4 changes: 2 additions & 2 deletions src/test/rustdoc/intra-doc/auxiliary/my-core.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#![feature(no_core, lang_items, rustdoc_internals)]
#![feature(no_core, lang_items, rustdoc_internals, rustc_attrs)]
#![no_core]
#![rustc_coherence_is_core]
#![crate_type="rlib"]

#[doc(primitive = "char")]
/// Some char docs
mod char {}

#[lang = "char"]
impl char {
pub fn len_utf8(self) -> usize {
42
Original file line number Diff line number Diff line change
@@ -2,10 +2,10 @@
// 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
// aux-build:extern-builtin-type-impl-dep.rs

#![no_std]

extern crate extern_lang_item_impl_dep;
extern crate extern_builtin_type_impl_dep;

pub use extern_lang_item_impl_dep::DerefsToF64;
pub use extern_builtin_type_impl_dep::DerefsToF64;
4 changes: 2 additions & 2 deletions src/test/rustdoc/intra-doc/prim-methods-local.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![deny(rustdoc::broken_intra_doc_links)]
#![feature(no_core, lang_items, rustdoc_internals)]
#![feature(no_core, lang_items, rustc_attrs, rustdoc_internals)]
#![no_core]
#![rustc_coherence_is_core]
#![crate_type = "rlib"]

// @has prim_methods_local/index.html
@@ -12,7 +13,6 @@
#[doc(primitive = "char")]
mod char {}

#[lang = "char"]
impl char {
pub fn len_utf8(self) -> usize {
42
4 changes: 2 additions & 2 deletions src/test/rustdoc/issue-23511.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#![feature(lang_items)]
#![feature(rustc_attrs)]
#![feature(rustdoc_internals)]
#![no_std]

pub mod str {
#![doc(primitive = "str")]

#[lang = "str_alloc"]
impl str {
// @has search-index.js foo
#[rustc_allow_incoherent_impl]
pub fn foo(&self) {}
}
}