Skip to content

Commit e9ab787

Browse files
committed
Auto merge of #107765 - petrochenkov:nomoclone, r=oli-obk
rustc/rustdoc: Perform name resolver cleanups enabled by #94857 Unblocks #105462. r? `@oli-obk`
2 parents c3c6d73 + fd73d01 commit e9ab787

File tree

23 files changed

+102
-325
lines changed

23 files changed

+102
-325
lines changed

compiler/rustc_hir/src/definitions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl DefPathTable {
9292
/// The definition table containing node definitions.
9393
/// It holds the `DefPathTable` for `LocalDefId`s/`DefPath`s.
9494
/// It also stores mappings to convert `LocalDefId`s to/from `HirId`s.
95-
#[derive(Clone, Debug)]
95+
#[derive(Debug)]
9696
pub struct Definitions {
9797
table: DefPathTable,
9898
next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>,

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3460,7 +3460,7 @@ pub struct Upvar {
34603460
// The TraitCandidate's import_ids is empty if the trait is defined in the same module, and
34613461
// has length > 0 if the trait is found through an chain of imports, starting with the
34623462
// import/use statement in the scope where the trait is used.
3463-
#[derive(Encodable, Decodable, Clone, Debug, HashStable_Generic)]
3463+
#[derive(Encodable, Decodable, Debug, HashStable_Generic)]
34643464
pub struct TraitCandidate {
34653465
pub def_id: DefId,
34663466
pub import_ids: SmallVec<[LocalDefId; 1]>,

compiler/rustc_interface/src/passes.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,11 @@ use rustc_target::spec::PanicStrategy;
3535
use rustc_trait_selection::traits;
3636

3737
use std::any::Any;
38-
use std::cell::RefCell;
3938
use std::ffi::OsString;
4039
use std::io::{self, BufWriter, Write};
4140
use std::marker::PhantomPinned;
4241
use std::path::{Path, PathBuf};
4342
use std::pin::Pin;
44-
use std::rc::Rc;
4543
use std::sync::{Arc, LazyLock};
4644
use std::{env, fs, iter};
4745

@@ -131,21 +129,12 @@ mod boxed_resolver {
131129
f((&mut *resolver).as_mut().unwrap())
132130
}
133131

134-
pub fn to_resolver_outputs(resolver: Rc<RefCell<BoxedResolver>>) -> ty::ResolverOutputs {
135-
match Rc::try_unwrap(resolver) {
136-
Ok(resolver) => {
137-
let mut resolver = resolver.into_inner();
138-
// SAFETY: The resolver doesn't need to be pinned.
139-
let mut resolver = unsafe {
140-
resolver
141-
.0
142-
.as_mut()
143-
.map_unchecked_mut(|boxed_resolver| &mut boxed_resolver.resolver)
144-
};
145-
resolver.take().unwrap().into_outputs()
146-
}
147-
Err(resolver) => resolver.borrow_mut().access(|resolver| resolver.clone_outputs()),
148-
}
132+
pub fn into_outputs(mut self) -> ty::ResolverOutputs {
133+
// SAFETY: The resolver doesn't need to be pinned.
134+
let mut resolver = unsafe {
135+
self.0.as_mut().map_unchecked_mut(|boxed_resolver| &mut boxed_resolver.resolver)
136+
};
137+
resolver.take().unwrap().into_outputs()
149138
}
150139
}
151140
}

compiler/rustc_interface/src/queries.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use rustc_span::symbol::sym;
2121
use rustc_span::Symbol;
2222
use std::any::Any;
2323
use std::cell::{RefCell, RefMut};
24-
use std::rc::Rc;
2524
use std::sync::Arc;
2625

2726
/// Represent the result of a query.
@@ -88,7 +87,7 @@ pub struct Queries<'tcx> {
8887
parse: Query<ast::Crate>,
8988
crate_name: Query<Symbol>,
9089
register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
91-
expansion: Query<(Lrc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>,
90+
expansion: Query<(Lrc<ast::Crate>, BoxedResolver, Lrc<LintStore>)>,
9291
dep_graph: Query<DepGraph>,
9392
// This just points to what's in `gcx_cell`.
9493
gcx: Query<&'tcx GlobalCtxt<'tcx>>,
@@ -171,8 +170,7 @@ impl<'tcx> Queries<'tcx> {
171170

172171
pub fn expansion(
173172
&self,
174-
) -> Result<QueryResult<'_, (Lrc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>>
175-
{
173+
) -> Result<QueryResult<'_, (Lrc<ast::Crate>, BoxedResolver, Lrc<LintStore>)>> {
176174
trace!("expansion");
177175
self.expansion.compute(|| {
178176
let crate_name = *self.crate_name()?.borrow();
@@ -188,7 +186,7 @@ impl<'tcx> Queries<'tcx> {
188186
let krate = resolver.access(|resolver| {
189187
passes::configure_and_expand(sess, &lint_store, krate, crate_name, resolver)
190188
})?;
191-
Ok((Lrc::new(krate), Rc::new(RefCell::new(resolver)), lint_store))
189+
Ok((Lrc::new(krate), resolver, lint_store))
192190
})
193191
}
194192

@@ -217,7 +215,7 @@ impl<'tcx> Queries<'tcx> {
217215
untracked,
218216
global_ctxt: untracked_resolutions,
219217
ast_lowering: untracked_resolver_for_lowering,
220-
} = BoxedResolver::to_resolver_outputs(resolver);
218+
} = resolver.into_outputs();
221219

222220
let gcx = passes::create_global_ctxt(
223221
self.compiler,

compiler/rustc_metadata/src/creader.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_ast::expand::allocator::AllocatorKind;
88
use rustc_ast::{self as ast, *};
99
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1010
use rustc_data_structures::svh::Svh;
11-
use rustc_data_structures::sync::{Lrc, ReadGuard};
11+
use rustc_data_structures::sync::ReadGuard;
1212
use rustc_expand::base::SyntaxExtension;
1313
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE};
1414
use rustc_hir::definitions::Definitions;
@@ -30,11 +30,10 @@ use proc_macro::bridge::client::ProcMacro;
3030
use std::ops::Fn;
3131
use std::path::Path;
3232
use std::time::Duration;
33-
use std::{cmp, env};
33+
use std::{cmp, env, iter};
3434

35-
#[derive(Clone)]
3635
pub struct CStore {
37-
metas: IndexVec<CrateNum, Option<Lrc<CrateMetadata>>>,
36+
metas: IndexVec<CrateNum, Option<Box<CrateMetadata>>>,
3837
injected_panic_runtime: Option<CrateNum>,
3938
/// This crate needs an allocator and either provides it itself, or finds it in a dependency.
4039
/// If the above is true, then this field denotes the kind of the found allocator.
@@ -153,7 +152,7 @@ impl CStore {
153152

154153
fn set_crate_data(&mut self, cnum: CrateNum, data: CrateMetadata) {
155154
assert!(self.metas[cnum].is_none(), "Overwriting crate metadata entry");
156-
self.metas[cnum] = Some(Lrc::new(data));
155+
self.metas[cnum] = Some(Box::new(data));
157156
}
158157

159158
pub(crate) fn iter_crate_data(&self) -> impl Iterator<Item = (CrateNum, &CrateMetadata)> {
@@ -245,7 +244,7 @@ impl CStore {
245244
// order to make array indices in `metas` match with the
246245
// corresponding `CrateNum`. This first entry will always remain
247246
// `None`.
248-
metas: IndexVec::from_elem_n(None, 1),
247+
metas: IndexVec::from_iter(iter::once(None)),
249248
injected_panic_runtime: None,
250249
allocator_kind: None,
251250
alloc_error_handler_kind: None,

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,15 +1169,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11691169
}
11701170

11711171
/// Decodes all trait impls in the crate (for rustdoc).
1172-
fn get_trait_impls(self) -> impl Iterator<Item = (DefId, DefId, Option<SimplifiedType>)> + 'a {
1173-
self.cdata.trait_impls.iter().flat_map(move |(&(trait_cnum_raw, trait_index), impls)| {
1174-
let trait_def_id = DefId {
1175-
krate: self.cnum_map[CrateNum::from_u32(trait_cnum_raw)],
1176-
index: trait_index,
1177-
};
1178-
impls.decode(self).map(move |(impl_index, simplified_self_ty)| {
1179-
(trait_def_id, self.local_def_id(impl_index), simplified_self_ty)
1180-
})
1172+
fn get_trait_impls(self) -> impl Iterator<Item = DefId> + 'a {
1173+
self.cdata.trait_impls.values().flat_map(move |impls| {
1174+
impls.decode(self).map(move |(impl_index, _)| self.local_def_id(impl_index))
11811175
})
11821176
}
11831177

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ provide! { tcx, def_id, other, cdata,
304304
extra_filename => { cdata.root.extra_filename.clone() }
305305

306306
traits_in_crate => { tcx.arena.alloc_from_iter(cdata.get_traits()) }
307+
trait_impls_in_crate => { tcx.arena.alloc_from_iter(cdata.get_trait_impls()) }
307308
implementations_of_trait => { cdata.get_implementations_of_trait(tcx, other) }
308309
crate_incoherent_impls => { cdata.get_incoherent_impls(tcx, other) }
309310

@@ -608,20 +609,6 @@ impl CStore {
608609
) -> Span {
609610
self.get_crate_data(cnum).get_proc_macro_quoted_span(id, sess)
610611
}
611-
612-
/// Decodes all trait impls in the crate (for rustdoc).
613-
pub fn trait_impls_in_crate_untracked(
614-
&self,
615-
cnum: CrateNum,
616-
) -> impl Iterator<Item = (DefId, DefId, Option<SimplifiedType>)> + '_ {
617-
self.get_crate_data(cnum).get_trait_impls()
618-
}
619-
620-
pub fn is_doc_hidden_untracked(&self, def_id: DefId) -> bool {
621-
self.get_crate_data(def_id.krate)
622-
.get_attr_flags(def_id.index)
623-
.contains(AttrFlags::IS_DOC_HIDDEN)
624-
}
625612
}
626613

627614
impl CrateStore for CStore {

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,6 +2256,22 @@ pub fn provide(providers: &mut Providers) {
22562256
traits.sort_by_cached_key(|&def_id| tcx.def_path_hash(def_id));
22572257
tcx.arena.alloc_slice(&traits)
22582258
},
2259+
trait_impls_in_crate: |tcx, cnum| {
2260+
assert_eq!(cnum, LOCAL_CRATE);
2261+
2262+
let mut trait_impls = Vec::new();
2263+
for id in tcx.hir().items() {
2264+
if matches!(tcx.def_kind(id.owner_id), DefKind::Impl)
2265+
&& tcx.impl_trait_ref(id.owner_id).is_some()
2266+
{
2267+
trait_impls.push(id.owner_id.to_def_id())
2268+
}
2269+
}
2270+
2271+
// Bring everything into deterministic order.
2272+
trait_impls.sort_by_cached_key(|&def_id| tcx.def_path_hash(def_id));
2273+
tcx.arena.alloc_slice(&trait_impls)
2274+
},
22592275

22602276
..*providers
22612277
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,11 @@ rustc_queries! {
18361836
separate_provide_extern
18371837
}
18381838

1839+
query trait_impls_in_crate(_: CrateNum) -> &'tcx [DefId] {
1840+
desc { "fetching all trait impls in a crate" }
1841+
separate_provide_extern
1842+
}
1843+
18391844
/// The list of symbols exported from the given crate.
18401845
///
18411846
/// - All names contained in `exported_symbols(cnum)` are guaranteed to

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ pub struct ResolverGlobalCtxt {
187187
pub registered_tools: RegisteredTools,
188188
pub doc_link_resolutions: FxHashMap<LocalDefId, DocLinkResMap>,
189189
pub doc_link_traits_in_scope: FxHashMap<LocalDefId, Vec<DefId>>,
190+
pub all_macro_rules: FxHashMap<Symbol, Res<ast::NodeId>>,
190191
}
191192

192193
/// Resolutions that should only be used for lowering.

0 commit comments

Comments
 (0)