Skip to content

Commit 7201301

Browse files
committed
Auto merge of #109500 - petrochenkov:modchainld, r=oli-obk
resolve: Preserve reexport chains in `ModChild`ren This may be potentially useful for - avoiding uses of `hir::ItemKind::Use` (which usually lead to correctness issues) - preserving documentation comments on all reexports, including those from other crates - preserving and checking stability/deprecation info on reexports - all kinds of diagnostics The second commit then migrates some hacky logic from rustdoc to `module_reexports` to make it simpler and more correct. Ideally rustdoc should use `module_reexports` immediately at the top level, so `hir::ItemKind::Use`s are never used. The second commit also fixes issues with #109330 and therefore Fixes #109631 Fixes #109614 Fixes #109424
2 parents 56e0626 + 9da9373 commit 7201301

File tree

14 files changed

+97
-168
lines changed

14 files changed

+97
-168
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
991991
_ => false,
992992
};
993993

994-
ModChild { ident, res, vis, span, macro_rules }
994+
ModChild { ident, res, vis, span, macro_rules, reexport_chain: Default::default() }
995995
}
996996

997997
/// Iterates over all named children of the given module,

compiler/rustc_metadata/src/rmeta/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1327,8 +1327,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13271327
}
13281328
}));
13291329

1330-
if let Some(reexports) = tcx.module_reexports(local_def_id) {
1331-
assert!(!reexports.is_empty());
1330+
let reexports = tcx.module_reexports(local_def_id);
1331+
if !reexports.is_empty() {
13321332
record_array!(self.tables.module_reexports[def_id] <- reexports);
13331333
}
13341334
}

compiler/rustc_middle/src/arena.rs

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ macro_rules! arena_types {
119119
[] external_constraints: rustc_middle::traits::solve::ExternalConstraintsData<'tcx>,
120120
[decode] doc_link_resolutions: rustc_hir::def::DocLinkResMap,
121121
[] closure_kind_origin: (rustc_span::Span, rustc_middle::hir::place::Place<'tcx>),
122+
[] mod_child: rustc_middle::metadata::ModChild,
122123
]);
123124
)
124125
}

compiler/rustc_middle/src/metadata.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,34 @@ use rustc_macros::HashStable;
55
use rustc_span::def_id::DefId;
66
use rustc_span::symbol::Ident;
77
use rustc_span::Span;
8+
use smallvec::SmallVec;
9+
10+
/// A simplified version of `ImportKind` from resolve.
11+
/// `DefId`s here correspond to `use` and `extern crate` items themselves, not their targets.
12+
#[derive(Clone, Copy, Debug, TyEncodable, TyDecodable, HashStable)]
13+
pub enum Reexport {
14+
Single(DefId),
15+
Glob(DefId),
16+
ExternCrate(DefId),
17+
MacroUse,
18+
MacroExport,
19+
}
20+
21+
impl Reexport {
22+
pub fn id(self) -> Option<DefId> {
23+
match self {
24+
Reexport::Single(id) | Reexport::Glob(id) | Reexport::ExternCrate(id) => Some(id),
25+
Reexport::MacroUse | Reexport::MacroExport => None,
26+
}
27+
}
28+
}
829

930
/// This structure is supposed to keep enough data to re-create `NameBinding`s for other crates
1031
/// during name resolution. Right now the bindings are not recreated entirely precisely so we may
1132
/// need to add more data in the future to correctly support macros 2.0, for example.
1233
/// Module child can be either a proper item or a reexport (including private imports).
1334
/// In case of reexport all the fields describe the reexport item itself, not what it refers to.
14-
#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)]
35+
#[derive(Debug, TyEncodable, TyDecodable, HashStable)]
1536
pub struct ModChild {
1637
/// Name of the item.
1738
pub ident: Ident,
@@ -24,4 +45,7 @@ pub struct ModChild {
2445
pub span: Span,
2546
/// A proper `macro_rules` item (not a reexport).
2647
pub macro_rules: bool,
48+
/// Reexport chain linking this module child to its original reexported item.
49+
/// Empty if the module child is a proper item.
50+
pub reexport_chain: SmallVec<[Reexport; 2]>,
2751
}

compiler/rustc_middle/src/query/erase.rs

-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ trivial! {
235235
rustc_hir::OwnerId,
236236
rustc_hir::Upvar,
237237
rustc_index::bit_set::FiniteBitSet<u32>,
238-
rustc_middle::metadata::ModChild,
239238
rustc_middle::middle::dependency_format::Linkage,
240239
rustc_middle::middle::exported_symbols::SymbolExportInfo,
241240
rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault,

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1510,7 +1510,7 @@ rustc_queries! {
15101510
desc { "getting traits in scope at a block" }
15111511
}
15121512

1513-
query module_reexports(def_id: LocalDefId) -> Option<&'tcx [ModChild]> {
1513+
query module_reexports(def_id: LocalDefId) -> &'tcx [ModChild] {
15141514
desc { |tcx| "looking up reexports of module `{}`", tcx.def_path_str(def_id.to_def_id()) }
15151515
}
15161516

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2502,7 +2502,7 @@ pub struct DeducedParamAttrs {
25022502

25032503
pub fn provide(providers: &mut ty::query::Providers) {
25042504
providers.module_reexports =
2505-
|tcx, id| tcx.resolutions(()).reexport_map.get(&id).map(|v| &v[..]);
2505+
|tcx, id| tcx.resolutions(()).reexport_map.get(&id).map_or(&[], |v| &v[..]);
25062506
providers.maybe_unused_trait_imports =
25072507
|tcx, ()| &tcx.resolutions(()).maybe_unused_trait_imports;
25082508
providers.names_imported_by_glob_use = |tcx, id| {

compiler/rustc_privacy/src/lib.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -515,16 +515,12 @@ impl<'tcx> EmbargoVisitor<'tcx> {
515515
let vis = self.tcx.local_visibility(item_id.owner_id.def_id);
516516
self.update_macro_reachable_def(item_id.owner_id.def_id, def_kind, vis, defining_mod);
517517
}
518-
if let Some(exports) = self.tcx.module_reexports(module_def_id) {
519-
for export in exports {
520-
if export.vis.is_accessible_from(defining_mod, self.tcx) {
521-
if let Res::Def(def_kind, def_id) = export.res {
522-
if let Some(def_id) = def_id.as_local() {
523-
let vis = self.tcx.local_visibility(def_id);
524-
self.update_macro_reachable_def(def_id, def_kind, vis, defining_mod);
525-
}
526-
}
527-
}
518+
for export in self.tcx.module_reexports(module_def_id) {
519+
if export.vis.is_accessible_from(defining_mod, self.tcx)
520+
&& let Res::Def(def_kind, def_id) = export.res
521+
&& let Some(def_id) = def_id.as_local() {
522+
let vis = self.tcx.local_visibility(def_id);
523+
self.update_macro_reachable_def(def_id, def_kind, vis, defining_mod);
528524
}
529525
}
530526
}

compiler/rustc_resolve/src/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
931931
/// Builds the reduced graph for a single item in an external crate.
932932
fn build_reduced_graph_for_external_crate_res(&mut self, child: ModChild) {
933933
let parent = self.parent_scope.module;
934-
let ModChild { ident, res, vis, span, macro_rules } = child;
934+
let ModChild { ident, res, vis, span, macro_rules, .. } = child;
935935
let res = res.expect_non_local();
936936
let expansion = self.parent_scope.expansion;
937937
// Record primary definitions.

compiler/rustc_resolve/src/imports.rs

+21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_data_structures::intern::Interned;
1717
use rustc_errors::{pluralize, struct_span_err, Applicability, MultiSpan};
1818
use rustc_hir::def::{self, DefKind, PartialRes};
1919
use rustc_middle::metadata::ModChild;
20+
use rustc_middle::metadata::Reexport;
2021
use rustc_middle::span_bug;
2122
use rustc_middle::ty;
2223
use rustc_session::lint::builtin::{
@@ -27,6 +28,7 @@ use rustc_span::edit_distance::find_best_match_for_name;
2728
use rustc_span::hygiene::LocalExpnId;
2829
use rustc_span::symbol::{kw, Ident, Symbol};
2930
use rustc_span::Span;
31+
use smallvec::SmallVec;
3032

3133
use std::cell::Cell;
3234
use std::{mem, ptr};
@@ -190,6 +192,17 @@ impl<'a> Import<'a> {
190192
ImportKind::MacroUse | ImportKind::MacroExport => None,
191193
}
192194
}
195+
196+
fn simplify(&self, r: &Resolver<'_, '_>) -> Reexport {
197+
let to_def_id = |id| r.local_def_id(id).to_def_id();
198+
match self.kind {
199+
ImportKind::Single { id, .. } => Reexport::Single(to_def_id(id)),
200+
ImportKind::Glob { id, .. } => Reexport::Glob(to_def_id(id)),
201+
ImportKind::ExternCrate { id, .. } => Reexport::ExternCrate(to_def_id(id)),
202+
ImportKind::MacroUse => Reexport::MacroUse,
203+
ImportKind::MacroExport => Reexport::MacroExport,
204+
}
205+
}
193206
}
194207

195208
/// Records information about the resolution of a name in a namespace of a module.
@@ -1252,12 +1265,20 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12521265

12531266
module.for_each_child(self, |this, ident, _, binding| {
12541267
if let Some(res) = this.is_reexport(binding) {
1268+
let mut reexport_chain = SmallVec::new();
1269+
let mut next_binding = binding;
1270+
while let NameBindingKind::Import { binding, import, .. } = next_binding.kind {
1271+
reexport_chain.push(import.simplify(this));
1272+
next_binding = binding;
1273+
}
1274+
12551275
reexports.push(ModChild {
12561276
ident,
12571277
res,
12581278
vis: binding.vis,
12591279
span: binding.span,
12601280
macro_rules: false,
1281+
reexport_chain,
12611282
});
12621283
}
12631284
});

library/std/src/collections/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,10 @@ pub use alloc_crate::collections::{BTreeMap, BTreeSet, BinaryHeap};
416416
pub use alloc_crate::collections::{LinkedList, VecDeque};
417417

418418
#[stable(feature = "rust1", since = "1.0.0")]
419+
#[doc(inline)]
419420
pub use self::hash_map::HashMap;
420421
#[stable(feature = "rust1", since = "1.0.0")]
422+
#[doc(inline)]
421423
pub use self::hash_set::HashSet;
422424

423425
#[stable(feature = "try_reserve", since = "1.57.0")]

src/librustdoc/clean/inline.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ pub(crate) fn try_inline_glob(
153153
let reexports = cx
154154
.tcx
155155
.module_reexports(current_mod)
156-
.unwrap_or_default()
157156
.iter()
158157
.filter_map(|child| child.res.opt_def_id())
159158
.collect();
@@ -558,7 +557,7 @@ fn build_module_items(
558557
// If we're re-exporting a re-export it may actually re-export something in
559558
// two namespaces, so the target may be listed twice. Make sure we only
560559
// visit each node at most once.
561-
for &item in cx.tcx.module_children(did).iter() {
560+
for item in cx.tcx.module_children(did).iter() {
562561
if item.vis.is_public() {
563562
let res = item.res.expect_non_local();
564563
if let Some(def_id) = res.opt_def_id()

0 commit comments

Comments
 (0)