Skip to content

Commit d11b916

Browse files
committed
resolve: Preserve reexport chains in ModChildren
This may be potentially useful for - avoiding uses of `hir::ItemKind::Use` - preserving documentation comments on all reexports - preserving and checking stability/deprecation info on reexports - all kinds of diagnostics
1 parent c49c4fb commit d11b916

File tree

7 files changed

+50
-5
lines changed

7 files changed

+50
-5
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 1 addition & 1 deletion
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_middle/src/arena.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 25 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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_resolve/src/build_reduced_graph.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 21 additions & 0 deletions
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
});

src/librustdoc/clean/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ fn build_module_items(
558558
// If we're re-exporting a re-export it may actually re-export something in
559559
// two namespaces, so the target may be listed twice. Make sure we only
560560
// visit each node at most once.
561-
for &item in cx.tcx.module_children(did).iter() {
561+
for item in cx.tcx.module_children(did).iter() {
562562
if item.vis.is_public() {
563563
let res = item.res.expect_non_local();
564564
if let Some(def_id) = res.opt_def_id()

0 commit comments

Comments
 (0)