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

+1-1
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

+1-1
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

+6-17
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

+4-6
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

+5-6
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

+3-9
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

+1-14
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

+16
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

+5
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

+1
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.

compiler/rustc_resolve/src/build_reduced_graph.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
12511251
};
12521252
let binding = (res, vis, span, expansion).to_name_binding(self.r.arenas);
12531253
self.r.set_binding_parent_module(binding, parent_scope.module);
1254+
self.r.all_macro_rules.insert(ident.name, res);
12541255
if is_macro_export {
12551256
let import = self.r.arenas.alloc_import(Import {
12561257
kind: ImportKind::MacroExport,

compiler/rustc_resolve/src/effective_visibilities.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl ParentId<'_> {
2929
}
3030
}
3131

32-
pub struct EffectiveVisibilitiesVisitor<'r, 'a> {
32+
pub(crate) struct EffectiveVisibilitiesVisitor<'r, 'a> {
3333
r: &'r mut Resolver<'a>,
3434
def_effective_visibilities: EffectiveVisibilities,
3535
/// While walking import chains we need to track effective visibilities per-binding, and def id
@@ -78,7 +78,7 @@ impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> {
7878
/// Fills the `Resolver::effective_visibilities` table with public & exported items
7979
/// For now, this doesn't resolve macros (FIXME) and cannot resolve Impl, as we
8080
/// need access to a TyCtxt for that.
81-
pub fn compute_effective_visibilities<'c>(r: &'r mut Resolver<'a>, krate: &'c Crate) {
81+
pub(crate) fn compute_effective_visibilities<'c>(r: &'r mut Resolver<'a>, krate: &'c Crate) {
8282
let mut visitor = EffectiveVisibilitiesVisitor {
8383
r,
8484
def_effective_visibilities: Default::default(),

compiler/rustc_resolve/src/imports.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Res = def::Res<NodeId>;
3333

3434
/// Contains data for specific kinds of imports.
3535
#[derive(Clone)]
36-
pub enum ImportKind<'a> {
36+
pub(crate) enum ImportKind<'a> {
3737
Single {
3838
/// `source` in `use prefix::source as target`.
3939
source: Ident,
@@ -157,11 +157,11 @@ pub(crate) struct Import<'a> {
157157
}
158158

159159
impl<'a> Import<'a> {
160-
pub fn is_glob(&self) -> bool {
160+
pub(crate) fn is_glob(&self) -> bool {
161161
matches!(self.kind, ImportKind::Glob { .. })
162162
}
163163

164-
pub fn is_nested(&self) -> bool {
164+
pub(crate) fn is_nested(&self) -> bool {
165165
match self.kind {
166166
ImportKind::Single { nested, .. } => nested,
167167
_ => false,
@@ -405,7 +405,7 @@ struct UnresolvedImportError {
405405
candidates: Option<Vec<ImportSuggestion>>,
406406
}
407407

408-
pub struct ImportResolver<'a, 'b> {
408+
pub(crate) struct ImportResolver<'a, 'b> {
409409
pub r: &'a mut Resolver<'b>,
410410
}
411411

@@ -420,7 +420,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
420420

421421
/// Resolves all imports for the crate. This method performs the fixed-
422422
/// point iteration.
423-
pub fn resolve_imports(&mut self) {
423+
pub(crate) fn resolve_imports(&mut self) {
424424
let mut prev_num_indeterminates = self.r.indeterminate_imports.len() + 1;
425425
while self.r.indeterminate_imports.len() < prev_num_indeterminates {
426426
prev_num_indeterminates = self.r.indeterminate_imports.len();
@@ -433,7 +433,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
433433
}
434434
}
435435

436-
pub fn finalize_imports(&mut self) {
436+
pub(crate) fn finalize_imports(&mut self) {
437437
for module in self.r.arenas.local_modules().iter() {
438438
self.finalize_resolutions_in(module);
439439
}

compiler/rustc_resolve/src/late.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct BindingInfo {
5656
}
5757

5858
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
59-
pub enum PatternSource {
59+
pub(crate) enum PatternSource {
6060
Match,
6161
Let,
6262
For,
@@ -70,7 +70,7 @@ enum IsRepeatExpr {
7070
}
7171

7272
impl PatternSource {
73-
pub fn descr(self) -> &'static str {
73+
fn descr(self) -> &'static str {
7474
match self {
7575
PatternSource::Match => "match binding",
7676
PatternSource::Let => "let binding",
@@ -2374,9 +2374,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
23742374
// Maintain macro_rules scopes in the same way as during early resolution
23752375
// for diagnostics and doc links.
23762376
if macro_def.macro_rules {
2377-
let (macro_rules_scope, _) =
2378-
self.r.macro_rules_scope(self.r.local_def_id(item.id));
2379-
self.parent_scope.macro_rules = macro_rules_scope;
2377+
let def_id = self.r.local_def_id(item.id);
2378+
self.parent_scope.macro_rules = self.r.macro_rules_scopes[&def_id];
23802379
}
23812380
}
23822381

compiler/rustc_resolve/src/late/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2626,7 +2626,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
26262626
}
26272627

26282628
/// Report lifetime/lifetime shadowing as an error.
2629-
pub fn signal_lifetime_shadowing(sess: &Session, orig: Ident, shadower: Ident) {
2629+
pub(super) fn signal_lifetime_shadowing(sess: &Session, orig: Ident, shadower: Ident) {
26302630
let mut err = struct_span_err!(
26312631
sess,
26322632
shadower.span,
@@ -2641,7 +2641,7 @@ pub fn signal_lifetime_shadowing(sess: &Session, orig: Ident, shadower: Ident) {
26412641

26422642
/// Shadowing involving a label is only a warning for historical reasons.
26432643
//FIXME: make this a proper lint.
2644-
pub fn signal_label_shadowing(sess: &Session, orig: Span, shadower: Ident) {
2644+
pub(super) fn signal_label_shadowing(sess: &Session, orig: Span, shadower: Ident) {
26452645
let name = shadower.name;
26462646
let shadower = shadower.span;
26472647
let mut err = sess.struct_span_warn(

0 commit comments

Comments
 (0)