Skip to content

Commit 68bb06a

Browse files
authored
Rollup merge of rust-lang#88677 - petrochenkov:exportid, r=davidtwco
rustc: Remove local variable IDs from `Export`s Local variables can never be exported.
2 parents f00af15 + 294510e commit 68bb06a

File tree

19 files changed

+71
-68
lines changed

19 files changed

+71
-68
lines changed

compiler/rustc_data_structures/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#![feature(iter_map_while)]
2222
#![feature(maybe_uninit_uninit_array)]
2323
#![feature(min_specialization)]
24+
#![feature(never_type)]
2425
#![feature(type_alias_impl_trait)]
2526
#![feature(new_uninit)]
2627
#![feature(nll)]

compiler/rustc_data_structures/src/stable_hasher.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ impl_stable_hash_via_hash!(i128);
209209
impl_stable_hash_via_hash!(char);
210210
impl_stable_hash_via_hash!(());
211211

212+
impl<CTX> HashStable<CTX> for ! {
213+
fn hash_stable(&self, _ctx: &mut CTX, _hasher: &mut StableHasher) {
214+
unreachable!()
215+
}
216+
}
217+
212218
impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
213219
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
214220
self.get().hash_stable(ctx, hasher)

compiler/rustc_hir/src/def.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,11 @@ impl<Id> Res<Id> {
598598
}
599599
}
600600

601+
#[track_caller]
602+
pub fn expect_non_local<OtherId>(self) -> Res<OtherId> {
603+
self.map_id(|_| panic!("unexpected `Res::Local`"))
604+
}
605+
601606
pub fn macro_kind(self) -> Option<MacroKind> {
602607
match self {
603608
Res::Def(DefKind::Macro(kind), _) => Some(kind),

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,10 +1020,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10201020
}
10211021

10221022
/// Iterates over each child of the given item.
1023-
fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F, sess: &Session)
1024-
where
1025-
F: FnMut(Export<hir::HirId>),
1026-
{
1023+
fn each_child_of_item(&self, id: DefIndex, mut callback: impl FnMut(Export), sess: &Session) {
10271024
if let Some(data) = &self.root.proc_macro_data {
10281025
/* If we are loading as a proc macro, we want to return the view of this crate
10291026
* as a proc macro crate.

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

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::rmeta::encoder;
55

66
use rustc_ast as ast;
77
use rustc_data_structures::stable_map::FxHashMap;
8-
use rustc_hir as hir;
98
use rustc_hir::def::{CtorKind, DefKind};
109
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
1110
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
@@ -326,28 +325,27 @@ pub fn provide(providers: &mut Providers) {
326325
// (restrict scope of mutable-borrow of `visible_parent_map`)
327326
{
328327
let visible_parent_map = &mut visible_parent_map;
329-
let mut add_child =
330-
|bfs_queue: &mut VecDeque<_>, child: &Export<hir::HirId>, parent: DefId| {
331-
if child.vis != ty::Visibility::Public {
332-
return;
333-
}
328+
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &Export, parent: DefId| {
329+
if child.vis != ty::Visibility::Public {
330+
return;
331+
}
334332

335-
if let Some(child) = child.res.opt_def_id() {
336-
match visible_parent_map.entry(child) {
337-
Entry::Occupied(mut entry) => {
338-
// If `child` is defined in crate `cnum`, ensure
339-
// that it is mapped to a parent in `cnum`.
340-
if child.is_local() && entry.get().is_local() {
341-
entry.insert(parent);
342-
}
343-
}
344-
Entry::Vacant(entry) => {
333+
if let Some(child) = child.res.opt_def_id() {
334+
match visible_parent_map.entry(child) {
335+
Entry::Occupied(mut entry) => {
336+
// If `child` is defined in crate `cnum`, ensure
337+
// that it is mapped to a parent in `cnum`.
338+
if child.is_local() && entry.get().is_local() {
345339
entry.insert(parent);
346-
bfs_queue.push_back(child);
347340
}
348341
}
342+
Entry::Vacant(entry) => {
343+
entry.insert(parent);
344+
bfs_queue.push_back(child);
345+
}
349346
}
350-
};
347+
}
348+
};
351349

352350
while let Some(def) = bfs_queue.pop_front() {
353351
for child in tcx.item_children(def).iter() {
@@ -393,11 +391,7 @@ impl CStore {
393391
self.get_crate_data(def.krate).get_visibility(def.index)
394392
}
395393

396-
pub fn item_children_untracked(
397-
&self,
398-
def_id: DefId,
399-
sess: &Session,
400-
) -> Vec<Export<hir::HirId>> {
394+
pub fn item_children_untracked(&self, def_id: DefId, sess: &Session) -> Vec<Export> {
401395
let mut result = vec![];
402396
self.get_crate_data(def_id.krate).each_child_of_item(
403397
def_id.index,

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,14 +1065,7 @@ impl EncodeContext<'a, 'tcx> {
10651065
// items - we encode information about proc-macros later on.
10661066
let reexports = if !self.is_proc_macro {
10671067
match tcx.module_exports(local_def_id) {
1068-
Some(exports) => {
1069-
let hir = self.tcx.hir();
1070-
self.lazy(
1071-
exports
1072-
.iter()
1073-
.map(|export| export.map_id(|id| hir.local_def_id_to_hir_id(id))),
1074-
)
1075-
}
1068+
Some(exports) => self.lazy(exports),
10761069
_ => Lazy::empty(),
10771070
}
10781071
} else {

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ struct RenderedConst(String);
359359

360360
#[derive(MetadataEncodable, MetadataDecodable)]
361361
struct ModData {
362-
reexports: Lazy<[Export<hir::HirId>]>,
362+
reexports: Lazy<[Export]>,
363363
expansion: ExpnId,
364364
}
365365

compiler/rustc_middle/src/hir/exports.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,18 @@ use std::fmt::Debug;
1111

1212
/// This is the replacement export map. It maps a module to all of the exports
1313
/// within.
14-
pub type ExportMap<Id> = FxHashMap<LocalDefId, Vec<Export<Id>>>;
14+
pub type ExportMap = FxHashMap<LocalDefId, Vec<Export>>;
1515

1616
#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)]
17-
pub struct Export<Id> {
17+
pub struct Export {
1818
/// The name of the target.
1919
pub ident: Ident,
2020
/// The resolution of the target.
21-
pub res: Res<Id>,
21+
/// Local variables cannot be exported, so this `Res` doesn't need the ID parameter.
22+
pub res: Res<!>,
2223
/// The span of the target.
2324
pub span: Span,
2425
/// The visibility of the export.
2526
/// We include non-`pub` exports for hygienic macros that get used from extern crates.
2627
pub vis: ty::Visibility,
2728
}
28-
29-
impl<Id> Export<Id> {
30-
pub fn map_id<R>(self, map: impl FnMut(Id) -> R) -> Export<R> {
31-
Export { ident: self.ident, res: self.res.map_id(map), span: self.span, vis: self.vis }
32-
}
33-
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ rustc_queries! {
11891189
desc { "traits in scope at a block" }
11901190
}
11911191

1192-
query module_exports(def_id: LocalDefId) -> Option<&'tcx [Export<LocalDefId>]> {
1192+
query module_exports(def_id: LocalDefId) -> Option<&'tcx [Export]> {
11931193
desc { |tcx| "looking up items exported by `{}`", tcx.def_path_str(def_id.to_def_id()) }
11941194
}
11951195

@@ -1401,7 +1401,7 @@ rustc_queries! {
14011401
eval_always
14021402
desc { "fetching what a crate is named" }
14031403
}
1404-
query item_children(def_id: DefId) -> &'tcx [Export<hir::HirId>] {
1404+
query item_children(def_id: DefId) -> &'tcx [Export] {
14051405
desc { |tcx| "collecting child items of `{}`", tcx.def_path_str(def_id) }
14061406
}
14071407
query extern_mod_stmt_cnum(def_id: LocalDefId) -> Option<CrateNum> {

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub struct ResolverOutputs {
127127
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
128128
pub maybe_unused_trait_imports: FxHashSet<LocalDefId>,
129129
pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
130-
pub export_map: ExportMap<LocalDefId>,
130+
pub export_map: ExportMap,
131131
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
132132
/// Extern prelude entries. The value is `true` if the entry was introduced
133133
/// via `extern crate` item and not `--extern` option or compiler built-in.

0 commit comments

Comments
 (0)